?? ogrshapedatasource.cpp
字號:
/****************************************************************************** * $Id: ogrshapedatasource.cpp 11223 2007-04-08 14:32:58Z mloskot $ * * Project: OpenGIS Simple Features Reference Implementation * Purpose: Implements OGRShapeDataSource 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. ****************************************************************************/#include "ogrshape.h"#include "cpl_conv.h"#include "cpl_string.h"CPL_CVSID("$Id: ogrshapedatasource.cpp 11223 2007-04-08 14:32:58Z mloskot $");/************************************************************************//* OGRShapeDataSource() *//************************************************************************/OGRShapeDataSource::OGRShapeDataSource(){ pszName = NULL; papoLayers = NULL; nLayers = 0; bSingleNewFile = FALSE;}/************************************************************************//* ~OGRShapeDataSource() *//************************************************************************/OGRShapeDataSource::~OGRShapeDataSource(){ CPLFree( pszName ); for( int i = 0; i < nLayers; i++ ) delete papoLayers[i]; CPLFree( papoLayers );}/************************************************************************//* Open() *//************************************************************************/int OGRShapeDataSource::Open( const char * pszNewName, int bUpdate, int bTestOpen, int bSingleNewFileIn ){ VSIStatBuf stat; CPLAssert( nLayers == 0 ); pszName = CPLStrdup( pszNewName ); bDSUpdate = bUpdate; bSingleNewFile = bSingleNewFileIn;/* -------------------------------------------------------------------- *//* If bSingleNewFile is TRUE we don't try to do anything else. *//* This is only utilized when the OGRShapeDriver::Create() *//* method wants to create a stub OGRShapeDataSource for a *//* single shapefile. The driver will take care of creating the *//* file by calling CreateLayer(). *//* -------------------------------------------------------------------- */ if( bSingleNewFile ) return TRUE; /* -------------------------------------------------------------------- *//* Is the given path a directory or a regular file? *//* -------------------------------------------------------------------- */ if( CPLStat( pszNewName, &stat ) != 0 || (!VSI_ISDIR(stat.st_mode) && !VSI_ISREG(stat.st_mode)) ) { if( !bTestOpen ) CPLError( CE_Failure, CPLE_AppDefined, "%s is neither a file or directory, Shape access failed.\n", pszNewName ); return FALSE; } /* -------------------------------------------------------------------- *//* Build a list of filenames we figure are Shape files. *//* -------------------------------------------------------------------- */ if( VSI_ISREG(stat.st_mode) ) { if( !OpenFile( pszNewName, bUpdate, bTestOpen ) ) { if( !bTestOpen ) CPLError( CE_Failure, CPLE_OpenFailed, "Failed to open shapefile %s.\n" "It may be corrupt.\n", pszNewName ); return FALSE; } return TRUE; } else { char **papszCandidates = CPLReadDir( pszNewName ); int iCan, nCandidateCount = CSLCount( papszCandidates ); int bMightBeOldCoverage = FALSE; for( iCan = 0; iCan < nCandidateCount; iCan++ ) { char *pszFilename; const char *pszCandidate = papszCandidates[iCan]; if( EQUAL(pszCandidate,"ARC") ) bMightBeOldCoverage = TRUE; if( strlen(pszCandidate) < 4 || !EQUAL(pszCandidate+strlen(pszCandidate)-4,".shp") ) continue; pszFilename = CPLStrdup(CPLFormFilename(pszNewName, pszCandidate, NULL)); if( !OpenFile( pszFilename, bUpdate, bTestOpen ) && !bTestOpen ) { CPLError( CE_Failure, CPLE_OpenFailed, "Failed to open shapefile %s.\n" "It may be corrupt.\n", pszFilename ); CPLFree( pszFilename ); return FALSE; } CPLFree( pszFilename ); } // Try and .dbf files without apparent associated shapefiles. for( iCan = 0; iCan < nCandidateCount; iCan++ ) { char *pszFilename; const char *pszCandidate = papszCandidates[iCan]; const char *pszLayerName; int iLayer, bGotAlready = FALSE; // We don't consume .dbf files in a directory that looks like // an old style Arc/Info (for PC?) that unless we found at least // some shapefiles. See Bug 493. if( bMightBeOldCoverage && nLayers == 0 ) continue; if( strlen(pszCandidate) < 4 || !EQUAL(pszCandidate+strlen(pszCandidate)-4,".dbf") ) continue; pszLayerName = CPLGetBasename(pszCandidate); for( iLayer = 0; iLayer < nLayers; iLayer++ ) { if( EQUAL(pszLayerName, GetLayer(iLayer)->GetLayerDefn()->GetName()) ) bGotAlready = TRUE; } if( bGotAlready ) continue; // We don't want to access .dbf files with an associated .tab // file, or it will never get recognised as a mapinfo dataset. int iCan2, bFoundTAB = FALSE; for( iCan2 = 0; iCan2 < nCandidateCount; iCan2++ ) { const char *pszCandidate2 = papszCandidates[iCan2]; if( EQUALN(pszCandidate2,pszLayerName,strlen(pszLayerName)) && EQUAL(pszCandidate2 + strlen(pszLayerName), ".tab") ) bFoundTAB = TRUE; } if( bFoundTAB ) continue; pszFilename = CPLStrdup(CPLFormFilename(pszNewName, pszCandidate, NULL)); if( !OpenFile( pszFilename, bUpdate, bTestOpen ) && !bTestOpen ) { CPLError( CE_Failure, CPLE_OpenFailed, "Failed to open dbf file %s.\n" "It may be corrupt.\n", pszFilename ); CPLFree( pszFilename ); return FALSE; } CPLFree( pszFilename ); } CSLDestroy( papszCandidates ); if( !bTestOpen && nLayers == 0 && !bUpdate ) { CPLError( CE_Failure, CPLE_OpenFailed, "No Shapefiles found in directory %s\n", pszNewName ); } } return nLayers > 0 || bUpdate;}/************************************************************************//* OpenFile() *//************************************************************************/int OGRShapeDataSource::OpenFile( const char *pszNewName, int bUpdate, int bTestOpen ){ SHPHandle hSHP; DBFHandle hDBF; const char *pszExtension = CPLGetExtension( pszNewName ); (void) bTestOpen; if( !EQUAL(pszExtension,"shp") && !EQUAL(pszExtension,"shx") && !EQUAL(pszExtension,"dbf") ) return FALSE;/* -------------------------------------------------------------------- *//* SHPOpen() should include better (CPL based) error reporting, *//* and we should be trying to distinquish at this point whether *//* failure is a result of trying to open a non-shapefile, or *//* whether it was a shapefile and we want to report the error *//* up. *//* *//* Care is taken to suppress the error and only reissue it if *//* we think it is appropriate. *//* -------------------------------------------------------------------- */ CPLPushErrorHandler( CPLQuietErrorHandler ); if( bUpdate ) hSHP = SHPOpen( pszNewName, "r+" ); else hSHP = SHPOpen( pszNewName, "r" ); CPLPopErrorHandler(); if( hSHP == NULL && (!EQUAL(CPLGetExtension(pszNewName),"dbf") || strstr(CPLGetLastErrorMsg(),".shp") == NULL) ) { CPLString osMsg = CPLGetLastErrorMsg(); CPLError( CE_Failure, CPLE_OpenFailed, "%s", osMsg.c_str() ); return FALSE; } CPLErrorReset(); /* -------------------------------------------------------------------- *//* Open the .dbf file, if it exists. To open a dbf file, the *//* filename has to either refer to a successfully opened shp *//* file or has to refer to the actual .dbf file. *//* -------------------------------------------------------------------- */ if( hSHP != NULL || EQUAL(CPLGetExtension(pszNewName),"dbf") ) { if( bUpdate ) hDBF = DBFOpen( pszNewName, "r+" ); else hDBF = DBFOpen( pszNewName, "r" ); } else hDBF = NULL; if( hDBF == NULL && hSHP == NULL ) return FALSE;/* -------------------------------------------------------------------- *//* Is there an associated .prj file we can read? *//* -------------------------------------------------------------------- */ OGRSpatialReference *poSRS = NULL; const char *pszPrjFile = CPLResetExtension( pszNewName, "prj" ); FILE *fp = NULL; fp = VSIFOpen( pszPrjFile, "r" );#ifndef WIN32 if( NULL == fp ) { pszPrjFile = CPLResetExtension( pszNewName, "PRJ" ); fp = VSIFOpen( pszPrjFile, "r" ); }#endif if( fp != NULL ) { char **papszLines; VSIFClose( fp ); papszLines = CSLLoad( pszPrjFile ); poSRS = new OGRSpatialReference(); if( poSRS->importFromESRI( papszLines ) != OGRERR_NONE ) { delete poSRS; poSRS = NULL; } CSLDestroy( papszLines ); }/* -------------------------------------------------------------------- *//* Create the layer object. *//* -------------------------------------------------------------------- */ OGRShapeLayer *poLayer; poLayer = new OGRShapeLayer( pszNewName, hSHP, hDBF, poSRS, bUpdate, wkbNone ); poLayer->InitializeIndexSupport( pszNewName );/* -------------------------------------------------------------------- *//* Add layer to data source layer list. *//* -------------------------------------------------------------------- */ papoLayers = (OGRShapeLayer **) CPLRealloc( papoLayers, sizeof(OGRShapeLayer *) * (nLayers+1) ); papoLayers[nLayers++] = poLayer; return TRUE;}/************************************************************************//* CreateLayer() *//************************************************************************/OGRLayer *OGRShapeDataSource::CreateLayer( const char * pszLayerName, OGRSpatialReference *poSRS, OGRwkbGeometryType eType, char ** papszOptions ){ SHPHandle hSHP; DBFHandle hDBF; int nShapeType;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -