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

📄 ogrshapelayer.cpp

📁 GIS系统支持库Geospatial Data Abstraction Library代码.GDAL is a translator library for raster geospatial dat
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************** * $Id: ogrshapelayer.cpp,v 1.19 2005/02/22 12:51:56 fwarmerdam Exp $ * * Project:  OpenGIS Simple Features Reference Implementation * Purpose:  Implements OGRShapeLayer class. * Author:   Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 1999,  Les Technologies SoftMap Inc. * * 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. ****************************************************************************** * * $Log: ogrshapelayer.cpp,v $ * Revision 1.19  2005/02/22 12:51:56  fwarmerdam * use OGRLayer base spatial filter support * * Revision 1.18  2005/02/02 20:01:02  fwarmerdam * added SetNextByIndex support * * Revision 1.17  2005/01/04 03:43:41  fwarmerdam * added support for creating and destroying spatial indexes * * Revision 1.16  2005/01/03 22:26:21  fwarmerdam * updated to use spatial indexing * * Revision 1.15  2003/05/21 04:03:54  warmerda * expand tabs * * Revision 1.14  2003/04/21 19:03:20  warmerda * added SyncToDisk support * * Revision 1.13  2003/04/15 20:45:54  warmerda * Added code to update the feature count in CreateFeature(). * * Revision 1.12  2003/03/05 05:09:41  warmerda * initialize panMatchingFIDs * * Revision 1.11  2003/03/04 05:49:05  warmerda * added attribute indexing support * * Revision 1.10  2002/10/09 14:13:40  warmerda * ensure width is at least 1 * * Revision 1.9  2002/04/05 19:59:37  warmerda * Output file was getting different geometries in some cases. * * Revision 1.8  2002/03/27 21:04:38  warmerda * Added support for reading, and creating lone .dbf files for wkbNone geometry * layers.  Added support for creating a single .shp file instead of a directory * if a path ending in .shp is passed to the data source create method. * * Revision 1.7  2001/09/04 15:35:14  warmerda * add support for deferring geometry type selection till first feature * * Revision 1.6  2001/08/21 21:44:27  warmerda * fixed count logic if attribute query in play * * Revision 1.5  2001/07/18 04:55:16  warmerda * added CPL_CSVID * * Revision 1.4  2001/06/19 15:50:23  warmerda * added feature attribute query support * * Revision 1.3  2001/03/16 22:16:10  warmerda * added support for ESRI .prj files * * Revision 1.2  2001/03/15 04:21:50  danmo * Added GetExtent() * * Revision 1.1  1999/11/04 21:16:11  warmerda * New * */#include "ogrshape.h"#include "cpl_conv.h"CPL_CVSID("$Id: ogrshapelayer.cpp,v 1.19 2005/02/22 12:51:56 fwarmerdam Exp $");/************************************************************************//*                           OGRShapeLayer()                            *//************************************************************************/OGRShapeLayer::OGRShapeLayer( const char * pszName,                              SHPHandle hSHPIn, DBFHandle hDBFIn,                               OGRSpatialReference *poSRSIn, int bUpdate,                              OGRwkbGeometryType eReqType ){    poSRS = poSRSIn;    pszFullName = CPLStrdup(pszName);        hSHP = hSHPIn;    hDBF = hDBFIn;    bUpdateAccess = bUpdate;    iNextShapeId = 0;    panMatchingFIDs = NULL;    bCheckedForQIX = FALSE;    fpQIX = NULL;    bHeaderDirty = FALSE;    if( hSHP != NULL )        nTotalShapeCount = hSHP->nRecords;    else         nTotalShapeCount = hDBF->nRecords;        poFeatureDefn = SHPReadOGRFeatureDefn( CPLGetBasename(pszName),                                            hSHP, hDBF );    eRequestedGeomType = eReqType;}/************************************************************************//*                           ~OGRShapeLayer()                           *//************************************************************************/OGRShapeLayer::~OGRShapeLayer(){    if( m_nFeaturesRead > 0 && poFeatureDefn != NULL )    {        CPLDebug( "Shape", "%d features read on layer '%s'.",                  (int) m_nFeaturesRead,                   poFeatureDefn->GetName() );    }    CPLFree( panMatchingFIDs );    panMatchingFIDs = NULL;    CPLFree( pszFullName );    delete poFeatureDefn;    if( poSRS != NULL )        delete poSRS;    if( hDBF != NULL )        DBFClose( hDBF );    if( hSHP != NULL )        SHPClose( hSHP );    if( fpQIX != NULL )        VSIFClose( fpQIX );}/************************************************************************//*                            CheckForQIX()                             *//************************************************************************/int OGRShapeLayer::CheckForQIX(){    const char *pszQIXFilename;    if( bCheckedForQIX )        return fpQIX != NULL;    pszQIXFilename = CPLResetExtension( pszFullName, "qix" );    fpQIX = VSIFOpen( pszQIXFilename, "rb" );    bCheckedForQIX = TRUE;    return fpQIX != NULL;}/************************************************************************//*                            ScanIndices()                             *//*                                                                      *//*      Utilize optional spatial and attribute indices if they are      *//*      available.                                                      *//************************************************************************/int OGRShapeLayer::ScanIndices(){    iMatchingFID = 0;/* -------------------------------------------------------------------- *//*      Utilize attribute index if appropriate.                         *//* -------------------------------------------------------------------- */    if( m_poAttrQuery != NULL )    {        CPLAssert( panMatchingFIDs == NULL );        panMatchingFIDs = m_poAttrQuery->EvaluateAgainstIndices( this,                                                                 NULL );    }/* -------------------------------------------------------------------- *//*      Check for spatial index if we have a spatial query.             *//* -------------------------------------------------------------------- */    if( m_poFilterGeom != NULL && !bCheckedForQIX )        CheckForQIX();/* -------------------------------------------------------------------- *//*      Utilize spatial index if appropriate.                           *//* -------------------------------------------------------------------- */    if( m_poFilterGeom && fpQIX )    {        int nSpatialFIDCount, *panSpatialFIDs;        double adfBoundsMin[4], adfBoundsMax[4];        OGREnvelope oEnvelope;        m_poFilterGeom->getEnvelope( &oEnvelope );        adfBoundsMin[0] = oEnvelope.MinX;        adfBoundsMin[1] = oEnvelope.MinY;        adfBoundsMin[2] = 0.0;        adfBoundsMin[3] = 0.0;        adfBoundsMax[0] = oEnvelope.MaxX;        adfBoundsMax[1] = oEnvelope.MaxY;        adfBoundsMax[2] = 0.0;        adfBoundsMax[3] = 0.0;        panSpatialFIDs = SHPSearchDiskTree( fpQIX,                                             adfBoundsMin, adfBoundsMax,                                             &nSpatialFIDCount );        CPLDebug( "SHAPE", "Used spatial index, got %d matches.",                   nSpatialFIDCount );        // Use resulting list as matching FID list (but reallocate and        // terminate with OGRNullFID).        if( panMatchingFIDs == NULL )        {            int i;            panMatchingFIDs = (long *)                 CPLMalloc(sizeof(long) * (nSpatialFIDCount+1) );            for( i = 0; i < nSpatialFIDCount; i++ )                panMatchingFIDs[i] = (long) panSpatialFIDs[i];            panMatchingFIDs[nSpatialFIDCount] = OGRNullFID;            free( panSpatialFIDs );        }        // Cull attribute index matches based on those in the spatial index        // result set.  We assume that the attribute results are in sorted        // order.        else        {            int iRead, iWrite=0, iSpatial=0;            for( iRead = 0; panMatchingFIDs[iRead] != OGRNullFID; iRead++ )            {                while( iSpatial < nSpatialFIDCount                       && panSpatialFIDs[iSpatial] < panMatchingFIDs[iRead] )                    iSpatial++;                if( iSpatial == nSpatialFIDCount )                    continue;                if( panSpatialFIDs[iSpatial] == panMatchingFIDs[iRead] )                    panMatchingFIDs[iWrite++] = panMatchingFIDs[iRead];            }            panMatchingFIDs[iWrite] = OGRNullFID;        }    }    return TRUE;}/************************************************************************//*                            ResetReading()                            *//************************************************************************/void OGRShapeLayer::ResetReading(){/* -------------------------------------------------------------------- *//*      Clear previous index search result, if any.                     *//* -------------------------------------------------------------------- */    CPLFree( panMatchingFIDs );    panMatchingFIDs = NULL;    iMatchingFID = 0;    iNextShapeId = 0;    if( bHeaderDirty )        SyncToDisk();}/************************************************************************//*                           SetNextByIndex()                           *//*                                                                      *//*      If we already have an FID list, we can easily resposition       *//*      ourselves in it.                                                *//************************************************************************/OGRErr OGRShapeLayer::SetNextByIndex( long nIndex ){    // Eventually we should try to use panMatchingFIDs list     // if available and appropriate.     if( m_poFilterGeom != NULL || m_poAttrQuery != NULL )        return OGRLayer::SetNextByIndex( nIndex );    iNextShapeId = nIndex;    return OGRERR_NONE;}/************************************************************************//*                           GetNextFeature()                           *//************************************************************************/OGRFeature *OGRShapeLayer::GetNextFeature(){    OGRFeature  *poFeature;/* -------------------------------------------------------------------- *//*      Collect a matching list if we have attribute or spatial         *//*      indices.  Only do this on the first request for a given pass    *//*      of course.                                                      *//* -------------------------------------------------------------------- */    if( (m_poAttrQuery != NULL || m_poFilterGeom != NULL)        && iNextShapeId == 0 && panMatchingFIDs == NULL )    {        ScanIndices();    }    /* -------------------------------------------------------------------- *//*      Loop till we find a feature matching our criteria.              *//* -------------------------------------------------------------------- */    while( TRUE )    {        if( panMatchingFIDs != NULL )        {            if( panMatchingFIDs[iMatchingFID] == OGRNullFID )                return NULL;            poFeature = SHPReadOGRFeature( hSHP, hDBF, poFeatureDefn,                                            panMatchingFIDs[iMatchingFID++] );            if( poFeature != NULL )                m_nFeaturesRead++;        }        else        {            if( iNextShapeId >= nTotalShapeCount )                return NULL;                poFeature = SHPReadOGRFeature( hSHP, hDBF, poFeatureDefn,                                           iNextShapeId++ );            if( poFeature != NULL )                m_nFeaturesRead++;        }        if( (m_poFilterGeom == NULL             || FilterGeometry( poFeature->GetGeometryRef() ) )            && (m_poAttrQuery == NULL                || m_poAttrQuery->Evaluate( poFeature )) )            return poFeature;        delete poFeature;    }        }/************************************************************************//*                             GetFeature()                             *//************************************************************************/OGRFeature *OGRShapeLayer::GetFeature( long nFeatureId ){    OGRFeature *poFeature =          SHPReadOGRFeature( hSHP, hDBF, poFeatureDefn, nFeatureId );        if( poFeature != NULL )        m_nFeaturesRead++;    return poFeature;}/************************************************************************//*                             SetFeature()                             *//************************************************************************/OGRErr OGRShapeLayer::SetFeature( OGRFeature *poFeature ){    return SHPWriteOGRFeature( hSHP, hDBF, poFeatureDefn, poFeature );}/************************************************************************//*                           CreateFeature()                            *//************************************************************************/OGRErr OGRShapeLayer::CreateFeature( OGRFeature *poFeature ){    OGRErr eErr;    bHeaderDirty = TRUE;    poFeature->SetFID( OGRNullFID );    if( nTotalShapeCount == 0 

⌨️ 快捷键说明

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