1 : /******************************************************************************
2 : * $Id: ogr_gpx.h 20996 2010-10-28 18:38:15Z rouault $
3 : *
4 : * Project: GPX Translator
5 : * Purpose: Definition of classes for OGR .gpx driver.
6 : * Author: Even Rouault, even dot rouault at mines dash paris dot org
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2007, 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 : #ifndef _OGR_GPX_H_INCLUDED
31 : #define _OGR_GPX_H_INCLUDED
32 :
33 : #include "ogrsf_frmts.h"
34 :
35 : #ifdef HAVE_EXPAT
36 : #include "ogr_expat.h"
37 : #endif
38 :
39 : class OGRGPXDataSource;
40 :
41 :
42 : typedef enum
43 : {
44 : GPX_NONE,
45 : GPX_WPT,
46 : GPX_TRACK,
47 : GPX_ROUTE,
48 : GPX_ROUTE_POINT,
49 : GPX_TRACK_POINT,
50 : } GPXGeometryType;
51 :
52 : /************************************************************************/
53 : /* OGRGPXLayer */
54 : /************************************************************************/
55 :
56 : class OGRGPXLayer : public OGRLayer
57 : {
58 : OGRFeatureDefn* poFeatureDefn;
59 : OGRSpatialReference *poSRS;
60 : OGRGPXDataSource* poDS;
61 :
62 : GPXGeometryType gpxGeomType;
63 :
64 : int nGPXFields;
65 :
66 : int bWriteMode;
67 : int nFeatures;
68 : int eof;
69 : int nNextFID;
70 : VSILFILE* fpGPX; /* Large file API */
71 : const char* pszElementToScan;
72 : #ifdef HAVE_EXPAT
73 : XML_Parser oParser;
74 : XML_Parser oSchemaParser;
75 : #endif
76 : int doParse;
77 : int inInterestingElement;
78 : int hasFoundLat;
79 : int hasFoundLon;
80 : double latVal;
81 : double lonVal;
82 : char* pszSubElementName;
83 : char* pszSubElementValue;
84 : int nSubElementValueLen;
85 : int iCurrentField;
86 :
87 : OGRFeature* poFeature;
88 : OGRFeature ** ppoFeatureTab;
89 : int nFeatureTabLength;
90 : int nFeatureTabIndex;
91 :
92 : OGRMultiLineString* multiLineString;
93 : OGRLineString* lineString;
94 :
95 : int depthLevel;
96 : int interestingDepthLevel;
97 :
98 : OGRFieldDefn* currentFieldDefn;
99 : int inExtensions;
100 : int extensionsDepthLevel;
101 :
102 : int inLink;
103 : int iCountLink;
104 : int nMaxLinks;
105 :
106 : int bEleAs25D;
107 :
108 : int trkFID;
109 : int trkSegId;
110 : int trkSegPtId;
111 :
112 : int rteFID;
113 : int rtePtId;
114 :
115 : int bStopParsing;
116 : int nWithoutEventCounter;
117 : int nDataHandlerCounter;
118 :
119 : int iFirstGPXField;
120 :
121 : private:
122 : void WriteFeatureAttributes( OGRFeature *poFeature, int nIdentLevel = 1 );
123 : void LoadExtensionsSchema();
124 : #ifdef HAVE_EXPAT
125 : void AddStrToSubElementValue(const char* pszStr);
126 : #endif
127 : int OGRGPX_WriteXMLExtension(const char* pszTagName,
128 : const char* pszContent);
129 :
130 : public:
131 : OGRGPXLayer(const char *pszFilename,
132 : const char* layerName,
133 : GPXGeometryType gpxGeomType,
134 : OGRGPXDataSource* poDS,
135 : int bWriteMode = FALSE);
136 : ~OGRGPXLayer();
137 :
138 : void ResetReading();
139 : OGRFeature * GetNextFeature();
140 :
141 : OGRErr CreateFeature( OGRFeature *poFeature );
142 : OGRErr CreateField( OGRFieldDefn *poField, int bApproxOK );
143 :
144 188 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
145 :
146 : int TestCapability( const char * );
147 :
148 : OGRSpatialReference *GetSpatialRef();
149 :
150 : #ifdef HAVE_EXPAT
151 : void startElementCbk(const char *pszName, const char **ppszAttr);
152 : void endElementCbk(const char *pszName);
153 : void dataHandlerCbk(const char *data, int nLen);
154 :
155 : void startElementLoadSchemaCbk(const char *pszName, const char **ppszAttr);
156 : void endElementLoadSchemaCbk(const char *pszName);
157 : void dataHandlerLoadSchemaCbk(const char *data, int nLen);
158 : #endif
159 :
160 : static OGRErr CheckAndFixCoordinatesValidity( double* pdfLatitude, double* pdfLongitude );
161 : };
162 :
163 : /************************************************************************/
164 : /* OGRGPXDataSource */
165 : /************************************************************************/
166 :
167 : typedef enum
168 : {
169 : GPX_VALIDITY_UNKNOWN,
170 : GPX_VALIDITY_INVALID,
171 : GPX_VALIDITY_VALID
172 : } OGRGPXValidity;
173 :
174 : class OGRGPXDataSource : public OGRDataSource
175 : {
176 : char* pszName;
177 :
178 : OGRGPXLayer** papoLayers;
179 : int nLayers;
180 :
181 : /* Export related */
182 : VSILFILE *fpOutput; /* Large file API */
183 : int bIsBackSeekable;
184 : const char *pszEOL;
185 : int nOffsetBounds;
186 : double dfMinLat, dfMinLon, dfMaxLat, dfMaxLon;
187 :
188 : GPXGeometryType lastGPXGeomTypeWritten;
189 :
190 : int bUseExtensions;
191 : char* pszExtensionsNS;
192 :
193 : OGRGPXValidity validity;
194 : int nElementsRead;
195 : char* pszVersion;
196 : #ifdef HAVE_EXPAT
197 : XML_Parser oCurrentParser;
198 : int nDataHandlerCounter;
199 : #endif
200 :
201 : public:
202 : OGRGPXDataSource();
203 : ~OGRGPXDataSource();
204 :
205 : int nLastRteId;
206 : int nLastTrkId;
207 : int nLastTrkSegId;
208 :
209 : int Open( const char * pszFilename,
210 : int bUpdate );
211 :
212 : int Create( const char *pszFilename,
213 : char **papszOptions );
214 :
215 14 : const char* GetName() { return pszName; }
216 :
217 118 : int GetLayerCount() { return nLayers; }
218 : OGRLayer* GetLayer( int );
219 :
220 : OGRLayer * CreateLayer( const char * pszLayerName,
221 : OGRSpatialReference *poSRS,
222 : OGRwkbGeometryType eType,
223 : char ** papszOptions );
224 :
225 : int TestCapability( const char * );
226 :
227 72 : VSILFILE * GetOutputFP() { return fpOutput; }
228 36 : void SetLastGPXGeomTypeWritten(GPXGeometryType gpxGeomType)
229 36 : { lastGPXGeomTypeWritten = gpxGeomType; }
230 66 : GPXGeometryType GetLastGPXGeomTypeWritten() { return lastGPXGeomTypeWritten; }
231 :
232 100 : int GetUseExtensions() { return bUseExtensions; }
233 4 : const char* GetExtensionsNS() { return pszExtensionsNS; }
234 :
235 : #ifdef HAVE_EXPAT
236 : void startElementValidateCbk(const char *pszName, const char **ppszAttr);
237 : void dataHandlerValidateCbk(const char *data, int nLen);
238 : #endif
239 :
240 74 : const char* GetVersion() { return pszVersion; }
241 :
242 : void AddCoord(double dfLon, double dfLat);
243 :
244 : void PrintLine(const char *fmt, ...) CPL_PRINT_FUNC_FORMAT (2, 3);
245 : };
246 :
247 : /************************************************************************/
248 : /* OGRGPXDriver */
249 : /************************************************************************/
250 :
251 : class OGRGPXDriver : public OGRSFDriver
252 389 : {
253 : public:
254 : ~OGRGPXDriver();
255 :
256 : const char* GetName();
257 : OGRDataSource* Open( const char *, int );
258 : OGRDataSource* CreateDataSource( const char * pszName, char **papszOptions );
259 : int DeleteDataSource( const char *pszFilename );
260 : int TestCapability( const char * );
261 :
262 : };
263 :
264 :
265 : #endif /* ndef _OGR_GPX_H_INCLUDED */
|