SURF_FETCH_E LandfireClient::FetchBoundingBox( double *bbox, double resolution, const char *filename, char **options ) { (void)resolution; if( NULL == filename ) { return SURF_FETCH_E_BAD_INPUT; } /* ** We have an arbitrary limit on request size, 0.001 degrees */ if( fabs( bbox[0] - bbox[2] ) < 0.001 || fabs( bbox[1] - bbox[3] ) < 0.001 ) { CPLError( CE_Failure, CPLE_AppDefined, "Bounding box too small, must be greater than " \ "0.001 x 0.001 degrees." ); return SURF_FETCH_E_BAD_INPUT; } /*----------------------------------------------------------------------------- * Local Variable Declarations *-----------------------------------------------------------------------------*/ int i = 0; char *p; int nMaxTries = atoi( CPLGetConfigOption( "LCP_MAX_DOWNLOAD_TRIES", "40" ) ); double dfWait = atof( CPLGetConfigOption( "LCP_DOWNLOAD_WAIT", "3" ) ); const char *pszProduct = CPLStrdup( CSLFetchNameValue( options, "PRODUCT" ) ); /* ** Stupidly simple heuristics to try to get the 'correct' product. */ if( pszProduct == NULL || EQUAL( pszProduct, "" ) ) { if( EQUAL( pszProduct, "" ) ) CPLFree( (void*)pszProduct ); std::string osDataPath = FindDataPath( "landfire.zip" ); osDataPath = "/vsizip/" + osDataPath; const char *pszGeom; pszGeom = CPLSPrintf( "POLYGON((%lf %lf,%lf %lf,%lf %lf,%lf %lf,%lf %lf))", bbox[1], bbox[0], bbox[3], bbox[0], bbox[3], bbox[2], bbox[1], bbox[2], bbox[1], bbox[0] ); CPLDebug( "LCP_CLIENT", "Testing if %s contains %s", osDataPath.c_str(), pszGeom ); if( NinjaOGRContain( pszGeom, osDataPath.c_str(), "conus" ) ) { pszProduct = CPLStrdup( "F4W21HZ" ); } else if( NinjaOGRContain( pszGeom, osDataPath.c_str(), "ak" ) ) { pszProduct = CPLStrdup( "F7C29HZ" ); } else if( NinjaOGRContain( pszGeom, osDataPath.c_str(), "hi" ) ) { pszProduct = CPLStrdup( "F4825HZ" ); } /* Contiguous US */ //if( bbox[0] < 52 && bbox[1] < -60 && bbox[2] > 22 && bbox[3] > -136 ) // pszProduct = CPLStrdup( "F4W21HZ" ); /* Alaska */ //else if( bbox[0] < 75 && bbox[1] < -125 && bbox[2] > 50 && bbox[3] > -179 ) // pszProduct = CPLStrdup( "F7C29HZ" ); /* Hawaii */ //else if( bbox[0] < 25 && bbox[1] < -150 && bbox[2] > 15 && bbox[3] > -170 ) // pszProduct = CPLStrdup( "F4825HZ" ); else { CPLError( CE_Failure, CPLE_AppDefined, "Failed to locate product." ); return SURF_FETCH_E_BAD_INPUT; } } CPLDebug( "LCP_CLIENT", "Using product: %s", pszProduct ); const char *pszUrl; const char *pszTemp = CSLFetchNameValue( options, "OVERRIDE_BEST_UTM" ); int nEpsgCode = -1; if( pszTemp == NULL ) { nEpsgCode = BoundingBoxUtm( bbox ); } else { nEpsgCode = atoi( pszTemp ); } /* ** Better check? */ if( nEpsgCode < 1 ) { CPLError( CE_Failure, CPLE_AppDefined, "Invalid EPSG code." ); CPLFree( (void*)pszProduct ); return SURF_FETCH_E_BAD_INPUT; } /*----------------------------------------------------------------------------- * Request a Model via the landfire.cr.usgs.gov REST client *-----------------------------------------------------------------------------*/ pszUrl = CPLSPrintf( LF_REQUEST_TEMPLATE, bbox[0], bbox[2], bbox[3], bbox[1], pszProduct ); CPLFree( (void*)pszProduct ); m_poResult = CPLHTTPFetch( pszUrl, NULL ); CHECK_HTTP_RESULT( "Failed to get download URL" ); CPLDebug( "LCP_CLIENT", "Request URL: %s", pszUrl ); /*----------------------------------------------------------------------------- * Parse the JSON result of the request *-----------------------------------------------------------------------------*/ int nSize = strlen( (char*) m_poResult->pabyData ); //Create a buffer so we can use sscanf, couldn't find a CPL version char *pszResponse = new char[ nSize + 1 ]; pszResponse[0] = '\0'; CPLDebug( "LCP_CLIENT", "JSON Response: %s", m_poResult->pabyData ); /* ** Regular expression support if we have C++11 support. isnan ambiguouity ** is causing non-C++11 compliance. Not tested or used, 0 disables. */ #if __cplusplus >= 201103 && 0 std::string s((const char*) m_poResult->pabyData ); std::smatch m; std::regex e( "\\b(?:(?:https?)://|www\\.)[a-z-A-Z0-9+&@#/%=~_|$?!:,.]" \ "*[a-z-A-Z0-9+&@#/%=~_|$]" ); std::regex_search( s, m, e ); std::string url = m[0].str(); //retrieve first match CPLStrlcpy( pszResponse, url.c_str(), nSize ); #else char **papszTokens = NULL; papszTokens = CSLTokenizeString2( (const char*)m_poResult->pabyData, ",:", CSLT_HONOURSTRINGS | CSLT_PRESERVEESCAPES | CSLT_STRIPENDSPACES | CSLT_STRIPLEADSPACES ); int nTokens = CSLCount( papszTokens ); if( nTokens < 2 ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to generate valid URL for LCP download." ); delete [] pszResponse; CPLHTTPDestroyResult( m_poResult ); CSLDestroy( papszTokens ); return SURF_FETCH_E_IO_ERR; } for( int i = 1; i < nTokens; i++ ) { if( EQUALN( papszTokens[i], "https://", 6 ) && EQUAL( papszTokens[i - 1], "DOWNLOAD_URL" ) ) { CPLStrlcpy( pszResponse, papszTokens[i], nSize ); break; } } CSLDestroy( papszTokens ); #endif //Grab the download URL from the JSON response, stores in pszResponse //std::sscanf( (char*) m_poResult->pabyData, LF_REQUEST_RETURN_TEMPLATE, pszResponse); CPLHTTPDestroyResult( m_poResult ); if( !EQUALN( pszResponse, "https://", 6 ) ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to generate valid URL for LCP download." ); delete [] pszResponse; return SURF_FETCH_E_IO_ERR; } p = strstr( pszResponse, "}]" ); if( p ) *p = '\0'; CPLDebug( "LCP_CLIENT", "Download URL: %s", pszResponse ); // Fix the SRS const char *pszNewUrl = ReplaceSRS( nEpsgCode, pszResponse ); CPLDebug( "LCP_CLIENT", "Sanitized SRS Download URL: %s", pszNewUrl ); /*----------------------------------------------------------------------------- * Get the Job ID by visiting the download URL *-----------------------------------------------------------------------------*/ m_poResult = CPLHTTPFetch( pszNewUrl, NULL ); CPLFree( (void*)pszNewUrl ); delete [] pszResponse; CHECK_HTTP_RESULT( "Failed to get Job ID" ); nSize = strlen( (char*) m_poResult->pabyData ); pszResponse = new char[ nSize + 1 ]; //grabs the Job ID from the Download URL response std::sscanf( (char*) m_poResult->pabyData, LF_INIT_RESPONSE_TEMPLATE, pszResponse); CPLHTTPDestroyResult( m_poResult ); //store the Job Id into a class attribute, so we can reuse pszResponse, but keep //the Job Id (needed for future parts) m_JobId = std::string( pszResponse ); CPLDebug( "LCP_CLIENT", "Job id: %s", m_JobId.c_str() ); /*----------------------------------------------------------------------------- * Initiate the download by using the obtained Job ID *-----------------------------------------------------------------------------*/ pszUrl = CPLSPrintf( LF_INIT_DOWNLOAD_TEMPLATE, m_JobId.c_str() ); //fetch the response of download initiation //note: for some reason it alway returns a key error, but download still works m_poResult = CPLHTTPFetch( pszUrl, NULL ); CPLHTTPDestroyResult( m_poResult ); /*----------------------------------------------------------------------------- * Check the status of the download, able to download when status=400 *-----------------------------------------------------------------------------*/ int dl_status = 0; //Obtain the readiness status of the current job pszUrl = CPLSPrintf( LF_GET_STATUS_TEMPLATE, m_JobId.c_str() ); CPLDebug( "LCP_CLIENT", "Status url: %s", pszUrl ); do { m_poResult = CPLHTTPFetch( pszUrl, NULL ); delete [] pszResponse; CHECK_HTTP_RESULT( "Failed to get job status" ); nSize = strlen( (char*) m_poResult->pabyData ); pszResponse = new char[ nSize + 1 ]; std::sscanf( (char*) m_poResult->pabyData, LF_STATUS_RESPONSE_TEMPLATE, &dl_status, pszResponse ); CPLHTTPDestroyResult( m_poResult ); i++; CPLSleep( dfWait ); CPLDebug( "LCP_CLIENT", "Attempting to fetch LCP, try %d of %d, " \ "status: %d", i, nMaxTries, dl_status ); } while( dl_status < 400 && dl_status > 0 && i < nMaxTries ); delete [] pszResponse; if( dl_status >= 900 && dl_status <= 902) { CPLError( CE_Warning, CPLE_AppDefined, "Failed to download lcp," \ "There was an extraction " \ "error on the server." ); return SURF_FETCH_E_IO_ERR; } else if( dl_status != 400 ) { CPLError( CE_Warning, CPLE_AppDefined, "Failed to download lcp, timed " \ "out. Try increasing " \ "LCP_MAX_DOWNLOAD_TRIES or " "LCP_DOWNLOAD_WAIT" ); return SURF_FETCH_E_TIMEOUT; } /*----------------------------------------------------------------------------- * Download the landfire model *-----------------------------------------------------------------------------*/ pszUrl = CPLSPrintf( LF_DOWNLOAD_JOB_TEMPLATE, m_JobId.c_str() ); m_poResult = CPLHTTPFetch( pszUrl, NULL ); CHECK_HTTP_RESULT( "Failed to get job status" ); /* ** Parse the URL from the returned string */ std::string ss((const char*) m_poResult->pabyData ); CPLHTTPDestroyResult( m_poResult ); std::size_t pos1 = ss.find("https://"); std::size_t pos2 = ss.find(".zip"); std::string url = ss.substr(pos1, (pos2+4-pos1)); pszUrl = url.c_str(); m_poResult = CPLHTTPFetch( pszUrl, NULL ); CHECK_HTTP_RESULT( "Failed to get job status" ); nSize = m_poResult->nDataLen; VSILFILE *fout; const char *pszTmpZip = CPLFormFilename( NULL, CPLGenerateTempFilename( "NINJA_LCP_CLIENT" ), ".zip" ); fout = VSIFOpenL( pszTmpZip, "w+" ); if( NULL == fout ) { CPLError( CE_Warning, CPLE_AppDefined, "Failed to create output file" ); CPLHTTPDestroyResult( m_poResult ); return SURF_FETCH_E_IO_ERR; } VSIFWriteL( m_poResult->pabyData, nSize, 1, fout ); VSIFCloseL( fout ); CPLHTTPDestroyResult( m_poResult ); /* ** Extract the lcp and the prj file, and 'save as' */ char **papszFileList = NULL; std::string osPathInZip; const char *pszVSIZip = CPLSPrintf( "/vsizip/%s", pszTmpZip ); CPLDebug( "LCP_CLIENT", "Extracting lcp from %s", pszVSIZip ); papszFileList = VSIReadDirRecursive( pszVSIZip ); int bFound = FALSE; std::string osFullPath; for( int i = 0; i < CSLCount( papszFileList ); i++ ) { osFullPath = papszFileList[i]; if( osFullPath.find( "Landscape_1.lcp" ) != std::string::npos ) { osPathInZip = CPLGetPath( papszFileList[i] ); CPLDebug( "LCP_CLIENT", "Found lcp in: %s", osPathInZip.c_str() ); bFound = TRUE; break; } } CSLDestroy( papszFileList ); if( !bFound ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to find lcp in archive" ); //VSIUnlink( pszTmpZip ); return SURF_FETCH_E_IO_ERR; } int nError = 0; const char *pszFileToFind = CPLSPrintf( "%s/Landscape_1.lcp", osPathInZip.c_str() ); nError = ExtractFileFromZip( pszTmpZip, pszFileToFind, filename ); if( nError ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to extract LCP from zip." ); VSIUnlink( pszTmpZip ); return SURF_FETCH_E_IO_ERR; } pszFileToFind = CPLSPrintf( "%s/Landscape_1.prj", osPathInZip.c_str() ); nError = ExtractFileFromZip( pszTmpZip, pszFileToFind, CPLFormFilename( CPLGetPath( filename ), CPLGetBasename( filename ), ".prj" ) ); if( nError ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to extract PRJ from zip." ); return SURF_FETCH_E_IO_ERR; } if( !CSLTestBoolean( CPLGetConfigOption( "LCP_KEEP_ARCHIVE", "FALSE" ) ) ) { VSIUnlink( pszTmpZip ); } return SURF_FETCH_E_NONE; }
int main( int argc, char ** argv ) { EarlySetConfigOptions(argc, argv); GDALAllRegister(); argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 ); if( argc < 1 ) exit( -argc ); for( int i = 0; argv != NULL && argv[i] != NULL; i++ ) { if( EQUAL(argv[i], "--utility_version") ) { printf("%s was compiled against GDAL %s and is running against GDAL %s\n", argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME")); CSLDestroy( argv ); return 0; } else if( EQUAL(argv[i],"--help") ) { Usage(); } } GDALInfoOptionsForBinary* psOptionsForBinary = GDALInfoOptionsForBinaryNew(); GDALInfoOptions *psOptions = GDALInfoOptionsNew(argv + 1, psOptionsForBinary); if( psOptions == NULL ) Usage(); if( psOptionsForBinary->pszFilename == NULL ) Usage("No datasource specified."); /* -------------------------------------------------------------------- */ /* Open dataset. */ /* -------------------------------------------------------------------- */ GDALDatasetH hDataset = GDALOpenEx( psOptionsForBinary->pszFilename, GDAL_OF_READONLY | GDAL_OF_RASTER, NULL, (const char* const* )psOptionsForBinary->papszOpenOptions, NULL ); if( hDataset == NULL ) { fprintf( stderr, "gdalinfo failed - unable to open '%s'.\n", psOptionsForBinary->pszFilename ); /* -------------------------------------------------------------------- */ /* If argument is a VSIFILE, then print its contents */ /* -------------------------------------------------------------------- */ if ( STARTS_WITH(psOptionsForBinary->pszFilename, "/vsizip/") || STARTS_WITH(psOptionsForBinary->pszFilename, "/vsitar/") ) { char** papszFileList = VSIReadDirRecursive( psOptionsForBinary->pszFilename ); if ( papszFileList ) { int nCount = CSLCount( papszFileList ); fprintf( stdout, "Unable to open source `%s' directly.\n" "The archive contains %d files:\n", psOptionsForBinary->pszFilename, nCount ); for ( int i = 0; i < nCount; i++ ) { fprintf( stdout, " %s/%s\n", psOptionsForBinary->pszFilename, papszFileList[i] ); } CSLDestroy( papszFileList ); } } CSLDestroy( argv ); GDALInfoOptionsForBinaryFree(psOptionsForBinary); GDALInfoOptionsFree( psOptions ); GDALDumpOpenDatasets( stderr ); GDALDestroyDriverManager(); CPLDumpSharedList( NULL ); exit( 1 ); } /* -------------------------------------------------------------------- */ /* Read specified subdataset if requested. */ /* -------------------------------------------------------------------- */ if ( psOptionsForBinary->nSubdataset > 0 ) { char **papszSubdatasets = GDALGetMetadata( hDataset, "SUBDATASETS" ); int nSubdatasets = CSLCount( papszSubdatasets ); if ( nSubdatasets > 0 && psOptionsForBinary->nSubdataset <= nSubdatasets ) { char szKeyName[1024]; char *pszSubdatasetName; snprintf( szKeyName, sizeof(szKeyName), "SUBDATASET_%d_NAME", psOptionsForBinary->nSubdataset ); szKeyName[sizeof(szKeyName) - 1] = '\0'; pszSubdatasetName = CPLStrdup( CSLFetchNameValue( papszSubdatasets, szKeyName ) ); GDALClose( hDataset ); hDataset = GDALOpen( pszSubdatasetName, GA_ReadOnly ); CPLFree( pszSubdatasetName ); } else { fprintf( stderr, "gdalinfo warning: subdataset %d of %d requested. " "Reading the main dataset.\n", psOptionsForBinary->nSubdataset, nSubdatasets ); } } GDALInfoOptionsForBinaryFree(psOptionsForBinary); char* pszGDALInfoOutput = GDALInfo( hDataset, psOptions ); printf( "%s", pszGDALInfoOutput ); CPLFree( pszGDALInfoOutput ); GDALInfoOptionsFree( psOptions ); GDALClose( hDataset ); CSLDestroy( argv ); GDALDumpOpenDatasets( stderr ); GDALDestroyDriverManager(); CPLDumpSharedList( NULL ); CPLCleanupTLS(); exit( 0 ); }
int main( int argc, char ** argv ) { GDALDatasetH hDataset = NULL; GDALRasterBandH hBand = NULL; int i, iBand; double adfGeoTransform[6]; GDALDriverH hDriver; char **papszMetadata; int bComputeMinMax = FALSE, bSample = FALSE; int bShowGCPs = TRUE, bShowMetadata = TRUE, bShowRAT=TRUE; int bStats = FALSE, bApproxStats = TRUE, iMDD; int bShowColorTable = TRUE, bComputeChecksum = FALSE; int bReportHistograms = FALSE; int bReportProj4 = FALSE; int nSubdataset = -1; const char *pszFilename = NULL; char **papszExtraMDDomains = NULL, **papszFileList; const char *pszProjection = NULL; OGRCoordinateTransformationH hTransform = NULL; int bShowFileList = TRUE; /* Check that we are running against at least GDAL 1.5 */ /* Note to developers : if we use newer API, please change the requirement */ if (atoi(GDALVersionInfo("VERSION_NUM")) < 1500) { fprintf(stderr, "At least, GDAL >= 1.5.0 is required for this version of %s, " "which was compiled against GDAL %s\n", argv[0], GDAL_RELEASE_NAME); exit(1); } EarlySetConfigOptions(argc, argv); GDALAllRegister(); argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 ); if( argc < 1 ) exit( -argc ); /* -------------------------------------------------------------------- */ /* Parse arguments. */ /* -------------------------------------------------------------------- */ for( i = 1; i < argc; i++ ) { if( EQUAL(argv[i], "--utility_version") ) { printf("%s was compiled against GDAL %s and is running against GDAL %s\n", argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME")); return 0; } else if( EQUAL(argv[i],"--help") ) Usage(NULL); else if( EQUAL(argv[i], "-mm") ) bComputeMinMax = TRUE; else if( EQUAL(argv[i], "-hist") ) bReportHistograms = TRUE; else if( EQUAL(argv[i], "-proj4") ) bReportProj4 = TRUE; else if( EQUAL(argv[i], "-stats") ) { bStats = TRUE; bApproxStats = FALSE; } else if( EQUAL(argv[i], "-approx_stats") ) { bStats = TRUE; bApproxStats = TRUE; } else if( EQUAL(argv[i], "-sample") ) bSample = TRUE; else if( EQUAL(argv[i], "-checksum") ) bComputeChecksum = TRUE; else if( EQUAL(argv[i], "-nogcp") ) bShowGCPs = FALSE; else if( EQUAL(argv[i], "-nomd") ) bShowMetadata = FALSE; else if( EQUAL(argv[i], "-norat") ) bShowRAT = FALSE; else if( EQUAL(argv[i], "-noct") ) bShowColorTable = FALSE; else if( EQUAL(argv[i], "-mdd") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); papszExtraMDDomains = CSLAddString( papszExtraMDDomains, argv[++i] ); } else if( EQUAL(argv[i], "-nofl") ) bShowFileList = FALSE; else if( EQUAL(argv[i], "-sd") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); nSubdataset = atoi(argv[++i]); } else if( argv[i][0] == '-' ) Usage(CPLSPrintf("Unkown option name '%s'", argv[i])); else if( pszFilename == NULL ) pszFilename = argv[i]; else Usage("Too many command options."); } if( pszFilename == NULL ) Usage("No datasource specified."); /* -------------------------------------------------------------------- */ /* Open dataset. */ /* -------------------------------------------------------------------- */ hDataset = GDALOpen( pszFilename, GA_ReadOnly ); if( hDataset == NULL ) { fprintf( stderr, "gdalinfo failed - unable to open '%s'.\n", pszFilename ); /* -------------------------------------------------------------------- */ /* If argument is a VSIFILE, then print its contents */ /* -------------------------------------------------------------------- */ if ( strncmp( pszFilename, "/vsizip/", 8 ) == 0 || strncmp( pszFilename, "/vsitar/", 8 ) == 0 ) { papszFileList = VSIReadDirRecursive( pszFilename ); if ( papszFileList ) { int nCount = CSLCount( papszFileList ); fprintf( stdout, "Unable to open source `%s' directly.\n" "The archive contains %d files:\n", pszFilename, nCount ); for ( i = 0; i < nCount; i++ ) { fprintf( stdout, " %s/%s\n", pszFilename, papszFileList[i] ); } CSLDestroy( papszFileList ); papszFileList = NULL; } } CSLDestroy( argv ); CSLDestroy( papszExtraMDDomains ); GDALDumpOpenDatasets( stderr ); GDALDestroyDriverManager(); CPLDumpSharedList( NULL ); exit( 1 ); } /* -------------------------------------------------------------------- */ /* Read specified subdataset if requested. */ /* -------------------------------------------------------------------- */ if ( nSubdataset > 0 ) { char **papszSubdatasets = GDALGetMetadata( hDataset, "SUBDATASETS" ); int nSubdatasets = CSLCount( papszSubdatasets ); if ( nSubdatasets > 0 && nSubdataset <= nSubdatasets ) { char szKeyName[1024]; char *pszSubdatasetName; snprintf( szKeyName, sizeof(szKeyName), "SUBDATASET_%d_NAME", nSubdataset ); szKeyName[sizeof(szKeyName) - 1] = '\0'; pszSubdatasetName = CPLStrdup( CSLFetchNameValue( papszSubdatasets, szKeyName ) ); GDALClose( hDataset ); hDataset = GDALOpen( pszSubdatasetName, GA_ReadOnly ); CPLFree( pszSubdatasetName ); } else { fprintf( stderr, "gdalinfo warning: subdataset %d of %d requested. " "Reading the main dataset.\n", nSubdataset, nSubdatasets ); } } /* -------------------------------------------------------------------- */ /* Report general info. */ /* -------------------------------------------------------------------- */ hDriver = GDALGetDatasetDriver( hDataset ); printf( "Driver: %s/%s\n", GDALGetDriverShortName( hDriver ), GDALGetDriverLongName( hDriver ) ); papszFileList = GDALGetFileList( hDataset ); if( CSLCount(papszFileList) == 0 ) { printf( "Files: none associated\n" ); } else { printf( "Files: %s\n", papszFileList[0] ); if( bShowFileList ) { for( i = 1; papszFileList[i] != NULL; i++ ) printf( " %s\n", papszFileList[i] ); } } CSLDestroy( papszFileList ); 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 ) ); if ( bReportProj4 ) { char *pszProj4 = NULL; OSRExportToProj4( hSRS, &pszProj4 ); printf("PROJ.4 string is:\n\'%s\'\n",pszProj4); CPLFree( pszProj4 ); } OSRDestroySpatialReference( hSRS ); } /* -------------------------------------------------------------------- */ /* Report Geotransform. */ /* -------------------------------------------------------------------- */ 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] ); } /* -------------------------------------------------------------------- */ /* Report GCPs. */ /* -------------------------------------------------------------------- */ if( bShowGCPs && GDALGetGCPCount( hDataset ) > 0 ) { if (GDALGetGCPProjection(hDataset) != NULL) { OGRSpatialReferenceH hSRS; char *pszProjection; pszProjection = (char *) GDALGetGCPProjection( hDataset ); hSRS = OSRNewSpatialReference(NULL); if( OSRImportFromWkt( hSRS, &pszProjection ) == CE_None ) { char *pszPrettyWkt = NULL; OSRExportToPrettyWkt( hSRS, &pszPrettyWkt, FALSE ); printf( "GCP Projection = \n%s\n", pszPrettyWkt ); CPLFree( pszPrettyWkt ); } else printf( "GCP Projection = %s\n", GDALGetGCPProjection( hDataset ) ); OSRDestroySpatialReference( hSRS ); } 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 ); } } /* -------------------------------------------------------------------- */ /* Report metadata. */ /* -------------------------------------------------------------------- */ papszMetadata = (bShowMetadata) ? GDALGetMetadata( hDataset, NULL ) : NULL; if( bShowMetadata && CSLCount(papszMetadata) > 0 ) { printf( "Metadata:\n" ); for( i = 0; papszMetadata[i] != NULL; i++ ) { printf( " %s\n", papszMetadata[i] ); } } for( iMDD = 0; bShowMetadata && iMDD < CSLCount(papszExtraMDDomains); iMDD++ ) { papszMetadata = GDALGetMetadata( hDataset, papszExtraMDDomains[iMDD] ); if( CSLCount(papszMetadata) > 0 ) { printf( "Metadata (%s):\n", papszExtraMDDomains[iMDD]); for( i = 0; papszMetadata[i] != NULL; i++ ) { if (EQUALN(papszExtraMDDomains[iMDD], "xml:", 4)) printf( "%s\n", papszMetadata[i] ); else printf( " %s\n", papszMetadata[i] ); } } } /* -------------------------------------------------------------------- */ /* Report "IMAGE_STRUCTURE" metadata. */ /* -------------------------------------------------------------------- */ papszMetadata = (bShowMetadata) ? GDALGetMetadata( hDataset, "IMAGE_STRUCTURE" ) : NULL; if( bShowMetadata && CSLCount(papszMetadata) > 0 ) { printf( "Image Structure 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 geolocation. */ /* -------------------------------------------------------------------- */ papszMetadata = (bShowMetadata) ? GDALGetMetadata( hDataset, "GEOLOCATION" ) : NULL; if( bShowMetadata && CSLCount(papszMetadata) > 0 ) { printf( "Geolocation:\n" ); for( i = 0; papszMetadata[i] != NULL; i++ ) { printf( " %s\n", papszMetadata[i] ); } } /* -------------------------------------------------------------------- */ /* Report RPCs */ /* -------------------------------------------------------------------- */ papszMetadata = (bShowMetadata) ? GDALGetMetadata( hDataset, "RPC" ) : NULL; if( bShowMetadata && CSLCount(papszMetadata) > 0 ) { printf( "RPC Metadata:\n" ); for( i = 0; papszMetadata[i] != NULL; i++ ) { printf( " %s\n", papszMetadata[i] ); } } /* -------------------------------------------------------------------- */ /* Setup projected to lat/long transform if appropriate. */ /* -------------------------------------------------------------------- */ if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None ) pszProjection = GDALGetProjectionRef(hDataset); 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 ); } /* -------------------------------------------------------------------- */ /* Report corners. */ /* -------------------------------------------------------------------- */ printf( "Corner Coordinates:\n" ); GDALInfoReportCorner( hDataset, hTransform, "Upper Left", 0.0, 0.0 ); GDALInfoReportCorner( hDataset, hTransform, "Lower Left", 0.0, GDALGetRasterYSize(hDataset)); GDALInfoReportCorner( hDataset, hTransform, "Upper Right", GDALGetRasterXSize(hDataset), 0.0 ); GDALInfoReportCorner( hDataset, hTransform, "Lower Right", GDALGetRasterXSize(hDataset), GDALGetRasterYSize(hDataset) ); GDALInfoReportCorner( hDataset, hTransform, "Center", GDALGetRasterXSize(hDataset)/2.0, GDALGetRasterYSize(hDataset)/2.0 ); if( hTransform != NULL ) { OCTDestroyCoordinateTransformation( hTransform ); hTransform = NULL; } /* ==================================================================== */ /* Loop over bands. */ /* ==================================================================== */ for( iBand = 0; iBand < GDALGetRasterCount( hDataset ); iBand++ ) { double dfMin, dfMax, adfCMinMax[2], dfNoData; int bGotMin, bGotMax, bGotNodata, bSuccess; int nBlockXSize, nBlockYSize, nMaskFlags; 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 ) { CPLErrorReset(); GDALComputeRasterMinMax( hBand, FALSE, adfCMinMax ); if (CPLGetLastErrorType() == CE_None) { printf( " Computed Min/Max=%.3f,%.3f", adfCMinMax[0], adfCMinMax[1] ); } } printf( "\n" ); } eErr = GDALGetRasterStatistics( hBand, bApproxStats, bStats, &dfMin, &dfMax, &dfMean, &dfStdDev ); if( eErr == CE_None ) { printf( " Minimum=%.3f, Maximum=%.3f, Mean=%.3f, StdDev=%.3f\n", dfMin, dfMax, dfMean, dfStdDev ); } if( bReportHistograms ) { int nBucketCount, *panHistogram = NULL; eErr = GDALGetDefaultHistogram( hBand, &dfMin, &dfMax, &nBucketCount, &panHistogram, TRUE, GDALTermProgress, NULL ); if( eErr == CE_None ) { int iBucket; printf( " %d buckets from %g to %g:\n ", nBucketCount, dfMin, dfMax ); for( iBucket = 0; iBucket < nBucketCount; iBucket++ ) printf( "%d ", panHistogram[iBucket] ); printf( "\n" ); CPLFree( panHistogram ); } } if ( bComputeChecksum) { printf( " Checksum=%d\n", GDALChecksumImage(hBand, 0, 0, GDALGetRasterXSize(hDataset), GDALGetRasterYSize(hDataset))); } dfNoData = GDALGetRasterNoDataValue( hBand, &bGotNodata ); if( bGotNodata ) { if (CPLIsNan(dfNoData)) printf( " NoData Value=nan\n" ); else printf( " NoData Value=%.18g\n", dfNoData ); } if( GDALGetOverviewCount(hBand) > 0 ) { int iOverview; printf( " Overviews: " ); for( iOverview = 0; iOverview < GDALGetOverviewCount(hBand); iOverview++ ) { GDALRasterBandH hOverview; const char *pszResampling = NULL; if( iOverview != 0 ) printf( ", " ); hOverview = GDALGetOverview( hBand, iOverview ); if (hOverview != NULL) { printf( "%dx%d", GDALGetRasterBandXSize( hOverview ), GDALGetRasterBandYSize( hOverview ) ); pszResampling = GDALGetMetadataItem( hOverview, "RESAMPLING", "" ); if( pszResampling != NULL && EQUALN(pszResampling,"AVERAGE_BIT2",12) ) printf( "*" ); } else printf( "(null)" ); } printf( "\n" ); if ( bComputeChecksum) { printf( " Overviews checksum: " ); for( iOverview = 0; iOverview < GDALGetOverviewCount(hBand); iOverview++ ) { GDALRasterBandH hOverview; if( iOverview != 0 ) printf( ", " ); hOverview = GDALGetOverview( hBand, iOverview ); if (hOverview) printf( "%d", GDALChecksumImage(hOverview, 0, 0, GDALGetRasterBandXSize(hOverview), GDALGetRasterBandYSize(hOverview))); else printf( "(null)" ); } printf( "\n" ); } } if( GDALHasArbitraryOverviews( hBand ) ) { printf( " Overviews: arbitrary\n" ); } nMaskFlags = GDALGetMaskFlags( hBand ); if( (nMaskFlags & (GMF_NODATA|GMF_ALL_VALID)) == 0 ) { GDALRasterBandH hMaskBand = GDALGetMaskBand(hBand) ; printf( " Mask Flags: " ); if( nMaskFlags & GMF_PER_DATASET ) printf( "PER_DATASET " ); if( nMaskFlags & GMF_ALPHA ) printf( "ALPHA " ); if( nMaskFlags & GMF_NODATA ) printf( "NODATA " ); if( nMaskFlags & GMF_ALL_VALID ) printf( "ALL_VALID " ); printf( "\n" ); if( hMaskBand != NULL && GDALGetOverviewCount(hMaskBand) > 0 ) { int iOverview; printf( " Overviews of mask band: " ); for( iOverview = 0; iOverview < GDALGetOverviewCount(hMaskBand); iOverview++ ) { GDALRasterBandH hOverview; if( iOverview != 0 ) printf( ", " ); hOverview = GDALGetOverview( hMaskBand, iOverview ); printf( "%dx%d", GDALGetRasterBandXSize( hOverview ), GDALGetRasterBandYSize( hOverview ) ); } printf( "\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 = (bShowMetadata) ? GDALGetMetadata( hBand, NULL ) : NULL; if( bShowMetadata && CSLCount(papszMetadata) > 0 ) { printf( " Metadata:\n" ); for( i = 0; papszMetadata[i] != NULL; i++ ) { printf( " %s\n", papszMetadata[i] ); } } papszMetadata = (bShowMetadata) ? GDALGetMetadata( hBand, "IMAGE_STRUCTURE" ) : NULL; 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 ) ); if (bShowColorTable) { 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( bShowRAT && GDALGetDefaultRAT( hBand ) != NULL ) { GDALRasterAttributeTableH hRAT = GDALGetDefaultRAT( hBand ); GDALRATDumpReadable( hRAT, NULL ); } } GDALClose( hDataset ); CSLDestroy( papszExtraMDDomains ); CSLDestroy( argv ); GDALDumpOpenDatasets( stderr ); GDALDestroyDriverManager(); CPLDumpSharedList( NULL ); CPLCleanupTLS(); exit( 0 ); }