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

« back to all changes in this revision

Viewing changes to ogr/ogrsf_frmts/sua/ogrsualayer.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: ogrsualayer.cpp 23066 2011-09-05 21:10:19Z rouault $
 
3
 *
 
4
 * Project:  SUA Translator
 
5
 * Purpose:  Implements OGRSUALayer 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_sua.h"
 
31
#include "cpl_conv.h"
 
32
#include "cpl_string.h"
 
33
#include "ogr_p.h"
 
34
#include "ogr_xplane_geo_utils.h"
 
35
#include "ogr_srs_api.h"
 
36
 
 
37
CPL_CVSID("$Id: ogrsualayer.cpp 23066 2011-09-05 21:10:19Z rouault $");
 
38
 
 
39
/************************************************************************/
 
40
/*                            OGRSUALayer()                             */
 
41
/************************************************************************/
 
42
 
 
43
OGRSUALayer::OGRSUALayer( VSILFILE* fp )
 
44
 
 
45
{
 
46
    fpSUA = fp;
 
47
    nNextFID = 0;
 
48
    bEOF = FALSE;
 
49
    bHasLastLine = FALSE;
 
50
 
 
51
    poSRS = new OGRSpatialReference(SRS_WKT_WGS84);
 
52
 
 
53
    poFeatureDefn = new OGRFeatureDefn( "layer" );
 
54
    poFeatureDefn->Reference();
 
55
    poFeatureDefn->SetGeomType( wkbPolygon );
 
56
 
 
57
    OGRFieldDefn    oField1( "TYPE", OFTString);
 
58
    poFeatureDefn->AddFieldDefn( &oField1 );
 
59
    OGRFieldDefn    oField2( "CLASS", OFTString);
 
60
    poFeatureDefn->AddFieldDefn( &oField2 );
 
61
    OGRFieldDefn    oField3( "TITLE", OFTString);
 
62
    poFeatureDefn->AddFieldDefn( &oField3 );
 
63
    OGRFieldDefn    oField4( "TOPS", OFTString);
 
64
    poFeatureDefn->AddFieldDefn( &oField4 );
 
65
    OGRFieldDefn    oField5( "BASE", OFTString);
 
66
    poFeatureDefn->AddFieldDefn( &oField5 );
 
67
}
 
68
 
 
69
/************************************************************************/
 
70
/*                            ~OGRSUALayer()                            */
 
71
/************************************************************************/
 
72
 
 
73
OGRSUALayer::~OGRSUALayer()
 
74
 
 
75
{
 
76
    if( poSRS != NULL )
 
77
        poSRS->Release();
 
78
 
 
79
    poFeatureDefn->Release();
 
80
 
 
81
    VSIFCloseL( fpSUA );
 
82
}
 
83
 
 
84
 
 
85
/************************************************************************/
 
86
/*                            ResetReading()                            */
 
87
/************************************************************************/
 
88
 
 
89
void OGRSUALayer::ResetReading()
 
90
 
 
91
{
 
92
    nNextFID = 0;
 
93
    bEOF = FALSE;
 
94
    bHasLastLine = FALSE;
 
95
    VSIFSeekL( fpSUA, 0, SEEK_SET );
 
96
}
 
97
 
 
98
 
 
99
/************************************************************************/
 
100
/*                           GetNextFeature()                           */
 
101
/************************************************************************/
 
102
 
 
103
OGRFeature *OGRSUALayer::GetNextFeature()
 
104
{
 
105
    OGRFeature  *poFeature;
 
106
 
 
107
    while(TRUE)
 
108
    {
 
109
        poFeature = GetNextRawFeature();
 
110
        if (poFeature == NULL)
 
111
            return NULL;
 
112
 
 
113
        if((m_poFilterGeom == NULL
 
114
            || FilterGeometry( poFeature->GetGeometryRef() ) )
 
115
        && (m_poAttrQuery == NULL
 
116
            || m_poAttrQuery->Evaluate( poFeature )) )
 
117
        {
 
118
            return poFeature;
 
119
        }
 
120
        else
 
121
            delete poFeature;
 
122
    }
 
123
}
 
