ogrmysqldatasource.cpp
来自「GIS系统支持库Geospatial Data Abstraction Libr」· C++ 代码 · 共 1,114 行 · 第 1/3 页
CPP
1,114 行
"SELECT DropGeometryColumn('%s','%s','wkb_geometry')", pszDBName, pszLayerName ); CPLDebug( "OGR_MYSQL", "PGexec(%s)", szCommand ); hResult = PQexec( hPGConn, szCommand ); PQclear( hResult ); } sprintf( szCommand, "DROP TABLE %s", pszLayerName ); CPLDebug( "OGR_MYSQL", "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_MYSQL", "PGexec(%s)", szCommand ); hResult = PQexec( hPGConn, szCommand ); PQclear( hResult ); hResult = PQexec(hPGConn, "COMMIT"); PQclear( hResult );}/************************************************************************//* CreateLayer() *//************************************************************************/OGRLayer *OGRMySQLDataSource::CreateLayer( const char * pszLayerNameIn, OGRSpatialReference *poSRS, OGRwkbGeometryType eType, char ** papszOptions ){ PGresult *hResult; char szCommand[1024]; const char *pszGeomType; char *pszLayerName; if( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) ) pszLayerName = LaunderName( pszLayerNameIn ); else pszLayerName = CPLStrdup( pszLayerNameIn );/* -------------------------------------------------------------------- *//* 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_MYSQL", "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; /* 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_MYSQL", "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, 3 ); CPLDebug( "OGR_MYSQL", "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. *//* -------------------------------------------------------------------- */ OGRMySQLTableLayer *poLayer; poLayer = new OGRMySQLTableLayer( this, pszLayerName, TRUE, nSRSId ); poLayer->SetLaunderFlag( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) ); poLayer->SetPrecisionFlag( CSLFetchBoolean(papszOptions,"PRECISION",TRUE));/* -------------------------------------------------------------------- *//* Add layer to data source layer list. *//* -------------------------------------------------------------------- */ papoLayers = (OGRMySQLLayer **) CPLRealloc( papoLayers, sizeof(OGRMySQLLayer *) * (nLayers+1) ); papoLayers[nLayers++] = poLayer; CPLFree( pszLayerName ); return poLayer;}#endif/************************************************************************//* TestCapability() *//************************************************************************/int OGRMySQLDataSource::TestCapability( const char * pszCap ){#ifdef notdef if( EQUAL(pszCap,ODsCCreateLayer) ) return TRUE; else#endif return FALSE;}/************************************************************************//* GetLayer() *//************************************************************************/OGRLayer *OGRMySQLDataSource::GetLayer( int iLayer ){ if( iLayer < 0 || iLayer >= nLayers ) return NULL; else return papoLayers[iLayer];}#ifdef notdef/************************************************************************//* InitializeMetadataTables() *//* *//* Create the metadata tables (SPATIAL_REF_SYS and *//* GEOMETRY_COLUMNS). *//************************************************************************/OGRErr OGRMySQLDataSource::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 *OGRMySQLDataSource::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]; }/* -------------------------------------------------------------------- *//* Try looking up in spatial_ref_sys table. *//* -------------------------------------------------------------------- */ PGresult *hResult; char szCommand[1024]; OGRSpatialReference *poSRS = NULL; SoftStartTransaction(); sprintf( szCommand, "SELECT srtext FROM spatial_ref_sys " "WHERE srid = %d", nId ); hResult = PQexec(hPGConn, szCommand ); if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK && PQntuples(hResult) == 1 ) { char *pszWKT; pszWKT = PQgetvalue(hResult,0,0); poSRS = new OGRSpatialReference(); if( poSRS->importFromWkt( &pszWKT ) != OGRERR_NONE ) { delete poSRS; poSRS = NULL; } } PQclear( hResult ); SoftCommit();/* -------------------------------------------------------------------- *//* Add to the cache. *//* -------------------------------------------------------------------- */ panSRID = (int *) CPLRealloc(panSRID,sizeof(int) * (nKnownSRID+1) ); papoSRS = (OGRSpatialReference **) CPLRealloc(papoSRS, sizeof(void*) * (nKnownSRID + 1) ); panSRID[nKnownSRID] = nId; papoSRS[nKnownSRID] = poSRS; return poSRS;}/************************************************************************//* FetchSRSId() *//* */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?