1 /******************************************************************************
2  * $Id: ogr_xplane_fix_reader.cpp
3  *
4  * Project:  X-Plane fix.dat file reader
5  * Purpose:  Implements OGRXPlaneFixReader class
6  * Author:   Even Rouault, even dot rouault at mines dash paris dot org
7  *
8  ******************************************************************************
9  * Copyright (c) 2008-2011, Even Rouault <even dot rouault at mines-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_xplane_fix_reader.h"
31 
32 CPL_CVSID("$Id: ogr_xplane_fix_reader.cpp 27044 2014-03-16 23:41:27Z rouault $");
33 
34 /************************************************************************/
35 /*                   OGRXPlaneCreateFixFileReader                       */
36 /************************************************************************/
37 
OGRXPlaneCreateFixFileReader(OGRXPlaneDataSource * poDataSource)38 OGRXPlaneReader* OGRXPlaneCreateFixFileReader( OGRXPlaneDataSource* poDataSource )
39 {
40     OGRXPlaneReader* poReader = new OGRXPlaneFixReader(poDataSource);
41     return poReader;
42 }
43 
44 
45 /************************************************************************/
46 /*                         OGRXPlaneFixReader()                         */
47 /************************************************************************/
OGRXPlaneFixReader()48 OGRXPlaneFixReader::OGRXPlaneFixReader()
49 {
50     poFIXLayer = NULL;
51 }
52 
53 /************************************************************************/
54 /*                          OGRXPlaneFixReader()                        */
55 /************************************************************************/
56 
OGRXPlaneFixReader(OGRXPlaneDataSource * poDataSource)57 OGRXPlaneFixReader::OGRXPlaneFixReader( OGRXPlaneDataSource* poDataSource )
58 {
59     poFIXLayer = new OGRXPlaneFIXLayer();
60 
61     poDataSource->RegisterLayer(poFIXLayer);
62 }
63 
64 /************************************************************************/
65 /*                        CloneForLayer()                               */
66 /************************************************************************/
67 
CloneForLayer(OGRXPlaneLayer * poLayer)68 OGRXPlaneReader* OGRXPlaneFixReader::CloneForLayer(OGRXPlaneLayer* poLayer)
69 {
70     OGRXPlaneFixReader* poReader = new OGRXPlaneFixReader();
71 
72     poReader->poInterestLayer = poLayer;
73 
74     SET_IF_INTEREST_LAYER(poFIXLayer);
75 
76     if (pszFilename)
77     {
78         poReader->pszFilename = CPLStrdup(pszFilename);
79         poReader->fp = VSIFOpenL( pszFilename, "rt" );
80     }
81 
82     return poReader;
83 }
84 
85 /************************************************************************/
86 /*                         IsRecognizedVersion()                        */
87 /************************************************************************/
88 
IsRecognizedVersion(const char * pszVersionString)89 int OGRXPlaneFixReader::IsRecognizedVersion( const char* pszVersionString)
90 {
91     return EQUALN(pszVersionString, "600 Version", 11);
92 }
93 
94 /************************************************************************/
95 /*                                Read()                                */
96 /************************************************************************/
97 
Read()98 void OGRXPlaneFixReader::Read()
99 {
100     const char* pszLine;
101     while((pszLine = CPLReadLineL(fp)) != NULL)
102     {
103         papszTokens = CSLTokenizeString(pszLine);
104         nTokens = CSLCount(papszTokens);
105 
106         nLineNumber ++;
107 
108         if (nTokens == 1 && strcmp(papszTokens[0], "99") == 0)
109         {
110             CSLDestroy(papszTokens);
111             papszTokens = NULL;
112             bEOF = TRUE;
113             return;
114         }
115         else if (nTokens == 0 || assertMinCol(3) == FALSE)
116         {
117             CSLDestroy(papszTokens);
118             papszTokens = NULL;
119             continue;
120         }
121 
122         ParseRecord();
123 
124         CSLDestroy(papszTokens);
125         papszTokens = NULL;
126 
127         if (poInterestLayer && poInterestLayer->IsEmpty() == FALSE)
128             return;
129     }
130 
131     papszTokens = NULL;
132     bEOF = TRUE;
133 }
134 
135 /************************************************************************/
136 /*                            ParseRecord()                             */
137 /************************************************************************/
138 
ParseRecord()139 void    OGRXPlaneFixReader::ParseRecord()
140 {
141     double dfLat, dfLon;
142     CPLString osName;
143 
144     RET_IF_FAIL(readLatLon(&dfLat, &dfLon, 0));
145     osName = readStringUntilEnd(2);
146 
147     if (poFIXLayer)
148         poFIXLayer->AddFeature(osName, dfLat, dfLon);
149 }
150 
151 
152 /************************************************************************/
153 /*                           OGRXPlaneFIXLayer()                        */
154 /************************************************************************/
155 
OGRXPlaneFIXLayer()156 OGRXPlaneFIXLayer::OGRXPlaneFIXLayer() : OGRXPlaneLayer("FIX")
157 {
158     poFeatureDefn->SetGeomType( wkbPoint );
159 
160     OGRFieldDefn oFieldName("fix_name", OFTString );
161     oFieldName.SetPrecision(5);
162     poFeatureDefn->AddFieldDefn( &oFieldName );
163 }
164 
165 /************************************************************************/
166 /*                           AddFeature()                               */
167 /************************************************************************/
168 
169 OGRFeature*
AddFeature(const char * pszFixName,double dfLat,double dfLon)170      OGRXPlaneFIXLayer::AddFeature(const char* pszFixName,
171                                    double dfLat,
172                                    double dfLon)
173 {
174     int nCount = 0;
175     OGRFeature* poFeature = new OGRFeature(poFeatureDefn);
176     poFeature->SetGeometryDirectly( new OGRPoint( dfLon, dfLat ) );
177     poFeature->SetField( nCount++, pszFixName );
178 
179     RegisterFeature(poFeature);
180 
181     return poFeature;
182 }
183