Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

Simple C Example: gdalinfo.c

/* **************************************************************************** * Copyright (c) 1998, Frank Warmerdam * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * **************************************************************************** * * gdalinfo.c * * Simple application for dumping all the data about a dataset. Mainly * serves as an early test harnass. * * $Log: gdalinfo.c,v $ * Revision 1.30 2003/11/24 17:46:58 warmerda * Report full geotransform when image is not northup. * * Revision 1.29 2003/05/01 13:16:37 warmerda * dont generate error reports if instantiating coordinate transform fails * * Revision 1.28 2003/04/22 16:02:08 dron * New switches: -nogcp and -nomd to suppress printing out GCPs list and metadata * strings respectively. * * Revision 1.27 2002/09/20 14:32:15 warmerda * don't build gdalinfo statically any more * * Revision 1.26 2002/09/04 06:53:42 warmerda * added GDALDestroyDriverManager() for cleanup * * Revision 1.25 2002/05/29 15:53:14 warmerda * added GDALDumpOpenDatasets * * Revision 1.24 2002/04/16 14:00:25 warmerda * added GDALVersionInfo * * Revision 1.23 2002/03/25 13:50:07 warmerda * only report min/max values if fetch successfully * * Revision 1.22 2002/01/13 01:42:37 warmerda * add -sample test * * Revision 1.21 2001/11/17 21:40:58 warmerda * converted to use OGR projection services * * Revision 1.20 2001/11/02 22:21:36 warmerda * fixed memory leak * * Revision 1.19 2001/07/18 05:05:12 warmerda * added CPL_CSVID * * Revision 1.18 2001/07/05 13:12:40 warmerda * added UnitType support * * Revision 1.17 2001/06/28 19:40:12 warmerda * added subdatset reporting * * Revision 1.16 2000/11/29 20:52:53 warmerda * Add pretty printing of projection. * * Revision 1.15 2000/08/25 14:26:02 warmerda * added nodata, and arbitrary overview reporting * * Revision 1.14 2000/06/12 14:21:43 warmerda * Fixed min/max printf. * * Revision 1.13 2000/05/15 14:06:26 warmerda * Added -mm for computing min/max, and fixed ovewriting of i. * * Revision 1.12 2000/04/21 21:57:33 warmerda * updated the way metadata is handled * * Revision 1.11 2000/03/31 13:43:21 warmerda * added code to report all corners and gcps * * Revision 1.10 2000/03/29 15:33:32 warmerda * added block size * * Revision 1.9 2000/03/06 21:50:37 warmerda * added min/max support * * Revision 1.8 2000/03/06 02:18:13 warmerda * added overviews, and colour table * * Revision 1.6 1999/12/30 02:40:17 warmerda * Report driver used. * * Revision 1.5 1999/10/21 13:22:59 warmerda * Print band type symbolically rather than numerically. * * Revision 1.4 1999/10/01 14:45:14 warmerda * prettied up * * Revision 1.3 1999/03/02 21:12:01 warmerda * add DMS reporting of lat/long * * Revision 1.2 1999/01/11 15:27:59 warmerda * Add projection support * * Revision 1.1 1998/12/03 18:35:06 warmerda * New * */ #include "gdal.h" #include "ogr_srs_api.h" #include "cpl_string.h" CPL_CVSID("$Id: gdalinfo.c,v 1.30 2003/11/24 17:46:58 warmerda Exp $"); static int GDALInfoReportCorner( GDALDatasetH hDataset, const char * corner_name, double x, double y ); /************************************************************************/ /* Usage() */ /************************************************************************/ void Usage() { printf( "Usage: gdalinfo [--version] [--formats] [-mm] [-nogcp] [-nomd] " "datasetname\n"); exit( 1 ); } /************************************************************************/ /* main() */ /************************************************************************/ int main( int argc, char ** argv ) { GDALDatasetH hDataset; GDALRasterBandH hBand; int i, iBand; double adfGeoTransform[6]; GDALDriverH hDriver; char **papszMetadata; int bComputeMinMax = FALSE, bSample = FALSE; int bShowGCPs = TRUE, bShowMetadata = TRUE ; const char *pszFilename = NULL; GDALAllRegister(); /* -------------------------------------------------------------------- */ /* Parse arguments. */ /* -------------------------------------------------------------------- */ for( i = 1; i < argc; i++ ) { if( EQUAL(argv[i], "-mm") ) { bComputeMinMax = TRUE; } else if( EQUAL(argv[i] ,"--version") ) { printf( "%s\n", GDALVersionInfo( "--version" ) ); exit( 0 ); } else if( EQUAL(argv[i], "--formats") ) { int iDr; printf( "Supported Formats:\n" ); for( iDr = 0; iDr < GDALGetDriverCount(); iDr++ ) { GDALDriverH hDriver = GDALGetDriver(iDr); printf( " %s: %s\n", GDALGetDriverShortName( hDriver ), GDALGetDriverLongName( hDriver ) ); } exit( 0 ); } else if( EQUAL(argv[i], "-sample") ) bSample = TRUE; else if( EQUAL(argv[i], "-nogcp") ) bShowGCPs = FALSE; else if( EQUAL(argv[i], "-nomd") ) bShowMetadata = FALSE; else if( argv[i][0] == '-' ) Usage(); else if( pszFilename == NULL ) pszFilename = argv[i]; else Usage(); } if( pszFilename == NULL ) Usage(); /* -------------------------------------------------------------------- */ /* Open dataset. */ /* -------------------------------------------------------------------- */ hDataset = GDALOpen( pszFilename, GA_ReadOnly ); if( hDataset == NULL ) { fprintf( stderr, "GDALOpen failed - %d\n%s\n", CPLGetLastErrorNo(), CPLGetLastErrorMsg() ); exit( 1 ); } /* -------------------------------------------------------------------- */ /* Report general info. */ /* -------------------------------------------------------------------- */ hDriver = GDALGetDatasetDriver( hDataset ); printf( "Driver: %s/%s\n", GDALGetDriverShortName( hDriver ), GDALGetDriverLongName( hDriver ) ); printf( "Size is %d, %d\n", GDALGetRasterXSize( hDataset ), GDALGetRasterYSize( hDataset ) ); /* -------------------------------------------------------------------- */ /* Report projection. */ /* -------------------------------------------------------------------- */ if( GDALGetProjectionRef( hDataset ) != NULL ) { OGRSpatialReferenceH hSRS; char *pszProjection; pszProjection = (char *) GDALGetProjectionRef( hDataset ); hSRS = OSRNewSpatialReference(NULL); if( OSRImportFromWkt( hSRS, &pszProjection ) == CE_None ) { char *pszPrettyWkt = NULL; OSRExportToPrettyWkt( hSRS, &pszPrettyWkt, FALSE ); printf( "Coordinate System is:\n%s\n", pszPrettyWkt ); CPLFree( pszPrettyWkt ); } else printf( "Coordinate System is `%s'\n", GDALGetProjectionRef( hDataset ) ); OSRDestroySpatialReference( hSRS ); } /* -------------------------------------------------------------------- */ /* Report Geotransform. */ /* -------------------------------------------------------------------- */ if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None ) { if( adfGeoTransform[2] == 0.0 && adfGeoTransform[4] == 0.0 ) { printf( "Origin = (%.6f,%.6f)\n", adfGeoTransform[0], adfGeoTransform[3] ); printf( "Pixel Size = (%.8f,%.8f)\n", adfGeoTransform[1], adfGeoTransform[5] ); } else printf( "GeoTransform =\n" " %.16g, %.16g, %.16g\n" " %.16g, %.16g, %.16g\n", adfGeoTransform[0], adfGeoTransform[1], adfGeoTransform[2], adfGeoTransform[3], adfGeoTransform[4], adfGeoTransform[5] ); } /* -------------------------------------------------------------------- */ /* Report GCPs. */ /* -------------------------------------------------------------------- */ if( bShowGCPs && GDALGetGCPCount( hDataset ) > 0 ) { printf( "GCP Projection = %s\n", GDALGetGCPProjection(hDataset) ); for( i = 0; i < GDALGetGCPCount(hDataset); i++ ) { const GDAL_GCP *psGCP; psGCP = GDALGetGCPs( hDataset ) + i; printf( "GCP[%3d]: Id=%s, Info=%s\n" " (%g,%g) -> (%g,%g,%g)\n", i, psGCP->pszId, psGCP->pszInfo, psGCP->dfGCPPixel, psGCP->dfGCPLine, psGCP->dfGCPX, psGCP->dfGCPY, psGCP->dfGCPZ ); } } /* -------------------------------------------------------------------- */ /* Report metadata. */ /* -------------------------------------------------------------------- */ papszMetadata = GDALGetMetadata( hDataset, NULL ); if( bShowMetadata && CSLCount(papszMetadata) > 0 ) { printf( "Metadata:\n" ); for( i = 0; papszMetadata[i] != NULL; i++ ) { printf( " %s\n", papszMetadata[i] ); } } /* -------------------------------------------------------------------- */ /* Report subdatasets. */ /* -------------------------------------------------------------------- */ papszMetadata = GDALGetMetadata( hDataset, "SUBDATASETS" ); if( CSLCount(papszMetadata) > 0 ) { printf( "Subdatasets:\n" ); for( i = 0; papszMetadata[i] != NULL; i++ ) { printf( " %s\n", papszMetadata[i] ); } } /* -------------------------------------------------------------------- */ /* Report corners. */ /* -------------------------------------------------------------------- */ printf( "Corner Coordinates:\n" ); GDALInfoReportCorner( hDataset, "Upper Left", 0.0, 0.0 ); GDALInfoReportCorner( hDataset, "Lower Left", 0.0, GDALGetRasterYSize(hDataset)); GDALInfoReportCorner( hDataset, "Upper Right", GDALGetRasterXSize(hDataset), 0.0 ); GDALInfoReportCorner( hDataset, "Lower Right", GDALGetRasterXSize(hDataset), GDALGetRasterYSize(hDataset) ); GDALInfoReportCorner( hDataset, "Center", GDALGetRasterXSize(hDataset)/2.0, GDALGetRasterYSize(hDataset)/2.0 ); /* ==================================================================== */ /* Loop over bands. */ /* ==================================================================== */ for( iBand = 0; iBand < GDALGetRasterCount( hDataset ); iBand++ ) { double dfMin, dfMax, adfCMinMax[2], dfNoData; int bGotMin, bGotMax, bGotNodata; int nBlockXSize, nBlockYSize; hBand = GDALGetRasterBand( hDataset, iBand+1 ); if( bSample ) { float afSample[10000]; int nCount; nCount = GDALGetRandomRasterSample( hBand, 10000, afSample ); printf( "Got %d samples.\n", nCount ); } GDALGetBlockSize( hBand, &nBlockXSize, &nBlockYSize ); printf( "Band %d Block=%dx%d Type=%s, ColorInterp=%s\n", iBand+1, nBlockXSize, nBlockYSize, GDALGetDataTypeName( GDALGetRasterDataType(hBand)), GDALGetColorInterpretationName( GDALGetRasterColorInterpretation(hBand)) ); if( GDALGetDescription( hBand ) != NULL && strlen(GDALGetDescription( hBand )) > 0 ) printf( " Description = %s\n", GDALGetDescription(hBand) ); dfMin = GDALGetRasterMinimum( hBand, &bGotMin ); dfMax = GDALGetRasterMaximum( hBand, &bGotMax ); if( bGotMin || bGotMax || bComputeMinMax ) { printf( " " ); if( bGotMin ) printf( "Min=%.3f ", dfMin ); if( bGotMax ) printf( "Max=%.3f ", dfMax ); if( bComputeMinMax ) { GDALComputeRasterMinMax( hBand, TRUE, adfCMinMax ); printf( " Computed Min/Max=%.3f,%.3f", adfCMinMax[0], adfCMinMax[1] ); } printf( "\n" ); } dfNoData = GDALGetRasterNoDataValue( hBand, &bGotNodata ); if( bGotNodata ) { printf( " NoData Value=%g\n", dfNoData ); } if( GDALGetOverviewCount(hBand) > 0 ) { int iOverview; printf( " Overviews: " ); for( iOverview = 0; iOverview < GDALGetOverviewCount(hBand); iOverview++ ) { GDALRasterBandH hOverview; if( iOverview != 0 ) printf( ", " ); hOverview = GDALGetOverview( hBand, iOverview ); printf( "%dx%d", GDALGetRasterBandXSize( hOverview ), GDALGetRasterBandYSize( hOverview ) ); } printf( "\n" ); } if( GDALHasArbitraryOverviews( hBand ) ) { printf( " Overviews: arbitrary\n" ); } if( strlen(GDALGetRasterUnitType(hBand)) > 0 ) { printf( " Unit Type: %s\n", GDALGetRasterUnitType(hBand) ); } papszMetadata = GDALGetMetadata( hBand, NULL ); if( CSLCount(papszMetadata) > 0 ) { printf( "Metadata:\n" ); for( i = 0; papszMetadata[i] != NULL; i++ ) { printf( " %s\n", papszMetadata[i] ); } } if( GDALGetRasterColorInterpretation(hBand) == GCI_PaletteIndex ) { GDALColorTableH hTable; int i; hTable = GDALGetRasterColorTable( hBand ); printf( " Color Table (%s with %d entries)\n", GDALGetPaletteInterpretationName( GDALGetPaletteInterpretation( hTable )), GDALGetColorEntryCount( hTable ) ); for( i = 0; i < GDALGetColorEntryCount( hTable ); i++ ) { GDALColorEntry sEntry; GDALGetColorEntryAsRGB( hTable, i, &sEntry ); printf( " %3d: %d,%d,%d,%d\n", i, sEntry.c1, sEntry.c2, sEntry.c3, sEntry.c4 ); } } } GDALClose( hDataset ); GDALDumpOpenDatasets( stderr ); GDALDestroyDriverManager(); exit( 0 ); } /************************************************************************/ /* GDALInfoReportCorner() */ /************************************************************************/ static int GDALInfoReportCorner( GDALDatasetH hDataset, const char * corner_name, double x, double y ) { double dfGeoX, dfGeoY; const char *pszProjection; double adfGeoTransform[6]; OGRCoordinateTransformationH hTransform = NULL; printf( "%-11s ", corner_name ); /* -------------------------------------------------------------------- */ /* Transform the point into georeferenced coordinates. */ /* -------------------------------------------------------------------- */ if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None ) { pszProjection = GDALGetProjectionRef(hDataset); dfGeoX = adfGeoTransform[0] + adfGeoTransform[1] * x + adfGeoTransform[2] * y; dfGeoY = adfGeoTransform[3] + adfGeoTransform[4] * x + adfGeoTransform[5] * y; } else { printf( "(%7.1f,%7.1f)\n", x, y ); return FALSE; } /* -------------------------------------------------------------------- */ /* Report the georeferenced coordinates. */ /* -------------------------------------------------------------------- */ if( ABS(dfGeoX) < 181 && ABS(dfGeoY) < 91 ) { printf( "(%12.7f,%12.7f) ", dfGeoX, dfGeoY ); } else { printf( "(%12.3f,%12.3f) ", dfGeoX, dfGeoY ); } /* -------------------------------------------------------------------- */ /* Setup transformation to lat/long. */ /* -------------------------------------------------------------------- */ if( pszProjection != NULL && strlen(pszProjection) > 0 ) { OGRSpatialReferenceH hProj, hLatLong = NULL; hProj = OSRNewSpatialReference( pszProjection ); if( hProj != NULL ) hLatLong = OSRCloneGeogCS( hProj ); if( hLatLong != NULL ) { CPLPushErrorHandler( CPLQuietErrorHandler ); hTransform = OCTNewCoordinateTransformation( hProj, hLatLong ); CPLPopErrorHandler(); OSRDestroySpatialReference( hLatLong ); } if( hProj != NULL ) OSRDestroySpatialReference( hProj ); } /* -------------------------------------------------------------------- */ /* Transform to latlong and report. */ /* -------------------------------------------------------------------- */ if( hTransform != NULL && OCTTransform(hTransform,1,&dfGeoX,&dfGeoY,NULL) ) { printf( "(%s,", GDALDecToDMS( dfGeoX, "Long", 2 ) ); printf( "%s)", GDALDecToDMS( dfGeoY, "Lat", 2 ) ); } if( hTransform != NULL ) OCTDestroyCoordinateTransformation( hTransform ); printf( "\n" ); return TRUE; }
00001 /* **************************************************************************** 00002 * Copyright (c) 1998, Frank Warmerdam 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00020 * DEALINGS IN THE SOFTWARE. 00021 * **************************************************************************** 00022 * 00023 * gdalinfo.c 00024 * 00025 * Simple application for dumping all the data about a dataset. Mainly 00026 * serves as an early test harnass. 00027 * 00028 * $Log: gdalinfo.c,v $ 00029 * Revision 1.30 2003/11/24 17:46:58 warmerda 00030 * Report full geotransform when image is not northup. 00031 * 00032 * Revision 1.29 2003/05/01 13:16:37 warmerda 00033 * dont generate error reports if instantiating coordinate transform fails 00034 * 00035 * Revision 1.28 2003/04/22 16:02:08 dron 00036 * New switches: -nogcp and -nomd to suppress printing out GCPs list and metadata 00037 * strings respectively. 00038 * 00039 * Revision 1.27 2002/09/20 14:32:15 warmerda 00040 * don't build gdalinfo statically any more 00041 * 00042 * Revision 1.26 2002/09/04 06:53:42 warmerda 00043 * added GDALDestroyDriverManager() for cleanup 00044 * 00045 * Revision 1.25 2002/05/29 15:53:14 warmerda 00046 * added GDALDumpOpenDatasets 00047 * 00048 * Revision 1.24 2002/04/16 14:00:25 warmerda 00049 * added GDALVersionInfo 00050 * 00051 * Revision 1.23 2002/03/25 13:50:07 warmerda 00052 * only report min/max values if fetch successfully 00053 * 00054 * Revision 1.22 2002/01/13 01:42:37 warmerda 00055 * add -sample test 00056 * 00057 * Revision 1.21 2001/11/17 21:40:58 warmerda 00058 * converted to use OGR projection services 00059 * 00060 * Revision 1.20 2001/11/02 22:21:36 warmerda 00061 * fixed memory leak 00062 * 00063 * Revision 1.19 2001/07/18 05:05:12 warmerda 00064 * added CPL_CSVID 00065 * 00066 * Revision 1.18 2001/07/05 13:12:40 warmerda 00067 * added UnitType support 00068 * 00069 * Revision 1.17 2001/06/28 19:40:12 warmerda 00070 * added subdatset reporting 00071 * 00072 * Revision 1.16 2000/11/29 20:52:53 warmerda 00073 * Add pretty printing of projection. 00074 * 00075 * Revision 1.15 2000/08/25 14:26:02 warmerda 00076 * added nodata, and arbitrary overview reporting 00077 * 00078 * Revision 1.14 2000/06/12 14:21:43 warmerda 00079 * Fixed min/max printf. 00080 * 00081 * Revision 1.13 2000/05/15 14:06:26 warmerda 00082 * Added -mm for computing min/max, and fixed ovewriting of i. 00083 * 00084 * Revision 1.12 2000/04/21 21:57:33 warmerda 00085 * updated the way metadata is handled 00086 * 00087 * Revision 1.11 2000/03/31 13:43:21 warmerda 00088 * added code to report all corners and gcps 00089 * 00090 * Revision 1.10 2000/03/29 15:33:32 warmerda 00091 * added block size 00092 * 00093 * Revision 1.9 2000/03/06 21:50:37 warmerda 00094 * added min/max support 00095 * 00096 * Revision 1.8 2000/03/06 02:18:13 warmerda 00097 * added overviews, and colour table 00098 * 00099 * Revision 1.6 1999/12/30 02:40:17 warmerda 00100 * Report driver used. 00101 * 00102 * Revision 1.5 1999/10/21 13:22:59 warmerda 00103 * Print band type symbolically rather than numerically. 00104 * 00105 * Revision 1.4 1999/10/01 14:45:14 warmerda 00106 * prettied up 00107 * 00108 * Revision 1.3 1999/03/02 21:12:01 warmerda 00109 * add DMS reporting of lat/long 00110 * 00111 * Revision 1.2 1999/01/11 15:27:59 warmerda 00112 * Add projection support 00113 * 00114 * Revision 1.1 1998/12/03 18:35:06 warmerda 00115 * New 00116 * 00117 */ 00118 00119 #include "gdal.h" 00120 #include "ogr_srs_api.h" 00121 #include "cpl_string.h" 00122 00123 CPL_CVSID("$Id: gdalinfo.c,v 1.30 2003/11/24 17:46:58 warmerda Exp $"); 00124 00125 static int 00126 GDALInfoReportCorner( GDALDatasetH hDataset, 00127 const char * corner_name, 00128 double x, double y ); 00129 00130 /************************************************************************/ 00131 /* Usage() */ 00132 /************************************************************************/ 00133 00134 void Usage() 00135 00136 { 00137 printf( "Usage: gdalinfo [--version] [--formats] [-mm] [-nogcp] [-nomd] " 00138 "datasetname\n"); 00139 exit( 1 ); 00140 } 00141 00142 /************************************************************************/ 00143 /* main() */ 00144 /************************************************************************/ 00145 00146 int main( int argc, char ** argv ) 00147 00148 { 00149 GDALDatasetH hDataset; 00150 GDALRasterBandH hBand; 00151 int i, iBand; 00152 double adfGeoTransform[6]; 00153 GDALDriverH hDriver; 00154 char **papszMetadata; 00155 int bComputeMinMax = FALSE, bSample = FALSE; 00156 int bShowGCPs = TRUE, bShowMetadata = TRUE ; 00157 const char *pszFilename = NULL; 00158 00159 GDALAllRegister(); 00160 00161 /* -------------------------------------------------------------------- */ 00162 /* Parse arguments. */ 00163 /* -------------------------------------------------------------------- */ 00164 for( i = 1; i < argc; i++ ) 00165 { 00166 if( EQUAL(argv[i], "-mm") ) 00167 { 00168 bComputeMinMax = TRUE; 00169 } 00170 else if( EQUAL(argv[i] ,"--version") ) 00171 { 00172 printf( "%s\n", GDALVersionInfo( "--version" ) ); 00173 exit( 0 ); 00174 } 00175 else if( EQUAL(argv[i], "--formats") ) 00176 { 00177 int iDr; 00178 00179 printf( "Supported Formats:\n" ); 00180 for( iDr = 0; iDr < GDALGetDriverCount(); iDr++ ) 00181 { 00182 GDALDriverH hDriver = GDALGetDriver(iDr); 00183 00184 printf( " %s: %s\n", 00185 GDALGetDriverShortName( hDriver ), 00186 GDALGetDriverLongName( hDriver ) ); 00187 } 00188 00189 exit( 0 ); 00190 } 00191 else if( EQUAL(argv[i], "-sample") ) 00192 bSample = TRUE; 00193 else if( EQUAL(argv[i], "-nogcp") ) 00194 bShowGCPs = FALSE; 00195 else if( EQUAL(argv[i], "-nomd") ) 00196 bShowMetadata = FALSE; 00197 else if( argv[i][0] == '-' ) 00198 Usage(); 00199 else if( pszFilename == NULL ) 00200 pszFilename = argv[i]; 00201 else 00202 Usage(); 00203 } 00204 00205 if( pszFilename == NULL ) 00206 Usage(); 00207 00208 /* -------------------------------------------------------------------- */ 00209 /* Open dataset. */ 00210 /* -------------------------------------------------------------------- */ 00211 hDataset = GDALOpen( pszFilename, GA_ReadOnly ); 00212 00213 if( hDataset == NULL ) 00214 { 00215 fprintf( stderr, 00216 "GDALOpen failed - %d\n%s\n", 00217 CPLGetLastErrorNo(), CPLGetLastErrorMsg() ); 00218 exit( 1 ); 00219 } 00220 00221 /* -------------------------------------------------------------------- */ 00222 /* Report general info. */ 00223 /* -------------------------------------------------------------------- */ 00224 hDriver = GDALGetDatasetDriver( hDataset ); 00225 printf( "Driver: %s/%s\n", 00226 GDALGetDriverShortName( hDriver ), 00227 GDALGetDriverLongName( hDriver ) ); 00228 00229 printf( "Size is %d, %d\n", 00230 GDALGetRasterXSize( hDataset ), 00231 GDALGetRasterYSize( hDataset ) ); 00232 00233 /* -------------------------------------------------------------------- */ 00234 /* Report projection. */ 00235 /* -------------------------------------------------------------------- */ 00236 if( GDALGetProjectionRef( hDataset ) != NULL ) 00237 { 00238 OGRSpatialReferenceH hSRS; 00239 char *pszProjection; 00240 00241 pszProjection = (char *) GDALGetProjectionRef( hDataset ); 00242 00243 hSRS = OSRNewSpatialReference(NULL); 00244 if( OSRImportFromWkt( hSRS, &pszProjection ) == CE_None ) 00245 { 00246 char *pszPrettyWkt = NULL; 00247 00248 OSRExportToPrettyWkt( hSRS, &pszPrettyWkt, FALSE ); 00249 printf( "Coordinate System is:\n%s\n", pszPrettyWkt ); 00250 CPLFree( pszPrettyWkt ); 00251 } 00252 else 00253 printf( "Coordinate System is `%s'\n", 00254 GDALGetProjectionRef( hDataset ) ); 00255 00256 OSRDestroySpatialReference( hSRS ); 00257 } 00258 00259 /* -------------------------------------------------------------------- */ 00260 /* Report Geotransform. */ 00261 /* -------------------------------------------------------------------- */ 00262 if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None ) 00263 { 00264 if( adfGeoTransform[2] == 0.0 && adfGeoTransform[4] == 0.0 ) 00265 { 00266 printf( "Origin = (%.6f,%.6f)\n", 00267 adfGeoTransform[0], adfGeoTransform[3] ); 00268 00269 printf( "Pixel Size = (%.8f,%.8f)\n", 00270 adfGeoTransform[1], adfGeoTransform[5] ); 00271 } 00272 else 00273 printf( "GeoTransform =\n" 00274 " %.16g, %.16g, %.16g\n" 00275 " %.16g, %.16g, %.16g\n", 00276 adfGeoTransform[0], 00277 adfGeoTransform[1], 00278 adfGeoTransform[2], 00279 adfGeoTransform[3], 00280 adfGeoTransform[4], 00281 adfGeoTransform[5] ); 00282 } 00283 00284 /* -------------------------------------------------------------------- */ 00285 /* Report GCPs. */ 00286 /* -------------------------------------------------------------------- */ 00287 if( bShowGCPs && GDALGetGCPCount( hDataset ) > 0 ) 00288 { 00289 printf( "GCP Projection = %s\n", GDALGetGCPProjection(hDataset) ); 00290 for( i = 0; i < GDALGetGCPCount(hDataset); i++ ) 00291 { 00292 const GDAL_GCP *psGCP; 00293 00294 psGCP = GDALGetGCPs( hDataset ) + i; 00295 00296 printf( "GCP[%3d]: Id=%s, Info=%s\n" 00297 " (%g,%g) -> (%g,%g,%g)\n", 00298 i, psGCP->pszId, psGCP->pszInfo, 00299 psGCP->dfGCPPixel, psGCP->dfGCPLine, 00300 psGCP->dfGCPX, psGCP->dfGCPY, psGCP->dfGCPZ ); 00301 } 00302 } 00303 00304 /* -------------------------------------------------------------------- */ 00305 /* Report metadata. */ 00306 /* -------------------------------------------------------------------- */ 00307 papszMetadata = GDALGetMetadata( hDataset, NULL ); 00308 if( bShowMetadata && CSLCount(papszMetadata) > 0 ) 00309 { 00310 printf( "Metadata:\n" ); 00311 for( i = 0; papszMetadata[i] != NULL; i++ ) 00312 { 00313 printf( " %s\n", papszMetadata[i] ); 00314 } 00315 } 00316 00317 /* -------------------------------------------------------------------- */ 00318 /* Report subdatasets. */ 00319 /* -------------------------------------------------------------------- */ 00320 papszMetadata = GDALGetMetadata( hDataset, "SUBDATASETS" ); 00321 if( CSLCount(papszMetadata) > 0 ) 00322 { 00323 printf( "Subdatasets:\n" ); 00324 for( i = 0; papszMetadata[i] != NULL; i++ ) 00325 { 00326 printf( " %s\n", papszMetadata[i] ); 00327 } 00328 } 00329 00330 /* -------------------------------------------------------------------- */ 00331 /* Report corners. */ 00332 /* -------------------------------------------------------------------- */ 00333 printf( "Corner Coordinates:\n" ); 00334 GDALInfoReportCorner( hDataset, "Upper Left", 00335 0.0, 0.0 ); 00336 GDALInfoReportCorner( hDataset, "Lower Left", 00337 0.0, GDALGetRasterYSize(hDataset)); 00338 GDALInfoReportCorner( hDataset, "Upper Right", 00339 GDALGetRasterXSize(hDataset), 0.0 ); 00340 GDALInfoReportCorner( hDataset, "Lower Right", 00341 GDALGetRasterXSize(hDataset), 00342 GDALGetRasterYSize(hDataset) ); 00343 GDALInfoReportCorner( hDataset, "Center", 00344 GDALGetRasterXSize(hDataset)/2.0, 00345 GDALGetRasterYSize(hDataset)/2.0 ); 00346 00347 /* ==================================================================== */ 00348 /* Loop over bands. */ 00349 /* ==================================================================== */ 00350 for( iBand = 0; iBand < GDALGetRasterCount( hDataset ); iBand++ ) 00351 { 00352 double dfMin, dfMax, adfCMinMax[2], dfNoData; 00353 int bGotMin, bGotMax, bGotNodata; 00354 int nBlockXSize, nBlockYSize; 00355 00356 hBand = GDALGetRasterBand( hDataset, iBand+1 ); 00357 00358 if( bSample ) 00359 { 00360 float afSample[10000]; 00361 int nCount; 00362 00363 nCount = GDALGetRandomRasterSample( hBand, 10000, afSample ); 00364 printf( "Got %d samples.\n", nCount ); 00365 } 00366 00367 GDALGetBlockSize( hBand, &nBlockXSize, &nBlockYSize ); 00368 printf( "Band %d Block=%dx%d Type=%s, ColorInterp=%s\n", iBand+1, 00369 nBlockXSize, nBlockYSize, 00370 GDALGetDataTypeName( 00371 GDALGetRasterDataType(hBand)), 00372 GDALGetColorInterpretationName( 00373 GDALGetRasterColorInterpretation(hBand)) ); 00374 00375 if( GDALGetDescription( hBand ) != NULL 00376 && strlen(GDALGetDescription( hBand )) > 0 ) 00377 printf( " Description = %s\n", GDALGetDescription(hBand) ); 00378 00379 dfMin = GDALGetRasterMinimum( hBand, &bGotMin ); 00380 dfMax = GDALGetRasterMaximum( hBand, &bGotMax ); 00381 if( bGotMin || bGotMax || bComputeMinMax ) 00382 { 00383 printf( " " ); 00384 if( bGotMin ) 00385 printf( "Min=%.3f ", dfMin ); 00386 if( bGotMax ) 00387 printf( "Max=%.3f ", dfMax ); 00388 00389 if( bComputeMinMax ) 00390 { 00391 GDALComputeRasterMinMax( hBand, TRUE, adfCMinMax ); 00392 printf( " Computed Min/Max=%.3f,%.3f", 00393 adfCMinMax[0], adfCMinMax[1] ); 00394 } 00395 00396 printf( "\n" ); 00397 } 00398 00399 dfNoData = GDALGetRasterNoDataValue( hBand, &bGotNodata ); 00400 if( bGotNodata ) 00401 { 00402 printf( " NoData Value=%g\n", dfNoData ); 00403 } 00404 00405 if( GDALGetOverviewCount(hBand) > 0 ) 00406 { 00407 int iOverview; 00408 00409 printf( " Overviews: " ); 00410 for( iOverview = 0; 00411 iOverview < GDALGetOverviewCount(hBand); 00412 iOverview++ ) 00413 { 00414 GDALRasterBandH hOverview; 00415 00416 if( iOverview != 0 ) 00417 printf( ", " ); 00418 00419 hOverview = GDALGetOverview( hBand, iOverview ); 00420 printf( "%dx%d", 00421 GDALGetRasterBandXSize( hOverview ), 00422 GDALGetRasterBandYSize( hOverview ) ); 00423 } 00424 printf( "\n" ); 00425 } 00426 00427 if( GDALHasArbitraryOverviews( hBand ) ) 00428 { 00429 printf( " Overviews: arbitrary\n" ); 00430 } 00431 00432 if( strlen(GDALGetRasterUnitType(hBand)) > 0 ) 00433 { 00434 printf( " Unit Type: %s\n", GDALGetRasterUnitType(hBand) ); 00435 } 00436 00437 papszMetadata = GDALGetMetadata( hBand, NULL ); 00438 if( CSLCount(papszMetadata) > 0 ) 00439 { 00440 printf( "Metadata:\n" ); 00441 for( i = 0; papszMetadata[i] != NULL; i++ ) 00442 { 00443 printf( " %s\n", papszMetadata[i] ); 00444 } 00445 } 00446 00447 if( GDALGetRasterColorInterpretation(hBand) == GCI_PaletteIndex ) 00448 { 00449 GDALColorTableH hTable; 00450 int i; 00451 00452 hTable = GDALGetRasterColorTable( hBand ); 00453 printf( " Color Table (%s with %d entries)\n", 00454 GDALGetPaletteInterpretationName( 00455 GDALGetPaletteInterpretation( hTable )), 00456 GDALGetColorEntryCount( hTable ) ); 00457 00458 for( i = 0; i < GDALGetColorEntryCount( hTable ); i++ ) 00459 { 00460 GDALColorEntry sEntry; 00461 00462 GDALGetColorEntryAsRGB( hTable, i, &sEntry ); 00463 printf( " %3d: %d,%d,%d,%d\n", 00464 i, 00465 sEntry.c1, 00466 sEntry.c2, 00467 sEntry.c3, 00468 sEntry.c4 ); 00469 } 00470 } 00471 } 00472 00473 GDALClose( hDataset ); 00474 00475 GDALDumpOpenDatasets( stderr ); 00476 00477 GDALDestroyDriverManager(); 00478 00479 exit( 0 ); 00480 } 00481 00482 /************************************************************************/ 00483 /* GDALInfoReportCorner() */ 00484 /************************************************************************/ 00485 00486 static int 00487 GDALInfoReportCorner( GDALDatasetH hDataset, 00488 const char * corner_name, 00489 double x, double y ) 00490 00491 { 00492 double dfGeoX, dfGeoY; 00493 const char *pszProjection; 00494 double adfGeoTransform[6]; 00495 OGRCoordinateTransformationH hTransform = NULL; 00496 00497 printf( "%-11s ", corner_name ); 00498 00499 /* -------------------------------------------------------------------- */ 00500 /* Transform the point into georeferenced coordinates. */ 00501 /* -------------------------------------------------------------------- */ 00502 if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None ) 00503 { 00504 pszProjection = GDALGetProjectionRef(hDataset); 00505 00506 dfGeoX = adfGeoTransform[0] + adfGeoTransform[1] * x 00507 + adfGeoTransform[2] * y; 00508 dfGeoY = adfGeoTransform[3] + adfGeoTransform[4] * x 00509 + adfGeoTransform[5] * y; 00510 } 00511 00512 else 00513 { 00514 printf( "(%7.1f,%7.1f)\n", x, y ); 00515 return FALSE; 00516 } 00517 00518 /* -------------------------------------------------------------------- */ 00519 /* Report the georeferenced coordinates. */ 00520 /* -------------------------------------------------------------------- */ 00521 if( ABS(dfGeoX) < 181 && ABS(dfGeoY) < 91 ) 00522 { 00523 printf( "(%12.7f,%12.7f) ", dfGeoX, dfGeoY ); 00524 00525 } 00526 else 00527 { 00528 printf( "(%12.3f,%12.3f) ", dfGeoX, dfGeoY ); 00529 } 00530 00531 /* -------------------------------------------------------------------- */ 00532 /* Setup transformation to lat/long. */ 00533 /* -------------------------------------------------------------------- */ 00534 if( pszProjection != NULL && strlen(pszProjection) > 0 ) 00535 { 00536 OGRSpatialReferenceH hProj, hLatLong = NULL; 00537 00538 hProj = OSRNewSpatialReference( pszProjection ); 00539 if( hProj != NULL ) 00540 hLatLong = OSRCloneGeogCS( hProj ); 00541 00542 if( hLatLong != NULL ) 00543 { 00544 CPLPushErrorHandler( CPLQuietErrorHandler ); 00545 hTransform = OCTNewCoordinateTransformation( hProj, hLatLong ); 00546 CPLPopErrorHandler(); 00547 00548 OSRDestroySpatialReference( hLatLong ); 00549 } 00550 00551 if( hProj != NULL ) 00552 OSRDestroySpatialReference( hProj ); 00553 } 00554 00555 /* -------------------------------------------------------------------- */ 00556 /* Transform to latlong and report. */ 00557 /* -------------------------------------------------------------------- */ 00558 if( hTransform != NULL 00559 && OCTTransform(hTransform,1,&dfGeoX,&dfGeoY,NULL) ) 00560 { 00561 00562 printf( "(%s,", GDALDecToDMS( dfGeoX, "Long", 2 ) ); 00563 printf( "%s)", GDALDecToDMS( dfGeoY, "Lat", 2 ) ); 00564 } 00565 00566 if( hTransform != NULL ) 00567 OCTDestroyCoordinateTransformation( hTransform ); 00568 00569 printf( "\n" ); 00570 00571 return TRUE; 00572 } 00573

Generated on Thu Jul 29 19:47:53 2004 for GDAL by doxygen 1.3.7