124
 
 
125
 
 
126
 
 
127
/************************************************************************/
 
128
/*                              GetLatLon()                             */
 
129
/************************************************************************/
 
130
 
 
131
static int GetLatLon(const char* pszStr, double& dfLat, double& dfLon)
 
132
{
 
133
    if (pszStr[7] != ' ')
 
134
        return FALSE;
 
135
    if (pszStr[0] != 'N' && pszStr[0] != 'S')
 
136
        return FALSE;
 
137
    if (pszStr[8] != 'E' && pszStr[8] != 'W')
 
138
        return FALSE;
 
139
 
 
140
    char szDeg[4], szMin[3], szSec[3];
 
141
    szDeg[0] = pszStr[1];
 
142
    szDeg[1] = pszStr[2];
 
143
    szDeg[2] = 0;
 
144
    szMin[0] = pszStr[3];
 
145
    szMin[1] = pszStr[4];
 
146
    szMin[2] = 0;
 
147
    szSec[0] = pszStr[5];
 
148
    szSec[1] = pszStr[6];
 
149
    szSec[2] = 0;
 
150
 
 
151
    dfLat = atoi(szDeg) + atoi(szMin) / 60. + atoi(szSec) / 3600.;
 
152
    if (pszStr[0] == 'S')
 
153
        dfLat = -dfLat;
 
154
 
 
155
    szDeg[0] = pszStr[9];
 
156
    szDeg[1] = pszStr[10];
 
157
    szDeg[2] = pszStr[11];
 
158
    szDeg[3] = 0;
 
159
    szMin[0] = pszStr[12];
 
160
    szMin[1] = pszStr[13];
 
161
    szMin[2] = 0;
 
162
    szSec[0] = pszStr[14];
 
163
    szSec[1] = pszStr[15];
 
164
    szSec[2] = 0;
 
165
 
 
166
    dfLon = atoi(szDeg) + atoi(szMin) / 60. + atoi(szSec) / 3600.;
 
167
    if (pszStr[8] == 'W')
 
168
        dfLon = -dfLon;
 
169
 
 
170
    return TRUE;
 
171
}
 
172
 
 
173
/************************************************************************/
 
174
/*                         GetNextRawFeature()                          */
 
175
/************************************************************************/
 
176
 
 
177
OGRFeature *OGRSUALayer::GetNextRawFeature()
 
