?? ogrpgdatasource.cpp
字號:
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','%s',%d,'%s',%d)", pszSchemaName, pszTableName, pszGFldName, 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 ); CPLFree( pszSchemaName ); OGRPGClearResult( hResult ); hResult = PQexec(hPGConn, "ROLLBACK"); OGRPGClearResult( hResult ); return NULL; } OGRPGClearResult( hResult ); }/* -------------------------------------------------------------------- *//* Complete, and commit the transaction. *//* -------------------------------------------------------------------- */ hResult = PQexec(hPGConn, "COMMIT"); OGRPGClearResult( hResult );/* -------------------------------------------------------------------- *//* Create the layer object. *//* -------------------------------------------------------------------- */ OGRPGTableLayer *poLayer; poLayer = new OGRPGTableLayer( this, pszTableName, pszSchemaName, TRUE, nSRSId ); poLayer->SetLaunderFlag( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) ); poLayer->SetPrecisionFlag( CSLFetchBoolean(papszOptions,"PRECISION",TRUE));/* -------------------------------------------------------------------- *//* Add layer to data source layer list. *//* -------------------------------------------------------------------- */ papoLayers = (OGRPGTableLayer **) CPLRealloc( papoLayers, sizeof(OGRPGTableLayer *) * (nLayers+1) ); papoLayers[nLayers++] = poLayer; CPLFree( pszLayerName ); CPLFree( pszSchemaName ); return poLayer;}/************************************************************************//* TestCapability() *//************************************************************************/int OGRPGDataSource::TestCapability( const char * pszCap ){ if( EQUAL(pszCap,ODsCCreateLayer) || EQUAL(pszCap,ODsCDeleteLayer) ) return TRUE; else return FALSE;}/************************************************************************//* GetLayer() *//************************************************************************/OGRLayer *OGRPGDataSource::GetLayer( int iLayer ){ if( iLayer < 0 || iLayer >= nLayers ) return NULL; else return papoLayers[iLayer];}/************************************************************************//* GetLayerByName() *//************************************************************************/OGRLayer *OGRPGDataSource::GetLayerByName( const char *pszName ){ if ( ! pszName ) return NULL; int i; int count = GetLayerCount(); /* first a case sensitive check */ for( i = 0; i < count; i++ ) { OGRPGTableLayer *poLayer = papoLayers[i]; if( strcmp( pszName, poLayer->GetLayerDefn()->GetName() ) == 0 ) return poLayer; } /* then case insensitive */ for( i = 0; i < count; i++ ) { OGRPGTableLayer *poLayer = papoLayers[i]; if( EQUAL( pszName, poLayer->GetLayerDefn()->GetName() ) ) return poLayer; } if( OpenTable( pszName, NULL, TRUE, FALSE ) ) return GetLayer(count); else return NULL;}/************************************************************************//* 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]; }/* -------------------------------------------------------------------- *//* Try looking up in spatial_ref_sys table. *//* -------------------------------------------------------------------- */ PGresult *hResult = NULL; 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; } } OGRPGClearResult( 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; nKnownSRID++; return poSRS;}/************************************************************************//* FetchSRSId() *//* *//* Fetch the id corresponding to an SRS, and if not found, add *//* it to the table. *//************************************************************************/int OGRPGDataSource::FetchSRSId( OGRSpatialReference * poSRS ){ PGresult *hResult = NULL; char szCommand[10000]; char *pszWKT = NULL; int nSRSId = -1; if( poSRS == NULL ) return -1;/* -------------------------------------------------------------------- *//* Translate SRS to WKT. *//* -------------------------------------------------------------------- */ if( poSRS->exportToWkt( &pszWKT ) != OGRERR_NONE ) return -1; CPLAssert( strlen(pszWKT) < sizeof(szCommand) - 500 );/* -------------------------------------------------------------------- *//* Try to find in the existing table. *//* -------------------------------------------------------------------- */ hResult = PQexec(hPGConn, "BEGIN"); OGRPGClearResult( hResult ); sprintf( szCommand, "SELECT srid FROM spatial_ref_sys WHERE srtext = '%s'", pszWKT ); hResult = PQexec(hPGConn, szCommand ); CPLFree( pszWKT ); // CM: Added to prevent mem leaks pszWKT = NULL; // CM: Added/* -------------------------------------------------------------------- *//* We got it! Return it. *//* -------------------------------------------------------------------- */ if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK && PQntuples(hResult) > 0 ) { // TODO: mloskot - replace with strtol() function. // atoi does not provide any level of diagnostics. // What about adding CPLAtoi() ? nSRSId = atoi(PQgetvalue( hResult, 0, 0 )); OGRPGClearResult( hResult ); hResult = PQexec(hPGConn, "COMMIT"); OGRPGClearResult( hResult ); return nSRSId; }/* -------------------------------------------------------------------- *//* If the command actually failed, then the metadata table is *//* likely missing. Try defining it. *//* -------------------------------------------------------------------- */ int bTableMissing; bTableMissing = hResult == NULL || PQresultStatus(hResult) == PGRES_NONFATAL_ERROR; hResult = PQexec(hPGConn, "COMMIT"); OGRPGClearResult( hResult ); if( bTableMissing ) { if( InitializeMetadataTables() != OGRERR_NONE ) return -1; }/* -------------------------------------------------------------------- *//* Get the current maximum srid in the srs table. *//* -------------------------------------------------------------------- */ hResult = PQexec(hPGConn, "BEGIN"); OGRPGClearResult( hResult ); hResult = PQexec(hPGConn, "SELECT MAX(srid) FROM spatial_ref_sys" ); if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK )
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -