📄 ogrvrtlayer.cpp
字号:
/****************************************************************************** * $Id: ogrvrtlayer.cpp,v 1.10 2005/02/22 12:50:10 fwarmerdam Exp $ * * Project: OpenGIS Simple Features Reference Implementation * Purpose: Implements OGRVRTLayer class. * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 2003, 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. ****************************************************************************** * * $Log: ogrvrtlayer.cpp,v $ * Revision 1.10 2005/02/22 12:50:10 fwarmerdam * use OGRLayer base spatial filter support * * Revision 1.9 2005/02/02 20:54:27 fwarmerdam * track m_nFeaturesRead * * Revision 1.8 2004/10/30 04:54:54 fwarmerdam * Improved geometry check error message. * * Revision 1.7 2004/10/30 04:44:00 fwarmerdam * Fixed error report when fetching layer. * * Revision 1.6 2004/10/16 21:56:36 fwarmerdam * Fixed initialization of "z". * * Revision 1.5 2004/03/25 13:23:41 warmerda * Fixed typo in error message. * * Revision 1.4 2003/12/30 18:34:57 warmerda * Added support for SrcSQL instead of SrcLayer. * * Revision 1.3 2003/11/10 20:11:55 warmerda * Allow any UserInput in LayerSYS * * Revision 1.2 2003/11/07 21:55:12 warmerda * complete fid support, relative dsname, fixes * * Revision 1.1 2003/11/07 17:50:36 warmerda * New * */#include "cpl_conv.h"#include "ogr_vrt.h"#include "cpl_string.h"CPL_CVSID("$Id: ogrvrtlayer.cpp,v 1.10 2005/02/22 12:50:10 fwarmerdam Exp $");typedef struct { OGRwkbGeometryType eType; const char *pszName;} OGRGeomTypeName;static OGRGeomTypeName asGeomTypeNames[] = { /* 25D versions are implicit */ { wkbUnknown, "wkbUnknown" }, { wkbPoint, "wkbPoint" }, { wkbLineString, "wkbLineString" }, { wkbPolygon, "wkbPolygon" }, { wkbMultiPoint, "wkbMultiPoint" }, { wkbMultiLineString, "wkbLineString" }, { wkbMultiPolygon, "wkbPolygon" }, { wkbGeometryCollection, "wkbGeometryCollection" }, { wkbNone, "wkbNone" }, { wkbLinearRing, "wkbLinearRing" }, { wkbNone, NULL }};/************************************************************************//* OGRVRTLayer() *//************************************************************************/OGRVRTLayer::OGRVRTLayer(){ poFeatureDefn = NULL; pszQuery = NULL; poSrcLayer = NULL; poSRS = NULL; iFIDField = -1; eGeometryType = VGS_Direct; iGeomField = iGeomXField = iGeomYField = iGeomZField = -1; panSrcField = NULL; pabDirectCopy = NULL; bNeedReset = TRUE;}/************************************************************************//* ~OGRVRTLayer() *//************************************************************************/OGRVRTLayer::~OGRVRTLayer(){ if( m_nFeaturesRead > 0 && poFeatureDefn != NULL ) { CPLDebug( "VRT", "%d features read on layer '%s'.", (int) m_nFeaturesRead, poFeatureDefn->GetName() ); } if( poSRS != NULL ) poSRS->Dereference(); if( poSrcDS != NULL ) { OGRSFDriverRegistrar::GetRegistrar()->ReleaseDataSource( poSrcDS ); } delete poFeatureDefn; CPLFree( panSrcField ); CPLFree( pabDirectCopy );}/************************************************************************//* Initialize() *//************************************************************************/int OGRVRTLayer::Initialize( CPLXMLNode *psLTree, const char *pszVRTDirectory ){ if( !EQUAL(psLTree->pszValue,"OGRVRTLayer") ) return FALSE;/* -------------------------------------------------------------------- *//* Get layer name. *//* -------------------------------------------------------------------- */ const char *pszLayerName = CPLGetXMLValue( psLTree, "name", NULL ); if( pszLayerName == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Missing name attribute on OGRVRTLayer" ); return FALSE; } poFeatureDefn = new OGRFeatureDefn( pszLayerName );/* -------------------------------------------------------------------- *//* Figure out the data source name. It may be treated relative *//* to vrt filename, but normally it is used directly. *//* -------------------------------------------------------------------- */ OGRSFDriverRegistrar *poReg = OGRSFDriverRegistrar::GetRegistrar(); char *pszSrcDSName = (char *) CPLGetXMLValue(psLTree,"SrcDataSource",NULL); if( pszSrcDSName == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Missing SrcDataSource for layer %s.", pszLayerName ); return FALSE; } if( atoi(CPLGetXMLValue( psLTree, "SrcDataSource.relativetoVRT", "0")) ) { pszSrcDSName = CPLStrdup( CPLProjectRelativeFilename( pszVRTDirectory, pszSrcDSName ) ); } else { pszSrcDSName = CPLStrdup(pszSrcDSName); }/* -------------------------------------------------------------------- *//* Try to access the datasource. *//* -------------------------------------------------------------------- */ CPLErrorReset(); poSrcDS = poReg->OpenShared( pszSrcDSName, FALSE, NULL ); if( poSrcDS == NULL ) { if( strlen(CPLGetLastErrorMsg()) == 0 ) CPLError( CE_Failure, CPLE_AppDefined, "Failed to open datasource `%s'.", pszSrcDSName ); CPLFree( pszSrcDSName ); return FALSE; }/* -------------------------------------------------------------------- *//* Is this layer derived from an SQL query result? *//* -------------------------------------------------------------------- */ const char *pszSQL = CPLGetXMLValue( psLTree, "SrcSQL", NULL ); if( pszSQL != NULL ) { poSrcLayer = poSrcDS->ExecuteSQL( pszSQL, NULL, NULL ); if( poSrcLayer == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "SQL statement failed, or returned no layer result:\n%s", pszSQL ); return FALSE; } }/* -------------------------------------------------------------------- *//* Fetch the layer if it is a regular layer. *//* -------------------------------------------------------------------- */ if( poSrcLayer == NULL ) { const char *pszSrcLayerName = CPLGetXMLValue( psLTree, "SrcLayer", pszLayerName ); poSrcLayer = poSrcDS->GetLayerByName( pszSrcLayerName ); if( poSrcLayer == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to find layer '%s' on datasource '%s'.", pszSrcLayerName, pszSrcDSName ); CPLFree( pszSrcDSName ); return FALSE; } } CPLFree( pszSrcDSName );/* -------------------------------------------------------------------- *//* Do we have a fixed geometry type? If so use it, otherwise *//* derive from the source layer. *//* -------------------------------------------------------------------- */ const char *pszGType = CPLGetXMLValue( psLTree, "GeometryType", NULL ); if( pszGType != NULL ) { int iType; for( iType = 0; asGeomTypeNames[iType].pszName != NULL; iType++ ) { if( EQUALN(pszGType, asGeomTypeNames[iType].pszName, strlen(asGeomTypeNames[iType].pszName)) ) { poFeatureDefn->SetGeomType( asGeomTypeNames[iType].eType ); if( strstr(pszGType,"25D") != NULL ) poFeatureDefn->SetGeomType( (OGRwkbGeometryType) (poFeatureDefn->GetGeomType() | wkb25DBit) ); break; } } if( asGeomTypeNames[iType].pszName == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "GeometryType %s not recognised.", pszGType ); return FALSE; } } else { poFeatureDefn->SetGeomType(poSrcLayer->GetLayerDefn()->GetGeomType()); } /* -------------------------------------------------------------------- *//* For now we copy the schema directly from the source layer. *//* -------------------------------------------------------------------- */ int iField; OGRFeatureDefn *poSrcDefn = poSrcLayer->GetLayerDefn(); panSrcField = (int *) CPLMalloc(sizeof(int) * poSrcDefn->GetFieldCount()); pabDirectCopy = (int *) CPLMalloc(sizeof(int)*poSrcDefn->GetFieldCount()); for( iField = 0; iField < poSrcDefn->GetFieldCount(); iField++ ) { poFeatureDefn->AddFieldDefn( poSrcDefn->GetFieldDefn( iField ) ); panSrcField[iField] = iField; pabDirectCopy[iField] = TRUE; } /* -------------------------------------------------------------------- *//* Apply a spatial reference system if provided, otherwise copy *//* from source. *//* -------------------------------------------------------------------- */ const char *pszLayerSRS = CPLGetXMLValue( psLTree, "LayerSRS", NULL ); if( pszLayerSRS != NULL ) { if( EQUAL(pszLayerSRS,"NULL") ) poSRS = NULL; else { OGRSpatialReference oSRS; if( oSRS.SetFromUserInput( pszLayerSRS ) != OGRERR_NONE ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to import LayerSRS `%s'.", pszLayerSRS ); return FALSE; } poSRS = oSRS.Clone(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -