ogrpglayer.cpp

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

CPP
1,045
字号
    {        memmove( pabyWKB+5, pabyWKB+9, iDst-9 );        iDst -= 4;        if( pabyWKB[0] == 0 )            pabyWKB[1] &= (~0x20);        else            pabyWKB[4] &= (~0x20);    }/* -------------------------------------------------------------------- *//*      Try to ingest the geometry.                                     *//* -------------------------------------------------------------------- */    poGeometry = NULL;    OGRGeometryFactory::createFromWkb( pabyWKB, NULL, &poGeometry, iDst );    CPLFree( pabyWKB );    return poGeometry;}/************************************************************************//*                           GeometryToHex()                            *//************************************************************************/char *OGRPGLayer::GeometryToHex( OGRGeometry * poGeometry, int nSRSId ){    GByte       *pabyWKB;    char        *pszTextBuf;    char        *pszTextBufCurrent;    char        *pszHex;    int nWkbSize = poGeometry->WkbSize();    pabyWKB = (GByte *) CPLMalloc(nWkbSize);    if( poGeometry->exportToWkb( wkbNDR, pabyWKB ) != OGRERR_NONE )    {        CPLFree( pabyWKB );        return CPLStrdup("");    }    /* When converting to hex, each byte takes 2 hex characters.  In addition       we add in 8 characters to represent the SRID integer in hex, and       one for a null terminator */    int pszSize = nWkbSize*2 + 8 + 1;    pszTextBuf = (char *) CPLMalloc(pszSize);    pszTextBufCurrent = pszTextBuf;    /* Convert the 1st byte, which is the endianess flag, to hex. */    pszHex = CPLBinaryToHex( 1, pabyWKB );    sprintf(pszTextBufCurrent, pszHex );    CPLFree ( pszHex );    pszTextBufCurrent += 2;    /* Next, get the geom type which is bytes 2 through 5 */    GUInt32 geomType;    memcpy( &geomType, pabyWKB+1, 4 );    /* Now add the SRID flag if an SRID is provided */    if (nSRSId != -1)    {        /* Change the flag to wkbNDR (little) endianess */        GUInt32 nGSrsFlag = CPL_LSBWORD32( WKBSRIDFLAG );        /* Apply the flag */        geomType = geomType | nGSrsFlag;    }    /* Now write the geom type which is 4 bytes */    pszHex = CPLBinaryToHex( 4, (GByte*) &geomType );    sprintf(pszTextBufCurrent, pszHex );    CPLFree ( pszHex );    pszTextBufCurrent += 8;    /* Now include SRID if provided */    if (nSRSId != -1)    {        /* Force the srsid to wkbNDR (little) endianess */        GUInt32 nGSRSId = CPL_LSBWORD32( nSRSId );        pszHex = CPLBinaryToHex( sizeof(nGSRSId),(GByte*) &nGSRSId );        sprintf(pszTextBufCurrent, pszHex );        CPLFree ( pszHex );        pszTextBufCurrent += 8;    }    /* Copy the rest of the data over - subtract       5 since we already copied 5 bytes above */    pszHex = CPLBinaryToHex( nWkbSize - 5, pabyWKB + 5 );    sprintf(pszTextBufCurrent, pszHex );    CPLFree ( pszHex );    CPLFree( pabyWKB );    return pszTextBuf;}/************************************************************************//*                          BYTEAToGeometry()                           *//************************************************************************/OGRGeometry *OGRPGLayer::BYTEAToGeometry( const char *pszBytea ){    GByte       *pabyWKB;    int iSrc=0, iDst=0;    OGRGeometry *poGeometry;    if( pszBytea == NULL )        return NULL;    pabyWKB = (GByte *) CPLMalloc(strlen(pszBytea));    while( pszBytea[iSrc] != '\0' )    {        if( pszBytea[iSrc] == '\\' )        {            if( pszBytea[iSrc+1] >= '0' && pszBytea[iSrc+1] <= '9' )            {                pabyWKB[iDst++] =                    (pszBytea[iSrc+1] - 48) * 64                    + (pszBytea[iSrc+2] - 48) * 8                    + (pszBytea[iSrc+3] - 48) * 1;                iSrc += 4;            }            else            {                pabyWKB[iDst++] = pszBytea[iSrc+1];                iSrc += 2;            }        }        else        {            pabyWKB[iDst++] = pszBytea[iSrc++];        }    }    poGeometry = NULL;    OGRGeometryFactory::createFromWkb( pabyWKB, NULL, &poGeometry, iDst );    CPLFree( pabyWKB );    return poGeometry;}/************************************************************************//*                          GeometryToBYTEA()                           *//************************************************************************/char *OGRPGLayer::GeometryToBYTEA( OGRGeometry * poGeometry ){    int         nWkbSize = poGeometry->WkbSize();    GByte       *pabyWKB;    char        *pszTextBuf, *pszRetBuf;    pabyWKB = (GByte *) CPLMalloc(nWkbSize);    if( poGeometry->exportToWkb( wkbNDR, pabyWKB ) != OGRERR_NONE )        return CPLStrdup("");    pszTextBuf = (char *) CPLMalloc(nWkbSize*5+1);    int  iSrc, iDst=0;    for( iSrc = 0; iSrc < nWkbSize; iSrc++ )    {        if( pabyWKB[iSrc] < 40 || pabyWKB[iSrc] > 126            || pabyWKB[iSrc] == '\\' )        {            sprintf( pszTextBuf+iDst, "\\\\%03o", pabyWKB[iSrc] );            iDst += 5;        }        else            pszTextBuf[iDst++] = pabyWKB[iSrc];    }    pszTextBuf[iDst] = '\0';    pszRetBuf = CPLStrdup( pszTextBuf );    CPLFree( pszTextBuf );    return pszRetBuf;}/************************************************************************//*                          OIDToGeometry()                             *//************************************************************************/OGRGeometry *OGRPGLayer::OIDToGeometry( Oid oid ){    PGconn      *hPGConn = poDS->GetPGConn();    GByte       *pabyWKB;    int         fd, nBytes;    OGRGeometry *poGeometry;#define MAX_WKB 500000    if( oid == 0 )        return NULL;    fd = lo_open( hPGConn, oid, INV_READ );    if( fd < 0 )        return NULL;    pabyWKB = (GByte *) CPLMalloc(MAX_WKB);    nBytes = lo_read( hPGConn, fd, (char *) pabyWKB, MAX_WKB );    lo_close( hPGConn, fd );    poGeometry = NULL;    OGRGeometryFactory::createFromWkb( pabyWKB, NULL, &poGeometry, nBytes );    CPLFree( pabyWKB );    return poGeometry;}/************************************************************************//*                           GeometryToOID()                            *//************************************************************************/Oid OGRPGLayer::GeometryToOID( OGRGeometry * poGeometry ){    PGconn      *hPGConn = poDS->GetPGConn();    int         nWkbSize = poGeometry->WkbSize();    GByte       *pabyWKB;    Oid         oid;    int         fd, nBytesWritten;    pabyWKB = (GByte *) CPLMalloc(nWkbSize);    if( poGeometry->exportToWkb( wkbNDR, pabyWKB ) != OGRERR_NONE )        return 0;    oid = lo_creat( hPGConn, INV_READ|INV_WRITE );    fd = lo_open( hPGConn, oid, INV_WRITE );    nBytesWritten = lo_write( hPGConn, fd, (char *) pabyWKB, nWkbSize );    lo_close( hPGConn, fd );    if( nBytesWritten != nWkbSize )    {        CPLDebug( "OGR_PG",                  "Only wrote %d bytes of %d intended for (fd=%d,oid=%d).\n",                  nBytesWritten, nWkbSize, fd, oid );    }    CPLFree( pabyWKB );    return oid;}/************************************************************************//*                           TestCapability()                           *//************************************************************************/int OGRPGLayer::TestCapability( const char * pszCap ){    if( EQUAL(pszCap,OLCRandomRead) )        return FALSE;    else if( EQUAL(pszCap,OLCFastFeatureCount) )        return m_poFilterGeom == NULL || bHasPostGISGeometry;    else if( EQUAL(pszCap,OLCFastSpatialFilter) )        return TRUE;    else if( EQUAL(pszCap,OLCTransactions) )        return TRUE;	else if( EQUAL(pszCap,OLCFastGetExtent) )		return bHasPostGISGeometry;    else        return FALSE;}/************************************************************************//*                          StartTransaction()                          *//************************************************************************/OGRErr OGRPGLayer::StartTransaction(){    return poDS->SoftStartTransaction();}/************************************************************************//*                         CommitTransaction()                          *//************************************************************************/OGRErr OGRPGLayer::CommitTransaction(){    return poDS->SoftCommit();}/************************************************************************//*                        RollbackTransaction()                         *//************************************************************************/OGRErr OGRPGLayer::RollbackTransaction(){    return poDS->SoftRollback();}/************************************************************************//*                           GetSpatialRef()                            *//************************************************************************/OGRSpatialReference *OGRPGLayer::GetSpatialRef(){    if( poSRS == NULL && nSRSId > -1 )    {        poSRS = poDS->FetchSRS( nSRSId );        if( poSRS != NULL )            poSRS->Reference();        else            nSRSId = -1;    }    return poSRS;}/************************************************************************//*                            GetFIDColumn()                            *//************************************************************************/const char *OGRPGLayer::GetFIDColumn() {    if( pszFIDColumn != NULL )        return pszFIDColumn;    else        return "";}/************************************************************************//*                         GetGeometryColumn()                          *//************************************************************************/const char *OGRPGLayer::GetGeometryColumn() {    if( pszGeomColumn != NULL )        return pszGeomColumn;    else        return "";}

⌨️ 快捷键说明

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