~ubuntu-branches/debian/sid/gdal/sid

« back to all changes in this revision

Viewing changes to ogr/ogrsf_frmts/htf/ogrhtfdatasource.cpp

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2012-05-07 15:04:42 UTC
  • mfrom: (5.5.16 experimental)
  • Revision ID: package-import@ubuntu.com-20120507150442-2eks97loeh6rq005
Tags: 1.9.0-1
* Ready for sid, starting transition.
* All symfiles updated to latest builds.
* Added dh_numpy call in debian/rules to depend on numpy ABI.
* Policy bumped to 3.9.3, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * $Id: ogrhtfdatasource.cpp 23042 2011-09-04 15:07:22Z rouault $
 
3
 *
 
4
 * Project:  HTF Translator
 
5
 * Purpose:  Implements OGRHTFDataSource class
 
6
 * Author:   Even Rouault, even dot rouault at mines dash paris dot org
 
7
 *
 
8
 ******************************************************************************
 
9
 * Copyright (c) 2010, Even Rouault <even dot rouault at mines dash paris dot org>
 
10
 *
 
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:
 
17
 *
 
18
 * The above copyright notice and this permission notice shall be included
 
19
 * in all copies or substantial portions of the Software.
 
20
 *
 
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
 ****************************************************************************/
 
29
 
 
30
#include "ogr_htf.h"
 
31
#include "cpl_conv.h"
 
32
#include "cpl_string.h"
 
33
 
 
34
CPL_CVSID("$Id: ogrhtfdatasource.cpp 23042 2011-09-04 15:07:22Z rouault $");
 
35
 
 
36
/************************************************************************/
 
37
/*                          OGRHTFDataSource()                          */
 
38
/************************************************************************/
 
39
 
 
40
OGRHTFDataSource::OGRHTFDataSource()
 
41
 
 
42
{
 
43
    papoLayers = NULL;
 
44
    nLayers = 0;
 
45
    poMetadataLayer = NULL;
 
46
 
 
47
    pszName = NULL;
 
48
}
 
49
 
 
50
/************************************************************************/
 
51
/*                         ~OGRHTFDataSource()                          */
 
52
/************************************************************************/
 
53
 
 
54
OGRHTFDataSource::~OGRHTFDataSource()
 
55
 
 
56
{
 
57
    for( int i = 0; i < nLayers; i++ )
 
58
        delete papoLayers[i];
 
59
    CPLFree( papoLayers );
 
60
    delete poMetadataLayer;
 
61
 
 
62
    CPLFree( pszName );
 
63
}
 
64
 
 
65
/************************************************************************/
 
66
/*                           TestCapability()                           */
 
67
/************************************************************************/
 
68
 
 
69
int OGRHTFDataSource::TestCapability( const char * pszCap )
 
70
 
 
71
{
 
72
    return FALSE;
 
73
}
 
74
 
 
75
/************************************************************************/
 
76
/*                              GetLayer()                              */
 
77
/************************************************************************/
 
78
 
 
79
OGRLayer *OGRHTFDataSource::GetLayer( int iLayer )
 
80
 
 
81
{
 
82
    if( iLayer < 0 || iLayer >= nLayers )
 
83
        return NULL;
 
84
    else
 
85
        return papoLayers[iLayer];
 
86
}
 
87
 
 
88
/************************************************************************/
 
89
/*                          GetLayerByName()                            */
 
90
/************************************************************************/
 
91
 
 
92
OGRLayer* OGRHTFDataSource::GetLayerByName( const char* pszLayerName )
 
93
{
 
94
    if (nLayers == 0)
 
95
        return NULL;
 
96
    if (EQUAL(pszLayerName, "polygon"))
 
97
        return papoLayers[0];
 
98
    if (EQUAL(pszLayerName, "sounding"))
 
99
        return papoLayers[1];
 
100
    if (EQUAL(pszLayerName, "metadata"))
 
101
        return poMetadataLayer;
 
102
    return NULL;
 
103
}
 
104
 
 
105
/************************************************************************/
 
106
/*                                Open()                                */
 
107
/************************************************************************/
 
108
 
 
109
int OGRHTFDataSource::Open( const char * pszFilename, int bUpdateIn)
 
110
 
 
111
{
 
112
    if (bUpdateIn)
 
113
    {
 
114
        return FALSE;
 
115
    }
 
116
 
 
117
    pszName = CPLStrdup( pszFilename );
 
118
 
 
119
// -------------------------------------------------------------------- 
 
120
//      Does this appear to be a .htf file?
 
121
// --------------------------------------------------------------------
 
122
 
 
123
    VSILFILE* fp = VSIFOpenL(pszFilename, "rb");
 
124
    if (fp == NULL)
 
125
        return FALSE;
 
126
 
 
127
    char szBuffer[11];
 
128
    int nbRead = (int)VSIFReadL(szBuffer, 1, sizeof(szBuffer) - 1, fp);
 
129
    szBuffer[nbRead] = '\0';
 
130
 
 
131
    int bIsHTF = strcmp(szBuffer, "HTF HEADER") == 0;
 
132
    if (!bIsHTF)
 
133
    {
 
134
        VSIFCloseL(fp);
 
135
        return FALSE;
 
136
    }
 
137
 
 
138
    VSIFSeekL(fp, 0, SEEK_SET);
 
139
 
 
140
    const char* pszLine;
 
141
    int bEndOfHTFHeader = FALSE;
 
142
    int bIsSouth = FALSE;
 
143
    int bGeodeticDatumIsWGS84 = FALSE;
 
144
    int bIsUTM = FALSE;
 
145
    int nZone = 0;
 
146
    int nLines = 0;
 
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)
 
