ogrgmldatasource.cpp
来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 773 行 · 第 1/3 页
CPP
773 行
/****************************************************************************** * $Id: ogrgmldatasource.cpp 11193 2007-04-03 22:29:41Z mloskot $ * * Project: OGR * Purpose: Implements OGRGMLDataSource class. * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 2002, 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 "ogr_gml.h"#include "cpl_conv.h"#include "cpl_string.h"CPL_CVSID("$Id: ogrgmldatasource.cpp 11193 2007-04-03 22:29:41Z mloskot $");/************************************************************************//* OGRGMLDataSource() *//************************************************************************/OGRGMLDataSource::OGRGMLDataSource(){ pszName = NULL; papoLayers = NULL; nLayers = 0; poReader = NULL; fpOutput = NULL; papszCreateOptions = NULL;}/************************************************************************//* ~OGRGMLDataSource() *//************************************************************************/OGRGMLDataSource::~OGRGMLDataSource(){ if( fpOutput != NULL ) { VSIFPrintf( fpOutput, "%s", "</ogr:FeatureCollection>\n" ); InsertHeader(); if( nBoundedByLocation != -1 && sBoundingRect.IsInit() && VSIFSeek( fpOutput, nBoundedByLocation, SEEK_SET ) == 0 ) { VSIFPrintf( fpOutput, " <gml:boundedBy>\n" ); VSIFPrintf( fpOutput, " <gml:Box>\n" ); VSIFPrintf( fpOutput, " <gml:coord><gml:X>%.16g</gml:X>" "<gml:Y>%.16g</gml:Y></gml:coord>\n", sBoundingRect.MinX, sBoundingRect.MinY ); VSIFPrintf( fpOutput, " <gml:coord><gml:X>%.16g</gml:X>" "<gml:Y>%.16g</gml:Y></gml:coord>\n", sBoundingRect.MaxX, sBoundingRect.MaxY ); VSIFPrintf( fpOutput, " </gml:Box>\n" ); VSIFPrintf( fpOutput, " </gml:boundedBy>" ); } if( fpOutput != stdout ) VSIFClose( fpOutput ); } CSLDestroy( papszCreateOptions ); CPLFree( pszName ); for( int i = 0; i < nLayers; i++ ) delete papoLayers[i]; CPLFree( papoLayers ); if( poReader ) delete poReader;}/************************************************************************//* Open() *//************************************************************************/int OGRGMLDataSource::Open( const char * pszNewName, int bTestOpen ){ FILE *fp; char szHeader[1000];/* -------------------------------------------------------------------- *//* Open the source file. *//* -------------------------------------------------------------------- */ fp = VSIFOpen( pszNewName, "r" ); if( fp == NULL ) { if( !bTestOpen ) CPLError( CE_Failure, CPLE_OpenFailed, "Failed to open GML file `%s'.", pszNewName ); return FALSE; }/* -------------------------------------------------------------------- *//* If we aren't sure it is GML, load a header chunk and check *//* for signs it is GML *//* -------------------------------------------------------------------- */ if( bTestOpen ) { VSIFRead( szHeader, 1, sizeof(szHeader), fp ); szHeader[sizeof(szHeader)-1] = '\0';/* -------------------------------------------------------------------- *//* Check for a UTF-8 BOM and skip if found *//* *//* TODO: BOM is variable-lenght parameter and depends on encoding. *//* Add BOM detection for other encodings. *//* -------------------------------------------------------------------- */ // Used to skip to actual beginning of XML data char* szPtr = szHeader; if( ( (unsigned char)szHeader[0] == 0xEF ) && ( (unsigned char)szHeader[1] == 0xBB ) && ( (unsigned char)szHeader[2] == 0xBF) ) { szPtr += 3; }/* -------------------------------------------------------------------- *//* Here, we expect the opening chevrons of GML tree root element *//* -------------------------------------------------------------------- */ if( szPtr[0] != '<' || strstr(szPtr,"opengis.net/gml") == NULL ) { VSIFClose( fp ); return FALSE; } } /* -------------------------------------------------------------------- *//* We assume now that it is GML. Close and instantiate a *//* GMLReader on it. *//* -------------------------------------------------------------------- */ VSIFClose( fp ); poReader = CreateGMLReader(); if( poReader == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "File %s appears to be GML but the GML reader can't\n" "be instantiated, likely because Xerces support wasn't\n" "configured in.", pszNewName ); return FALSE; } poReader->SetSourceFile( pszNewName ); pszName = CPLStrdup( pszNewName );/* -------------------------------------------------------------------- *//* Can we find a GML Feature Schema (.gfs) for the input file? *//* -------------------------------------------------------------------- */ const char *pszGFSFilename; VSIStatBuf sGFSStatBuf, sGMLStatBuf; int bHaveSchema = FALSE; pszGFSFilename = CPLResetExtension( pszNewName, "gfs" ); if( CPLStat( pszGFSFilename, &sGFSStatBuf ) == 0 ) { CPLStat( pszNewName, &sGMLStatBuf ); if( sGMLStatBuf.st_mtime > sGFSStatBuf.st_mtime ) { CPLDebug( "GML", "Found %s but ignoring because it appears\n" "be older than the associated GML file.", pszGFSFilename ); } else { bHaveSchema = poReader->LoadClasses( pszGFSFilename ); } }/* -------------------------------------------------------------------- *//* Can we find an xsd which might conform to tbe GML3 Level 0 *//* profile? We really ought to look for it based on the rules *//* schemaLocation in the GML feature collection but for now we *//* just hopes it is in the same director with the same name. *//* -------------------------------------------------------------------- */ const char *pszXSDFilename; if( !bHaveSchema ) { pszXSDFilename = CPLResetExtension( pszNewName, "xsd" ); if( CPLStat( pszXSDFilename, &sGMLStatBuf ) == 0 ) { bHaveSchema = poReader->ParseXSD( pszXSDFilename ); } } /* -------------------------------------------------------------------- *//* Force a first pass to establish the schema. Eventually we *//* will have mechanisms for remembering the schema and related *//* information. *//* -------------------------------------------------------------------- */ if( !bHaveSchema && !poReader->PrescanForSchema( TRUE ) ) { // we assume an errors have been reported. return FALSE; }/* -------------------------------------------------------------------- *//* Save the schema file if possible. Don't make a fuss if we *//* can't ... could be read-only directory or something. *//* -------------------------------------------------------------------- */ if( !bHaveSchema ) { FILE *fp = NULL; pszGFSFilename = CPLResetExtension( pszNewName, "gfs" ); if( CPLStat( pszGFSFilename, &sGFSStatBuf ) != 0 && (fp = VSIFOpen( pszGFSFilename, "wt" )) != NULL ) { VSIFClose( fp ); poReader->SaveClasses( pszGFSFilename ); } else { CPLDebug("GML", "Not saving %s files already exists or can't be created.", pszGFSFilename ); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?