📄 ntffilereader.cpp
字号:
/****************************************************************************** * $Id: ntffilereader.cpp 10646 2007-01-18 02:38:10Z warmerdam $ * * Project: NTF Translator * Purpose: NTFFileReader class implementation. * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 1999, 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. ****************************************************************************/#include <stdarg.h>#include "ntf.h"#include "cpl_conv.h"#include "cpl_string.h"#include "ogr_api.h"CPL_CVSID("$Id: ntffilereader.cpp 10646 2007-01-18 02:38:10Z warmerdam $");static int DefaultNTFRecordGrouper( NTFFileReader *, NTFRecord **, NTFRecord * );#ifndef PI# define PI 3.14159265358979323846#endif/************************************************************************//* NTFFileReader *//************************************************************************/NTFFileReader::NTFFileReader( OGRNTFDataSource * poDataSource ){ fp = NULL; nFCCount = 0; papszFCNum = NULL; papszFCName = NULL; nPreSavedPos = nPostSavedPos = 0; nSavedFeatureId = nBaseFeatureId = 1; nFeatureCount = -1; poSavedRecord = NULL; nAttCount = 0; pasAttDesc = NULL; pszTileName = NULL; pszProduct = NULL; pszPVName = NULL; pszFilename = NULL; apoCGroup[0] = NULL; poDS = poDataSource; memset( apoTypeTranslation, 0, sizeof(apoTypeTranslation) ); nProduct = NPC_UNKNOWN; pfnRecordGrouper = DefaultNTFRecordGrouper; dfXYMult = 1.0; dfZMult = 1.0; dfXOrigin = 0; dfYOrigin = 0; nNTFLevel = 0; dfTileXSize = 0; dfTileYSize = 0; dfScale = 0.0; dfPaperToGround = 0.0; nCoordWidth = 6; nZWidth = 6; for( int i = 0; i < 100; i++ ) { anIndexSize[i] = 0; apapoRecordIndex[i] = NULL; } panColumnOffset = NULL; poRasterLayer = NULL; nRasterXSize = nRasterYSize = nRasterDataType = 1; bIndexBuilt = FALSE; bIndexNeeded = FALSE; if( poDS->GetOption("CACHE_LINES") != NULL && EQUAL(poDS->GetOption("CACHE_LINES"),"OFF") ) bCacheLines = FALSE; else bCacheLines = TRUE; nLineCacheSize = 0; papoLineCache = NULL;}/************************************************************************//* ~NTFFileReader() *//************************************************************************/NTFFileReader::~NTFFileReader(){ CacheClean(); DestroyIndex(); ClearDefs(); CPLFree( pszFilename ); CPLFree( panColumnOffset );}/************************************************************************//* SetBaseFID() *//************************************************************************/void NTFFileReader::SetBaseFID( long nNewBase ){ CPLAssert( nSavedFeatureId == 1 ); nBaseFeatureId = nNewBase; nSavedFeatureId = nBaseFeatureId;}/************************************************************************//* ClearDefs() *//* *//* Clear attribute definitions and feature classes. All the *//* stuff that would have to be cleaned up by Open(), and the *//* destructor. *//************************************************************************/void NTFFileReader::ClearDefs(){ int i; Close(); ClearCGroup(); CSLDestroy( papszFCNum ); papszFCNum = NULL; CSLDestroy( papszFCName ); papszFCName = NULL; nFCCount = 0; for( i = 0; i < nAttCount; i++ ) { if( pasAttDesc[i].poCodeList != NULL ) delete pasAttDesc[i].poCodeList; } CPLFree( pasAttDesc ); nAttCount = 0; pasAttDesc = NULL; CPLFree( pszProduct ); pszProduct = NULL; CPLFree( pszPVName ); pszPVName = NULL; CPLFree( pszTileName ); pszTileName = NULL;}/************************************************************************//* Close() *//* *//* Close the file, but don't wipe out our knowledge about this *//* file. *//************************************************************************/void NTFFileReader::Close(){ if( poSavedRecord != NULL ) delete poSavedRecord; poSavedRecord = NULL; nPreSavedPos = nPostSavedPos = 0; nSavedFeatureId = nBaseFeatureId; if( fp != NULL ) { VSIFClose( fp ); fp = NULL; } CacheClean();}/************************************************************************//* Open() *//************************************************************************/int NTFFileReader::Open( const char * pszFilenameIn ){ if( pszFilenameIn != NULL ) { ClearDefs(); CPLFree( pszFilename ); pszFilename = CPLStrdup( pszFilenameIn ); } else Close(); /* -------------------------------------------------------------------- *//* Open the file. *//* -------------------------------------------------------------------- */ fp = VSIFOpen( pszFilename, "rb" ); // notdef: we should likely issue a proper CPL error message based // based on errno here. if( fp == NULL ) { CPLError( CE_Failure, CPLE_OpenFailed, "Unable to open file `%s' for read access.\n", pszFilename ); return FALSE; }/* -------------------------------------------------------------------- *//* If we are just reopening an existing file we will just scan *//* past the section header ... no need to reform all the definitions.*//* -------------------------------------------------------------------- */ if( pszFilenameIn == NULL ) { NTFRecord *poRecord; for( poRecord = new NTFRecord( fp ); poRecord->GetType() != NRT_VTR && poRecord->GetType() != NRT_SHR; poRecord = new NTFRecord( fp ) ) { delete poRecord; } delete poRecord; return TRUE; }/* -------------------------------------------------------------------- *//* Read the first record, and verify it is a proper volume header. *//* -------------------------------------------------------------------- */ NTFRecord oVHR( fp ); if( oVHR.GetType() != NRT_VHR ) { CPLError( CE_Failure, CPLE_AppDefined, "File `%s' appears to not be a UK NTF file.\n", pszFilename ); return FALSE; } nNTFLevel = atoi(oVHR.GetField( 57, 57 )); CPLAssert( nNTFLevel >= 1 && nNTFLevel <= 5 );/* -------------------------------------------------------------------- *//* Read records till we get the section header. *//* -------------------------------------------------------------------- */ NTFRecord *poRecord; for( poRecord = new NTFRecord( fp ); poRecord->GetType() != NRT_VTR && poRecord->GetType() != NRT_SHR; poRecord = new NTFRecord( fp ) ) {/* -------------------------------------------------------------------- *//* Handle feature class name records. *//* -------------------------------------------------------------------- */ if( poRecord->GetType() == NRT_FCR ) { const char *pszData; int iChar; char szFCName[100]; nFCCount++; papszFCNum = CSLAddString( papszFCNum, poRecord->GetField(3,6) ); szFCName[0] = '\0'; pszData = poRecord->GetData(); // CODE_COM for( iChar = 15; pszData[iChar] == ' ' && iChar > 5; iChar-- ) {} if( iChar > 6 ) strcat( szFCName, poRecord->GetField(7,iChar+1) ); // STCLASS for( iChar = 35; pszData[iChar] == ' ' && iChar > 15; iChar-- ) {} if( iChar > 15 ) { if( strlen(szFCName) > 0 ) strcat( szFCName, " : " ); strcat( szFCName, poRecord->GetField(17,iChar+1) ); } // FEATDES for( iChar = 36; pszData[iChar] != '\0' && pszData[iChar] != '\\'; iChar++ ) {} if( iChar > 37 ) { if( strlen(szFCName) > 0 ) strcat( szFCName, " : " ); strcat( szFCName, poRecord->GetField(37,iChar) ); } papszFCName = CSLAddString(papszFCName, szFCName ); }/* -------------------------------------------------------------------- *//* Handle attribute description records. *//* -------------------------------------------------------------------- */ else if( poRecord->GetType() == NRT_ADR ) { nAttCount++; pasAttDesc = (NTFAttDesc *) CPLRealloc( pasAttDesc, sizeof(NTFAttDesc) * nAttCount ); ProcessAttDesc( poRecord, pasAttDesc + nAttCount - 1 ); }/* -------------------------------------------------------------------- *//* Handle attribute description records. *//* -------------------------------------------------------------------- */ else if( poRecord->GetType() == NRT_CODELIST ) { NTFCodeList *poCodeList; NTFAttDesc *psAttDesc; poCodeList = new NTFCodeList( poRecord ); psAttDesc = GetAttDesc( poCodeList->szValType ); if( psAttDesc == NULL ) { CPLDebug( "NTF", "Got CODELIST for %s without ATTDESC.", poCodeList->szValType ); delete poCodeList;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -