#include "gdal.h"
#include "ogr_srs_api.h"
#include "cpl_string.h"
#include "cpl_conv.h"
#include "cpl_multiproc.h"
CPL_CVSID("$Id: gdalinfo.c 10646 2007-01-18 02:38:10Z warmerdam $");
static int
GDALInfoReportCorner( GDALDatasetH hDataset,
const char * corner_name,
double x, double y );
void Usage()
{
printf( "Usage: gdalinfo [--help-general] [-mm] [-stats] [-nogcp] [-nomd]\n"
" [-mdd domain]* datasetname\n" );
exit( 1 );
}
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 ;
int bStats = FALSE, iMDD;
const char *pszFilename = NULL;
char **papszExtraMDDomains = NULL;
GDALAllRegister();
argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 );
if( argc < 1 )
exit( -argc );
for( i = 1; i < argc; i++ )
{
if( EQUAL(argv[i], "-mm") )
bComputeMinMax = TRUE;
else if( EQUAL(argv[i], "-stats") )
bStats = TRUE;
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( EQUAL(argv[i], "-mdd") && i < argc-1 )
papszExtraMDDomains = CSLAddString( papszExtraMDDomains,
argv[++i] );
else if( argv[i][0] == '-' )
Usage();
else if( pszFilename == NULL )
pszFilename = argv[i];
else
Usage();
}
if( pszFilename == NULL )
Usage();
hDataset = GDALOpen( pszFilename, GA_ReadOnly );
if( hDataset == NULL )
{
fprintf( stderr,
"GDALOpen failed - %d\n%s\n",
CPLGetLastErrorNo(), CPLGetLastErrorMsg() );
CSLDestroy( argv );
GDALDumpOpenDatasets( stderr );
GDALDestroyDriverManager();
CPLDumpSharedList( NULL );
exit( 1 );
}
hDriver = GDALGetDatasetDriver( hDataset );
printf( "Driver: %s/%s\n",
GDALGetDriverShortName( hDriver ),
GDALGetDriverLongName( hDriver ) );
printf( "Size is %d, %d\n",
GDALGetRasterXSize( hDataset ),
GDALGetRasterYSize( hDataset ) );
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 );
}
if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None )
{
if( adfGeoTransform[2] == 0.0 && adfGeoTransform[4] == 0.0 )
{
printf( "Origin = (%.15f,%.15f)\n",
adfGeoTransform[0], adfGeoTransform[3] );
printf( "Pixel Size = (%.15f,%.15f)\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] );
}
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"
" (%.15g,%.15g) -> (%.15g,%.15g,%.15g)\n",
i, psGCP->pszId, psGCP->pszInfo,
psGCP->dfGCPPixel, psGCP->dfGCPLine,
psGCP->dfGCPX, psGCP->dfGCPY, psGCP->dfGCPZ );
}
}
papszMetadata = GDALGetMetadata( hDataset, NULL );
if( bShowMetadata && CSLCount(papszMetadata) > 0 )
{
printf( "Metadata:\n" );
for( i = 0; papszMetadata[i] != NULL; i++ )
{
printf( " %s\n", papszMetadata[i] );
}
}
for( iMDD = 0; iMDD < CSLCount(papszExtraMDDomains); iMDD++ )
{
papszMetadata = GDALGetMetadata( hDataset, papszExtraMDDomains[iMDD] );
if( bShowMetadata && CSLCount(papszMetadata) > 0 )
{
printf( "Metadata (%s):\n", papszExtraMDDomains[iMDD]);
for( i = 0; papszMetadata[i] != NULL; i++ )
{
printf( " %s\n", papszMetadata[i] );
}
}
}
papszMetadata = GDALGetMetadata( hDataset, "IMAGE_STRUCTURE" );
if( bShowMetadata && CSLCount(papszMetadata) > 0 )
{
printf( "Image Structure Metadata:\n" );
for( i = 0; papszMetadata[i] != NULL; i++ )
{
printf( " %s\n", papszMetadata[i] );
}
}
papszMetadata = GDALGetMetadata( hDataset, "SUBDATASETS" );
if( CSLCount(papszMetadata) > 0 )
{
printf( "Subdatasets:\n" );
for( i = 0; papszMetadata[i] != NULL; i++ )
{
printf( " %s\n", papszMetadata[i] );
}
}
papszMetadata = GDALGetMetadata( hDataset, "GEOLOCATION" );
if( CSLCount(papszMetadata) > 0 )
{
printf( "Geolocation:\n" );
for( i = 0; papszMetadata[i] != NULL; i++ )
{
printf( " %s\n", papszMetadata[i] );
}
}
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 );
for( iBand = 0; iBand < GDALGetRasterCount( hDataset ); iBand++ )
{
double dfMin, dfMax, adfCMinMax[2], dfNoData;
int bGotMin, bGotMax, bGotNodata, bSuccess;
int nBlockXSize, nBlockYSize;
double dfMean, dfStdDev;
GDALColorTableH hTable;
CPLErr eErr;
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, FALSE, adfCMinMax );
printf( " Computed Min/Max=%.3f,%.3f",
adfCMinMax[0], adfCMinMax[1] );
}
printf( "\n" );
}
eErr = GDALGetRasterStatistics( hBand, FALSE, bStats,
&dfMin, &dfMax, &dfMean, &dfStdDev );
if( eErr == CE_None )
{
printf( " Minimum=%.3f, Maximum=%.3f, Mean=%.3f, StdDev=%.3f\n",
dfMin, dfMax, dfMean, dfStdDev );
}
dfNoData = GDALGetRasterNoDataValue( hBand, &bGotNodata );
if( bGotNodata )
{
printf( " NoData Value=%.15g\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) );
}
if( GDALGetRasterCategoryNames(hBand) != NULL )
{
char **papszCategories = GDALGetRasterCategoryNames(hBand);
int i;
printf( " Categories:\n" );
for( i = 0; papszCategories[i] != NULL; i++ )
printf( " %3d: %s\n", i, papszCategories[i] );
}
if( GDALGetRasterScale( hBand, &bSuccess ) != 1.0
|| GDALGetRasterOffset( hBand, &bSuccess ) != 0.0 )
printf( " Offset: %.15g, Scale:%.15g\n",
GDALGetRasterOffset( hBand, &bSuccess ),
GDALGetRasterScale( hBand, &bSuccess ) );
papszMetadata = GDALGetMetadata( hBand, NULL );
if( bShowMetadata && CSLCount(papszMetadata) > 0 )
{
printf( " Metadata:\n" );
for( i = 0; papszMetadata[i] != NULL; i++ )
{
printf( " %s\n", papszMetadata[i] );
}
}
papszMetadata = GDALGetMetadata( hBand, "IMAGE_STRUCTURE" );
if( bShowMetadata && CSLCount(papszMetadata) > 0 )
{
printf( " Image Structure Metadata:\n" );
for( i = 0; papszMetadata[i] != NULL; i++ )
{
printf( " %s\n", papszMetadata[i] );
}
}
if( GDALGetRasterColorInterpretation(hBand) == GCI_PaletteIndex
&& (hTable = GDALGetRasterColorTable( hBand )) != NULL )
{
int i;
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 );
}
}
if( GDALGetDefaultRAT( hBand ) != NULL )
{
GDALRasterAttributeTableH hRAT = GDALGetDefaultRAT( hBand );
GDALRATDumpReadable( hRAT, NULL );
}
}
GDALClose( hDataset );
CSLDestroy( papszExtraMDDomains );
CSLDestroy( argv );
GDALDumpOpenDatasets( stderr );
GDALDestroyDriverManager();
CPLDumpSharedList( NULL );
CPLCleanupTLS();
exit( 0 );
}
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 );
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;
}
if( ABS(dfGeoX) < 181 && ABS(dfGeoY) < 91 )
{
printf( "(%12.7f,%12.7f) ", dfGeoX, dfGeoY );
}
else
{
printf( "(%12.3f,%12.3f) ", dfGeoX, dfGeoY );
}
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 );
}
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;
}