<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta name="robots" content="noindex"> <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>Simple C Example: gdalinfo.c</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body bgcolor="#ffffff"> <!-- Generated by Doxygen 1.2.3-20001105 on Thu Mar 28 09:47:33 2002 --> <center> <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> <a class="qindex" href="pages.html">Related Pages</a> </center> <hr><a name="gdalinfo.c"><h2>Simple C Example: gdalinfo.c</h2></a> <center> <p> </center> <p> <div class="fragment"><pre><font class="comment">/* ****************************************************************************</font> <font class="comment"> * Copyright (c) 1998, Frank Warmerdam</font> <font class="comment"> *</font> <font class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a</font> <font class="comment"> * copy of this software and associated documentation files (the "Software"),</font> <font class="comment"> * to deal in the Software without restriction, including without limitation</font> <font class="comment"> * the rights to use, copy, modify, merge, publish, distribute, sublicense,</font> <font class="comment"> * and/or sell copies of the Software, and to permit persons to whom the</font> <font class="comment"> * Software is furnished to do so, subject to the following conditions:</font> <font class="comment"> *</font> <font class="comment"> * The above copyright notice and this permission notice shall be included</font> <font class="comment"> * in all copies or substantial portions of the Software.</font> <font class="comment"> *</font> <font class="comment"> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS</font> <font class="comment"> * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</font> <font class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL</font> <font class="comment"> * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</font> <font class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING</font> <font class="comment"> * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER</font> <font class="comment"> * DEALINGS IN THE SOFTWARE.</font> <font class="comment"> * ****************************************************************************</font> <font class="comment"> *</font> <font class="comment"> * gdalinfo.c</font> <font class="comment"> *</font> <font class="comment"> * Simple application for dumping all the data about a dataset. Mainly</font> <font class="comment"> * serves as an early test harnass.</font> <font class="comment"> *</font> <font class="comment"> * $Log: gdalinfo.c,v $</font> <font class="comment"> * Revision 1.23 2002/03/25 13:50:07 warmerda</font> <font class="comment"> * only report min/max values if fetch successfully</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.22 2002/01/13 01:42:37 warmerda</font> <font class="comment"> * add -sample test</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.21 2001/11/17 21:40:58 warmerda</font> <font class="comment"> * converted to use OGR projection services</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.20 2001/11/02 22:21:36 warmerda</font> <font class="comment"> * fixed memory leak</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.19 2001/07/18 05:05:12 warmerda</font> <font class="comment"> * added CPL_CSVID</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.18 2001/07/05 13:12:40 warmerda</font> <font class="comment"> * added UnitType support</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.17 2001/06/28 19:40:12 warmerda</font> <font class="comment"> * added subdatset reporting</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.16 2000/11/29 20:52:53 warmerda</font> <font class="comment"> * Add pretty printing of projection.</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.15 2000/08/25 14:26:02 warmerda</font> <font class="comment"> * added nodata, and arbitrary overview reporting</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.14 2000/06/12 14:21:43 warmerda</font> <font class="comment"> * Fixed min/max printf.</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.13 2000/05/15 14:06:26 warmerda</font> <font class="comment"> * Added -mm for computing min/max, and fixed ovewriting of i.</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.12 2000/04/21 21:57:33 warmerda</font> <font class="comment"> * updated the way metadata is handled</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.11 2000/03/31 13:43:21 warmerda</font> <font class="comment"> * added code to report all corners and gcps</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.10 2000/03/29 15:33:32 warmerda</font> <font class="comment"> * added block size</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.9 2000/03/06 21:50:37 warmerda</font> <font class="comment"> * added min/max support</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.8 2000/03/06 02:18:13 warmerda</font> <font class="comment"> * added overviews, and colour table</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.6 1999/12/30 02:40:17 warmerda</font> <font class="comment"> * Report driver used.</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.5 1999/10/21 13:22:59 warmerda</font> <font class="comment"> * Print band type symbolically rather than numerically.</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.4 1999/10/01 14:45:14 warmerda</font> <font class="comment"> * prettied up</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.3 1999/03/02 21:12:01 warmerda</font> <font class="comment"> * add DMS reporting of lat/long</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.2 1999/01/11 15:27:59 warmerda</font> <font class="comment"> * Add projection support</font> <font class="comment"> *</font> <font class="comment"> * Revision 1.1 1998/12/03 18:35:06 warmerda</font> <font class="comment"> * New</font> <font class="comment"> *</font> <font class="comment"> */</font> <font class="preprocessor">#include "<a class="code" href="gdal_h.html">gdal.h</a>"</font> <font class="preprocessor">#include "ogr_srs_api.h"</font> <font class="preprocessor">#include "cpl_string.h"</font> CPL_CVSID(<font class="stringliteral">"$Id: gdalinfo.c,v 1.23 2002/03/25 13:50:07 warmerda Exp $"</font>); <font class="keyword">static</font> <font class="keywordtype">int</font> GDALInfoReportCorner( GDALDatasetH hDataset, <font class="keyword">const</font> <font class="keywordtype">char</font> * corner_name, <font class="keywordtype">double</font> x, <font class="keywordtype">double</font> y ); <font class="comment">/************************************************************************/</font> <font class="comment">/* main() */</font> <font class="comment">/************************************************************************/</font> <font class="keywordtype">int</font> main( <font class="keywordtype">int</font> argc, <font class="keywordtype">char</font> ** argv )<font class="keyword"></font> <font class="keyword"></font> <font class="keyword"></font>{ GDALDatasetH hDataset; GDALRasterBandH hBand; <font class="keywordtype">int</font> i, iBand; <font class="keywordtype">double</font> adfGeoTransform[6]; GDALDriverH hDriver; <font class="keywordtype">char</font> **papszMetadata; <font class="keywordtype">int</font> bComputeMinMax = FALSE, bSample = FALSE; <font class="keywordflow">if</font>( argc < 2 ) { printf( <font class="stringliteral">"Usage: gdalinfo [-mm] datasetname\n"</font> ); exit( 10 ); } <font class="keywordflow">if</font>( EQUAL(argv[1],<font class="stringliteral">"-mm"</font>) ) { bComputeMinMax = TRUE; argv++; } <font class="keywordflow">if</font>( EQUAL(argv[1],<font class="stringliteral">"-sample"</font>) ) { bSample = TRUE; argv++; } <a class="code" href="gdal_h.html#a57">GDALAllRegister</a>(); hDataset = <a class="code" href="gdal_h.html#a60">GDALOpen</a>( argv[1], GA_ReadOnly ); <font class="keywordflow">if</font>( hDataset == NULL ) { fprintf( stderr, <font class="stringliteral">"GDALOpen failed - %d\n%s\n"</font>, <a class="code" href="cpl_error_h.html#a20">CPLGetLastErrorNo</a>(), <a class="code" href="cpl_error_h.html#a22">CPLGetLastErrorMsg</a>() ); exit( 1 ); } <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="comment">/* Report general info. */</font> <font class="comment">/* -------------------------------------------------------------------- */</font> hDriver = GDALGetDatasetDriver( hDataset ); printf( <font class="stringliteral">"Driver: %s/%s\n"</font>, GDALGetDriverShortName( hDriver ), GDALGetDriverLongName( hDriver ) ); printf( <font class="stringliteral">"Size is %d, %d\n"</font>, GDALGetRasterXSize( hDataset ), GDALGetRasterYSize( hDataset ) ); <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="comment">/* Report projection. */</font> <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="keywordflow">if</font>( GDALGetProjectionRef( hDataset ) != NULL ) { OGRSpatialReferenceH hSRS; <font class="keywordtype">char</font> *pszProjection; pszProjection = (<font class="keywordtype">char</font> *) GDALGetProjectionRef( hDataset ); hSRS = OSRNewSpatialReference(NULL); <font class="keywordflow">if</font>( OSRImportFromWkt( hSRS, &pszProjection ) == CE_None ) { <font class="keywordtype">char</font> *pszPrettyWkt = NULL; OSRExportToPrettyWkt( hSRS, &pszPrettyWkt, FALSE ); printf( <font class="stringliteral">"Coordinate System is:\n%s\n"</font>, pszPrettyWkt ); } <font class="keywordflow">else</font> printf( <font class="stringliteral">"Coordinate System is `%s'\n"</font>, GDALGetProjectionRef( hDataset ) ); OSRDestroySpatialReference( hSRS ); } <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="comment">/* Report Geotransform. */</font> <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="keywordflow">if</font>( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None ) { printf( <font class="stringliteral">"Origin = (%.6f,%.6f)\n"</font>, adfGeoTransform[0], adfGeoTransform[3] ); printf( <font class="stringliteral">"Pixel Size = (%.6f,%.6f)\n"</font>, adfGeoTransform[1], adfGeoTransform[5] ); } <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="comment">/* Report GCPs. */</font> <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="keywordflow">if</font>( GDALGetGCPCount( hDataset ) > 0 ) { printf( <font class="stringliteral">"GCP Projection = %s\n"</font>, GDALGetGCPProjection(hDataset) ); <font class="keywordflow">for</font>( i = 0; i < GDALGetGCPCount(hDataset); i++ ) { <font class="keyword">const</font> <a class="code" href="struct_GDAL_GCP.html">GDAL_GCP</a> *psGCP; psGCP = GDALGetGCPs( hDataset ) + i; printf( <font class="stringliteral">"GCP[%3d]: Id=%s, Info=%s\n"</font> <font class="stringliteral">" (%g,%g) -> (%g,%g,%g)\n"</font>, i, psGCP->pszId, psGCP->pszInfo, psGCP->dfGCPPixel, psGCP->dfGCPLine, psGCP->dfGCPX, psGCP->dfGCPY, psGCP->dfGCPZ ); } } <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="comment">/* Report metadata. */</font> <font class="comment">/* -------------------------------------------------------------------- */</font> papszMetadata = GDALGetMetadata( hDataset, NULL ); <font class="keywordflow">if</font>( CSLCount(papszMetadata) > 0 ) { printf( <font class="stringliteral">"Metadata:\n"</font> ); <font class="keywordflow">for</font>( i = 0; papszMetadata[i] != NULL; i++ ) { printf( <font class="stringliteral">" %s\n"</font>, papszMetadata[i] ); } } <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="comment">/* Report subdatasets. */</font> <font class="comment">/* -------------------------------------------------------------------- */</font> papszMetadata = GDALGetMetadata( hDataset, <font class="stringliteral">"SUBDATASETS"</font> ); <font class="keywordflow">if</font>( CSLCount(papszMetadata) > 0 ) { printf( <font class="stringliteral">"Subdatasets:\n"</font> ); <font class="keywordflow">for</font>( i = 0; papszMetadata[i] != NULL; i++ ) { printf( <font class="stringliteral">" %s\n"</font>, papszMetadata[i] ); } } <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="comment">/* Report corners. */</font> <font class="comment">/* -------------------------------------------------------------------- */</font> printf( <font class="stringliteral">"Corner Coordinates:\n"</font> ); GDALInfoReportCorner( hDataset, <font class="stringliteral">"Upper Left"</font>, 0.0, 0.0 ); GDALInfoReportCorner( hDataset, <font class="stringliteral">"Lower Left"</font>, 0.0, GDALGetRasterYSize(hDataset)); GDALInfoReportCorner( hDataset, <font class="stringliteral">"Upper Right"</font>, GDALGetRasterXSize(hDataset), 0.0 ); GDALInfoReportCorner( hDataset, <font class="stringliteral">"Lower Right"</font>, GDALGetRasterXSize(hDataset), GDALGetRasterYSize(hDataset) ); GDALInfoReportCorner( hDataset, <font class="stringliteral">"Center"</font>, GDALGetRasterXSize(hDataset)/2.0, GDALGetRasterYSize(hDataset)/2.0 ); <font class="comment">/* ==================================================================== */</font> <font class="comment">/* Loop over bands. */</font> <font class="comment">/* ==================================================================== */</font> <font class="keywordflow">for</font>( iBand = 0; iBand < GDALGetRasterCount( hDataset ); iBand++ ) { <font class="keywordtype">double</font> dfMin, dfMax, adfCMinMax[2], dfNoData; <font class="keywordtype">int</font> bGotMin, bGotMax, bGotNodata; <font class="keywordtype">int</font> nBlockXSize, nBlockYSize; hBand = GDALGetRasterBand( hDataset, iBand+1 ); <font class="keywordflow">if</font>( bSample ) { <font class="keywordtype">float</font> afSample[10000]; <font class="keywordtype">int</font> nCount; nCount = GDALGetRandomRasterSample( hBand, 10000, afSample ); printf( <font class="stringliteral">"Got %d samples.\n"</font>, nCount ); } GDALGetBlockSize( hBand, &nBlockXSize, &nBlockYSize ); printf( <font class="stringliteral">"Band %d Block=%dx%d Type=%s, ColorInterp=%s\n"</font>, iBand+1, nBlockXSize, nBlockYSize, GDALGetDataTypeName( GDALGetRasterDataType(hBand)), <a class="code" href="gdal_h.html#a50">GDALGetColorInterpretationName</a>( GDALGetRasterColorInterpretation(hBand)) ); <font class="keywordflow">if</font>( GDALGetDescription( hBand ) != NULL && strlen(GDALGetDescription( hBand )) > 0 ) printf( <font class="stringliteral">" Description = %s\n"</font>, GDALGetDescription(hBand) ); dfMin = GDALGetRasterMinimum( hBand, &bGotMin ); dfMax = GDALGetRasterMaximum( hBand, &bGotMax ); <font class="keywordflow">if</font>( bGotMin || bGotMax || bComputeMinMax ) { printf( <font class="stringliteral">" "</font> ); <font class="keywordflow">if</font>( bGotMin ) printf( <font class="stringliteral">"Min=%.3f "</font>, dfMin ); <font class="keywordflow">if</font>( bGotMax ) printf( <font class="stringliteral">"Max=%.3f "</font>, dfMax ); <font class="keywordflow">if</font>( bComputeMinMax ) { <a class="code" href="gdal_h.html#a118">GDALComputeRasterMinMax</a>( hBand, TRUE, adfCMinMax ); printf( <font class="stringliteral">" Computed Min/Max=%.3f,%.3f"</font>, adfCMinMax[0], adfCMinMax[1] ); } printf( <font class="stringliteral">"\n"</font> ); } dfNoData = GDALGetRasterNoDataValue( hBand, &bGotNodata ); <font class="keywordflow">if</font>( bGotNodata ) { printf( <font class="stringliteral">" NoData Value=%g\n"</font>, dfNoData ); } <font class="keywordflow">if</font>( GDALGetOverviewCount(hBand) > 0 ) { <font class="keywordtype">int</font> iOverview; printf( <font class="stringliteral">" Overviews: "</font> ); <font class="keywordflow">for</font>( iOverview = 0; iOverview < GDALGetOverviewCount(hBand); iOverview++ ) { GDALRasterBandH hOverview; <font class="keywordflow">if</font>( iOverview != 0 ) printf( <font class="stringliteral">", "</font> ); hOverview = GDALGetOverview( hBand, iOverview ); printf( <font class="stringliteral">"%dx%d"</font>, GDALGetRasterBandXSize( hOverview ), GDALGetRasterBandYSize( hOverview ) ); } printf( <font class="stringliteral">"\n"</font> ); } <font class="keywordflow">if</font>( GDALHasArbitraryOverviews( hBand ) ) { printf( <font class="stringliteral">" Overviews: arbitrary\n"</font> ); } <font class="keywordflow">if</font>( strlen(GDALGetRasterUnitType(hBand)) > 0 ) { printf( <font class="stringliteral">" Unit Type: %s\n"</font>, GDALGetRasterUnitType(hBand) ); } papszMetadata = GDALGetMetadata( hBand, NULL ); <font class="keywordflow">if</font>( CSLCount(papszMetadata) > 0 ) { printf( <font class="stringliteral">"Metadata:\n"</font> ); <font class="keywordflow">for</font>( i = 0; papszMetadata[i] != NULL; i++ ) { printf( <font class="stringliteral">" %s\n"</font>, papszMetadata[i] ); } } <font class="keywordflow">if</font>( GDALGetRasterColorInterpretation(hBand) == GCI_PaletteIndex ) { GDALColorTableH hTable; <font class="keywordtype">int</font> i; hTable = GDALGetRasterColorTable( hBand ); printf( <font class="stringliteral">" Color Table (%s with %d entries)\n"</font>, <a class="code" href="gdal_h.html#a51">GDALGetPaletteInterpretationName</a>( GDALGetPaletteInterpretation( hTable )), GDALGetColorEntryCount( hTable ) ); <font class="keywordflow">for</font>( i = 0; i < GDALGetColorEntryCount( hTable ); i++ ) { <a class="code" href="struct_GDALColorEntry.html">GDALColorEntry</a> sEntry; GDALGetColorEntryAsRGB( hTable, i, &sEntry ); printf( <font class="stringliteral">" %3d: %d,%d,%d,%d\n"</font>, i, sEntry.c1, sEntry.c2, sEntry.c3, sEntry.c4 ); } } } GDALClose( hDataset ); exit( 0 ); } <font class="comment">/************************************************************************/</font> <font class="comment">/* GDALInfoReportCorner() */</font> <font class="comment">/************************************************************************/</font> <font class="keyword">static</font> <font class="keywordtype">int</font> GDALInfoReportCorner( GDALDatasetH hDataset, <font class="keyword">const</font> <font class="keywordtype">char</font> * corner_name, <font class="keywordtype">double</font> x, <font class="keywordtype">double</font> y )<font class="keyword"></font> <font class="keyword"></font> <font class="keyword"></font>{ <font class="keywordtype">double</font> dfGeoX, dfGeoY; <font class="keyword">const</font> <font class="keywordtype">char</font> *pszProjection; <font class="keywordtype">double</font> adfGeoTransform[6]; OGRCoordinateTransformationH hTransform = NULL; printf( <font class="stringliteral">"%-11s "</font>, corner_name ); <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="comment">/* Transform the point into georeferenced coordinates. */</font> <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="keywordflow">if</font>( 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; } <font class="keywordflow">else</font> { printf( <font class="stringliteral">"(%7.1f,%7.1f)\n"</font>, x, y ); <font class="keywordflow">return</font> FALSE; } <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="comment">/* Report the georeferenced coordinates. */</font> <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="keywordflow">if</font>( ABS(dfGeoX) < 181 && ABS(dfGeoY) < 91 ) { printf( <font class="stringliteral">"(%12.7f,%12.7f) "</font>, dfGeoX, dfGeoY ); } <font class="keywordflow">else</font> { printf( <font class="stringliteral">"(%12.3f,%12.3f) "</font>, dfGeoX, dfGeoY ); } <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="comment">/* Setup transformation to lat/long. */</font> <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="keywordflow">if</font>( pszProjection != NULL && strlen(pszProjection) > 0 ) { OGRSpatialReferenceH hProj, hLatLong = NULL; hProj = OSRNewSpatialReference( pszProjection ); <font class="keywordflow">if</font>( hProj != NULL ) hLatLong = OSRCloneGeogCS( hProj ); <font class="keywordflow">if</font>( hLatLong != NULL ) { hTransform = OCTNewCoordinateTransformation( hProj, hLatLong ); OSRDestroySpatialReference( hLatLong ); } <font class="keywordflow">if</font>( hProj != NULL ) OSRDestroySpatialReference( hProj ); } <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="comment">/* Transform to latlong and report. */</font> <font class="comment">/* -------------------------------------------------------------------- */</font> <font class="keywordflow">if</font>( hTransform != NULL && OCTTransform(hTransform,1,&dfGeoX,&dfGeoY,NULL) ) { printf( <font class="stringliteral">"(%s,"</font>, GDALDecToDMS( dfGeoX, <font class="stringliteral">"Long"</font>, 2 ) ); printf( <font class="stringliteral">"%s)"</font>, GDALDecToDMS( dfGeoY, <font class="stringliteral">"Lat"</font>, 2 ) ); } <font class="keywordflow">if</font>( hTransform != NULL ) OCTDestroyCoordinateTransformation( hTransform ); printf( <font class="stringliteral">"\n"</font> ); <font class="keywordflow">return</font> TRUE; } </div></pre> <p> <hr><address><small>Generated at Thu Mar 28 09:47:33 2002 for GDAL by <a href="http://www.stack.nl/~dimitri/doxygen/index.html"> <img src="doxygen.gif" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.3-20001105 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>, © 1997-2000</small></address> </body> </html>