📄 gmlreader.cpp
字号:
/****************************************************************************** * $Id: gmlreader.cpp,v 1.19 2004/11/17 17:17:31 fwarmerdam Exp $ * * Project: GML Reader * Purpose: Implementation of GMLReader class. * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 2002, Frank Warmerdam * * 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: gmlreader.cpp,v $ * Revision 1.19 2004/11/17 17:17:31 fwarmerdam * added GML_FIELDTYPES config option * * Revision 1.18 2004/10/05 20:53:00 fwarmerdam * Added try around some setup stuff to try and recover somewhat gracefully. * * Revision 1.17 2004/01/19 16:54:44 warmerda * added logic to capture field types * * Revision 1.16 2003/05/21 03:48:35 warmerda * Expand tabs * * Revision 1.15 2003/05/12 16:09:37 warmerda * Write classes file in binary mode. * * Revision 1.14 2003/05/12 16:07:15 warmerda * Open classes file in binary mode to avoid problem reported in * http://bugzilla.remotesensing.org/show_bug.cgi?id=335 * * Revision 1.13 2003/05/05 15:36:33 warmerda * Treat any element ending in "member" as a feature container. * * Revision 1.12 2003/03/06 20:30:28 warmerda * use GML/OGR geometry translations from ogr_geometry.h now * * Revision 1.11 2002/12/18 21:36:12 danmo * Disabled XML validation by default, -DOGR_GML_VALIDATION=1 can turn it on * * Revision 1.10 2002/12/03 04:24:21 warmerda * Fixed leak of geometry in PrescanForSchema() ... fix from Duncan. * * Revision 1.9 2002/04/01 17:51:59 warmerda * allow featureMember with or without gml: namespace tag * * Revision 1.8 2002/03/08 20:20:09 warmerda * initialized class locked flag * * Revision 1.6 2002/03/06 20:08:02 warmerda * added tracking of extents, feature count and extrainfo * * Revision 1.5 2002/01/25 21:23:21 warmerda * handle IGMLReader destructor properly in gmlreader.cpp * * Revision 1.4 2002/01/25 21:00:00 warmerda * fix build without Xerces * * Revision 1.3 2002/01/25 20:39:19 warmerda * added prescan, and resetreading. Provide stub if Xerces missing * * Revision 1.2 2002/01/24 17:37:33 warmerda * added to/from XML support * * Revision 1.1 2002/01/04 19:46:30 warmerda * New */#include "gmlreader.h"#include "cpl_error.h"#define SUPPORT_GEOMETRY#ifdef SUPPORT_GEOMETRY# include "ogr_geometry.h"#endif/************************************************************************//* ~IGMLReader() *//************************************************************************/IGMLReader::~IGMLReader(){}/************************************************************************//* ==================================================================== *//* No XERCES Library *//* ==================================================================== *//************************************************************************/#if HAVE_XERCES == 0/************************************************************************//* CreateGMLReader() *//************************************************************************/IGMLReader *CreateGMLReader(){ CPLError( CE_Failure, CPLE_AppDefined, "Unable to create Xerces C++ based GML reader, Xerces support\n" "not configured into GDAL/OGR." ); return NULL;}/************************************************************************//* ==================================================================== *//* With XERCES Library *//* ==================================================================== *//************************************************************************/#else /* HAVE_XERCES == 1 */#include "gmlreaderp.h"#include "cpl_conv.h"/************************************************************************//* CreateGMLReader() *//************************************************************************/IGMLReader *CreateGMLReader(){ return new GMLReader();}/************************************************************************//* GMLReader() *//************************************************************************/GMLReader::GMLReader(){ m_nClassCount = 0; m_papoClass = NULL; m_bClassListLocked = FALSE; m_poGMLHandler = NULL; m_poSAXReader = NULL; m_bReadStarted = FALSE; m_poState = NULL; m_poCompleteFeature = NULL; m_pszFilename = NULL;}/************************************************************************//* ~GMLReader() *//************************************************************************/GMLReader::~GMLReader(){ ClearClasses(); CPLFree( m_pszFilename ); CleanupParser();}/************************************************************************//* SetSource() *//************************************************************************/void GMLReader::SetSourceFile( const char *pszFilename ){ CPLFree( m_pszFilename ); m_pszFilename = CPLStrdup( pszFilename );}/************************************************************************//* SetupParser() *//************************************************************************/int GMLReader::SetupParser(){ static int bXercesInitialized = FALSE; if( !bXercesInitialized ) { try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { CPLError( CE_Warning, CPLE_AppDefined, "Exception initializing Xerces based GML reader.\n%s", tr_strdup(toCatch.getMessage()) ); return FALSE; } bXercesInitialized = TRUE; } // Cleanup any old parser. if( m_poSAXReader != NULL ) CleanupParser(); // Create and initialize parser. try{ m_poSAXReader = XMLReaderFactory::createXMLReader(); m_poGMLHandler = new GMLHandler( this ); m_poSAXReader->setContentHandler( m_poGMLHandler ); m_poSAXReader->setErrorHandler( m_poGMLHandler ); m_poSAXReader->setLexicalHandler( m_poGMLHandler ); m_poSAXReader->setEntityResolver( m_poGMLHandler ); m_poSAXReader->setDTDHandler( m_poGMLHandler );#if (OGR_GML_VALIDATION) m_poSAXReader->setFeature( XMLString::transcode("http://xml.org/sax/features/validation"), true); m_poSAXReader->setFeature( XMLString::transcode("http://xml.org/sax/features/namespaces"), true); m_poSAXReader->setFeature( XMLUni::fgSAX2CoreNameSpaces, true ); m_poSAXReader->setFeature( XMLUni::fgXercesSchema, true );// m_poSAXReader->setDoSchema(true);// m_poSAXReader->setValidationSchemaFullChecking(true);#else m_poSAXReader->setFeature( XMLString::transcode("http://xml.org/sax/features/validation"), false); m_poSAXReader->setFeature( XMLString::transcode("http://xml.org/sax/features/namespaces"), false);#endif } catch (...) { CPLError( CE_Warning, CPLE_AppDefined, "Exception initializing Xerces based GML reader.\n" ); return FALSE; } m_bReadStarted = FALSE; // Push an empty state. PushState( new GMLReadState() ); return TRUE;}/************************************************************************//* CleanupParser() *//************************************************************************/void GMLReader::CleanupParser(){ if( m_poSAXReader == NULL ) return; while( m_poState ) PopState(); delete m_poSAXReader; m_poSAXReader = NULL; delete m_poGMLHandler; m_poGMLHandler = NULL; m_bReadStarted = FALSE;}/************************************************************************//* NextFeature() *//************************************************************************/GMLFeature *GMLReader::NextFeature(){ GMLFeature *poReturn = NULL; try { if( !m_bReadStarted ) { if( m_poSAXReader == NULL ) SetupParser(); if( !m_poSAXReader->parseFirst( m_pszFilename, m_oToFill ) ) return NULL; m_bReadStarted = TRUE; } while( m_poCompleteFeature == NULL && m_poSAXReader->parseNext( m_oToFill ) ) {} poReturn = m_poCompleteFeature; m_poCompleteFeature = NULL; } catch (const XMLException& toCatch) { CPLDebug( "GML", "Error during NextFeature()! Message:\n%s", tr_strdup( toCatch.getMessage() ) ); } return poReturn;}/************************************************************************//* PushFeature() *//* *//* Create a feature based on the named element. If the *//* corresponding feature class doesn't exist yet, then create *//* it now. A new GMLReadState will be created for the feature, *//* and it will be placed within that state. The state is *//* pushed onto the readstate stack. *//************************************************************************/void GMLReader::PushFeature( const char *pszElement, const Attributes &attrs ){ int iClass;/* -------------------------------------------------------------------- *//* Find the class of this element. *//* -------------------------------------------------------------------- */ for( iClass = 0; iClass < GetClassCount(); iClass++ ) { if( EQUAL(pszElement,GetClass(iClass)->GetElementName()) ) break; }/* -------------------------------------------------------------------- *//* Create a new feature class for this element, if there is no *//* existing class for it. *//* -------------------------------------------------------------------- */ if( iClass == GetClassCount() ) { CPLAssert( !IsClassListLocked() ); GMLFeatureClass *poNewClass = new GMLFeatureClass( pszElement ); AddClass( poNewClass ); }/* -------------------------------------------------------------------- *//* Create a feature of this feature class. Try to set the fid *//* if available. *//* -------------------------------------------------------------------- */ GMLFeature *poFeature = new GMLFeature( GetClass( iClass ) ); int nFIDIndex; XMLCh anFID[100]; tr_strcpy( anFID, "fid" ); nFIDIndex = attrs.getIndex( anFID ); if( nFIDIndex != -1 ) { char *pszFID = tr_strdup( attrs.getValue( nFIDIndex ) ); poFeature->SetFID( pszFID ); CPLFree( pszFID ); }/* -------------------------------------------------------------------- *//* Create and push a new read state. *//* -------------------------------------------------------------------- */ GMLReadState *poState; poState = new GMLReadState(); poState->m_poFeature = poFeature; PushState( poState );}/************************************************************************//* IsFeatureElement() *//* *//* Based on context and the element name, is this element a new *//* GML feature element? *//************************************************************************/int GMLReader::IsFeatureElement( const char *pszElement ){ CPLAssert( m_poState != NULL ); const char *pszLast = m_poState->GetLastComponent(); int nLen = strlen(pszLast); if( nLen < 6 || !EQUAL(pszLast+nLen-6,"member") ) return FALSE; // If the class list isn't locked, any element that is a featureMember // will do. if( !IsClassListLocked() ) return TRUE; // otherwise, find a class with the desired element name. for( int i = 0; i < GetClassCount(); i++ ) { if( EQUAL(pszElement,GetClass(i)->GetElementName()) ) return TRUE; } return FALSE;}/************************************************************************//* IsAttributeElement() *//************************************************************************/int GMLReader::IsAttributeElement( const char *pszElement ){ if( m_poState->m_poFeature == NULL )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -