void GMLFeature::Dump( CPL_UNUSED FILE * fp ) { printf( "GMLFeature(%s):\n", m_poClass->GetName() ); if( m_pszFID != NULL ) printf( " FID = %s\n", m_pszFID ); int i; for( i = 0; i < m_nPropertyCount; i++ ) { const GMLProperty * psGMLProperty = GetProperty( i ); printf( " %s = ", m_poClass->GetProperty( i )->GetName()); for ( int j = 0; j < psGMLProperty->nSubProperties; j ++) { if (j > 0) printf(", "); printf("%s", psGMLProperty->papszSubProperties[j]); } printf("\n"); } for(i=0;i<m_nGeometryCount;i++) { char* pszXML = CPLSerializeXMLTree(m_papsGeometry[i]); printf( " %s\n", pszXML ); CPLFree(pszXML); } }
int GDALJP2Metadata::GMLSRSLookup( const char *pszURN ) { CPLXMLNode *psDictEntry = GetDictionaryItem( papszGMLMetadata, pszURN ); if( psDictEntry == NULL ) return FALSE; /* -------------------------------------------------------------------- */ /* Reserialize this fragment. */ /* -------------------------------------------------------------------- */ char *pszDictEntryXML = CPLSerializeXMLTree( psDictEntry ); CPLDestroyXMLNode( psDictEntry ); /* -------------------------------------------------------------------- */ /* Try to convert into an OGRSpatialReference. */ /* -------------------------------------------------------------------- */ OGRSpatialReference oSRS; int bSuccess = FALSE; if( oSRS.importFromXML( pszDictEntryXML ) == OGRERR_NONE ) { CPLFree( pszProjection ); pszProjection = NULL; oSRS.exportToWkt( &pszProjection ); bSuccess = TRUE; } CPLFree( pszDictEntryXML ); return bSuccess; }
int OGRFMECacheIndex::Save() { if( hLock == NULL ) return FALSE; /* -------------------------------------------------------------------- */ /* Convert the XML tree into one big character buffer, and */ /* write it out. */ /* -------------------------------------------------------------------- */ char *pszIndexBuffer = CPLSerializeXMLTree( psTree ); if( pszIndexBuffer == NULL ) return FALSE; FILE *fpIndex = VSIFOpen( GetPath(), "wb" ); if( fpIndex == NULL ) return FALSE; VSIFWrite( pszIndexBuffer, 1, strlen(pszIndexBuffer), fpIndex ); CPLFree( pszIndexBuffer ); VSIFClose( fpIndex ); Unlock(); return TRUE; }
OGRErr OGRMILayerAttrIndex::SaveConfigToXML() { if( nIndexCount == 0 ) return OGRERR_NONE; /* -------------------------------------------------------------------- */ /* Create the XML tree corresponding to this layer. */ /* -------------------------------------------------------------------- */ CPLXMLNode *psRoot; psRoot = CPLCreateXMLNode( NULL, CXT_Element, "OGRMILayerAttrIndex" ); CPLCreateXMLElementAndValue( psRoot, "MIIDFilename", CPLGetFilename( pszMIINDFilename ) ); for( int i = 0; i < nIndexCount; i++ ) { OGRMIAttrIndex *poAI = papoIndexList[i]; CPLXMLNode *psIndex; psIndex = CPLCreateXMLNode( psRoot, CXT_Element, "OGRMIAttrIndex" ); CPLCreateXMLElementAndValue( psIndex, "FieldIndex", CPLSPrintf( "%d", poAI->iField ) ); CPLCreateXMLElementAndValue( psIndex, "FieldName", poLayer->GetLayerDefn()->GetFieldDefn(poAI->iField)->GetNameRef() ); CPLCreateXMLElementAndValue( psIndex, "IndexIndex", CPLSPrintf( "%d", poAI->iIndex ) ); } /* -------------------------------------------------------------------- */ /* Save it. */ /* -------------------------------------------------------------------- */ char *pszRawXML = CPLSerializeXMLTree( psRoot ); FILE *fp; CPLDestroyXMLNode( psRoot ); fp = VSIFOpen( pszMetadataFilename, "wb" ); if( fp == NULL ) { CPLError( CE_Failure, CPLE_OpenFailed, "Failed to pen `%s' for write.", pszMetadataFilename ); CPLFree( pszRawXML ); return OGRERR_FAILURE; } VSIFWrite( pszRawXML, 1, strlen(pszRawXML), fp ); VSIFClose( fp ); CPLFree( pszRawXML ); return OGRERR_NONE; }
void VRTDataset::FlushCache() { GDALDataset::FlushCache(); if( !bNeedsFlush || bWritable == FALSE) return; bNeedsFlush = FALSE; // We don't write to disk if there is no filename. This is a // memory only dataset. if( strlen(GetDescription()) == 0 || EQUALN(GetDescription(),"<VRTDataset",11) ) return; /* -------------------------------------------------------------------- */ /* Create the output file. */ /* -------------------------------------------------------------------- */ VSILFILE *fpVRT; fpVRT = VSIFOpenL( GetDescription(), "w" ); if( fpVRT == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to write .vrt file in FlushCache()." ); return; } /* -------------------------------------------------------------------- */ /* Convert tree to a single block of XML text. */ /* -------------------------------------------------------------------- */ char *pszVRTPath = CPLStrdup(CPLGetPath(GetDescription())); CPLXMLNode *psDSTree = SerializeToXML( pszVRTPath ); char *pszXML; pszXML = CPLSerializeXMLTree( psDSTree ); CPLDestroyXMLNode( psDSTree ); CPLFree( pszVRTPath ); /* -------------------------------------------------------------------- */ /* Write to disk. */ /* -------------------------------------------------------------------- */ VSIFWriteL( pszXML, 1, strlen(pszXML), fpVRT ); VSIFCloseL( fpVRT ); CPLFree( pszXML ); }
int main( int argc, char **argv ) { static char szXML[20000000]; FILE *fp; if( argc == 1 ) fp = stdin; else if( argv[1][0] == '-' ) { printf( "Usage: xmlreformat [filename]\n" ); exit( 0 ); } else { fp = fopen( argv[1], "rt" ); if( fp == NULL ) { printf( "Failed to open file %s.\n", argv[1] ); exit( 1 ); } } int nLen = fread( szXML, 1, sizeof(szXML), fp ); if( nLen >= (int) sizeof(szXML)-2 ) { fprintf( stderr, "xmlreformat fixed sized buffer (%d bytes) exceeded.\n", (int) sizeof(szXML) ); exit(1); } if( fp != stdin ) fclose( fp ); szXML[nLen] = '\0'; CPLXMLNode *poTree = CPLParseXMLString( szXML ); if( poTree != NULL ) { char *pszRawXML = CPLSerializeXMLTree( poTree ); printf( "%s", pszRawXML ); CPLFree( pszRawXML ); CPLDestroyXMLNode( poTree ); } return 0; }
int main( int argc, char **argv ) { CPLXMLNode *poTree; static char szXML[10000000]; FILE *fp; int nLen; if( argc == 1 ) fp = stdin; else if( argv[1][0] == '-' ) { printf( "Usage: xmlreformat [filename]\n" ); exit( 0 ); } else { fp = fopen( argv[1], "rt" ); if( fp == NULL ) { printf( "Failed to open file %s.\n", argv[1] ); exit( 1 ); } } nLen = fread( szXML, 1, sizeof(szXML), fp ); if( fp != stdin ) fclose( fp ); szXML[nLen] = '\0'; poTree = CPLParseXMLString( szXML ); if( poTree != NULL ) { char *pszRawXML = CPLSerializeXMLTree( poTree ); printf( "%s", pszRawXML ); CPLFree( pszRawXML ); CPLDestroyXMLNode( poTree ); } return 0; }
int GMLReader::SaveClasses( const char *pszFile ) { // Add logic later to determine reasonable default schema file. if( pszFile == NULL ) return FALSE; /* -------------------------------------------------------------------- */ /* Create in memory schema tree. */ /* -------------------------------------------------------------------- */ CPLXMLNode *psRoot; psRoot = CPLCreateXMLNode( NULL, CXT_Element, "GMLFeatureClassList" ); for( int iClass = 0; iClass < GetClassCount(); iClass++ ) { GMLFeatureClass *poClass = GetClass( iClass ); CPLAddXMLChild( psRoot, poClass->SerializeToXML() ); } /* -------------------------------------------------------------------- */ /* Serialize to disk. */ /* -------------------------------------------------------------------- */ FILE *fp; int bSuccess = TRUE; char *pszWholeText = CPLSerializeXMLTree( psRoot ); CPLDestroyXMLNode( psRoot ); fp = VSIFOpen( pszFile, "wb" ); if( fp == NULL ) bSuccess = FALSE; else if( VSIFWrite( pszWholeText, strlen(pszWholeText), 1, fp ) != 1 ) bSuccess = FALSE; else VSIFClose( fp ); CPLFree( pszWholeText ); return bSuccess; }
OGRErr OGRSpatialReference::exportToXML(char **ppszRawXML, const char *pszDialect) const { CPLXMLNode *psXMLTree = NULL; if (IsGeographic()) { psXMLTree = exportGeogCSToXML(this); } else if (IsProjected()) { psXMLTree = exportProjCSToXML(this); } else return OGRERR_UNSUPPORTED_SRS; *ppszRawXML = CPLSerializeXMLTree(psXMLTree); CPLDestroyXMLNode(psXMLTree); return OGRERR_NONE; }
static char* CPLLoadSchemaStr(const char* pszXSDFilename) { #ifdef HAS_VALIDATION_BUG CPLHasLibXMLBug(); #endif CPLHashSet* hSetSchemas = CPLHashSetNew(CPLHashSetHashStr, CPLHashSetEqualStr, CPLFree); CPLXMLNode* psSchema = CPLLoadSchemaStrInternal(hSetSchemas, pszXSDFilename); char* pszStr = NULL; if (psSchema) { CPLMoveImportAtBeginning(psSchema); pszStr = CPLSerializeXMLTree(psSchema); CPLDestroyXMLNode(psSchema); } CPLHashSetDestroy(hSetSchemas); return pszStr; }
CPLErr GDALWMSDataset::Initialize(CPLXMLNode *config) { CPLErr ret = CE_None; char* pszXML = CPLSerializeXMLTree( config ); if (pszXML) { m_osXML = pszXML; CPLFree(pszXML); } // Initialize the minidriver, which can set parameters for the dataset using member functions CPLXMLNode *service_node = CPLGetXMLNode(config, "Service"); if (service_node != NULL) { const CPLString service_name = CPLGetXMLValue(service_node, "name", ""); if (!service_name.empty()) { GDALWMSMiniDriverManager *const mdm = GetGDALWMSMiniDriverManager(); GDALWMSMiniDriverFactory *const mdf = mdm->Find(service_name); if (mdf != NULL) { m_mini_driver = mdf->New(); m_mini_driver->m_parent_dataset = this; if (m_mini_driver->Initialize(service_node) == CE_None) { m_mini_driver_caps.m_capabilities_version = -1; m_mini_driver->GetCapabilities(&m_mini_driver_caps); if (m_mini_driver_caps.m_capabilities_version == -1) { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Internal error, mini-driver capabilities version not set."); ret = CE_Failure; } } else { delete m_mini_driver; m_mini_driver = NULL; CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Failed to initialize minidriver."); ret = CE_Failure; } } else { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: No mini-driver registered for '%s'.", service_name.c_str()); ret = CE_Failure; } } else { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: No Service specified."); ret = CE_Failure; } } else { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: No Service specified."); ret = CE_Failure; } /* Parameters that could be set by minidriver already, based on server side information. If the size is set, minidriver has done this already A "server" side minidriver needs to set at least: - Blocksize (x and y) - Clamp flag (defaults to true) - DataWindow - Band Count - Data Type It should also initialize and register the bands and overviews. */ if (m_data_window.m_sx<1) { int nOverviews = 0; if (ret == CE_None) { m_block_size_x = atoi(CPLGetXMLValue(config, "BlockSizeX", CPLString().Printf("%d", m_default_block_size_x))); m_block_size_y = atoi(CPLGetXMLValue(config, "BlockSizeY", CPLString().Printf("%d", m_default_block_size_y))); if (m_block_size_x <= 0 || m_block_size_y <= 0) { CPLError( CE_Failure, CPLE_AppDefined, "GDALWMS: Invalid value in BlockSizeX or BlockSizeY" ); ret = CE_Failure; } } if (ret == CE_None) { m_clamp_requests = StrToBool(CPLGetXMLValue(config, "ClampRequests", "true")); if (m_clamp_requests<0) { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Invalid value of ClampRequests, true/false expected."); ret = CE_Failure; } } if (ret == CE_None) { CPLXMLNode *data_window_node = CPLGetXMLNode(config, "DataWindow"); if (data_window_node == NULL && m_bNeedsDataWindow) { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: DataWindow missing."); ret = CE_Failure; } else { CPLString osDefaultX0, osDefaultX1, osDefaultY0, osDefaultY1; CPLString osDefaultTileCountX, osDefaultTileCountY, osDefaultTileLevel; CPLString osDefaultOverviewCount; osDefaultX0.Printf("%.8f", m_default_data_window.m_x0); osDefaultX1.Printf("%.8f", m_default_data_window.m_x1); osDefaultY0.Printf("%.8f", m_default_data_window.m_y0); osDefaultY1.Printf("%.8f", m_default_data_window.m_y1); osDefaultTileCountX.Printf("%d", m_default_tile_count_x); osDefaultTileCountY.Printf("%d", m_default_tile_count_y); if (m_default_data_window.m_tlevel >= 0) osDefaultTileLevel.Printf("%d", m_default_data_window.m_tlevel); if (m_default_overview_count >= 0) osDefaultOverviewCount.Printf("%d", m_default_overview_count); const char *overview_count = CPLGetXMLValue(config, "OverviewCount", osDefaultOverviewCount); const char *ulx = CPLGetXMLValue(data_window_node, "UpperLeftX", osDefaultX0); const char *uly = CPLGetXMLValue(data_window_node, "UpperLeftY", osDefaultY0); const char *lrx = CPLGetXMLValue(data_window_node, "LowerRightX", osDefaultX1); const char *lry = CPLGetXMLValue(data_window_node, "LowerRightY", osDefaultY1); const char *sx = CPLGetXMLValue(data_window_node, "SizeX", ""); const char *sy = CPLGetXMLValue(data_window_node, "SizeY", ""); const char *tx = CPLGetXMLValue(data_window_node, "TileX", "0"); const char *ty = CPLGetXMLValue(data_window_node, "TileY", "0"); const char *tlevel = CPLGetXMLValue(data_window_node, "TileLevel", osDefaultTileLevel); const char *str_tile_count_x = CPLGetXMLValue(data_window_node, "TileCountX", osDefaultTileCountX); const char *str_tile_count_y = CPLGetXMLValue(data_window_node, "TileCountY", osDefaultTileCountY); const char *y_origin = CPLGetXMLValue(data_window_node, "YOrigin", "default"); if (ret == CE_None) { if ((ulx[0] != '\0') && (uly[0] != '\0') && (lrx[0] != '\0') && (lry[0] != '\0')) { m_data_window.m_x0 = CPLAtof(ulx); m_data_window.m_y0 = CPLAtof(uly); m_data_window.m_x1 = CPLAtof(lrx); m_data_window.m_y1 = CPLAtof(lry); } else { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Mandatory elements of DataWindow missing: UpperLeftX, UpperLeftY, LowerRightX, LowerRightY."); ret = CE_Failure; } } m_data_window.m_tlevel = atoi(tlevel); if (ret == CE_None) { if ((sx[0] != '\0') && (sy[0] != '\0')) { m_data_window.m_sx = atoi(sx); m_data_window.m_sy = atoi(sy); } else if ((tlevel[0] != '\0') && (str_tile_count_x[0] != '\0') && (str_tile_count_y[0] != '\0')) { int tile_count_x = atoi(str_tile_count_x); int tile_count_y = atoi(str_tile_count_y); m_data_window.m_sx = tile_count_x * m_block_size_x * (1 << m_data_window.m_tlevel); m_data_window.m_sy = tile_count_y * m_block_size_y * (1 << m_data_window.m_tlevel); } else { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Mandatory elements of DataWindow missing: SizeX, SizeY."); ret = CE_Failure; } } if (ret == CE_None) { if ((tx[0] != '\0') && (ty[0] != '\0')) { m_data_window.m_tx = atoi(tx); m_data_window.m_ty = atoi(ty); } else { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Mandatory elements of DataWindow missing: TileX, TileY."); ret = CE_Failure; } } if (ret == CE_None) { if (overview_count[0] != '\0') { nOverviews = atoi(overview_count); } else if (tlevel[0] != '\0') { nOverviews = m_data_window.m_tlevel; } else { const int min_overview_size = MAX(32, MIN(m_block_size_x, m_block_size_y)); double a = log(static_cast<double>(MIN(m_data_window.m_sx, m_data_window.m_sy))) / log(2.0) - log(static_cast<double>(min_overview_size)) / log(2.0); nOverviews = MAX(0, MIN(static_cast<int>(ceil(a)), 32)); } } if (ret == CE_None) { CPLString y_origin_str = y_origin; if (y_origin_str == "top") { m_data_window.m_y_origin = GDALWMSDataWindow::TOP; } else if (y_origin_str == "bottom") { m_data_window.m_y_origin = GDALWMSDataWindow::BOTTOM; } else if (y_origin_str == "default") { m_data_window.m_y_origin = GDALWMSDataWindow::DEFAULT; } else { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: DataWindow YOrigin must be set to " "one of 'default', 'top', or 'bottom', not '%s'.", y_origin_str.c_str()); ret = CE_Failure; } } } } if (ret == CE_None) { if (nBands<1) nBands=atoi(CPLGetXMLValue(config,"BandsCount","3")); if (nBands<1) { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Bad number of bands."); ret = CE_Failure; } } if (ret == CE_None) { const char *data_type = CPLGetXMLValue(config, "DataType", "Byte"); m_data_type = GDALGetDataTypeByName( data_type ); if ( m_data_type == GDT_Unknown || m_data_type >= GDT_TypeCount ) { CPLError( CE_Failure, CPLE_AppDefined, "GDALWMS: Invalid value in DataType. Data type \"%s\" is not supported.", data_type ); ret = CE_Failure; } } // Initialize the bands and the overviews. Assumes overviews are powers of two if (ret == CE_None) { nRasterXSize = m_data_window.m_sx; nRasterYSize = m_data_window.m_sy; if (!GDALCheckDatasetDimensions(nRasterXSize, nRasterYSize) || !GDALCheckBandCount(nBands, TRUE)) { return CE_Failure; } GDALColorInterp default_color_interp[4][4] = { { GCI_GrayIndex, GCI_Undefined, GCI_Undefined, GCI_Undefined }, { GCI_GrayIndex, GCI_AlphaBand, GCI_Undefined, GCI_Undefined }, { GCI_RedBand, GCI_GreenBand, GCI_BlueBand, GCI_Undefined }, { GCI_RedBand, GCI_GreenBand, GCI_BlueBand, GCI_AlphaBand } }; for (int i = 0; i < nBands; ++i) { GDALColorInterp color_interp = (nBands <= 4 && i <= 3 ? default_color_interp[nBands - 1][i] : GCI_Undefined); GDALWMSRasterBand *band = new GDALWMSRasterBand(this, i, 1.0); band->m_color_interp = color_interp; SetBand(i + 1, band); double scale = 0.5; for (int j = 0; j < nOverviews; ++j) { band->AddOverview(scale); band->m_color_interp = color_interp; scale *= 0.5; } } } } // UserPwd const char *pszUserPwd = CPLGetXMLValue(config, "UserPwd", ""); if (pszUserPwd[0] != '\0') m_osUserPwd = pszUserPwd; const char *pszUserAgent = CPLGetXMLValue(config, "UserAgent", ""); if (pszUserAgent[0] != '\0') m_osUserAgent = pszUserAgent; const char *pszReferer = CPLGetXMLValue(config, "Referer", ""); if (pszReferer[0] != '\0') m_osReferer = pszReferer; if (ret == CE_None) { const char *pszHttpZeroBlockCodes = CPLGetXMLValue(config, "ZeroBlockHttpCodes", ""); if(pszHttpZeroBlockCodes[0] == '\0') { m_http_zeroblock_codes.push_back(204); } else { char **kv = CSLTokenizeString2(pszHttpZeroBlockCodes,",",CSLT_HONOURSTRINGS); int nCount = CSLCount(kv); for(int i=0; i<nCount; i++) { int code = atoi(kv[i]); if(code <= 0) { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Invalid value of ZeroBlockHttpCodes \"%s\", comma separated HTTP response codes expected.", kv[i]); ret = CE_Failure; break; } m_http_zeroblock_codes.push_back(code); } CSLDestroy(kv); } } if (ret == CE_None) { const char *pszZeroExceptions = CPLGetXMLValue(config, "ZeroBlockOnServerException", ""); if(pszZeroExceptions[0] != '\0') { m_zeroblock_on_serverexceptions = StrToBool(pszZeroExceptions); if (m_zeroblock_on_serverexceptions == -1) { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Invalid value of ZeroBlockOnServerException \"%s\", true/false expected.", pszZeroExceptions); ret = CE_Failure; } } } if (ret == CE_None) { const char *max_conn = CPLGetXMLValue(config, "MaxConnections", ""); if (max_conn[0] != '\0') { m_http_max_conn = atoi(max_conn); } else { m_http_max_conn = 2; } } if (ret == CE_None) { const char *timeout = CPLGetXMLValue(config, "Timeout", ""); if (timeout[0] != '\0') { m_http_timeout = atoi(timeout); } else { m_http_timeout = 300; } } if (ret == CE_None) { const char *offline_mode = CPLGetXMLValue(config, "OfflineMode", ""); if (offline_mode[0] != '\0') { const int offline_mode_bool = StrToBool(offline_mode); if (offline_mode_bool == -1) { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Invalid value of OfflineMode, true / false expected."); ret = CE_Failure; } else { m_offline_mode = offline_mode_bool; } } else { m_offline_mode = 0; } } if (ret == CE_None) { const char *advise_read = CPLGetXMLValue(config, "AdviseRead", ""); if (advise_read[0] != '\0') { const int advise_read_bool = StrToBool(advise_read); if (advise_read_bool == -1) { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Invalid value of AdviseRead, true / false expected."); ret = CE_Failure; } else { m_use_advise_read = advise_read_bool; } } else { m_use_advise_read = 0; } } if (ret == CE_None) { const char *verify_advise_read = CPLGetXMLValue(config, "VerifyAdviseRead", ""); if (m_use_advise_read) { if (verify_advise_read[0] != '\0') { const int verify_advise_read_bool = StrToBool(verify_advise_read); if (verify_advise_read_bool == -1) { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Invalid value of VerifyAdviseRead, true / false expected."); ret = CE_Failure; } else { m_verify_advise_read = verify_advise_read_bool; } } else { m_verify_advise_read = 1; } } } // Let the local configuration override the minidriver supplied projection if (ret == CE_None) { const char *proj = CPLGetXMLValue(config, "Projection", ""); if (proj[0] != '\0') { m_projection = ProjToWKT(proj); if (m_projection.size() == 0) { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Bad projection specified."); ret = CE_Failure; } } } // Same for Min, Max and NoData, defined per band or per dataset // If they are set as null strings, they clear the server declared values if (ret == CE_None) { // Data values are attributes, they include NoData Min and Max // TODO: document those options if (0!=CPLGetXMLNode(config,"DataValues")) { const char *nodata=CPLGetXMLValue(config,"DataValues.NoData",NULL); if (nodata!=NULL) WMSSetNoDataValue(nodata); const char *min=CPLGetXMLValue(config,"DataValues.min",NULL); if (min!=NULL) WMSSetMinValue(min); const char *max=CPLGetXMLValue(config,"DataValues.max",NULL); if (max!=NULL) WMSSetMaxValue(max); } } if (ret == CE_None) { CPLXMLNode *cache_node = CPLGetXMLNode(config, "Cache"); if (cache_node != NULL) { m_cache = new GDALWMSCache(); if (m_cache->Initialize(cache_node) != CE_None) { delete m_cache; m_cache = NULL; CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Failed to initialize cache."); ret = CE_Failure; } } } if (ret == CE_None) { const int v = StrToBool(CPLGetXMLValue(config, "UnsafeSSL", "false")); if (v == -1) { CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Invalid value of UnsafeSSL: true or false expected."); ret = CE_Failure; } else { m_unsafeSsl = v; } } if (ret == CE_None) { /* If we dont have projection already set ask mini-driver. */ if (!m_projection.size()) { const char *proj = m_mini_driver->GetProjectionInWKT(); if (proj != NULL) { m_projection = proj; } } } return ret; }
void WCTSTransform( CPLXMLNode *psOperation ) { OGRSpatialReference *poSrcCRS, *poDstCRS; CPLXMLNode *psSrcXMLCRS, *psDstXMLCRS; /* -------------------------------------------------------------------- */ /* Translate the source CRS. */ /* -------------------------------------------------------------------- */ psSrcXMLCRS = CPLGetXMLNode( psOperation, "SourceCRS" ); if( psSrcXMLCRS == NULL ) WCTSEmitServiceException( "Unable to identify SourceCRS.CoordinateReferenceSystem" ); poSrcCRS = WCTSImportCoordinateReferenceSystem( psSrcXMLCRS ); /* -------------------------------------------------------------------- */ /* Translate the destination CRS. */ /* -------------------------------------------------------------------- */ psDstXMLCRS = CPLGetXMLNode( psOperation, "TargetCRS" ); if( psDstXMLCRS == NULL ) WCTSEmitServiceException( "Unable to identify DestinationCRS.CoordinateReferenceSystem" ); poDstCRS = WCTSImportCoordinateReferenceSystem( psDstXMLCRS ); /* -------------------------------------------------------------------- */ /* Create the coordinate transformation object. */ /* -------------------------------------------------------------------- */ OGRCoordinateTransformation *poCT; poCT = OGRCreateCoordinateTransformation( poSrcCRS, poDstCRS ); if( poCT == NULL ) WCTSEmitServiceException( "Unable to transform between source and destination CRSs." ); delete poSrcCRS; delete poDstCRS; /* -------------------------------------------------------------------- */ /* We will recurse over the GML data tree looking for segments */ /* that are recognizably geometries to be transformed in place. */ /* -------------------------------------------------------------------- */ CPLXMLNode *psData = CPLGetXMLNode(psOperation,"Data"); if( psData == NULL ) WCTSEmitServiceException( "Unable to find GML Data contents." ); WCTSGetData( psData ); WCTSRecurseAndTransform( psData, poCT ); /* -------------------------------------------------------------------- */ /* Now translate the data back into a serialized form suitable */ /* for including in the reply. */ /* -------------------------------------------------------------------- */ char *pszDataText = CPLSerializeXMLTree( psData ); /* -------------------------------------------------------------------- */ /* Return result. */ /* -------------------------------------------------------------------- */ printf( "Content-type: text/xml%c%c", 10, 10 ); printf( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); printf( "<TransformResponse xmlns:gml=\"http://www.opengis.net/gml\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" >\n" ); fwrite( pszDataText, 1, strlen(pszDataText), stdout ); printf( "</TransformResponse>\n" ); exit( 0 ); }
static GDALDataset * VRTCreateCopy( const char * pszFilename, GDALDataset *poSrcDS, int /* bStrict */, char ** /* papszOptions */, GDALProgressFunc /* pfnProgress */, void * /* pProgressData */ ) { CPLAssert( NULL != poSrcDS ); /* -------------------------------------------------------------------- */ /* If the source dataset is a virtual dataset then just write */ /* it to disk as a special case to avoid extra layers of */ /* indirection. */ /* -------------------------------------------------------------------- */ if( poSrcDS->GetDriver() != NULL && EQUAL(poSrcDS->GetDriver()->GetDescription(),"VRT") ) { /* -------------------------------------------------------------------- */ /* Convert tree to a single block of XML text. */ /* -------------------------------------------------------------------- */ char *pszVRTPath = CPLStrdup(CPLGetPath(pszFilename)); reinterpret_cast<VRTDataset *>( poSrcDS )->UnsetPreservedRelativeFilenames(); CPLXMLNode *psDSTree = reinterpret_cast<VRTDataset *>( poSrcDS )->SerializeToXML( pszVRTPath ); char *pszXML = CPLSerializeXMLTree( psDSTree ); CPLDestroyXMLNode( psDSTree ); CPLFree( pszVRTPath ); /* -------------------------------------------------------------------- */ /* Write to disk. */ /* -------------------------------------------------------------------- */ GDALDataset* pCopyDS = NULL; if( 0 != strlen( pszFilename ) ) { VSILFILE *fpVRT = VSIFOpenL( pszFilename, "wb" ); if( fpVRT == NULL ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot create %s", pszFilename); CPLFree( pszXML ); return NULL; } bool bRet = VSIFWriteL( pszXML, strlen(pszXML), 1, fpVRT ) > 0; if( VSIFCloseL( fpVRT ) != 0 ) bRet = false; if( bRet ) pCopyDS = reinterpret_cast<GDALDataset *>( GDALOpen( pszFilename, GA_Update ) ); } else { /* No destination file is given, so pass serialized XML directly. */ pCopyDS = reinterpret_cast<GDALDataset *>( GDALOpen( pszXML, GA_Update ) ); } CPLFree( pszXML ); return pCopyDS; } /* -------------------------------------------------------------------- */ /* Create the virtual dataset. */ /* -------------------------------------------------------------------- */ VRTDataset *poVRTDS = reinterpret_cast<VRTDataset *>( VRTDataset::Create( pszFilename, poSrcDS->GetRasterXSize(), poSrcDS->GetRasterYSize(), 0, GDT_Byte, NULL ) ); if( poVRTDS == NULL ) return NULL; /* -------------------------------------------------------------------- */ /* Do we have a geotransform? */ /* -------------------------------------------------------------------- */ double adfGeoTransform[6] = { 0.0 }; if( poSrcDS->GetGeoTransform( adfGeoTransform ) == CE_None ) { poVRTDS->SetGeoTransform( adfGeoTransform ); } /* -------------------------------------------------------------------- */ /* Copy projection */ /* -------------------------------------------------------------------- */ poVRTDS->SetProjection( poSrcDS->GetProjectionRef() ); /* -------------------------------------------------------------------- */ /* Emit dataset level metadata. */ /* -------------------------------------------------------------------- */ poVRTDS->SetMetadata( poSrcDS->GetMetadata() ); /* -------------------------------------------------------------------- */ /* Copy any special domains that should be transportable. */ /* -------------------------------------------------------------------- */ char **papszMD = poSrcDS->GetMetadata( "RPC" ); if( papszMD ) poVRTDS->SetMetadata( papszMD, "RPC" ); papszMD = poSrcDS->GetMetadata( "IMD" ); if( papszMD ) poVRTDS->SetMetadata( papszMD, "IMD" ); papszMD = poSrcDS->GetMetadata( "GEOLOCATION" ); if( papszMD ) poVRTDS->SetMetadata( papszMD, "GEOLOCATION" ); /* -------------------------------------------------------------------- */ /* GCPs */ /* -------------------------------------------------------------------- */ if( poSrcDS->GetGCPCount() > 0 ) { poVRTDS->SetGCPs( poSrcDS->GetGCPCount(), poSrcDS->GetGCPs(), poSrcDS->GetGCPProjection() ); } /* -------------------------------------------------------------------- */ /* Loop over all the bands. */ /* -------------------------------------------------------------------- */ for( int iBand = 0; iBand < poSrcDS->GetRasterCount(); iBand++ ) { GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand( iBand+1 ); /* -------------------------------------------------------------------- */ /* Create the band with the appropriate band type. */ /* -------------------------------------------------------------------- */ poVRTDS->AddBand( poSrcBand->GetRasterDataType(), NULL ); VRTSourcedRasterBand *poVRTBand = reinterpret_cast<VRTSourcedRasterBand *>( poVRTDS->GetRasterBand( iBand+1 ) ); /* -------------------------------------------------------------------- */ /* Setup source mapping. */ /* -------------------------------------------------------------------- */ poVRTBand->AddSimpleSource( poSrcBand ); /* -------------------------------------------------------------------- */ /* Emit various band level metadata. */ /* -------------------------------------------------------------------- */ poVRTBand->CopyCommonInfoFrom( poSrcBand ); /* -------------------------------------------------------------------- */ /* Add specific mask band. */ /* -------------------------------------------------------------------- */ if( (poSrcBand->GetMaskFlags() & (GMF_PER_DATASET | GMF_ALL_VALID | GMF_NODATA)) == 0) { VRTSourcedRasterBand* poVRTMaskBand = new VRTSourcedRasterBand( poVRTDS, 0, poSrcBand->GetMaskBand()->GetRasterDataType(), poSrcDS->GetRasterXSize(), poSrcDS->GetRasterYSize()); poVRTMaskBand->AddMaskBandSource( poSrcBand ); poVRTBand->SetMaskBand( poVRTMaskBand ); } } /* -------------------------------------------------------------------- */ /* Add dataset mask band */ /* -------------------------------------------------------------------- */ if( poSrcDS->GetRasterCount() != 0 && poSrcDS->GetRasterBand(1) != NULL && poSrcDS->GetRasterBand(1)->GetMaskFlags() == GMF_PER_DATASET ) { GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand(1); VRTSourcedRasterBand* poVRTMaskBand = new VRTSourcedRasterBand( poVRTDS, 0, poSrcBand->GetMaskBand()->GetRasterDataType(), poSrcDS->GetRasterXSize(), poSrcDS->GetRasterYSize() ); poVRTMaskBand->AddMaskBandSource( poSrcBand ); poVRTDS->SetMaskBand( poVRTMaskBand ); } poVRTDS->FlushCache(); return poVRTDS; }
const char *GDALWMSRasterBand::GetMetadataItem( const char * pszName, const char * pszDomain ) { /* ==================================================================== */ /* LocationInfo handling. */ /* ==================================================================== */ if( pszDomain != NULL && EQUAL(pszDomain,"LocationInfo") && (STARTS_WITH_CI(pszName, "Pixel_") || STARTS_WITH_CI(pszName, "GeoPixel_")) ) { int iPixel, iLine; /* -------------------------------------------------------------------- */ /* What pixel are we aiming at? */ /* -------------------------------------------------------------------- */ if( STARTS_WITH_CI(pszName, "Pixel_") ) { if( sscanf( pszName+6, "%d_%d", &iPixel, &iLine ) != 2 ) return NULL; } else if( STARTS_WITH_CI(pszName, "GeoPixel_") ) { double adfGeoTransform[6]; double adfInvGeoTransform[6]; double dfGeoX, dfGeoY; { dfGeoX = CPLAtof(pszName + 9); const char* pszUnderscore = strchr(pszName + 9, '_'); if( !pszUnderscore ) return NULL; dfGeoY = CPLAtof(pszUnderscore+1); } if( m_parent_dataset->GetGeoTransform( adfGeoTransform ) != CE_None ) return NULL; if( !GDALInvGeoTransform( adfGeoTransform, adfInvGeoTransform ) ) return NULL; iPixel = (int) floor( adfInvGeoTransform[0] + adfInvGeoTransform[1] * dfGeoX + adfInvGeoTransform[2] * dfGeoY ); iLine = (int) floor( adfInvGeoTransform[3] + adfInvGeoTransform[4] * dfGeoX + adfInvGeoTransform[5] * dfGeoY ); /* The GetDataset() for the WMS driver is always the main overview level, so rescale */ /* the values if we are an overview */ if (m_overview >= 0) { iPixel = (int) (1.0 * iPixel * GetXSize() / m_parent_dataset->GetRasterBand(1)->GetXSize()); iLine = (int) (1.0 * iLine * GetYSize() / m_parent_dataset->GetRasterBand(1)->GetYSize()); } } else return NULL; if( iPixel < 0 || iLine < 0 || iPixel >= GetXSize() || iLine >= GetYSize() ) return NULL; if (nBand != 1) { GDALRasterBand* poFirstBand = m_parent_dataset->GetRasterBand(1); if (m_overview >= 0) poFirstBand = poFirstBand->GetOverview(m_overview); if (poFirstBand) return poFirstBand->GetMetadataItem(pszName, pszDomain); } GDALWMSImageRequestInfo iri; GDALWMSTiledImageRequestInfo tiri; int nBlockXOff = iPixel / nBlockXSize; int nBlockYOff = iLine / nBlockYSize; ComputeRequestInfo(iri, tiri, nBlockXOff, nBlockYOff); CPLString url; m_parent_dataset->m_mini_driver->GetTiledImageInfo(&url, iri, tiri, iPixel % nBlockXSize, iLine % nBlockXSize); char* pszRes = NULL; if (url.size() != 0) { if (url == osMetadataItemURL) { return osMetadataItem.size() != 0 ? osMetadataItem.c_str() : NULL; } osMetadataItemURL = url; char **http_request_opts = BuildHTTPRequestOpts(); CPLHTTPResult* psResult = CPLHTTPFetch( url.c_str(), http_request_opts); if( psResult && psResult->pabyData ) pszRes = CPLStrdup((const char*) psResult->pabyData); CPLHTTPDestroyResult(psResult); CSLDestroy(http_request_opts); } if (pszRes) { osMetadataItem = "<LocationInfo>"; CPLPushErrorHandler(CPLQuietErrorHandler); CPLXMLNode* psXML = CPLParseXMLString(pszRes); CPLPopErrorHandler(); if (psXML != NULL && psXML->eType == CXT_Element) { if (strcmp(psXML->pszValue, "?xml") == 0) { if (psXML->psNext) { char* pszXML = CPLSerializeXMLTree(psXML->psNext); osMetadataItem += pszXML; CPLFree(pszXML); } } else { osMetadataItem += pszRes; } } else { char* pszEscapedXML = CPLEscapeString(pszRes, -1, CPLES_XML_BUT_QUOTES); osMetadataItem += pszEscapedXML; CPLFree(pszEscapedXML); } if (psXML != NULL) CPLDestroyXMLNode(psXML); osMetadataItem += "</LocationInfo>"; CPLFree(pszRes); return osMetadataItem.c_str(); } else { osMetadataItem = ""; return NULL; } } return GDALPamRasterBand::GetMetadataItem(pszName, pszDomain); }
int main( int argc, char ** argv ) { const char *pszLocX = NULL, *pszLocY = NULL; const char *pszSrcFilename = NULL; char *pszSourceSRS = NULL; std::vector<int> anBandList; bool bAsXML = false, bLIFOnly = false; bool bQuiet = false, bValOnly = false; int nOverview = -1; char **papszOpenOptions = NULL; GDALAllRegister(); argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 ); if( argc < 1 ) exit( -argc ); /* -------------------------------------------------------------------- */ /* Parse arguments. */ /* -------------------------------------------------------------------- */ int i; 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],"-b") && i < argc-1 ) { anBandList.push_back( atoi(argv[++i]) ); } else if( EQUAL(argv[i],"-overview") && i < argc-1 ) { nOverview = atoi(argv[++i]) - 1; } else if( EQUAL(argv[i],"-l_srs") && i < argc-1 ) { CPLFree(pszSourceSRS); pszSourceSRS = SanitizeSRS(argv[++i]); } else if( EQUAL(argv[i],"-geoloc") ) { CPLFree(pszSourceSRS); pszSourceSRS = CPLStrdup("-geoloc"); } else if( EQUAL(argv[i],"-wgs84") ) { CPLFree(pszSourceSRS); pszSourceSRS = SanitizeSRS("WGS84"); } else if( EQUAL(argv[i],"-xml") ) { bAsXML = true; } else if( EQUAL(argv[i],"-lifonly") ) { bLIFOnly = true; bQuiet = true; } else if( EQUAL(argv[i],"-valonly") ) { bValOnly = true; bQuiet = true; } else if( EQUAL(argv[i], "-oo") && i < argc-1 ) { papszOpenOptions = CSLAddString( papszOpenOptions, argv[++i] ); } else if( argv[i][0] == '-' && !isdigit(argv[i][1]) ) Usage(); else if( pszSrcFilename == NULL ) pszSrcFilename = argv[i]; else if( pszLocX == NULL ) pszLocX = argv[i]; else if( pszLocY == NULL ) pszLocY = argv[i]; else Usage(); } if( pszSrcFilename == NULL || (pszLocX != NULL && pszLocY == NULL) ) Usage(); /* -------------------------------------------------------------------- */ /* Open source file. */ /* -------------------------------------------------------------------- */ GDALDatasetH hSrcDS = NULL; hSrcDS = GDALOpenEx( pszSrcFilename, GDAL_OF_RASTER, NULL, (const char* const* )papszOpenOptions, NULL ); if( hSrcDS == NULL ) exit( 1 ); /* -------------------------------------------------------------------- */ /* Setup coordinate transformation, if required */ /* -------------------------------------------------------------------- */ OGRSpatialReferenceH hSrcSRS = NULL, hTrgSRS = NULL; OGRCoordinateTransformationH hCT = NULL; if( pszSourceSRS != NULL && !EQUAL(pszSourceSRS,"-geoloc") ) { hSrcSRS = OSRNewSpatialReference( pszSourceSRS ); hTrgSRS = OSRNewSpatialReference( GDALGetProjectionRef( hSrcDS ) ); hCT = OCTNewCoordinateTransformation( hSrcSRS, hTrgSRS ); if( hCT == NULL ) exit( 1 ); } /* -------------------------------------------------------------------- */ /* If no bands were requested, we will query them all. */ /* -------------------------------------------------------------------- */ if( anBandList.size() == 0 ) { for( i = 0; i < GDALGetRasterCount( hSrcDS ); i++ ) anBandList.push_back( i+1 ); } /* -------------------------------------------------------------------- */ /* Turn the location into a pixel and line location. */ /* -------------------------------------------------------------------- */ int inputAvailable = 1; double dfGeoX; double dfGeoY; CPLString osXML; if( pszLocX == NULL && pszLocY == NULL ) { if (fscanf(stdin, "%lf %lf", &dfGeoX, &dfGeoY) != 2) { inputAvailable = 0; } } else { dfGeoX = CPLAtof(pszLocX); dfGeoY = CPLAtof(pszLocY); } while (inputAvailable) { int iPixel, iLine; if (hCT) { if( !OCTTransform( hCT, 1, &dfGeoX, &dfGeoY, NULL ) ) exit( 1 ); } if( pszSourceSRS != NULL ) { double adfGeoTransform[6], adfInvGeoTransform[6]; if( GDALGetGeoTransform( hSrcDS, adfGeoTransform ) != CE_None ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot get geotransform"); exit( 1 ); } if( !GDALInvGeoTransform( adfGeoTransform, adfInvGeoTransform ) ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot invert geotransform"); exit( 1 ); } iPixel = (int) floor( adfInvGeoTransform[0] + adfInvGeoTransform[1] * dfGeoX + adfInvGeoTransform[2] * dfGeoY ); iLine = (int) floor( adfInvGeoTransform[3] + adfInvGeoTransform[4] * dfGeoX + adfInvGeoTransform[5] * dfGeoY ); } else { iPixel = (int) floor(dfGeoX); iLine = (int) floor(dfGeoY); } /* -------------------------------------------------------------------- */ /* Prepare report. */ /* -------------------------------------------------------------------- */ CPLString osLine; if( bAsXML ) { osLine.Printf( "<Report pixel=\"%d\" line=\"%d\">", iPixel, iLine ); osXML += osLine; } else if( !bQuiet ) { printf( "Report:\n" ); printf( " Location: (%dP,%dL)\n", iPixel, iLine ); } int bPixelReport = TRUE; if( iPixel < 0 || iLine < 0 || iPixel >= GDALGetRasterXSize( hSrcDS ) || iLine >= GDALGetRasterYSize( hSrcDS ) ) { if( bAsXML ) osXML += "<Alert>Location is off this file! No further details to report.</Alert>"; else if( bValOnly ) printf("\n"); else if( !bQuiet ) printf( "\nLocation is off this file! No further details to report.\n"); bPixelReport = FALSE; } /* -------------------------------------------------------------------- */ /* Process each band. */ /* -------------------------------------------------------------------- */ for( i = 0; bPixelReport && i < (int) anBandList.size(); i++ ) { GDALRasterBandH hBand = GDALGetRasterBand( hSrcDS, anBandList[i] ); int iPixelToQuery = iPixel; int iLineToQuery = iLine; if (nOverview >= 0 && hBand != NULL) { GDALRasterBandH hOvrBand = GDALGetOverview(hBand, nOverview); if (hOvrBand != NULL) { int nOvrXSize = GDALGetRasterBandXSize(hOvrBand); int nOvrYSize = GDALGetRasterBandYSize(hOvrBand); iPixelToQuery = (int)(0.5 + 1.0 * iPixel / GDALGetRasterXSize( hSrcDS ) * nOvrXSize); iLineToQuery = (int)(0.5 + 1.0 * iLine / GDALGetRasterYSize( hSrcDS ) * nOvrYSize); if (iPixelToQuery >= nOvrXSize) iPixelToQuery = nOvrXSize - 1; if (iLineToQuery >= nOvrYSize) iLineToQuery = nOvrYSize - 1; } else { CPLError(CE_Failure, CPLE_AppDefined, "Cannot get overview %d of band %d", nOverview + 1, anBandList[i] ); } hBand = hOvrBand; } if (hBand == NULL) continue; if( bAsXML ) { osLine.Printf( "<BandReport band=\"%d\">", anBandList[i] ); osXML += osLine; } else if( !bQuiet ) { printf( " Band %d:\n", anBandList[i] ); } /* -------------------------------------------------------------------- */ /* Request location info for this location. It is possible */ /* only the VRT driver actually supports this. */ /* -------------------------------------------------------------------- */ CPLString osItem; osItem.Printf( "Pixel_%d_%d", iPixelToQuery, iLineToQuery ); const char *pszLI = GDALGetMetadataItem( hBand, osItem, "LocationInfo"); if( pszLI != NULL ) { if( bAsXML ) osXML += pszLI; else if( !bQuiet ) printf( " %s\n", pszLI ); else if( bLIFOnly ) { /* Extract all files, if any. */ CPLXMLNode *psRoot = CPLParseXMLString( pszLI ); if( psRoot != NULL && psRoot->psChild != NULL && psRoot->eType == CXT_Element && EQUAL(psRoot->pszValue,"LocationInfo") ) { CPLXMLNode *psNode; for( psNode = psRoot->psChild; psNode != NULL; psNode = psNode->psNext ) { if( psNode->eType == CXT_Element && EQUAL(psNode->pszValue,"File") && psNode->psChild != NULL ) { char* pszUnescaped = CPLUnescapeString( psNode->psChild->pszValue, NULL, CPLES_XML); printf( "%s\n", pszUnescaped ); CPLFree(pszUnescaped); } } } CPLDestroyXMLNode( psRoot ); } } /* -------------------------------------------------------------------- */ /* Report the pixel value of this band. */ /* -------------------------------------------------------------------- */ double adfPixel[2]; if( GDALRasterIO( hBand, GF_Read, iPixelToQuery, iLineToQuery, 1, 1, adfPixel, 1, 1, GDT_CFloat64, 0, 0) == CE_None ) { CPLString osValue; if( GDALDataTypeIsComplex( GDALGetRasterDataType( hBand ) ) ) osValue.Printf( "%.15g+%.15gi", adfPixel[0], adfPixel[1] ); else osValue.Printf( "%.15g", adfPixel[0] ); if( bAsXML ) { osXML += "<Value>"; osXML += osValue; osXML += "</Value>"; } else if( !bQuiet ) printf( " Value: %s\n", osValue.c_str() ); else if( bValOnly ) printf( "%s\n", osValue.c_str() ); // Report unscaled if we have scale/offset values. int bSuccess; double dfOffset = GDALGetRasterOffset( hBand, &bSuccess ); double dfScale = GDALGetRasterScale( hBand, &bSuccess ); if( dfOffset != 0.0 || dfScale != 1.0 ) { adfPixel[0] = adfPixel[0] * dfScale + dfOffset; adfPixel[1] = adfPixel[1] * dfScale + dfOffset; if( GDALDataTypeIsComplex( GDALGetRasterDataType( hBand ) ) ) osValue.Printf( "%.15g+%.15gi", adfPixel[0], adfPixel[1] ); else osValue.Printf( "%.15g", adfPixel[0] ); if( bAsXML ) { osXML += "<DescaledValue>"; osXML += osValue; osXML += "</DescaledValue>"; } else if( !bQuiet ) printf( " Descaled Value: %s\n", osValue.c_str() ); } } if( bAsXML ) osXML += "</BandReport>"; } osXML += "</Report>"; if( (pszLocX != NULL && pszLocY != NULL) || (fscanf(stdin, "%lf %lf", &dfGeoX, &dfGeoY) != 2) ) { inputAvailable = 0; } } /* -------------------------------------------------------------------- */ /* Finalize xml report and print. */ /* -------------------------------------------------------------------- */ if( bAsXML ) { CPLXMLNode *psRoot; char *pszFormattedXML; psRoot = CPLParseXMLString( osXML ); pszFormattedXML = CPLSerializeXMLTree( psRoot ); CPLDestroyXMLNode( psRoot ); printf( "%s", pszFormattedXML ); CPLFree( pszFormattedXML ); } /* -------------------------------------------------------------------- */ /* Cleanup */ /* -------------------------------------------------------------------- */ if (hCT) { OSRDestroySpatialReference( hSrcSRS ); OSRDestroySpatialReference( hTrgSRS ); OCTDestroyCoordinateTransformation( hCT ); } if (hSrcDS) GDALClose(hSrcDS); GDALDumpOpenDatasets( stderr ); GDALDestroyDriverManager(); CPLFree(pszSourceSRS); CSLDestroy(papszOpenOptions); CSLDestroy( argv ); return 0; }
int GDALMultiDomainMetadata::XMLInit( CPLXMLNode *psTree, CPL_UNUSED int bMerge ) { CPLXMLNode *psMetadata; /* ==================================================================== */ /* Process all <Metadata> elements, each for one domain. */ /* ==================================================================== */ for( psMetadata = psTree->psChild; psMetadata != NULL; psMetadata = psMetadata->psNext ) { CPLXMLNode *psMDI; const char *pszDomain, *pszFormat; if( psMetadata->eType != CXT_Element || !EQUAL(psMetadata->pszValue,"Metadata") ) continue; pszDomain = CPLGetXMLValue( psMetadata, "domain", "" ); pszFormat = CPLGetXMLValue( psMetadata, "format", "" ); // Make sure we have a CPLStringList for this domain, // without wiping out an existing one. if( GetMetadata( pszDomain ) == NULL ) SetMetadata( NULL, pszDomain ); int iDomain = CSLFindString( papszDomainList, pszDomain ); CPLAssert( iDomain != -1 ); CPLStringList *poMDList = papoMetadataLists[iDomain]; /* -------------------------------------------------------------------- */ /* XML format subdocuments. */ /* -------------------------------------------------------------------- */ if( EQUAL(pszFormat,"xml") ) { CPLXMLNode *psSubDoc; /* find first non-attribute child of current element */ psSubDoc = psMetadata->psChild; while( psSubDoc != NULL && psSubDoc->eType == CXT_Attribute ) psSubDoc = psSubDoc->psNext; char *pszDoc = CPLSerializeXMLTree( psSubDoc ); poMDList->Clear(); poMDList->AddStringDirectly( pszDoc ); } /* -------------------------------------------------------------------- */ /* Name value format. */ /* <MDI key="...">value_Text</MDI> */ /* -------------------------------------------------------------------- */ else { for( psMDI = psMetadata->psChild; psMDI != NULL; psMDI = psMDI->psNext ) { if( !EQUAL(psMDI->pszValue,"MDI") || psMDI->eType != CXT_Element || psMDI->psChild == NULL || psMDI->psChild->psNext == NULL || psMDI->psChild->eType != CXT_Attribute || psMDI->psChild->psChild == NULL ) continue; char* pszName = psMDI->psChild->psChild->pszValue; char* pszValue = psMDI->psChild->psNext->pszValue; if( pszName != NULL && pszValue != NULL ) poMDList->SetNameValue( pszName, pszValue ); } } } return CSLCount(papszDomainList) != 0; }
OGRSpatialReference * WCTSImportCoordinateReferenceSystem( CPLXMLNode *psXMLCRS ) { CPLStripXMLNamespace( psXMLCRS->psChild, NULL, TRUE ); /* ==================================================================== */ /* Try to find a direct crsID as per old specification. */ /* ==================================================================== */ const char *pszCode = CPLGetXMLValue( psXMLCRS, "crsID.code", NULL ); const char *pszCodeSpace = CPLGetXMLValue( psXMLCRS, "crsID.codeSpace", NULL ); if( pszCode != NULL && pszCodeSpace != NULL ) { /* -------------------------------------------------------------------- */ /* Get the EPSG code, and verify that it is in the EPSG */ /* codeSpace. */ /* -------------------------------------------------------------------- */ OGRSpatialReference oSRS; if( EQUAL(pszCodeSpace,"EPSG") ) { int nEPSGCode = atoi(pszCode); if( nEPSGCode == 0 ) { WCTSEmitServiceException( "Failed to decode CoordinateReferenceSystem with missing,\n" "or zero crsID.code" ); } CPLErrorReset(); if( oSRS.importFromEPSG( nEPSGCode ) != OGRERR_NONE ) { if( strlen(CPLGetLastErrorMsg()) > 0 ) WCTSEmitServiceException( CPLGetLastErrorMsg() ); else WCTSEmitServiceException( CPLSPrintf( "OGRSpatialReference::importFromEPSG(%d) " "failed. Is this a defined EPSG code?", nEPSGCode ) ); } } /* -------------------------------------------------------------------- */ /* Handle AUTO case. */ /* -------------------------------------------------------------------- */ else if( EQUAL(pszCodeSpace,"AUTO") ) { if( oSRS.importFromWMSAUTO( pszCode ) != OGRERR_NONE ) { if( strlen(CPLGetLastErrorMsg()) > 0 ) WCTSEmitServiceException( CPLGetLastErrorMsg() ); else WCTSEmitServiceException( CPLSPrintf( "OGRSpatialReference::importFromWMSAUTO(%s) " "failed. Is this a defined EPSG code?", pszCode ) ); } } /* -------------------------------------------------------------------- */ /* Otherwise blow a gasket. */ /* -------------------------------------------------------------------- */ else { WCTSEmitServiceException( "Failed to decode CoordinateReferenceSystem with missing,\n" "or non-EPSG crsID.codeSpace" ); } /* -------------------------------------------------------------------- */ /* Translate into an OGRSpatialReference from EPSG code. */ /* -------------------------------------------------------------------- */ return oSRS.Clone(); } /* ==================================================================== */ /* Try to import a projectedCRS or geographicCRS. */ /* ==================================================================== */ if( CPLGetXMLNode( psXMLCRS, "ProjectedCRS" ) != NULL || CPLGetXMLNode( psXMLCRS, "GeographicCRS" ) != NULL ) { #ifdef DISABLE_USER_DEFINED_CRS WCTSEmitServiceException( "User defined ProjectedCRS and GeographicCRS support\n" "disabled for security reasons." ); #else char *pszSerializedForm; OGRSpatialReference oSRS; pszSerializedForm = CPLSerializeXMLTree( psXMLCRS->psChild ); if( oSRS.importFromXML( pszSerializedForm ) != OGRERR_NONE ) { CPLFree( pszSerializedForm ); if( strlen(CPLGetLastErrorMsg()) > 0 ) WCTSEmitServiceException( CPLGetLastErrorMsg() ); else WCTSEmitServiceException( "Failed to import CRS" ); } CPLFree( pszSerializedForm ); return oSRS.Clone(); #endif } /* -------------------------------------------------------------------- */ /* We don't seem to recognise a CRS here. */ /* -------------------------------------------------------------------- */ WCTSEmitServiceException( "Unable to identify CRS in one of SourceCRS or TargetCRS elements" ); return NULL; }
void NASHandler::endElement( const XMLCh* const /* uri */ , const XMLCh* const localname, const XMLCh* const /* qname */) { char szElementName[MAX_TOKEN_SIZE]; GMLReadState *poState = m_poReader->GetState(); tr_strcpy( szElementName, localname ); m_nDepth --; if (m_bIgnoreFeature && m_nDepth >= m_nDepthFeature) { if (m_nDepth == m_nDepthFeature) { m_bIgnoreFeature = false; m_nDepthFeature = 0; } return; } if ( m_osIgnoredElement != "" && m_nDepth >= m_nDepthElement ) { if ( m_nDepth == m_nDepthElement ) { m_osIgnoredElement = ""; m_nDepthElement = 0; } return; } #ifdef DEBUG_VERBOSE CPLDebug("NAS", "%*sendElement %s m_bIgnoreFeature:%d depth:%d depthFeature:%d featureClass:%s", m_nDepth, "", szElementName, m_bIgnoreFeature, m_nDepth, m_nDepthFeature, poState->m_poFeature ? poState->m_poFeature->GetClass()->GetElementName() : "(no feature)" ); #endif if( m_bInUpdateProperty ) { if( EQUAL( szElementName, "Name" ) ) { CPLAssert( m_osLastPropertyName == "" ); m_osLastPropertyName = m_pszCurField; m_pszCurField = NULL; } else if( EQUAL( szElementName, "Value" ) ) { CPLAssert( m_osLastPropertyValue == "" ); m_osLastPropertyValue = m_pszCurField; m_pszCurField = NULL; } else if( EQUAL( szElementName, "Property" ) ) { if( EQUAL( m_osLastPropertyName, "adv:lebenszeitintervall/adv:AA_Lebenszeitintervall/adv:endet" ) ) { CPLAssert( m_osLastPropertyValue != "" ); m_osLastEnded = m_osLastPropertyValue; } else if( EQUAL( m_osLastPropertyName, "adv:anlass" ) ) { CPLAssert( m_osLastPropertyValue != "" ); m_osLastOccasion = m_osLastPropertyValue; } else { CPLError( CE_Warning, CPLE_AppDefined, "NAS: Expected property name or value instead of %s", m_osLastPropertyName.c_str() ); } m_osLastPropertyName = ""; m_osLastPropertyValue = ""; m_bInUpdateProperty = false; } poState->PopPath(); return; } if ( m_bInUpdate && EQUAL( szElementName, "Update" ) ) { m_bInUpdate = false; } /* -------------------------------------------------------------------- */ /* Is this closing off an attribute value? We assume so if */ /* we are collecting an attribute value and got to this point. */ /* We don't bother validating that the closing tag matches the */ /* opening tag. */ /* -------------------------------------------------------------------- */ if( m_pszCurField != NULL ) { CPLAssert( poState->m_poFeature != NULL ); m_poReader->SetFeaturePropertyDirectly( poState->osPath.c_str(), m_pszCurField ); m_pszCurField = NULL; } /* -------------------------------------------------------------------- */ /* If we are collecting Geometry than store it, and consider if */ /* this is the end of the geometry. */ /* -------------------------------------------------------------------- */ if( m_pszGeometry != NULL ) { int nLNLen = tr_strlen( localname ); /* should save attributes too! */ if( m_nGeomLen + nLNLen + 4 > m_nGeomAlloc ) { m_nGeomAlloc = (int) (m_nGeomAlloc * 1.3 + nLNLen + 1000); m_pszGeometry = (char *) CPLRealloc( m_pszGeometry, m_nGeomAlloc); } strcat( m_pszGeometry+m_nGeomLen, "</" ); tr_strcpy( m_pszGeometry+m_nGeomLen+2, localname ); strcat( m_pszGeometry+m_nGeomLen+nLNLen+2, ">" ); m_nGeomLen += static_cast<int>(strlen(m_pszGeometry+m_nGeomLen)); if( poState->m_nPathLength == m_nGeometryDepth+1 ) { if( poState->m_poFeature != NULL ) { CPLXMLNode* psNode = CPLParseXMLString(m_pszGeometry); if (psNode) { /* workaround for common malformed gml:pos with just a * elevation value instead of a full 3D coordinate: * * <gml:Point gml:id="BII2H"> * <gml:pos srsName="urn:adv:crs:ETRS89_h">41.394</gml:pos> * </gml:Point> * */ const char *pszPos; if( (pszPos = CPLGetXMLValue( psNode, "=Point.pos", NULL ) ) != NULL && strstr(pszPos, " ") == NULL ) { CPLSetXMLValue( psNode, "pos", CPLSPrintf("0 0 %s", pszPos) ); } if ( poState->m_poFeature->GetGeometryList() && poState->m_poFeature->GetGeometryList()[0] ) { int iId = poState->m_poFeature->GetClass()->GetPropertyIndex( "gml_id" ); const GMLProperty *poIdProp = poState->m_poFeature->GetProperty(iId); #ifdef DEBUG_VERBOSE char *pszOldGeom = CPLSerializeXMLTree( poState->m_poFeature->GetGeometryList()[0] ); CPLDebug("NAS", "Overwriting other geometry (%s; replace:%s; with:%s)", poIdProp && poIdProp->nSubProperties>0 && poIdProp->papszSubProperties[0] ? poIdProp->papszSubProperties[0] : "(null)", m_pszGeometry, pszOldGeom ); CPLFree( pszOldGeom ); #else CPLError( CE_Warning, CPLE_AppDefined, "NAS: Overwriting other geometry (%s)", poIdProp && poIdProp->nSubProperties>0 && poIdProp->papszSubProperties[0] ? poIdProp->papszSubProperties[0] : "(null)" ); #endif } poState->m_poFeature->SetGeometryDirectly( psNode ); } else CPLError( CE_Warning, CPLE_AppDefined, "NAS: Invalid geometry skipped" ); } else CPLError( CE_Warning, CPLE_AppDefined, "NAS: Skipping geometry without feature" ); CPLFree( m_pszGeometry ); m_pszGeometry = NULL; m_nGeomAlloc = m_nGeomLen = 0; } } /* -------------------------------------------------------------------- */ /* If we are collecting a feature, and this element tag matches */ /* element name for the class, then we have finished the */ /* feature, and we pop the feature read state. */ /* -------------------------------------------------------------------- */ const char *pszLast = NULL; if( m_nDepth == m_nDepthFeature && poState->m_poFeature != NULL && EQUAL(szElementName, poState->m_poFeature->GetClass()->GetElementName()) ) { m_nDepthFeature = 0; m_poReader->PopState(); } /* -------------------------------------------------------------------- */ /* Ends of a wfs:Delete or wfs:Update should be triggered on the */ /* close of the <Filter> element. */ /* -------------------------------------------------------------------- */ else if( m_nDepth == m_nDepthFeature && poState->m_poFeature != NULL && EQUAL(szElementName,"Filter") && (pszLast=poState->m_poFeature->GetClass()->GetElementName()) != NULL && ( EQUAL(pszLast, "Delete") || EQUAL(pszLast, "Update") ) ) { m_nDepthFeature = 0; m_poReader->PopState(); } /* -------------------------------------------------------------------- */ /* Otherwise, we just pop the element off the local read states */ /* element stack. */ /* -------------------------------------------------------------------- */ else { if( EQUAL(szElementName,poState->GetLastComponent()) ) poState->PopPath(); else { CPLAssert( false ); } } }