ogrpglayer.cpp

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

CPP
1,045
字号
            int nCount, i;            double *padfList;#if !defined(PG_PRE74)            if ( PQfformat( hCursorResult, iField ) == 1 ) // Binary data representation            {                char * pData = PQgetvalue( hCursorResult, iRecord, iField );                // goto number of array elements                pData += 3 * sizeof(int);                memcpy( &nCount, pData, sizeof(int) );                CPL_MSBPTR32( &nCount );                padfList = (double *) CPLCalloc(sizeof(double),nCount);                // goto first array element                pData += 2 * sizeof(int);                for( i = 0; i < nCount; i++ )                {                    // get element size                    int nSize = *(int *)(pData);                    CPL_MSBPTR32( &nSize );                    CPLAssert( nSize == sizeof(double) );                    pData += sizeof(int);                    memcpy( &padfList[i], pData, nSize );                    CPL_MSBPTR64(&padfList[i]);                    pData += nSize;                }            }            else#endif /* notdef PG_PRE74 */            {                char **papszTokens;                papszTokens = CSLTokenizeStringComplex(                    PQgetvalue( hCursorResult, iRecord, iField ),                    "{,}", FALSE, FALSE );                nCount = CSLCount(papszTokens);                padfList = (double *) CPLCalloc(sizeof(double),nCount);                for( i = 0; i < nCount; i++ )                    padfList[i] = atof(papszTokens[i]);                CSLDestroy( papszTokens );            }            poFeature->SetField( iOGRField, nCount, padfList );            CPLFree( padfList );        }        else if( eOGRType == OFTStringList )        {            char **papszTokens = 0;#if !defined(PG_PRE74)            if ( PQfformat( hCursorResult, iField ) == 1 ) // Binary data representation            {                char * pData = PQgetvalue( hCursorResult, iRecord, iField );                int nCount, i;                // goto number of array elements                pData += 3 * sizeof(int);                memcpy( &nCount, pData, sizeof(int) );                CPL_MSBPTR32( &nCount );                // goto first array element                pData += 2 * sizeof(int);                for( i = 0; i < nCount; i++ )                {                    // get element size                    int nSize = *(int *)(pData);                    CPL_MSBPTR32( &nSize );                    pData += sizeof(int);                    papszTokens = CSLAddString(papszTokens, pData);                    pData += nSize;                }            }            else#endif /* notdef PG_PRE74 */            {                papszTokens = CSLTokenizeStringComplex(                        PQgetvalue( hCursorResult, iRecord, iField ),                        "{,}", FALSE, FALSE );            }            if ( papszTokens )            {                poFeature->SetField( iOGRField, papszTokens );                CSLDestroy( papszTokens );            }        }        else if( eOGRType == OFTDate                  || eOGRType == OFTTime                  || eOGRType == OFTDateTime )        {#if !defined(PG_PRE74)            if ( PQfformat( hCursorResult, iField ) == 1 ) // Binary data            {                CPLDebug( "PG", "Binary DATE format not yet implemented." );            }            else#endif /* notdef PG_PRE74 */            {                OGRField  sFieldValue;                if( OGRParseDate( PQgetvalue( hCursorResult, iRecord, iField ),                                  &sFieldValue, 0 ) )                {                    poFeature->SetField( iOGRField, &sFieldValue );                }            }        }        else        {#if !defined(PG_PRE74)            if ( PQfformat( hCursorResult, iField ) == 1 &&                 eOGRType != OFTString ) // Binary data            {                if ( eOGRType == OFTInteger )                {                    int nVal;                    memcpy( &nVal, PQgetvalue( hCursorResult, iRecord, iField ), sizeof(int) );                    CPL_MSBPTR32(&nVal);                    poFeature->SetField( iOGRField, nVal );                }                else if ( eOGRType == OFTReal )                {                    double dfVal;                    memcpy( &dfVal, PQgetvalue( hCursorResult, iRecord, iField ), sizeof(double) );                    CPL_MSBPTR64(&dfVal);                    poFeature->SetField( iOGRField, dfVal );                }            }            else#endif /* notdef PG_PRE74 */                poFeature->SetField( iOGRField,                                     PQgetvalue( hCursorResult, iRecord, iField ) );        }    }    return poFeature;}/************************************************************************//*                         GetNextRawFeature()                          *//************************************************************************/OGRFeature *OGRPGLayer::GetNextRawFeature(){    PGconn      *hPGConn = poDS->GetPGConn();    CPLString   osCommand;/* -------------------------------------------------------------------- *//*      Do we need to establish an initial query?                       *//* -------------------------------------------------------------------- */    if( iNextShapeId == 0 && hCursorResult == NULL )    {        CPLAssert( pszQueryStatement != NULL );        poDS->FlushSoftTransaction();        poDS->SoftStartTransaction();        if ( poDS->bUseBinaryCursor )            osCommand.Printf( "DECLARE %s BINARY CURSOR for %s",                              pszCursorName, pszQueryStatement );        else            osCommand.Printf( "DECLARE %s CURSOR for %s",                              pszCursorName, pszQueryStatement );        CPLDebug( "OGR_PG", "PQexec(%s)", osCommand.c_str() );        hCursorResult = PQexec(hPGConn, osCommand );        OGRPGClearResult( hCursorResult );        osCommand.Printf( "FETCH %d in %s", CURSOR_PAGE, pszCursorName );        hCursorResult = PQexec(hPGConn, osCommand );        bCursorActive = TRUE;        nResultOffset = 0;    }/* -------------------------------------------------------------------- *//*      Are we in some sort of error condition?                         *//* -------------------------------------------------------------------- */    if( hCursorResult == NULL        || PQresultStatus(hCursorResult) != PGRES_TUPLES_OK )    {        CPLDebug( "OGR_PG", "PQclear() on an error condition");        OGRPGClearResult( hCursorResult );        iNextShapeId = MAX(1,iNextShapeId);        return NULL;    }/* -------------------------------------------------------------------- *//*      Do we need to fetch more records?                               *//* -------------------------------------------------------------------- */    if( nResultOffset >= PQntuples(hCursorResult)        && bCursorActive )    {        OGRPGClearResult( hCursorResult );                osCommand.Printf( "FETCH %d in %s", CURSOR_PAGE, pszCursorName );        hCursorResult = PQexec(hPGConn, osCommand );        nResultOffset = 0;    }/* -------------------------------------------------------------------- *//*      Are we out of results?  If so complete the transaction, and     *//*      cleanup, but don't reset the next shapeid.                      *//* -------------------------------------------------------------------- */    if( nResultOffset >= PQntuples(hCursorResult) )    {        OGRPGClearResult( hCursorResult );        if( bCursorActive )        {            osCommand.Printf( "CLOSE %s", pszCursorName );            hCursorResult = PQexec(hPGConn, osCommand);            OGRPGClearResult( hCursorResult );        }        poDS->FlushSoftTransaction();        hCursorResult = NULL;        bCursorActive = FALSE;        iNextShapeId = MAX(1,iNextShapeId);        return NULL;    }/* -------------------------------------------------------------------- *//*      Create a feature from the current result.                       *//* -------------------------------------------------------------------- */    OGRFeature *poFeature = RecordToFeature( nResultOffset );    nResultOffset++;    iNextShapeId++;    return poFeature;}/************************************************************************//*                             GetFeature()                             *//************************************************************************/OGRFeature *OGRPGLayer::GetFeature( long nFeatureId ){    /*     * TODO: This should be implemented!     * See related Bug 1445     * http://bugzilla.remotesensing.org/show_bug.cgi?id=1445     */    return NULL;}/************************************************************************//*                           HEXToGeometry()                            *//************************************************************************/OGRGeometry *OGRPGLayer::HEXToGeometry( const char *pszBytea ){    GByte   *pabyWKB = NULL;    int     iSrc=0;    int     iDst=0;    OGRGeometry *poGeometry = NULL;    unsigned int ewkbFlags = 0;    if( pszBytea == NULL )        return NULL;/* -------------------------------------------------------------------- *//*      Convert hex to binary.                                          *//* -------------------------------------------------------------------- */    pabyWKB = (GByte *) CPLMalloc(strlen(pszBytea)+1);    while( pszBytea[iSrc] != '\0' )    {        if( pszBytea[iSrc] >= '0' && pszBytea[iSrc] <= '9' )            pabyWKB[iDst] = pszBytea[iSrc] - '0';        else if( pszBytea[iSrc] >= 'A' && pszBytea[iSrc] <= 'F' )            pabyWKB[iDst] = pszBytea[iSrc] - 'A' + 10;        else if( pszBytea[iSrc] >= 'a' && pszBytea[iSrc] <= 'f' )            pabyWKB[iDst] = pszBytea[iSrc] - 'a' + 10;        else            pabyWKB[iDst] = 0;        pabyWKB[iDst] *= 16;        iSrc++;        if( pszBytea[iSrc] >= '0' && pszBytea[iSrc] <= '9' )            pabyWKB[iDst] += pszBytea[iSrc] - '0';        else if( pszBytea[iSrc] >= 'A' && pszBytea[iSrc] <= 'F' )            pabyWKB[iDst] += pszBytea[iSrc] - 'A' + 10;        else if( pszBytea[iSrc] >= 'a' && pszBytea[iSrc] <= 'f' )            pabyWKB[iDst] += pszBytea[iSrc] - 'a' + 10;        else            pabyWKB[iDst] += 0;        iSrc++;        iDst++;    }/* -------------------------------------------------------------------- *//*      Detect XYZM variant of PostGIS EWKB                             *//*                                                                      *//*      OGR does not support parsing M coordinate,                      *//*      so we return NULL geometry.                                     *//* -------------------------------------------------------------------- */    memcpy(&ewkbFlags, pabyWKB+1, 4);    OGRwkbByteOrder eByteOrder = (pabyWKB[0] == 0 ? wkbXDR : wkbNDR);    if( OGR_SWAP( eByteOrder ) )        ewkbFlags= CPL_SWAP32(ewkbFlags);    if (ewkbFlags & 0x40000000)    {        CPLError( CE_Failure, CPLE_AppDefined,            "Reading EWKB with 4-dimensional coordinates (XYZM) is not supported" );        CPLFree( pabyWKB );        return NULL;    }/* -------------------------------------------------------------------- *//*      PostGIS EWKB format includes an  SRID, but this won't be        *//*      understood by OGR, so if the SRID flag is set, we remove the    *//*      SRID (bytes at offset 5 to 8).                                  *//* -------------------------------------------------------------------- */    if( (pabyWKB[0] == 0 /* big endian */ && (pabyWKB[1] & 0x20) )        || (pabyWKB[0] != 0 /* little endian */ && (pabyWKB[4] & 0x20)) )

⌨️ 快捷键说明

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