1
/******************************************************************************
2
* $Id: ogrhtfdatasource.cpp 23042 2011-09-04 15:07:22Z rouault $
4
* Project: HTF Translator
5
* Purpose: Implements OGRHTFDataSource class
6
* Author: Even Rouault, even dot rouault at mines dash paris dot org
8
******************************************************************************
9
* Copyright (c) 2010, Even Rouault <even dot rouault at mines dash paris dot org>
11
* Permission is hereby granted, free of charge, to any person obtaining a
12
* copy of this software and associated documentation files (the "Software"),
13
* to deal in the Software without restriction, including without limitation
14
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
15
* and/or sell copies of the Software, and to permit persons to whom the
16
* Software is furnished to do so, subject to the following conditions:
18
* The above copyright notice and this permission notice shall be included
19
* in all copies or substantial portions of the Software.
21
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
* DEALINGS IN THE SOFTWARE.
28
****************************************************************************/
32
#include "cpl_string.h"
34
CPL_CVSID("$Id: ogrhtfdatasource.cpp 23042 2011-09-04 15:07:22Z rouault $");
36
/************************************************************************/
37
/* OGRHTFDataSource() */
38
/************************************************************************/
40
OGRHTFDataSource::OGRHTFDataSource()
45
poMetadataLayer = NULL;
50
/************************************************************************/
51
/* ~OGRHTFDataSource() */
52
/************************************************************************/
54
OGRHTFDataSource::~OGRHTFDataSource()
57
for( int i = 0; i < nLayers; i++ )
59
CPLFree( papoLayers );
60
delete poMetadataLayer;
65
/************************************************************************/
66
/* TestCapability() */
67
/************************************************************************/
69
int OGRHTFDataSource::TestCapability( const char * pszCap )
75
/************************************************************************/
77
/************************************************************************/
79
OGRLayer *OGRHTFDataSource::GetLayer( int iLayer )
82
if( iLayer < 0 || iLayer >= nLayers )
85
return papoLayers[iLayer];
88
/************************************************************************/
89
/* GetLayerByName() */
90
/************************************************************************/
92
OGRLayer* OGRHTFDataSource::GetLayerByName( const char* pszLayerName )
96
if (EQUAL(pszLayerName, "polygon"))
98
if (EQUAL(pszLayerName, "sounding"))
100
if (EQUAL(pszLayerName, "metadata"))
101
return poMetadataLayer;
105
/************************************************************************/
107
/************************************************************************/
109
int OGRHTFDataSource::Open( const char * pszFilename, int bUpdateIn)
117
pszName = CPLStrdup( pszFilename );
119
// --------------------------------------------------------------------
120
// Does this appear to be a .htf file?
121
// --------------------------------------------------------------------
123
VSILFILE* fp = VSIFOpenL(pszFilename, "rb");
128
int nbRead = (int)VSIFReadL(szBuffer, 1, sizeof(szBuffer) - 1, fp);
129
szBuffer[nbRead] = '\0';
131
int bIsHTF = strcmp(szBuffer, "HTF HEADER") == 0;
138
VSIFSeekL(fp, 0, SEEK_SET);
141
int bEndOfHTFHeader = FALSE;
142
int bIsSouth = FALSE;
143
int bGeodeticDatumIsWGS84 = FALSE;
147
int bHasSWEasting = FALSE, bHasSWNorthing = FALSE, bHasNEEasting = FALSE, bHasNENorthing = FALSE;
148
double dfSWEasting = 0, dfSWNorthing = 0, dfNEEasting = 0, dfNENorthing = 0;
149
std::vector<CPLString> aosMD;
150
int nTotalSoundings = 0;
151
while( (pszLine = CPLReadLine2L(fp, 1024, NULL)) != NULL)
158
if (*pszLine == ';' || *pszLine == '\0')
161
if (strcmp(pszLine, "END OF HTF HEADER") == 0)
163
bEndOfHTFHeader = TRUE;
167
aosMD.push_back(pszLine);
169
if (strncmp(pszLine, "GEODETIC DATUM: ", 16) == 0)
171
if (strcmp(pszLine + 16, "WG84") == 0 ||
172
strcmp(pszLine + 16, "WGS84") == 0)
173
bGeodeticDatumIsWGS84 = TRUE;
177
CPLError(CE_Failure, CPLE_NotSupported,
178
"Unsupported datum : %s", pszLine + 16);
182
else if (strncmp(pszLine, "NE LATITUDE: -", 14) == 0)
184
else if (strncmp(pszLine, "GRID REFERENCE SYSTEM: ", 23) == 0)
186
if (strncmp(pszLine + 23, "UTM", 3) == 0)
191
CPLError(CE_Failure, CPLE_NotSupported,
192
"Unsupported grid : %s", pszLine + 23);
196
else if (strncmp(pszLine, "GRID ZONE: ", 11) == 0)
198
nZone = atoi(pszLine + 11);
200
else if (strncmp(pszLine, "SW GRID COORDINATE - EASTING: ", 30) == 0)
202
bHasSWEasting = TRUE;
203
dfSWEasting = atof(pszLine + 30);
205
else if (strncmp(pszLine, "SW GRID COORDINATE - NORTHING: ", 31) == 0)
207
bHasSWNorthing = TRUE;
208
dfSWNorthing = atof(pszLine + 31);
210
else if (strncmp(pszLine, "NE GRID COORDINATE - EASTING: ", 30) == 0)
212
bHasNEEasting = TRUE;
213
dfNEEasting = atof(pszLine + 30);
215
else if (strncmp(pszLine, "NE GRID COORDINATE - NORTHING: ", 31) == 0)
217
bHasNENorthing = TRUE;
218
dfNENorthing = atof(pszLine + 31);
220
else if (strncmp(pszLine, "TOTAL SOUNDINGS: ", 17) == 0)
222
nTotalSoundings = atoi(pszLine + 17);
228
if (!bEndOfHTFHeader)
230
if (!bGeodeticDatumIsWGS84)
238
papoLayers = (OGRHTFLayer**) CPLMalloc(sizeof(OGRHTFLayer*) * 2);
239
papoLayers[0] = new OGRHTFPolygonLayer(pszFilename, nZone, !bIsSouth);
240
papoLayers[1] = new OGRHTFSoundingLayer(pszFilename, nZone, !bIsSouth, nTotalSoundings);
242
if (bHasSWEasting && bHasSWNorthing && bHasNEEasting && bHasNENorthing)
244
papoLayers[0]->SetExtent(dfSWEasting, dfSWNorthing, dfNEEasting, dfNENorthing);
245
papoLayers[1]->SetExtent(dfSWEasting, dfSWNorthing, dfNEEasting, dfNENorthing);
248
poMetadataLayer = new OGRHTFMetadataLayer(aosMD);