📄 ogrpgdatasource.cpp
字号:
/* DeleteLayer() *//************************************************************************/void OGRPGDataSource::DeleteLayer( const char *pszLayerName ){ int iLayer;/* -------------------------------------------------------------------- *//* Try to find layer. *//* -------------------------------------------------------------------- */ for( iLayer = 0; iLayer < nLayers; iLayer++ ) { if( EQUAL(pszLayerName,papoLayers[iLayer]->GetLayerDefn()->GetName()) ) break; } if( iLayer == nLayers ) { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to delete layer '%s', but this layer is not known to OGR.", pszLayerName ); return; }/* -------------------------------------------------------------------- *//* Blow away our OGR structures related to the layer. This is *//* pretty dangerous if anything has a reference to this layer! *//* -------------------------------------------------------------------- */ CPLDebug( "OGR_PG", "DeleteLayer(%s)", pszLayerName ); delete papoLayers[iLayer]; memmove( papoLayers + iLayer, papoLayers + iLayer + 1, sizeof(void *) * (nLayers - iLayer - 1) ); nLayers--;/* -------------------------------------------------------------------- *//* Remove from the database. *//* -------------------------------------------------------------------- */ PGresult *hResult; char szCommand[1024]; hResult = PQexec(hPGConn, "BEGIN"); PQclear( hResult ); if( bHavePostGIS ) { sprintf( szCommand, "SELECT DropGeometryColumn('%s','%s','wkb_geometry')", pszDBName, pszLayerName ); CPLDebug( "OGR_PG", "PGexec(%s)", szCommand ); hResult = PQexec( hPGConn, szCommand ); PQclear( hResult ); } sprintf( szCommand, "DROP TABLE %s", pszLayerName ); CPLDebug( "OGR_PG", "PGexec(%s)", szCommand ); hResult = PQexec( hPGConn, szCommand ); PQclear( hResult ); hResult = PQexec(hPGConn, "COMMIT"); PQclear( hResult ); hResult = PQexec(hPGConn, "BEGIN"); PQclear( hResult ); sprintf( szCommand, "DROP SEQUENCE %s_ogc_fid_seq", pszLayerName ); CPLDebug( "OGR_PG", "PGexec(%s)", szCommand ); hResult = PQexec( hPGConn, szCommand ); PQclear( hResult ); hResult = PQexec(hPGConn, "COMMIT"); PQclear( hResult );}/************************************************************************//* CreateLayer() *//************************************************************************/OGRLayer *OGRPGDataSource::CreateLayer( const char * pszLayerNameIn, OGRSpatialReference *poSRS, OGRwkbGeometryType eType, char ** papszOptions ){ PGresult *hResult; char szCommand[1024]; const char *pszGeomType; char *pszLayerName; int nDimension = 3; if( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) ) pszLayerName = LaunderName( pszLayerNameIn ); else pszLayerName = CPLStrdup( pszLayerNameIn ); if( wkbFlatten(eType) == eType ) nDimension = 2;/* -------------------------------------------------------------------- *//* Do we already have this layer? If so, should we blow it *//* away? *//* -------------------------------------------------------------------- */ int iLayer; for( iLayer = 0; iLayer < nLayers; iLayer++ ) { if( EQUAL(pszLayerName,papoLayers[iLayer]->GetLayerDefn()->GetName()) ) { if( CSLFetchNameValue( papszOptions, "OVERWRITE" ) != NULL && !EQUAL(CSLFetchNameValue(papszOptions,"OVERWRITE"),"NO") ) { DeleteLayer( pszLayerName ); } else { CPLFree( pszLayerName ); CPLError( CE_Failure, CPLE_AppDefined, "Layer %s already exists, CreateLayer failed.\n" "Use the layer creation option OVERWRITE=YES to " "replace it.", pszLayerName ); return NULL; } } }/* -------------------------------------------------------------------- *//* Handle the GEOM_TYPE option. *//* -------------------------------------------------------------------- */ pszGeomType = CSLFetchNameValue( papszOptions, "GEOM_TYPE" ); if( pszGeomType == NULL ) { if( bHavePostGIS ) pszGeomType = "geometry"; else pszGeomType = "bytea"; } if( bHavePostGIS && !EQUAL(pszGeomType,"geometry") ) { CPLError( CE_Failure, CPLE_AppDefined, "Can't override GEOM_TYPE in PostGIS enabled databases.\n" "Creation of layer %s with GEOM_TYPE %s has failed.", pszLayerName, pszGeomType ); CPLFree( pszLayerName ); return NULL; }/* -------------------------------------------------------------------- *//* Try to get the SRS Id of this spatial reference system, *//* adding tot the srs table if needed. *//* -------------------------------------------------------------------- */ int nSRSId = -1; if( poSRS != NULL ) nSRSId = FetchSRSId( poSRS );/* -------------------------------------------------------------------- *//* Create a basic table with the FID. Also include the *//* geometry if this is not a PostGIS enabled table. *//* -------------------------------------------------------------------- */ hResult = PQexec(hPGConn, "BEGIN"); PQclear( hResult ); if( !bHavePostGIS ) sprintf( szCommand, "CREATE TABLE \"%s\" ( " " OGC_FID SERIAL, " " WKB_GEOMETRY %s )", pszLayerName, pszGeomType ); else sprintf( szCommand, "CREATE TABLE \"%s\" ( OGC_FID SERIAL )", pszLayerName ); CPLDebug( "OGR_PG", "PQexec(%s)", szCommand ); hResult = PQexec(hPGConn, szCommand); if( PQresultStatus(hResult) != PGRES_COMMAND_OK ) { CPLError( CE_Failure, CPLE_AppDefined, "%s\n%s", szCommand, PQerrorMessage(hPGConn) ); CPLFree( pszLayerName ); PQclear( hResult ); hResult = PQexec( hPGConn, "ROLLBACK" ); PQclear( hResult ); return NULL; } PQclear( hResult );/* -------------------------------------------------------------------- *//* Eventually we should be adding this table to a table of *//* "geometric layers", capturing the WKT projection, and *//* perhaps some other housekeeping. *//* -------------------------------------------------------------------- */ if( bHavePostGIS ) { const char *pszGeometryType; if( CSLFetchNameValue( papszOptions, "DIM") != NULL ) nDimension = atoi(CSLFetchNameValue( papszOptions, "DIM")); /* Sometimes there is an old cruft entry in the geometry_columns * table if things were not properly cleaned up before. We make * an effort to clean out such cruft. */ sprintf( szCommand, "DELETE FROM geometry_columns WHERE f_table_name = '%s'", pszLayerName ); CPLDebug( "OGR_PG", "PQexec(%s)", szCommand ); hResult = PQexec(hPGConn, szCommand); PQclear( hResult ); switch( wkbFlatten(eType) ) { case wkbPoint: pszGeometryType = "POINT"; break; case wkbLineString: pszGeometryType = "LINESTRING"; break; case wkbPolygon: pszGeometryType = "POLYGON"; break; case wkbMultiPoint: pszGeometryType = "MULTIPOINT"; break; case wkbMultiLineString: pszGeometryType = "MULTILINESTRING"; break; case wkbMultiPolygon: pszGeometryType = "MULTIPOLYGON"; break; case wkbGeometryCollection: pszGeometryType = "GEOMETRYCOLLECTION"; break; default: pszGeometryType = "GEOMETRY"; break; } sprintf( szCommand, "select AddGeometryColumn('%s','%s','wkb_geometry',%d,'%s',%d)", pszDBName, pszLayerName, nSRSId, pszGeometryType, nDimension ); CPLDebug( "OGR_PG", "PQexec(%s)", szCommand ); hResult = PQexec(hPGConn, szCommand); if( !hResult || PQresultStatus(hResult) != PGRES_TUPLES_OK ) { CPLError( CE_Failure, CPLE_AppDefined, "AddGeometryColumn failed for layer %s, layer creation has failed.", pszLayerName ); CPLFree( pszLayerName ); PQclear( hResult ); hResult = PQexec(hPGConn, "ROLLBACK"); PQclear( hResult ); return NULL; } }/* -------------------------------------------------------------------- *//* Complete, and commit the transaction. *//* -------------------------------------------------------------------- */ hResult = PQexec(hPGConn, "COMMIT"); PQclear( hResult );/* -------------------------------------------------------------------- *//* Create the layer object. *//* -------------------------------------------------------------------- */ OGRPGTableLayer *poLayer; poLayer = new OGRPGTableLayer( this, pszLayerName, TRUE, nSRSId ); poLayer->SetLaunderFlag( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) ); poLayer->SetPrecisionFlag( CSLFetchBoolean(papszOptions,"PRECISION",TRUE));/* -------------------------------------------------------------------- *//* Add layer to data source layer list. *//* -------------------------------------------------------------------- */ papoLayers = (OGRPGLayer **) CPLRealloc( papoLayers, sizeof(OGRPGLayer *) * (nLayers+1) ); papoLayers[nLayers++] = poLayer; CPLFree( pszLayerName ); return poLayer;}/************************************************************************//* TestCapability() *//************************************************************************/int OGRPGDataSource::TestCapability( const char * pszCap ){ if( EQUAL(pszCap,ODsCCreateLayer) ) return TRUE; else return FALSE;}/************************************************************************//* GetLayer() *//************************************************************************/OGRLayer *OGRPGDataSource::GetLayer( int iLayer ){ if( iLayer < 0 || iLayer >= nLayers ) return NULL; else return papoLayers[iLayer];}/************************************************************************//* OGRPGNoticeProcessor() *//************************************************************************/static void OGRPGNoticeProcessor( void *arg, const char * pszMessage ){ CPLDebug( "OGR_PG_NOTICE", "%s", pszMessage );}/************************************************************************//* InitializeMetadataTables() *//* *//* Create the metadata tables (SPATIAL_REF_SYS and *//* GEOMETRY_COLUMNS). *//************************************************************************/OGRErr OGRPGDataSource::InitializeMetadataTables(){ // implement later. return OGRERR_FAILURE;}/************************************************************************//* FetchSRS() *//* *//* Return a SRS corresponding to a particular id. Note that *//* reference counting should be honoured on the returned *//* OGRSpatialReference, as handles may be cached. *//************************************************************************/OGRSpatialReference *OGRPGDataSource::FetchSRS( int nId ){ if( nId < 0 ) return NULL;/* -------------------------------------------------------------------- *//* First, we look through our SRID cache, is it there? *//* -------------------------------------------------------------------- */ int i; for( i = 0; i < nKnownSRID; i++ ) { if( panSRID[i] == nId ) return papoSRS[i]; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -