⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ogrpgtablelayer.cpp

📁 GIS系统支持库Geospatial Data Abstraction Library代码.GDAL is a translator library for raster geospatial dat
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    char *pszFields = BuildFields();    pszQueryStatement = (char *)         CPLMalloc(strlen(pszFields)+strlen(pszWHERE)                  +strlen(poFeatureDefn->GetName()) + 40);    sprintf( pszQueryStatement,             "SELECT %s FROM \"%s\" %s",              pszFields, poFeatureDefn->GetName(), pszWHERE );        CPLFree( pszFields );}/************************************************************************//*                            ResetReading()                            *//************************************************************************/void OGRPGTableLayer::ResetReading(){    BuildFullQueryStatement();    OGRPGLayer::ResetReading();}/************************************************************************//*                            BuildFields()                             *//*                                                                      *//*      Build list of fields to fetch, performing any required          *//*      transformations (such as on geometry).                          *//************************************************************************/char *OGRPGTableLayer::BuildFields(){    int         i, nSize;    char        *pszFieldList;    nSize = 25;    if( pszGeomColumn )        nSize += strlen(pszGeomColumn);    if( bHasFid )        nSize += strlen(pszFIDColumn);    for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ )        nSize += strlen(poFeatureDefn->GetFieldDefn(i)->GetNameRef()) + 4;    pszFieldList = (char *) CPLMalloc(nSize);    pszFieldList[0] = '\0';    if( bHasFid && poFeatureDefn->GetFieldIndex( pszFIDColumn ) == -1 )        sprintf( pszFieldList, "\"%s\"", pszFIDColumn );    if( pszGeomColumn )    {        if( strlen(pszFieldList) > 0 )            strcat( pszFieldList, ", " );        if( bHasPostGISGeometry )        {            sprintf( pszFieldList+strlen(pszFieldList),                      "AsText(\"%s\")", pszGeomColumn );        }        else        {            sprintf( pszFieldList+strlen(pszFieldList),                      "\"%s\"", pszGeomColumn );        }    }    for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ )    {        const char *pszName = poFeatureDefn->GetFieldDefn(i)->GetNameRef();        if( strlen(pszFieldList) > 0 )            strcat( pszFieldList, ", " );        strcat( pszFieldList, "\"" );        strcat( pszFieldList, pszName );        strcat( pszFieldList, "\"" );    }    CPLAssert( (int) strlen(pszFieldList) < nSize );    return pszFieldList;}/************************************************************************//*                         SetAttributeFilter()                         *//************************************************************************/OGRErr OGRPGTableLayer::SetAttributeFilter( const char *pszQuery ){    CPLFree( this->pszQuery );    if( pszQuery == NULL || strlen(pszQuery) == 0 )        this->pszQuery = NULL;    else        this->pszQuery = CPLStrdup( pszQuery );    BuildWhere();    ResetReading();    return OGRERR_NONE;}/************************************************************************//*                           DeleteFeature()                            *//************************************************************************/OGRErr OGRPGTableLayer::DeleteFeature( long nFID ){    PGconn              *hPGConn = poDS->GetPGConn();    PGresult            *hResult;    char                *pszCommand;/* -------------------------------------------------------------------- *//*      We can only delete features if we have a well defined FID       *//*      column to target.                                               *//* -------------------------------------------------------------------- */    if( !bHasFid )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "DeleteFeature(%d) failed.  Unable to delete features in tables without\n"                  "a recognised FID column.",                  nFID );        return OGRERR_FAILURE;    }/* -------------------------------------------------------------------- *//*      Form the statement to drop the record.                          *//* -------------------------------------------------------------------- */    pszCommand = (char *) CPLMalloc( strlen(pszFIDColumn)                                     + strlen(poFeatureDefn->GetName())                                     + 100 );    sprintf( pszCommand, "DELETE FROM \"%s\" WHERE \"%s\" = %ld",             poFeatureDefn->GetName(), pszFIDColumn, nFID );             /* -------------------------------------------------------------------- *//*      Execute the insert.                                             *//* -------------------------------------------------------------------- */    OGRErr eErr;    eErr = poDS->SoftStartTransaction();    if( eErr != OGRERR_NONE )        return eErr;    CPLDebug( "OGR_PG", "PQexec(%s)\n", pszCommand );    hResult = PQexec(hPGConn, pszCommand);    CPLFree( pszCommand );    if( PQresultStatus(hResult) != PGRES_COMMAND_OK )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "DeleteFeature() DELETE statement failed.\n%s",                   PQerrorMessage(hPGConn) );        PQclear( hResult );                poDS->SoftRollback();        return OGRERR_FAILURE;    }    return poDS->SoftCommit();}/************************************************************************//*                             SetFeature()                             *//*                                                                      *//*      SetFeature() is implemented by dropping the old copy of the     *//*      feature in question (if there is one) and then creating a       *//*      new one with the provided feature id.                           *//************************************************************************/OGRErr OGRPGTableLayer::SetFeature( OGRFeature *poFeature ){    OGRErr eErr;    if( poFeature->GetFID() == OGRNullFID )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "FID required on features given to SetFeature()." );        return OGRERR_FAILURE;    }    eErr = DeleteFeature( poFeature->GetFID() );    if( eErr != OGRERR_NONE )        return eErr;    return CreateFeature( poFeature );}/************************************************************************//*                           CreateFeature()                            *//************************************************************************/OGRErr OGRPGTableLayer::CreateFeature( OGRFeature *poFeature ){    PGconn              *hPGConn = poDS->GetPGConn();    PGresult            *hResult;    char                *pszCommand;    int                 i, bNeedComma = FALSE;    unsigned int        nCommandBufSize;;    OGRErr              eErr;    eErr = poDS->SoftStartTransaction();    if( eErr != OGRERR_NONE )        return eErr;    nCommandBufSize = 40000;    pszCommand = (char *) CPLMalloc(nCommandBufSize);/* -------------------------------------------------------------------- *//*      Form the INSERT command.                                        *//* -------------------------------------------------------------------- */    sprintf( pszCommand, "INSERT INTO \"%s\" (", poFeatureDefn->GetName() );    if( bHasWkb && poFeature->GetGeometryRef() != NULL )    {        strcat( pszCommand, "WKB_GEOMETRY " );        bNeedComma = TRUE;    }        if( bHasPostGISGeometry && poFeature->GetGeometryRef() != NULL )    {        strcat( pszCommand, pszGeomColumn );        strcat( pszCommand, " " );        bNeedComma = TRUE;    }    if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != NULL )    {        if( bNeedComma )            strcat( pszCommand, ", " );        strcat( pszCommand, pszFIDColumn );        strcat( pszCommand, " " );        bNeedComma = TRUE;    }    for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ )    {        if( !poFeature->IsFieldSet( i ) )            continue;        if( !bNeedComma )            bNeedComma = TRUE;        else            strcat( pszCommand, ", " );        sprintf( pszCommand + strlen(pszCommand), "\"%s\"",                 poFeatureDefn->GetFieldDefn(i)->GetNameRef() );    }    strcat( pszCommand, ") VALUES (" );    /* Set the geometry */    bNeedComma = poFeature->GetGeometryRef() != NULL;    if( bHasPostGISGeometry && poFeature->GetGeometryRef() != NULL)    {        char    *pszWKT = NULL;        // Do we need to force nSRSId to be set?        if( nSRSId == -2 )            GetSpatialRef();        if( poFeature->GetGeometryRef() != NULL )        {            OGRGeometry *poGeom = (OGRGeometry *) poFeature->GetGeometryRef();            poGeom->closeRings();            poGeom->exportToWkt( &pszWKT );        }                if( pszWKT != NULL             && strlen(pszCommand) + strlen(pszWKT) > nCommandBufSize - 10000 )        {            nCommandBufSize = strlen(pszCommand) + strlen(pszWKT) + 10000;            pszCommand = (char *) CPLRealloc(pszCommand, nCommandBufSize );        }        if( pszWKT != NULL )        {            sprintf( pszCommand + strlen(pszCommand),                      "GeometryFromText('%s'::TEXT,%d) ", pszWKT, nSRSId );            OGRFree( pszWKT );        }        else            strcat( pszCommand, "''" );    }    else if( bHasWkb && !bWkbAsOid && poFeature->GetGeometryRef() != NULL )    {        char    *pszBytea = GeometryToBYTEA( poFeature->GetGeometryRef() );        if( strlen(pszCommand) + strlen(pszBytea) > nCommandBufSize - 10000 )        {            nCommandBufSize = strlen(pszCommand) + strlen(pszBytea) + 10000;            pszCommand = (char *) CPLRealloc(pszCommand, nCommandBufSize );        }        if( pszBytea != NULL )        {            sprintf( pszCommand + strlen(pszCommand),                      "'%s'", pszBytea );            CPLFree( pszBytea );        }        else            strcat( pszCommand, "''" );    }    else if( bHasWkb && bWkbAsOid && poFeature->GetGeometryRef() != NULL )    {        Oid     oid = GeometryToOID( poFeature->GetGeometryRef() );        if( oid != 0 )        {            sprintf( pszCommand + strlen(pszCommand),                      "'%d' ", oid );        }        else            strcat( pszCommand, "''" );    }    /* Set the FID */    if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != NULL )    {        if( bNeedComma )            strcat( pszCommand, ", " );        sprintf( pszCommand + strlen(pszCommand), "%ld ", poFeature->GetFID());        bNeedComma = TRUE;    }    /* Set the other fields */    int nOffset = strlen(pszCommand);    for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ )    {        const char *pszStrValue = poFeature->GetFieldAsString(i);        char *pszNeedToFree = NULL;        if( !poFeature->IsFieldSet( i ) )            continue;        if( bNeedComma )            strcat( pszCommand+nOffset, ", " );        else            bNeedComma = TRUE;        // We need special formatting for integer list values.        if( poFeatureDefn->GetFieldDefn(i)->GetType() == OFTIntegerList )        {            int nCount, nOff = 0, j;            const int *panItems = poFeature->GetFieldAsIntegerList(i,&nCount);                        pszNeedToFree = (char *) CPLMalloc(nCount * 13 + 10);            strcpy( pszNeedToFree, "{" );            for( j = 0; j < nCount; j++ )            {                if( j != 0 )                    strcat( pszNeedToFree+nOff, "," );                nOff += strlen(pszNeedToFree+nOff);                sprintf( pszNeedToFree+nOff, "%d", panItems[j] );            }            strcat( pszNeedToFree+nOff, "}" );            pszStrValue = pszNeedToFree;        }        // We need special formatting for real list values.        if( poFeatureDefn->GetFieldDefn(i)->GetType() == OFTRealList )        {            int nCount, nOff = 0, j;            const double *padfItems =poFeature->GetFieldAsDoubleList(i,&nCount);                        pszNeedToFree = (char *) CPLMalloc(nCount * 40 + 10);            strcpy( pszNeedToFree, "{" );            for( j = 0; j < nCount; j++ )            {                if( j != 0 )                    strcat( pszNeedToFree+nOff, "," );                nOff += strlen(pszNeedToFree+nOff);                sprintf( pszNeedToFree+nOff, "%.16g", padfItems[j] );            }            strcat( pszNeedToFree+nOff, "}" );            pszStrValue = pszNeedToFree;        }

⌨️ 快捷键说明

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