152
    {
 
153
        nLines ++;
 
154
        if (nLines == 1000)
 
155
        {
 
156
            break;
 
157
        }
 
158
        if (*pszLine == ';' || *pszLine == '\0')
 
159
            continue;
 
160
 
 
161
        if (strcmp(pszLine, "END OF HTF HEADER") == 0)
 
162
        {
 
163
            bEndOfHTFHeader = TRUE;
 
164
            break;
 
165
        }
 
166
 
 
167
        aosMD.push_back(pszLine);
 
168
 
 
169
        if (strncmp(pszLine, "GEODETIC DATUM: ", 16) == 0)
 
170
        {
 
171
            if (strcmp(pszLine + 16, "WG84") == 0 ||
 
172
                strcmp(pszLine + 16, "WGS84") == 0)
 
173
                bGeodeticDatumIsWGS84 = TRUE;
 
174
            else
 
175
            {
 
176
                VSIFCloseL(fp);
 
177
                CPLError(CE_Failure, CPLE_NotSupported,
 
178
                         "Unsupported datum : %s", pszLine + 16);
 
179
                return FALSE;
 
180
            }
 
181
        }
 
182
        else if (strncmp(pszLine, "NE LATITUDE: -", 14) == 0)
 
183
            bIsSouth = TRUE;
 
184
        else if (strncmp(pszLine, "GRID REFERENCE SYSTEM: ", 23) == 0)
 
185
        {
 
186
            if (strncmp(pszLine + 23, "UTM", 3) == 0)
 
187
                bIsUTM = TRUE;
 
188
            else
 
189
            {
 
190
                VSIFCloseL(fp);
 
191
                CPLError(CE_Failure, CPLE_NotSupported,
 
192
                         "Unsupported grid : %s", pszLine + 23);
 
193
                return FALSE;
 
194
            }
 
195
        }
 
196
        else if (strncmp(pszLine, "GRID ZONE: ", 11) == 0)
 
197
        {
 
198
            nZone = atoi(pszLine + 11);
 
199
        }
 
200
        else if (strncmp(pszLine, "SW GRID COORDINATE - EASTING: ", 30) == 0)
 
201
        {
 
202
            bHasSWEasting = TRUE;
 
203
            dfSWEasting = atof(pszLine + 30);
 
204
        }
 
205
        else if (strncmp(pszLine, "SW GRID COORDINATE - NORTHING: ", 31) == 0)
 
206
        {
 
207
            bHasSWNorthing = TRUE;
 
208
            dfSWNorthing = atof(pszLine + 31);
 
209
        }
 
210
        else if (strncmp(pszLine, "NE GRID COORDINATE - EASTING: ", 30) == 0)
 
211
        {
 
212
            bHasNEEasting = TRUE;
 
213
            dfNEEasting = atof(pszLine + 30);
 
214
        }
 
215
        else if (strncmp(pszLine, "NE GRID COORDINATE - NORTHING: ", 31) == 0)
 
216
        {
 
217
            bHasNENorthing = TRUE;
 
218
            dfNENorthing = atof(pszLine + 31);
 
219
        }
 
220
        else if (strncmp(pszLine, "TOTAL SOUNDINGS: ", 17) == 0)
 
221
        {
 
222
            nTotalSoundings = atoi(pszLine + 17);
 
223
        }
 
224
    }
 
225
 
 
226
    VSIFCloseL(fp);
 
227
 
 
228
    if (!bEndOfHTFHeader)
 
229
        return FALSE;
 
230
    if (!bGeodeticDatumIsWGS84)
 
231
        return FALSE;
 
232
    if (!bIsUTM)
 
233
        return FALSE;
 
234
    if (nZone == 0)
 
235
        return FALSE;
 
236
  
 
237
    nLayers = 2;
 
238
    papoLayers = (OGRHTFLayer**) CPLMalloc(sizeof(OGRHTFLayer*) * 2);
 
239
    papoLayers[0] = new OGRHTFPolygonLayer(pszFilename, nZone, !bIsSouth);
 
240
    papoLayers[1] = new OGRHTFSoundingLayer(pszFilename, nZone, !bIsSouth, nTotalSoundings);
 
241
 
 
242
    if (bHasSWEasting && bHasSWNorthing && bHasNEEasting && bHasNENorthing)
 
243
    {
 
244
        papoLayers[0]->SetExtent(dfSWEasting, dfSWNorthing, dfNEEasting, dfNENorthing);
 
245
        papoLayers[1]->SetExtent(dfSWEasting, dfSWNorthing, dfNEEasting, dfNENorthing);
 
246
    }
 
247
 
 
248
    poMetadataLayer = new OGRHTFMetadataLayer(aosMD);
 
249
 
 
250
    return TRUE;
 
251
}