ogrpgdatasource.cpp

来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 1,355 行 · 第 1/4 页

CPP
1,355
字号
    }/* -------------------------------------------------------------------- *//*      Parse the returned table list                                   *//* -------------------------------------------------------------------- */    char        **papszTableNames=NULL;    char        **papszSchemaNames=NULL;    int           iRecord;    for( iRecord = 0; iRecord < PQntuples(hResult); iRecord++ )    {        const char *pszTable = PQgetvalue(hResult, iRecord, 0);        if( EQUAL(pszTable,"spatial_ref_sys")            || EQUAL(pszTable,"geometry_columns") )            continue;        papszTableNames = CSLAddString(papszTableNames,                                       PQgetvalue(hResult, iRecord, 0));        papszSchemaNames = CSLAddString(papszSchemaNames,                                       PQgetvalue(hResult, iRecord, 1));    }/* -------------------------------------------------------------------- *//*      Cleanup                                                         *//* -------------------------------------------------------------------- */    OGRPGClearResult( hResult );    hResult = PQexec(hPGConn, "CLOSE mycursor");    OGRPGClearResult( hResult );    hResult = PQexec(hPGConn, "COMMIT");    OGRPGClearResult( hResult );/* -------------------------------------------------------------------- *//*      Register the available tables.                                  *//* -------------------------------------------------------------------- */    for( iRecord = 0;         papszTableNames != NULL && papszTableNames[iRecord] != NULL;         iRecord++ )    {        OpenTable( papszTableNames[iRecord], papszSchemaNames[iRecord], bUpdate, FALSE );    }    CSLDestroy( papszSchemaNames );    CSLDestroy( papszTableNames );/* -------------------------------------------------------------------- */    return nLayers > 0 || bUpdate;}/************************************************************************//*                             OpenTable()                              *//************************************************************************/int OGRPGDataSource::OpenTable( const char *pszNewName, const char *pszSchemaName, int bUpdate,                                int bTestOpen ){/* -------------------------------------------------------------------- *//*      Create the layer object.                                        *//* -------------------------------------------------------------------- */    OGRPGTableLayer  *poLayer;    poLayer = new OGRPGTableLayer( this, pszNewName, pszSchemaName, bUpdate );    if( poLayer->GetLayerDefn() == NULL )    {        delete poLayer;        return FALSE;    }/* -------------------------------------------------------------------- *//*      Add layer to data source layer list.                            *//* -------------------------------------------------------------------- */    papoLayers = (OGRPGTableLayer **)        CPLRealloc( papoLayers,  sizeof(OGRPGTableLayer *) * (nLayers+1) );    papoLayers[nLayers++] = poLayer;    return TRUE;}/************************************************************************//*                            DeleteLayer()                             *//************************************************************************/int OGRPGDataSource::DeleteLayer( int iLayer ){    if( iLayer < 0 || iLayer >= nLayers )        return OGRERR_FAILURE;/* -------------------------------------------------------------------- *//*      Blow away our OGR structures related to the layer.  This is     *//*      pretty dangerous if anything has a reference to this layer!     *//* -------------------------------------------------------------------- */    CPLString osLayerName = papoLayers[iLayer]->GetLayerDefn()->GetName();    CPLString osTableName = papoLayers[iLayer]->GetTableName();    CPLString osSchemaName = papoLayers[iLayer]->GetSchemaName();    CPLDebug( "OGR_PG", "DeleteLayer(%s)", osLayerName.c_str() );    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");    OGRPGClearResult( hResult );    if( bHavePostGIS )    {        sprintf( szCommand,                 "SELECT DropGeometryColumn('%s','%s',(SELECT f_geometry_column from geometry_columns where f_table_name='%s' and f_table_schema='%s' order by f_geometry_column limit 1))",                 osSchemaName.c_str(), osTableName.c_str(), osTableName.c_str(), osSchemaName.c_str() );        CPLDebug( "OGR_PG", "PGexec(%s)", szCommand );        hResult = PQexec( hPGConn, szCommand );        OGRPGClearResult( hResult );    }    sprintf( szCommand, "DROP TABLE %s.\"%s\" CASCADE", osSchemaName.c_str(), osTableName.c_str() );    CPLDebug( "OGR_PG", "PGexec(%s)", szCommand );    hResult = PQexec( hPGConn, szCommand );    OGRPGClearResult( hResult );    hResult = PQexec(hPGConn, "COMMIT");    OGRPGClearResult( hResult );    return OGRERR_NONE;}/************************************************************************//*                            CreateLayer()                             *//************************************************************************/OGRLayer *OGRPGDataSource::CreateLayer( const char * pszLayerNameIn,                              OGRSpatialReference *poSRS,                              OGRwkbGeometryType eType,                              char ** papszOptions ){    PGresult            *hResult;    char                szCommand[1024];    const char          *pszGeomType;    char                *pszLayerName;    const char          *pszTableName;    char                *pszSchemaName;    int                 nDimension = 3;    if( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) )        pszLayerName = LaunderName( pszLayerNameIn );    else        pszLayerName = CPLStrdup( pszLayerNameIn );    if( wkbFlatten(eType) == eType )        nDimension = 2;    /* Postgres Schema handling:       Extract schema name from input layer name or passed with -lco SCHEMA.       Set layer name to "schema.table" or to "table" if schema == current_schema()       Usage without schema name is backwards compatible    */    pszTableName = strstr(pszLayerNameIn,".");    if ( pszTableName != NULL )    {      int length = pszTableName-pszLayerNameIn;      pszSchemaName = (char*)CPLMalloc(length);      strncpy(pszSchemaName, pszLayerNameIn, length);      pszSchemaName[length] = '\0';      ++pszTableName; //skip "."    }    else    {      pszSchemaName = NULL;      pszTableName = pszLayerNameIn;    }/* -------------------------------------------------------------------- *//*      Set the default schema for the layers.                          *//* -------------------------------------------------------------------- */    if( CSLFetchNameValue( papszOptions, "SCHEMA" ) != NULL )    {        pszSchemaName = CPLStrdup(CSLFetchNameValue( papszOptions, "SCHEMA" ));    }    if ( pszSchemaName == NULL )    {      //pszSchemaName = current_schema()      hResult = PQexec(hPGConn,"SELECT current_schema()");      if ( hResult && PQntuples(hResult) == 1 && !PQgetisnull(hResult,0,0) )      {          pszSchemaName = CPLStrdup(PQgetvalue(hResult,0,0));      }      OGRPGClearResult( hResult );    }/* -------------------------------------------------------------------- *//*      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( iLayer );            }            else            {                CPLError( CE_Failure, CPLE_AppDefined,                          "Layer %s already exists, CreateLayer failed.\n"                          "Use the layer creation option OVERWRITE=YES to "                          "replace it.",                          pszLayerName );                CPLFree( pszLayerName );                CPLFree( pszSchemaName );                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 );        CPLFree( pszSchemaName );        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");    OGRPGClearResult( hResult );    if( !bHavePostGIS )    {        sprintf( szCommand,                 "CREATE TABLE %s.\"%s\" ( "                 "   OGC_FID SERIAL, "                 "   WKB_GEOMETRY %s, "                 "   CONSTRAINT \"%s_pk\" PRIMARY KEY (OGC_FID) )",                 pszSchemaName, pszTableName, pszGeomType, pszTableName );    }    else    {        sprintf( szCommand,                 "CREATE TABLE %s.\"%s\" ( OGC_FID SERIAL, CONSTRAINT \"%s_pk\" PRIMARY KEY (OGC_FID) )",                 pszSchemaName, pszTableName, pszTableName );    }    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 );        CPLFree( pszSchemaName );        OGRPGClearResult( hResult );        hResult = PQexec( hPGConn, "ROLLBACK" );        OGRPGClearResult( hResult );        return NULL;    }    OGRPGClearResult( 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;        const char *pszGFldName;         if( CSLFetchNameValue( papszOptions, "DIM") != NULL )            nDimension = atoi(CSLFetchNameValue( papszOptions, "DIM"));        if( CSLFetchNameValue( papszOptions, "GEOMETRY_NAME") != NULL )            pszGFldName = CSLFetchNameValue( papszOptions, "GEOMETRY_NAME");	else	    pszGFldName = "wkb_geometry";        /* 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' AND f_table_schema = '%s'",                 pszTableName, pszSchemaName );        CPLDebug( "OGR_PG", "PQexec(%s)", szCommand );        hResult = PQexec(hPGConn, szCommand);        OGRPGClearResult( hResult );        switch( wkbFlatten(eType) )        {            case wkbPoint:

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?