ogrpgeolayer.cpp

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

CPP
713
字号
/****************************************************************************** * $Id: ogrpgeolayer.cpp 10646 2007-01-18 02:38:10Z warmerdam $ * * Project:  OpenGIS Simple Features Reference Implementation * Purpose:  Implements OGRPGeoLayer class, code shared between  *           the direct table access, and the generic SQL results. * Author:   Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com> * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. ****************************************************************************/#include "cpl_conv.h"#include "ogr_pgeo.h"#include "cpl_string.h"CPL_CVSID("$Id: ogrpgeolayer.cpp 10646 2007-01-18 02:38:10Z warmerdam $");/************************************************************************//*                            OGRPGeoLayer()                            *//************************************************************************/OGRPGeoLayer::OGRPGeoLayer(){    poDS = NULL;    pszGeomColumn = NULL;    pszFIDColumn = NULL;    poStmt = NULL;    iNextShapeId = 0;    poSRS = NULL;    nSRSId = -2; // we haven't even queried the database for it yet. }/************************************************************************//*                            ~OGRPGeoLayer()                             *//************************************************************************/OGRPGeoLayer::~OGRPGeoLayer(){    if( m_nFeaturesRead > 0 && poFeatureDefn != NULL )    {        CPLDebug( "PGeo", "%d features read on layer '%s'.",                  (int) m_nFeaturesRead,                   poFeatureDefn->GetName() );    }    if( poStmt != NULL )    {        delete poStmt;        poStmt = NULL;    }    if( poFeatureDefn != NULL )    {        poFeatureDefn->Release();        poFeatureDefn = NULL;    }    CPLFree( pszGeomColumn );    CPLFree( panFieldOrdinals );     CPLFree( pszFIDColumn );    if( poSRS != NULL )    {        poSRS->Release();        poSRS = NULL;    }}/************************************************************************//*                          BuildFeatureDefn()                          *//*                                                                      *//*      Build feature definition from a set of column definitions       *//*      set on a statement.  Sift out geometry and FID fields.          *//************************************************************************/CPLErr OGRPGeoLayer::BuildFeatureDefn( const char *pszLayerName,                                        CPLODBCStatement *poStmt ){    poFeatureDefn = new OGRFeatureDefn( pszLayerName );    int    nRawColumns = poStmt->GetColCount();    poFeatureDefn->Reference();    panFieldOrdinals = (int *) CPLMalloc( sizeof(int) * nRawColumns );    for( int iCol = 0; iCol < nRawColumns; iCol++ )    {        OGRFieldDefn    oField( poStmt->GetColName(iCol), OFTString );        oField.SetWidth( MAX(0,poStmt->GetColSize( iCol )) );        if( pszGeomColumn != NULL             && EQUAL(poStmt->GetColName(iCol),pszGeomColumn) )            continue;        if( pszFIDColumn == NULL             && EQUAL(poStmt->GetColName(iCol),"OBJECTID") )        {            pszFIDColumn = CPLStrdup(poStmt->GetColName(iCol));        }        if( pszGeomColumn == NULL             && EQUAL(poStmt->GetColName(iCol),"Shape") )        {            pszGeomColumn = CPLStrdup(poStmt->GetColName(iCol));            continue;        }                switch( poStmt->GetColType(iCol) )        {          case SQL_INTEGER:            oField.SetType( OFTInteger );            break;          case SQL_BINARY:          case SQL_VARBINARY:          case SQL_LONGVARBINARY:            oField.SetType( OFTBinary );            break;          case SQL_DECIMAL:            oField.SetType( OFTReal );            oField.SetPrecision( poStmt->GetColPrecision(iCol) );            break;          case SQL_FLOAT:          case SQL_REAL:          case SQL_DOUBLE:            oField.SetType( OFTReal );            oField.SetWidth( 0 );            break;          default:            /* leave it as OFTString */;        }        poFeatureDefn->AddFieldDefn( &oField );        panFieldOrdinals[poFeatureDefn->GetFieldCount() - 1] = iCol+1;    }    return CE_None;}/************************************************************************//*                            ResetReading()                            *//************************************************************************/void OGRPGeoLayer::ResetReading(){    iNextShapeId = 0;}/************************************************************************//*                           GetNextFeature()                           *//************************************************************************/OGRFeature *OGRPGeoLayer::GetNextFeature(){    for( ; TRUE; )    {        OGRFeature      *poFeature;        poFeature = GetNextRawFeature();        if( poFeature == NULL )            return NULL;        if( (m_poFilterGeom == NULL            || FilterGeometry( poFeature->GetGeometryRef() ) )            && (m_poAttrQuery == NULL                || m_poAttrQuery->Evaluate( poFeature )) )            return poFeature;        delete poFeature;    }}/************************************************************************//*                         GetNextRawFeature()                          *//************************************************************************/OGRFeature *OGRPGeoLayer::GetNextRawFeature(){    if( GetStatement() == NULL )        return NULL;/* -------------------------------------------------------------------- *//*      If we are marked to restart then do so, and fetch a record.     *//* -------------------------------------------------------------------- */    if( !poStmt->Fetch() )    {        delete poStmt;        poStmt = NULL;        return NULL;    }/* -------------------------------------------------------------------- *//*      Create a feature from the current result.                       *//* -------------------------------------------------------------------- */    int         iField;    OGRFeature *poFeature = new OGRFeature( poFeatureDefn );    if( pszFIDColumn != NULL && poStmt->GetColId(pszFIDColumn) > -1 )        poFeature->SetFID(             atoi(poStmt->GetColData(poStmt->GetColId(pszFIDColumn))) );    else        poFeature->SetFID( iNextShapeId );    iNextShapeId++;    m_nFeaturesRead++;/* -------------------------------------------------------------------- *//*      Set the fields.                                                 *//* -------------------------------------------------------------------- */    for( iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++ )    {        int iSrcField = panFieldOrdinals[iField]-1;        const char *pszValue = poStmt->GetColData( iSrcField );        if( pszValue == NULL )            /* no value */;        else if( poFeature->GetFieldDefnRef(iField)->GetType() == OFTBinary )            poFeature->SetField( iField,                                  poStmt->GetColDataLength(iSrcField),                                 (GByte *) pszValue );        else            poFeature->SetField( iField, pszValue );    }/* -------------------------------------------------------------------- *//*      Try to extract a geometry.                                      *//* -------------------------------------------------------------------- */    if( pszGeomColumn != NULL )    {        int iField = poStmt->GetColId( pszGeomColumn );        GByte *pabyShape = (GByte *) poStmt->GetColData( iField );        int nBytes = poStmt->GetColDataLength(iField);        OGRGeometry *poGeom = NULL;        if( pabyShape != NULL )            createFromShapeBin( pabyShape, &poGeom, nBytes );        if( poGeom != NULL )        {            poGeom->assignSpatialReference( poSRS );            poFeature->SetGeometryDirectly( poGeom );        }    }    return poFeature;}/************************************************************************//*                             GetFeature()                             *//************************************************************************/OGRFeature *OGRPGeoLayer::GetFeature( long nFeatureId ){    /* This should be implemented directly! */    return OGRLayer::GetFeature( nFeatureId );}/************************************************************************//*                           TestCapability()                           *//************************************************************************/int OGRPGeoLayer::TestCapability( const char * pszCap ){    if( EQUAL(pszCap,OLCRandomRead) )        return FALSE;    else if( EQUAL(pszCap,OLCFastFeatureCount) )        return FALSE;    else if( EQUAL(pszCap,OLCFastSpatialFilter) )        return FALSE;    else if( EQUAL(pszCap,OLCTransactions) )        return FALSE;    else         return FALSE;}/************************************************************************//*                           GetSpatialRef()                            *//************************************************************************/OGRSpatialReference *OGRPGeoLayer::GetSpatialRef(){    return poSRS;}/************************************************************************//*                           TestCapability()                           *//************************************************************************/void OGRPGeoLayer::LookupSRID( int nSRID ){/* -------------------------------------------------------------------- *//*      Fetch the corresponding WKT from the SpatialRef table.          *//* -------------------------------------------------------------------- */    CPLODBCStatement oStmt( poDS->GetSession() );            oStmt.Appendf( "SELECT srtext FROM GDB_SpatialRefs WHERE srid = %d",                  nSRID );    if( !oStmt.ExecuteSQL() )    {        CPLError( CE_Failure, CPLE_AppDefined,                  "'%s' failed.\n%s",                   oStmt.GetCommand(),                  poDS->GetSession()->GetLastError() );        return;    }    if( !oStmt.Fetch() )    {        CPLError( CE_Warning, CPLE_AppDefined,                  "SRID %d lookup failed.\n%s",                   nSRID, poDS->GetSession()->GetLastError() );        return;    }

⌨️ 快捷键说明

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