ogrcsvlayer.cpp
来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 455 行 · 第 1/2 页
CPP
455 行
/****************************************************************************** * $Id: ogrcsvlayer.cpp 10646 2007-01-18 02:38:10Z warmerdam $ * * Project: CSV Translator * Purpose: Implements OGRCSVLayer class. * Author: Frank Warmerdam <warmerdam@pobox.com> * ****************************************************************************** * Copyright (c) 2004, 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_csv.h"#include "cpl_conv.h"#include "cpl_string.h"#include "cpl_csv.h"CPL_CVSID("$Id: ogrcsvlayer.cpp 10646 2007-01-18 02:38:10Z warmerdam $");/************************************************************************//* OGRCSVLayer() *//* *//* Note that the OGRCSVLayer assumes ownership of the passed *//* file pointer. *//************************************************************************/OGRCSVLayer::OGRCSVLayer( const char *pszLayerNameIn, FILE * fp, const char *pszFilename, int bNew, int bInWriteMode ){ fpCSV = fp; this->bInWriteMode = bInWriteMode; this->bNew = bNew; bUseCRLF = FALSE; bNeedRewind = FALSE; nNextFID = 1; poFeatureDefn = new OGRFeatureDefn( pszLayerNameIn ); poFeatureDefn->Reference(); poFeatureDefn->SetGeomType( wkbNone );/* -------------------------------------------------------------------- *//* If this is not a new file, read ahead to establish if it is *//* already in CRLF (DOS) mode, or just a normal unix CR mode. *//* -------------------------------------------------------------------- */ if( !bNew ) { int nBytesRead = 0; char chNewByte; while( nBytesRead < 10000 && VSIFRead( &chNewByte, 1, 1, fpCSV ) == 1 ) { if( chNewByte == 13 ) { bUseCRLF = TRUE; break; } } VSIRewind( fpCSV ); }/* -------------------------------------------------------------------- *//* Check if the first record seems to be field definitions or *//* not. We assume it is field definitions if none of the *//* values are strictly numeric. *//* -------------------------------------------------------------------- */ char **papszTokens = NULL; int nFieldCount=0, iField; if( !bNew ) { papszTokens = CSVReadParseLine( fpCSV ); nFieldCount = CSLCount( papszTokens ); bHasFieldNames = TRUE; } else bHasFieldNames = FALSE; for( iField = 0; iField < nFieldCount && bHasFieldNames; iField++ ) { const char *pszToken = papszTokens[iField]; int bAllNumeric = TRUE; while( *pszToken != '\0' && bAllNumeric ) { if( *pszToken != '.' && *pszToken != '-' && (*pszToken < '0' || *pszToken > '9') ) bAllNumeric = FALSE; pszToken++; } if( bAllNumeric ) bHasFieldNames = FALSE; } if( !bHasFieldNames ) VSIRewind( fpCSV );/* -------------------------------------------------------------------- *//* Search a csvt file for types *//* -------------------------------------------------------------------- */ char** papszFieldTypes = NULL; if (!bNew) { char* dname = strdup(CPLGetDirname(pszFilename)); char* fname = strdup(CPLGetBasename(pszFilename)); FILE* fpCSVT = fopen(CPLFormFilename(dname, fname, ".csvt"), "r"); free(dname); free(fname); if (fpCSVT!=NULL) { VSIRewind(fpCSVT); papszFieldTypes = CSVReadParseLine(fpCSVT); fclose(fpCSVT); } } /* -------------------------------------------------------------------- *//* Build field definitions. *//* -------------------------------------------------------------------- */ for( iField = 0; iField < nFieldCount; iField++ ) { char *pszFieldName; char szFieldNameBuffer[100]; if( bHasFieldNames ) { pszFieldName = papszTokens[iField]; // trim white space. while( *pszFieldName == ' ' ) pszFieldName++; while( pszFieldName[0] != '\0' && pszFieldName[strlen(pszFieldName)-1] == ' ' ) pszFieldName[strlen(pszFieldName)-1] = '\0'; } else { pszFieldName = szFieldNameBuffer; sprintf( szFieldNameBuffer, "field_%d", iField+1 ); } OGRFieldDefn oField(pszFieldName, OFTString); if (papszFieldTypes!=NULL && iField<CSLCount(papszFieldTypes)) { if (EQUAL(papszFieldTypes[iField], "Integer")) oField.SetType(OFTInteger); else if (EQUAL(papszFieldTypes[iField], "Real")) oField.SetType(OFTReal); else if (EQUAL(papszFieldTypes[iField], "String")) oField.SetType(OFTString); } poFeatureDefn->AddFieldDefn( &oField ); } CSLDestroy( papszTokens ); CSLDestroy( papszFieldTypes );}/************************************************************************//* ~OGRCSVLayer() *//************************************************************************/OGRCSVLayer::~OGRCSVLayer(){ if( m_nFeaturesRead > 0 && poFeatureDefn != NULL ) { CPLDebug( "CSV", "%d features read on layer '%s'.", (int) m_nFeaturesRead, poFeatureDefn->GetName() ); } poFeatureDefn->Release(); VSIFClose( fpCSV );}/************************************************************************//* ResetReading() *//************************************************************************/void OGRCSVLayer::ResetReading(){ VSIRewind( fpCSV ); if( bHasFieldNames ) CSLDestroy( CSVReadParseLine( fpCSV ) ); bNeedRewind = FALSE; nNextFID = 1;}/************************************************************************//* GetNextUnfilteredFeature() *//************************************************************************/OGRFeature * OGRCSVLayer::GetNextUnfilteredFeature(){/* -------------------------------------------------------------------- *//* Read the CSV record. *//* -------------------------------------------------------------------- */ char **papszTokens = CSVReadParseLine( fpCSV );
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?