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 3 : void ResetReading ( ) { iFeature = 0; nFID = 1; };
92 : OGRFeature *GetNextFeature ( );
93 : OGRFeature *GetNextRawFeature ( );
94 9 : OGRFeatureDefn *GetLayerDefn ( ) { return m_poOgrFeatureDefn; };
95 : //OGRErr SetAttributeFilter (const char * );
96 : OGRErr CreateFeature ( OGRFeature * poOgrFeat );
97 :
98 0 : 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 35 : const char *GetName( ) { return m_pszName; };
116 : int TestCapability ( const char * );
117 0 : ContainerPtr GetKmlLayer () { return m_poKmlLayer; };
118 0 : ElementPtr GetKmlLayerRoot () { return m_poKmlLayerRoot; };
119 11 : SchemaPtr GetKmlSchema () { return m_poKmlSchema; };
120 0 : const char *GetFileName ( ) { return m_pszFileName; };
121 : };
122 :
123 : /******************************************************************************
124 : datasource class
125 : ******************************************************************************/
126 :
127 : class OGRLIBKMLDataSource:public OGRDataSource
128 : {
129 : char *pszName;
130 :
131 : /***** layers *****/
132 :
133 : OGRLIBKMLLayer **papoLayers;
134 : int nLayers;
135 : int nAlloced;
136 :
137 :
138 : int bUpdate;
139 : int bUpdated;
140 :
141 : /***** for kml files *****/
142 : int m_isKml;
143 : KmlPtr m_poKmlDSKml;
144 : ContainerPtr m_poKmlDSContainer;
145 :
146 : /***** for kmz files *****/
147 :
148 : int m_isKmz;
149 : ContainerPtr m_poKmlDocKml;
150 : ElementPtr m_poKmlDocKmlRoot;
151 : ContainerPtr m_poKmlStyleKml;
152 : const char *pszStylePath;
153 :
154 : /***** for dir *****/
155 :
156 : int m_isDir;
157 :
158 : /***** the kml factory *****/
159 :
160 : KmlFactory *m_poKmlFactory;
161 :
162 : /***** style table pointer *****/
163 :
164 : //OGRStyleTable *m_poStyleTable;
165 :
166 : public:
167 : OGRLIBKMLDataSource ( KmlFactory *poKmlFactory );
168 : ~OGRLIBKMLDataSource ( );
169 :
170 4 : const char *GetName ( ) { return pszName; };
171 :
172 1 : int GetLayerCount ( ) { return nLayers; }
173 : OGRLayer *GetLayer ( int );
174 : OGRLayer *GetLayerByName ( const char * );
175 : OGRErr DeleteLayer ( int );
176 :
177 :
178 : OGRLayer *CreateLayer ( const char *pszName,
179 : OGRSpatialReference * poSpatialRef = NULL,
180 : OGRwkbGeometryType eGType = wkbUnknown,
181 : char **papszOptions = NULL );
182 :
183 : OGRStyleTable *GetStyleTable ( );
184 : void SetStyleTableDirectly ( OGRStyleTable * poStyleTable );
185 : void SetStyleTable ( OGRStyleTable * poStyleTable );
186 :
187 : int Open ( const char *pszFilename,
188 : int bUpdate );
189 : int Create ( const char *pszFilename,
190 : char **papszOptions );
191 :
192 : OGRErr SyncToDisk ( );
193 : int TestCapability (const char * );
194 :
195 13 : KmlFactory *GetKmlFactory() { return m_poKmlFactory; };
196 30 : const char *GetStylePath() {return pszStylePath; };
197 : //KmzFile *GetKmz() { return m_poKmlKmzfile; };
198 :
199 3 : int IsKml() {return m_isKml;};
200 0 : int IsKmz() {return m_isKmz;};
201 0 : int IsDir() {return m_isDir;};
202 :
203 11 : void Updated() {bUpdated = TRUE;};
204 :
205 : int ParseLayers ( ContainerPtr poKmlContainer,
206 : OGRSpatialReference *poOgrSRS );
207 : SchemaPtr FindSchema ( const char *pszSchemaUrl);
208 :
209 : private:
210 :
211 : /***** methods to write out various datasource types at destroy *****/
212 :
213 : void WriteKml();
214 : void WriteKmz();
215 : void WriteDir();
216 :
217 : /***** methods to open various datasource types *****/
218 :
219 : int OpenKmz ( const char *pszFilename,
220 : int bUpdate );
221 : int OpenKml ( const char *pszFilename,
222 : int bUpdate );
223 : int OpenDir ( const char *pszFilename,
224 : int bUpdate );
225 :
226 : /***** methods to create various datasource types *****/
227 :
228 : int CreateKml ( const char *pszFilename,
229 : char **papszOptions );
230 : int CreateKmz ( const char *pszFilename,
231 : char **papszOptions );
232 : int CreateDir ( const char *pszFilename,
233 : char **papszOptions );
234 :
235 : /***** methods to create layers on various datasource types *****/
236 :
237 : OGRLayer *CreateLayerKml ( const char *pszLayerName,
238 : OGRSpatialReference * poOgrSRS,
239 : OGRwkbGeometryType eGType,
240 : char **papszOptions );
241 : OGRLayer *CreateLayerKmz ( const char *pszLayerName,
242 : OGRSpatialReference * poOgrSRS,
243 : OGRwkbGeometryType eGType,
244 : char **papszOptions );
245 :
246 : /***** methods to delete layers on various datasource types *****/
247 :
248 : OGRErr DeleteLayerKml ( int );
249 : OGRErr DeleteLayerKmz ( int );
250 :
251 : /***** methods to write a styletable to various datasource types *****/
252 :
253 : void SetStyleTable2Kml ( OGRStyleTable * poStyleTable );
254 : void SetStyleTable2Kmz ( OGRStyleTable * poStyleTable );
255 :
256 :
257 :
258 :
259 : OGRLIBKMLLayer *AddLayer ( const char *pszLayerName,
260 : OGRSpatialReference * poSpatialRef,
261 : OGRwkbGeometryType eGType,
262 : OGRLIBKMLDataSource * poOgrDS,
263 : ElementPtr poKmlRoot,
264 : ContainerPtr poKmlContainer,
265 : const char *pszFileName,
266 : int bNew,
267 : int bUpdate,
268 : int nGuess);
269 : };
270 :
271 :
272 : /******************************************************************************
273 : driver class
274 : ******************************************************************************/
275 :
276 : class OGRLIBKMLDriver:public OGRSFDriver
277 : {
278 : int bUpdate;
279 : KmlFactory *m_poKmlFactory;
280 :
281 : public:
282 : OGRLIBKMLDriver ( );
283 : ~OGRLIBKMLDriver ( );
284 :
285 : const char *GetName ( );
286 : OGRDataSource *Open ( const char *pszFilename,
287 : int bUpdate );
288 : OGRDataSource *CreateDataSource ( const char *pszFilename,
289 : char **papszOptions );
290 :
291 : OGRErr DeleteDataSource ( const char *pszName );
292 :
293 : int TestCapability ( const char * );
294 : };
295 :
296 : #endif
|