178
{
 
179
    const char* pszLine;
 
180
    CPLString osTYPE, osCLASS, osTITLE, osTOPS, osBASE;
 
181
    OGRLinearRing oLR;
 
182
    double dfLastLat = 0, dfLastLon = 0;
 
183
    int bFirst = TRUE;
 
184
 
 
185
    if (bEOF)
 
186
        return NULL;
 
187
 
 
188
    while(TRUE)
 
189
    {
 
190
        if (bFirst && bHasLastLine)
 
191
        {
 
192
            pszLine = osLastLine.c_str();
 
193
            bFirst = FALSE;
 
194
        }
 
195
        else
 
196
        {
 
197
            pszLine = CPLReadLine2L(fpSUA, 1024, NULL);
 
198
            if (pszLine == NULL)
 
199
            {
 
200
                bEOF = TRUE;
 
201
                if (oLR.getNumPoints() == 0)
 
202
                    return NULL;
 
203
                break;
 
204
            }
 
205
            osLastLine = pszLine;
 
206
            bHasLastLine = TRUE;
 
207
        }
 
208
 
 
209
        if (pszLine[0] == '#' || pszLine[0] == '\0')
 
210
            continue;
 
211
 
 
212
        if (EQUALN(pszLine, "TYPE=", 5))
 
213
        {
 
214
            if (osTYPE.size() != 0)
 
215
                break;
 
216
            osTYPE = pszLine + 5;
 
217
        }
 
218
        else if (EQUALN(pszLine, "CLASS=", 6))
 
219
        {
 
220
            if (osCLASS.size() != 0)
 
221
                break;
 
222
            osCLASS = pszLine + 6;
 
223
        }
 
224
        else if (EQUALN(pszLine, "TITLE=", 6))
 
225
        {
 
226
            if (osTITLE.size() != 0)
 
227
                break;
 
228
            osTITLE = pszLine + 6;
 
229
        }
 
230
        else if (EQUALN(pszLine, "TOPS=", 5))
 
231
            osTOPS = pszLine + 5;
 
232
        else if (EQUALN(pszLine, "BASE=", 5))
 
233
            osBASE = pszLine + 5;
 
234
        else if (EQUALN(pszLine, "POINT=", 6))
 
235
        {
 
236
            pszLine += 6;
 
237
            if (strlen(pszLine) != 16)
 
238
                continue;
 
239
 
 
240
            double dfLat, dfLon;
 
241
            if (!GetLatLon(pszLine, dfLat, dfLon))
 
242
                continue;
 
243
 
 
244
            oLR.addPoint(dfLon, dfLat);
 
245
            dfLastLat = dfLat;
 
246
            dfLastLon = dfLon;
 
247
        }
 
248
        else if (EQUALN(pszLine, "CLOCKWISE", 9) || EQUALN(pszLine, "ANTI-CLOCKWISE", 14))
 
249
        {
 
250
            if (oLR.getNumPoints() == 0)
 
251
                continue;
 
252
 
 
253
            int bClockWise = EQUALN(pszLine, "CLOCKWISE", 9);
 
254
 
 
255
            /*const char* pszRADIUS = strstr(pszLine, "RADIUS=");
 
256
            if (pszRADIUS == NULL)
 
257
                continue;
 
258
            double dfRADIUS = atof(pszRADIUS + 7) * 1852;*/
 
259
 
 
260
            const char* pszCENTRE = strstr(pszLine, "CENTRE=");
 
261
            if (pszCENTRE == NULL)
 
262
                continue;
 
263
            pszCENTRE += 7;
 
264
            if (strlen(pszCENTRE) < 17 || pszCENTRE[16] != ' ')
 
265
                continue;
 
266
            double dfCenterLat, dfCenterLon;
 
267
            if (!GetLatLon(pszCENTRE, dfCenterLat, dfCenterLon))
 
268
                continue;
 
269
 
 
270
            const char* pszTO = strstr(pszLine, "TO=");
 
271
            if (pszTO == NULL)
 
272
                continue;
 
273
            pszTO += 3;
 
274
            if (strlen(pszTO) != 16)
 
275
                continue;
 
276
            double dfToLat, dfToLon;
 
277
            if (!GetLatLon(pszTO, dfToLat, dfToLon))
 
278
                continue;
 
279
 
 
280
            double dfStartDistance = OGRXPlane_Distance(dfCenterLat, dfCenterLon, dfLastLat, dfLastLon);
 
281
            double dfEndDistance = OGRXPlane_Distance(dfCenterLat, dfCenterLon, dfToLat, dfToLon);
 
282
            double dfStartAngle = OGRXPlane_Track(dfCenterLat, dfCenterLon, dfLastLat, dfLastLon);
 
283
            double dfEndAngle = OGRXPlane_Track(dfCenterLat, dfCenterLon, dfToLat, dfToLon);
 
284
            if (bClockWise && dfEndAngle < dfStartAngle)
 
285
                dfEndAngle += 360;
 
286
            else if (!bClockWise && dfStartAngle < dfEndAngle)
 
287
                dfEndAngle -= 360;
 
288
 
 
289
            int nSign = (bClockWise) ? 1 : -1;
 
290
            double dfAngle;
 
291
            for(dfAngle = dfStartAngle; (dfAngle - dfEndAngle) * nSign < 0; dfAngle += nSign)
 
292
            {
 
293
                double dfLat, dfLon;
 
294
                double pct = (dfAngle - dfStartAngle) / (dfEndAngle - dfStartAngle);
 
295
                double dfDist = dfStartDistance * (1-pct) + dfEndDistance * pct;
 
296
                OGRXPlane_ExtendPosition(dfCenterLat, dfCenterLon, dfDist, dfAngle, &dfLat, &dfLon);
 
297
                oLR.addPoint(dfLon, dfLat);
 
298
            }
 
299
            oLR.addPoint(dfToLon, dfToLat);
 
300
 
 
301
            dfLastLat = oLR.getY(oLR.getNumPoints() - 1);
 
302
            dfLastLon = oLR.getX(oLR.getNumPoints() - 1);
 
303
        }
 
304
        else if (EQUALN(pszLine, "CIRCLE", 6))
 
305
        {
 
306
            const char* pszRADIUS = strstr(pszLine, "RADIUS=");
 
307
            if (pszRADIUS == NULL)
 
308
                continue;
 
309
            double dfRADIUS = atof(pszRADIUS + 7) * 1852;
 
310
 
 
311
            const char* pszCENTRE = strstr(pszLine, "CENTRE=");
 
312
            if (pszCENTRE == NULL)
 
313
                continue;
 
314
            pszCENTRE += 7;
 
315
            if (strlen(pszCENTRE) != 16)
 
316
                continue;
 
317
            double dfCenterLat, dfCenterLon;
 
318
            if (!GetLatLon(pszCENTRE, dfCenterLat, dfCenterLon))
 
319
                continue;
 
320
 
 
321
            double dfAngle;
 
322
            double dfLat, dfLon;
 
323
            for(dfAngle = 0; dfAngle < 360; dfAngle += 1)
 
324
            {
 
325
                OGRXPlane_ExtendPosition(dfCenterLat, dfCenterLon, dfRADIUS, dfAngle, &dfLat, &dfLon);
 
326
                oLR.addPoint(dfLon, dfLat);
 
327
            }
 
328
            OGRXPlane_ExtendPosition(dfCenterLat, dfCenterLon, dfRADIUS, 0, &dfLat, &dfLon);
 
329
            oLR.addPoint(dfLon, dfLat);
 
330
 
 
331
            dfLastLat = oLR.getY(oLR.getNumPoints() - 1);
 
332
            dfLastLon = oLR.getX(oLR.getNumPoints() - 1);
 
333
        }
 
334
        else if (EQUALN(pszLine, "INCLUDE", 7) || EQUALN(pszLine, "END", 3))
 
335
        {
 
336
        }
 
337
        else
 
338
        {
 
339
            CPLDebug("SUA", "Unexpected content : %s", pszLine);
 
340
        }
 
341
    }
 
342
 
 
343
    OGRFeature* poFeature = new OGRFeature(poFeatureDefn);
 
344
    poFeature->SetField(0, osTYPE.c_str());
 
345
    poFeature->SetField(1, osCLASS.c_str());
 
346
    poFeature->SetField(2, osTITLE.c_str());
 
347
    poFeature->SetField(3, osTOPS.c_str());
 
348
    poFeature->SetField(4, osBASE.c_str());
 
349
 
 
350
    OGRPolygon* poPoly = new OGRPolygon();
 
351
    poPoly->assignSpatialReference(poSRS);
 
352
    oLR.closeRings();
 
353
    poPoly->addRing(&oLR);
 
354
    poFeature->SetGeometryDirectly(poPoly);
 
355
    poFeature->SetFID(nNextFID++);
 
356
 
 
357
    return poFeature;
 
358
}
 
359
/************************************************************************/
 
360
/*                           TestCapability()                           */
 
361
/************************************************************************/
 
362
 
 
363
int OGRSUALayer::TestCapability( const char * pszCap )
 
364
 
 
365
{
 
366
    return FALSE;
 
367
}
 
368