1 : /******************************************************************************
2 : *
3 : * Project: KML Translator
4 : * Purpose: Implements OGRLIBKMLDriver
5 : * Author: Brian Case, rush at winkey dot org
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2010, Brian Case
9 : *
10 : * Permission is hereby granted, free of charge, to any person obtaining a
11 : * copy of this software and associated documentation files (the "Software"),
12 : * to deal in the Software without restriction, including without limitation
13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 : * and/or sell copies of the Software, and to permit persons to whom the
15 : * Software is furnished to do so, subject to the following conditions:
16 : *
17 : * The above copyright notice and this permission notice shall be included
18 : * in all copies or substantial portions of the Software.
19 : *
20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 : * DEALINGS IN THE SOFTWARE.
27 : *****************************************************************************/
28 :
29 : #ifndef HAVE_OGR_LIBKML_H
30 : #define HAVE_OGR_LIBKML_H
31 :
32 : #include "ogrsf_frmts.h"
33 :
34 : #include <kml/engine.h>
35 : #include <kml/dom.h>
36 :
37 :
38 : using kmldom::KmlFactory;
39 : using kmldom::KmlPtr;
40 : using kmldom::DocumentPtr;
41 : using kmldom::ContainerPtr;
42 : using kmldom::ElementPtr;
43 : using kmldom::SchemaPtr;
44 : using kmlengine::KmzFile;
45 : using kmlengine::KmzFilePtr;
46 :
47 : using kmlengine::KmlFile;
48 : using kmlengine::KmlFilePtr;
49 :
50 : class OGRLIBKMLDataSource;
51 :
52 : /******************************************************************************
53 : layer class
54 : ******************************************************************************/
55 :
56 : class OGRLIBKMLLayer:public OGRLayer
57 : {
58 : OGRFeatureDefn *poFeatureDefn;
59 :
60 : FILE fp;
61 : int bUpdate;
62 : int bUpdated;
63 : int nFeatures;
64 : int iFeature;
65 : long nFID;
66 : const char *m_pszName;
67 : const char *m_pszFileName;
68 :
69 : ContainerPtr m_poKmlLayer;
70 : ElementPtr m_poKmlLayerRoot;
71 : //KmlFile *m_poKmlKmlfile;
72 :
73 : DocumentPtr m_poKmlDocument;
74 : //OGRStyleTable *m_poStyleTable;
75 : OGRLIBKMLDataSource *m_poOgrDS;
76 : OGRFeatureDefn *m_poOgrFeatureDefn;
77 : SchemaPtr m_poKmlSchema;
78 : OGRSpatialReference *m_poOgrSRS;
79 : public:
80 : OGRLIBKMLLayer ( const char *pszLayerName,
81 : OGRSpatialReference * poSpatialRef,
82 : OGRwkbGeometryType eGType,
83 : OGRLIBKMLDataSource *poOgrDS,
84 : ElementPtr poKmlRoot,
85 : ContainerPtr poKmlContainer,
86 : const char *pszFileName,
87 : int bNew,
88 : int bUpdate);
89 : ~OGRLIBKMLLayer ( );
90 :
91 400 : void ResetReading ( ) { iFeature = 0; nFID = 1; };
92 : OGRFeature *GetNextFeature ( );
93 : OGRFeature *GetNextRawFeature ( );
94 742 : OGRFeatureDefn *GetLayerDefn ( ) { return m_poOgrFeatureDefn; };
95 : //OGRErr SetAttributeFilter (const char * );
96 : OGRErr CreateFeature ( OGRFeature * poOgrFeat );
97 :
98 20 : OGRSpatialReference *GetSpatialRef ( ) { return m_poOgrSRS; };
99 :
100 : int GetFeatureCount ( int bForce = TRUE );
101 : OGRErr GetExtent ( OGREnvelope * psExtent,
102 : int bForce = TRUE );
103 :
104 :
105 : //const char *GetInfo ( const char * );
106 :
107 : OGRErr CreateField ( OGRFieldDefn * poField,
108 : int bApproxOK = TRUE );
109 :
110 : OGRErr SyncToDisk ( );
111 :
112 : OGRStyleTable *GetStyleTable ( );
113 : void SetStyleTableDirectly ( OGRStyleTable * poStyleTable );
114 : void SetStyleTable ( OGRStyleTable * poStyleTable );
115 462 : const char *GetName( ) { return m_pszName; };
116 : int TestCapability ( const char * );
117 8 : ContainerPtr GetKmlLayer () { return m_poKmlLayer; };
118 8 : ElementPtr GetKmlLayerRoot () { return m_poKmlLayerRoot; };
119 66 : SchemaPtr GetKmlSchema () { return m_poKmlSchema; };
120 8 : const char *GetFileName ( ) { return m_pszFileName; };
121 :
122 :
123 : static CPLString LaunderFieldNames(CPLString osName);
124 :
125 : };
126 :
127 : /******************************************************************************
128 : datasource class
129 : ******************************************************************************/
130 :
131 : class OGRLIBKMLDataSource:public OGRDataSource
132 : {
133 : char *pszName;
134 :
135 : /***** layers *****/
136 :
137 : OGRLIBKMLLayer **papoLayers;
138 : int nLayers;
139 : int nAlloced;
140 :
141 :
142 : int bUpdate;
143 : int bUpdated;
144 :
145 : /***** for kml files *****/
146 : int m_isKml;
147 : KmlPtr m_poKmlDSKml;
148 : ContainerPtr m_poKmlDSContainer;
149 :
150 : /***** for kmz files *****/
151 :
152 : int m_isKmz;
153 : ContainerPtr m_poKmlDocKml;
154 : ElementPtr m_poKmlDocKmlRoot;
155 : ContainerPtr m_poKmlStyleKml;
156 : char *pszStylePath;
157 :
158 : /***** for dir *****/
159 :
160 : int m_isDir;
161 :
162 : /***** the kml factory *****/
163 :
164 : KmlFactory *m_poKmlFactory;
165 :
166 : /***** style table pointer *****/
167 :
168 : //OGRStyleTable *m_poStyleTable;
169 :
170 : public:
171 : OGRLIBKMLDataSource ( KmlFactory *poKmlFactory );
172 : ~OGRLIBKMLDataSource ( );
173 :
174 26 : const char *GetName ( ) { return pszName; };
175 :
176 30 : int GetLayerCount ( ) { return nLayers; }
177 : OGRLayer *GetLayer ( int );
178 : OGRLayer *GetLayerByName ( const char * );
179 : OGRErr DeleteLayer ( int );
180 :
181 :
182 : OGRLayer *CreateLayer ( const char *pszName,
183 : OGRSpatialReference * poSpatialRef = NULL,
184 : OGRwkbGeometryType eGType = wkbUnknown,
185 : char **papszOptions = NULL );
186 :
187 : OGRStyleTable *GetStyleTable ( );
188 : void SetStyleTableDirectly ( OGRStyleTable * poStyleTable );
189 : void SetStyleTable ( OGRStyleTable * poStyleTable );
190 :
191 : int Open ( const char *pszFilename,
192 : int bUpdate );
193 : int Create ( const char *pszFilename,
194 : char **papszOptions );
195 :
196 : OGRErr SyncToDisk ( );
197 : int TestCapability (const char * );
198 :
199 84 : KmlFactory *GetKmlFactory() { return m_poKmlFactory; };
200 :
201 296 : const char *GetStylePath() {return pszStylePath; };
202 : int ParseIntoStyleTable ( std::string * oKmlStyleKml,
203 : const char *pszStylePath);
204 :
205 : //KmzFile *GetKmz() { return m_poKmlKmzfile; };
206 :
207 20 : int IsKml() {return m_isKml;};
208 12 : int IsKmz() {return m_isKmz;};
209 6 : int IsDir() {return m_isDir;};
210 :
211 72 : void Updated() {bUpdated = TRUE;};
212 :
213 : int ParseLayers ( ContainerPtr poKmlContainer,
214 : OGRSpatialReference *poOgrSRS );
215 : SchemaPtr FindSchema ( const char *pszSchemaUrl);
216 :
217 : private:
218 :
219 : /***** methods to write out various datasource types at destroy *****/
220 :
221 : void WriteKml();
222 : void WriteKmz();
223 : void WriteDir();
224 :
225 : /***** methods to open various datasource types *****/
226 :
227 : int OpenKmz ( const char *pszFilename,
228 : int bUpdate );
229 : int OpenKml ( const char *pszFilename,
230 : int bUpdate );
231 : int OpenDir ( const char *pszFilename,
232 : int bUpdate );
233 :
234 : /***** methods to create various datasource types *****/
235 :
236 : int CreateKml ( const char *pszFilename,
237 : char **papszOptions );
238 : int CreateKmz ( const char *pszFilename,
239 : char **papszOptions );
240 : int CreateDir ( const char *pszFilename,
241 : char **papszOptions );
242 :
243 : /***** methods to create layers on various datasource types *****/
244 :
245 : OGRLayer *CreateLayerKml ( const char *pszLayerName,
246 : OGRSpatialReference * poOgrSRS,
247 : OGRwkbGeometryType eGType,
248 : char **papszOptions );
249 : OGRLayer *CreateLayerKmz ( const char *pszLayerName,
250 : OGRSpatialReference * poOgrSRS,
251 : OGRwkbGeometryType eGType,
252 : char **papszOptions );
253 :
254 : /***** methods to delete layers on various datasource types *****/
255 :
256 : OGRErr DeleteLayerKml ( int );
257 : OGRErr DeleteLayerKmz ( int );
258 :
259 : /***** methods to write a styletable to various datasource types *****/
260 :
261 : void SetStyleTable2Kml ( OGRStyleTable * poStyleTable );
262 : void SetStyleTable2Kmz ( OGRStyleTable * poStyleTable );
263 :
264 :
265 :
266 :
267 : OGRLIBKMLLayer *AddLayer ( const char *pszLayerName,
268 : OGRSpatialReference * poSpatialRef,
269 : OGRwkbGeometryType eGType,
270 : OGRLIBKMLDataSource * poOgrDS,
271 : ElementPtr poKmlRoot,
272 : ContainerPtr poKmlContainer,
273 : const char *pszFileName,
274 : int bNew,
275 : int bUpdate,
276 : int nGuess);
277 : };
278 :
279 :
280 : /******************************************************************************
281 : driver class
282 : ******************************************************************************/
283 :
284 : class OGRLIBKMLDriver:public OGRSFDriver
285 : {
286 : KmlFactory *m_poKmlFactory;
287 :
288 : public:
289 : OGRLIBKMLDriver ( );
290 : ~OGRLIBKMLDriver ( );
291 :
292 : const char *GetName ( );
293 : OGRDataSource *Open ( const char *pszFilename,
294 : int bUpdate );
295 : OGRDataSource *CreateDataSource ( const char *pszFilename,
296 : char **papszOptions );
297 :
298 : OGRErr DeleteDataSource ( const char *pszName );
299 :
300 : int TestCapability ( const char * );
301 : };
302 :
303 : #endif
|