📄 ogrgmldatasource.cpp
字号:
/* -------------------------------------------------------------------- *//* Save the schema file if possible. Don't make a fuss if we *//* can't ... could be read-only directory or something. *//* -------------------------------------------------------------------- */ if( !bHaveSchema ) { FILE *fp = NULL; pszGFSFilename = CPLResetExtension( pszNewName, "gfs" ); if( CPLStat( pszGFSFilename, &sGFSStatBuf ) != 0 && (fp = VSIFOpen( pszGFSFilename, "wt" )) != NULL ) { VSIFClose( fp ); poReader->SaveClasses( pszGFSFilename ); } else { CPLDebug("GML", "Not saving %s files already exists or can't be created.", pszGFSFilename ); } }/* -------------------------------------------------------------------- *//* Translate the GMLFeatureClasses into layers. *//* -------------------------------------------------------------------- */ papoLayers = (OGRGMLLayer **) CPLCalloc( sizeof(OGRGMLLayer *), poReader->GetClassCount()); nLayers = 0; while( nLayers < poReader->GetClassCount() ) { papoLayers[nLayers] = TranslateGMLSchema(poReader->GetClass(nLayers)); nLayers++; } return TRUE;}/************************************************************************//* TranslateGMLSchema() *//************************************************************************/OGRGMLLayer *OGRGMLDataSource::TranslateGMLSchema( GMLFeatureClass *poClass ){ OGRGMLLayer *poLayer;/* -------------------------------------------------------------------- *//* Create an empty layer. *//* -------------------------------------------------------------------- */ poLayer = new OGRGMLLayer( poClass->GetName(), NULL, FALSE, wkbUnknown, this );/* -------------------------------------------------------------------- *//* Added attributes (properties). *//* -------------------------------------------------------------------- */ for( int iField = 0; iField < poClass->GetPropertyCount(); iField++ ) { GMLPropertyDefn *poProperty = poClass->GetProperty( iField ); OGRFieldType eFType; if( poProperty->GetType() == GMLPT_Untyped ) eFType = OFTString; else if( poProperty->GetType() == GMLPT_String ) eFType = OFTString; else if( poProperty->GetType() == GMLPT_Integer ) eFType = OFTInteger; else if( poProperty->GetType() == GMLPT_Real ) eFType = OFTReal; else eFType = OFTString; OGRFieldDefn oField( poProperty->GetName(), eFType ); poLayer->GetLayerDefn()->AddFieldDefn( &oField ); } return poLayer;}/************************************************************************//* Create() *//************************************************************************/int OGRGMLDataSource::Create( const char *pszFilename, char **papszOptions ){ if( fpOutput != NULL || poReader != NULL ) { CPLAssert( FALSE ); return FALSE; }/* -------------------------------------------------------------------- *//* Create the output file. *//* -------------------------------------------------------------------- */ pszName = CPLStrdup( pszFilename ); if( EQUAL(pszFilename,"stdout") ) fpOutput = stdout; else fpOutput = VSIFOpen( pszFilename, "wt+" ); if( fpOutput == NULL ) { CPLError( CE_Failure, CPLE_OpenFailed, "Failed to create GML file %s.", pszFilename ); return FALSE; }/* -------------------------------------------------------------------- *//* Write out "standard" header. *//* -------------------------------------------------------------------- */ VSIFPrintf( fpOutput, "%s", "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" ); nSchemaInsertLocation = VSIFTell( fpOutput ); VSIFPrintf( fpOutput, "%s", "<ogr:FeatureCollection\n" );/* -------------------------------------------------------------------- *//* Write out schema info if provided in creation options. *//* -------------------------------------------------------------------- */ const char *pszSchemaURI = CSLFetchNameValue(papszOptions,"XSISCHEMAURI"); const char *pszSchemaOpt = CSLFetchNameValue( papszOptions, "XSISCHEMA" ); if( pszSchemaURI != NULL ) { VSIFPrintf( fpOutput, " xmlns:xsi=\"http://www.w3c.org/2001/XMLSchema-instance\"\n" " xsi:schemaLocation=\"%s\"\n", CSLFetchNameValue( papszOptions, "XSISCHEMAURI" ) ); } else if( pszSchemaOpt == NULL || EQUAL(pszSchemaOpt,"EXTERNAL") ) { char *pszBasename = CPLStrdup(CPLGetBasename( pszName )); VSIFPrintf( fpOutput, " xmlns:xsi=\"http://www.w3c.org/2001/XMLSchema-instance\"\n" " xsi:schemaLocation=\". %s\"\n", CPLResetExtension( pszBasename, "xsd" ) ); CPLFree( pszBasename ); } VSIFPrintf( fpOutput, "%s", " xmlns:ogr=\"http://ogr.maptools.org/\"\n" ); VSIFPrintf( fpOutput, "%s", " xmlns=\"http://ogr.maptools.org/\"\n" ); VSIFPrintf( fpOutput, "%s", " xmlns:gml=\"http://www.opengis.net/gml\">\n" );/* -------------------------------------------------------------------- *//* Should we initialize an area to place the boundedBy element? *//* We will need to seek back to fill it in. *//* -------------------------------------------------------------------- */ if( CSLFetchBoolean( papszOptions, "BOUNDEDBY", TRUE ) ) { nBoundedByLocation = VSIFTell( fpOutput ); if( nBoundedByLocation != -1 ) VSIFPrintf( fpOutput, "%280s\n", "" ); } else nBoundedByLocation = -1; return TRUE;}/************************************************************************//* CreateLayer() *//************************************************************************/OGRLayer *OGRGMLDataSource::CreateLayer( const char * pszLayerName, OGRSpatialReference *poSRS, OGRwkbGeometryType eType, char ** papszOptions ){/* -------------------------------------------------------------------- *//* Verify we are in update mode. *//* -------------------------------------------------------------------- */ if( fpOutput == NULL ) { CPLError( CE_Failure, CPLE_NoWriteAccess, "Data source %s opened for read access.\n" "New layer %s cannot be created.\n", pszName, pszLayerName ); return NULL; }/* -------------------------------------------------------------------- *//* Ensure name is safe as an element name. *//* -------------------------------------------------------------------- */ char *pszCleanLayerName = CPLStrdup( pszLayerName ); CPLCleanXMLElementName( pszCleanLayerName ); if( strcmp(pszCleanLayerName,pszLayerName) != 0 ) { CPLError( CE_Warning, CPLE_AppDefined, "Layer name '%s' adjusted to '%s' for XML validity.", pszLayerName, pszCleanLayerName ); }/* -------------------------------------------------------------------- *//* Create the layer object. *//* -------------------------------------------------------------------- */ OGRGMLLayer *poLayer; poLayer = new OGRGMLLayer( pszCleanLayerName, poSRS, TRUE, eType, this ); CPLFree( pszCleanLayerName );/* -------------------------------------------------------------------- *//* Add layer to data source layer list. *//* -------------------------------------------------------------------- */ papoLayers = (OGRGMLLayer **) CPLRealloc( papoLayers, sizeof(OGRGMLLayer *) * (nLayers+1) ); papoLayers[nLayers++] = poLayer; return poLayer;}/************************************************************************//* TestCapability() *//************************************************************************/int OGRGMLDataSource::TestCapability( const char * pszCap ){ if( EQUAL(pszCap,ODsCCreateLayer) ) return TRUE; else return FALSE;}/************************************************************************//* GetLayer() *//************************************************************************/OGRLayer *OGRGMLDataSource::GetLayer( int iLayer ){ if( iLayer < 0 || iLayer >= nLayers ) return NULL; else return papoLayers[iLayer];}/************************************************************************//* GrowExtents() *//************************************************************************/void OGRGMLDataSource::GrowExtents( OGREnvelope *psGeomBounds ){ sBoundingRect.Merge( *psGeomBounds );}/************************************************************************//* InsertHeader() *//* */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -