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