Line data Source code
1 : /******************************************************************************
2 : * $Id$
3 : *
4 : * Project: VFK Reader
5 : * Purpose: Public Declarations for OGR free VFK Reader code.
6 : * Author: Martin Landa, landa.martin gmail.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2009-2014, Martin Landa <landa.martin gmail.com>
10 : *
11 : * Permission is hereby granted, free of charge, to any person
12 : * obtaining a copy of this software and associated documentation
13 : * files (the "Software"), to deal in the Software without
14 : * restriction, including without limitation the rights to use, copy,
15 : * modify, merge, publish, distribute, sublicense, and/or sell copies
16 : * of the Software, and to permit persons to whom the Software is
17 : * furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be
20 : * included in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 : * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26 : * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 : * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 : * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 : * SOFTWARE.
30 : ****************************************************************************/
31 :
32 : #ifndef GDAL_OGR_VFK_VFKREADER_H_INCLUDED
33 : #define GDAL_OGR_VFK_VFKREADER_H_INCLUDED
34 :
35 : #include <vector>
36 : #include <string>
37 :
38 : #include "ogrsf_frmts.h"
39 :
40 : #include "cpl_port.h"
41 : #include "cpl_minixml.h"
42 : #include "cpl_string.h"
43 :
44 : #include "sqlite3.h"
45 :
46 : class IVFKReader;
47 : class IVFKDataBlock;
48 : class VFKFeature;
49 : class VFKFeatureSQLite;
50 :
51 : typedef std::vector<VFKFeature *> VFKFeatureList;
52 : typedef std::vector<VFKFeatureSQLite *> VFKFeatureSQLiteList;
53 :
54 : #define FID_COLUMN "ogr_fid"
55 : #define GEOM_COLUMN "geometry"
56 : #define FILE_COLUMN "VFK_FILENAME"
57 :
58 : #define VFK_DB_HEADER_TABLE "vfk_header"
59 : #define VFK_DB_TABLE "vfk_tables"
60 : #define VFK_DB_GEOMETRY_TABLE "geometry_columns"
61 : #define VFK_DB_SPATIAL_REF_TABLE "spatial_ref_sys"
62 :
63 : enum RecordType
64 : {
65 : RecordValid,
66 : RecordSkipped,
67 : RecordDuplicated
68 : };
69 :
70 : /************************************************************************/
71 : /* VFKProperty */
72 : /************************************************************************/
73 10920 : class VFKProperty
74 : {
75 : private:
76 : bool m_bIsNull;
77 :
78 : GIntBig m_iValue;
79 : double m_dValue;
80 : CPLString m_strValue;
81 :
82 : public:
83 : VFKProperty();
84 : explicit VFKProperty(int);
85 : explicit VFKProperty(GIntBig);
86 : explicit VFKProperty(double);
87 : explicit VFKProperty(const char *);
88 : explicit VFKProperty(CPLString const &);
89 : virtual ~VFKProperty();
90 :
91 10920 : VFKProperty(VFKProperty const &other) = default;
92 : VFKProperty &operator=(VFKProperty const &) = default;
93 : VFKProperty &operator=(VFKProperty &&) = default;
94 :
95 10920 : bool IsNull() const
96 : {
97 10920 : return m_bIsNull;
98 : }
99 :
100 2205 : int GetValueI() const
101 : {
102 2205 : return static_cast<int>(m_iValue);
103 : }
104 :
105 4305 : GIntBig GetValueI64() const
106 : {
107 4305 : return m_iValue;
108 : }
109 :
110 390 : double GetValueD() const
111 : {
112 390 : return m_dValue;
113 : }
114 :
115 : const char *GetValueS(bool = false) const;
116 : };
117 :
118 : /************************************************************************/
119 : /* IVFKFeature */
120 : /************************************************************************/
121 : class IVFKFeature
122 : {
123 : private:
124 : static double GetDeterminatOfMatrixDim3(double[3], double[3], double[3]);
125 : static void GetCircleCenterFrom3Points(double[2], double[3], double[3]);
126 : static void AddCirclePointsToGeomString(OGRCircularString &, double, double,
127 : double);
128 :
129 : protected:
130 : IVFKDataBlock *m_poDataBlock;
131 : GIntBig m_nFID;
132 : OGRwkbGeometryType m_nGeometryType;
133 : bool m_bGeometry;
134 : bool m_bValid;
135 : std::unique_ptr<OGRGeometry> m_paGeom{};
136 :
137 : virtual bool LoadGeometryPoint() = 0;
138 : virtual bool LoadGeometryLineStringSBP() = 0;
139 : virtual bool LoadGeometryLineStringHP() = 0;
140 : virtual bool LoadGeometryPolygon() = 0;
141 :
142 : public:
143 : explicit IVFKFeature(IVFKDataBlock *);
144 : virtual ~IVFKFeature();
145 :
146 2162 : GIntBig GetFID() const
147 : {
148 2162 : return m_nFID;
149 : }
150 :
151 : void SetFID(GIntBig);
152 : void SetGeometryType(OGRwkbGeometryType);
153 :
154 : bool IsValid() const
155 : {
156 : return m_bValid;
157 : }
158 :
159 : IVFKDataBlock *GetDataBlock() const
160 : {
161 : return m_poDataBlock;
162 : }
163 :
164 61 : OGRwkbGeometryType GetGeometryType() const
165 : {
166 61 : return m_nGeometryType;
167 : }
168 :
169 : bool SetGeometry(const OGRGeometry *, const char * = nullptr);
170 : const OGRGeometry *GetGeometry();
171 :
172 : bool LoadGeometry();
173 : virtual OGRErr LoadProperties(OGRFeature *) = 0;
174 : };
175 :
176 : /************************************************************************/
177 : /* VFKFeature */
178 : /************************************************************************/
179 : class VFKFeature : public IVFKFeature
180 : {
181 : private:
182 : typedef std::vector<VFKProperty> VFKPropertyList;
183 :
184 : VFKPropertyList m_propertyList;
185 :
186 : bool SetProperty(int, const char *);
187 :
188 : friend class VFKFeatureSQLite;
189 :
190 : bool LoadGeometryPoint() override;
191 : bool LoadGeometryLineStringSBP() override;
192 : bool LoadGeometryLineStringHP() override;
193 : bool LoadGeometryPolygon() override;
194 :
195 : public:
196 : VFKFeature(IVFKDataBlock *, GIntBig);
197 :
198 : bool SetProperties(const char *);
199 : const VFKProperty *GetProperty(int) const;
200 : const VFKProperty *GetProperty(const char *) const;
201 :
202 : OGRErr LoadProperties(OGRFeature *) override;
203 :
204 : bool AppendLineToRing(int, const OGRLineString *);
205 : };
206 :
207 : /************************************************************************/
208 : /* VFKFeatureSQLite */
209 : /************************************************************************/
210 : class VFKFeatureSQLite : public IVFKFeature
211 : {
212 : private:
213 : int m_iRowId; /* rowid in DB */
214 : sqlite3_stmt *m_hStmt;
215 :
216 : bool LoadGeometryPoint() override;
217 : bool LoadGeometryLineStringSBP() override;
218 : bool LoadGeometryLineStringHP() override;
219 : bool LoadGeometryPolygon() override;
220 :
221 : OGRErr SetFIDFromDB();
222 : OGRErr ExecuteSQL(const char *);
223 : void FinalizeSQL();
224 :
225 : public:
226 : explicit VFKFeatureSQLite(IVFKDataBlock *);
227 : VFKFeatureSQLite(IVFKDataBlock *, int, GIntBig);
228 : explicit VFKFeatureSQLite(const VFKFeature *);
229 :
230 : OGRErr LoadProperties(OGRFeature *) override;
231 : void SetRowId(int);
232 : };
233 :
234 : /************************************************************************/
235 : /* VFKPropertyDefn */
236 : /************************************************************************/
237 : class VFKPropertyDefn
238 : {
239 : private:
240 : char *m_pszName;
241 :
242 : char *m_pszType;
243 : char *m_pszEncoding;
244 : OGRFieldType m_eFType;
245 :
246 : int m_nWidth;
247 : int m_nPrecision;
248 :
249 : public:
250 : VFKPropertyDefn(const char *, const char *, const char *);
251 : virtual ~VFKPropertyDefn();
252 :
253 21970 : const char *GetName() const
254 : {
255 21970 : return m_pszName;
256 : }
257 :
258 18464 : int GetWidth() const
259 : {
260 18464 : return m_nWidth;
261 : }
262 :
263 9712 : int GetPrecision() const
264 : {
265 9712 : return m_nPrecision;
266 : }
267 :
268 27937 : OGRFieldType GetType() const
269 : {
270 27937 : return m_eFType;
271 : }
272 :
273 : CPLString GetTypeSQL() const;
274 :
275 1320 : const char *GetEncoding() const
276 : {
277 1320 : return m_pszEncoding;
278 : }
279 : };
280 :
281 : /************************************************************************/
282 : /* IVFKDataBlock */
283 : /************************************************************************/
284 : class IVFKDataBlock
285 : {
286 : private:
287 : IVFKFeature **m_papoFeature;
288 :
289 : int m_nPropertyCount;
290 : VFKPropertyDefn **m_papoProperty;
291 :
292 : int AddProperty(const char *, const char *);
293 :
294 : protected:
295 : typedef std::vector<OGRPoint> PointList;
296 : typedef std::vector<PointList *> PointListArray;
297 :
298 : char *m_pszName;
299 : bool m_bGeometry;
300 :
301 : OGRwkbGeometryType m_nGeometryType;
302 : bool m_bGeometryPerBlock;
303 :
304 : int m_nFeatureCount;
305 : int m_iNextFeature;
306 :
307 : // TODO: Make m_poReader const.
308 : IVFKReader *m_poReader;
309 :
310 : GIntBig m_nRecordCount[3];
311 :
312 : bool AppendLineToRing(PointListArray *, const OGRLineString *, bool,
313 : bool = false);
314 : int LoadData();
315 :
316 : virtual int LoadGeometryPoint() = 0;
317 : virtual int LoadGeometryLineStringSBP() = 0;
318 : virtual int LoadGeometryLineStringHP() = 0;
319 : virtual int LoadGeometryPolygon() = 0;
320 :
321 : static void FillPointList(PointList *poList, const OGRLineString *poLine);
322 :
323 : public:
324 : IVFKDataBlock(const char *, const IVFKReader *);
325 : virtual ~IVFKDataBlock();
326 :
327 70847 : const char *GetName() const
328 : {
329 70847 : return m_pszName;
330 : }
331 :
332 56652 : int GetPropertyCount() const
333 : {
334 56652 : return m_nPropertyCount;
335 : }
336 :
337 : VFKPropertyDefn *GetProperty(int) const;
338 : void SetProperties(const char *);
339 : int GetPropertyIndex(const char *) const;
340 :
341 : GIntBig GetFeatureCount(bool = true);
342 : void SetFeatureCount(int, bool = false);
343 : IVFKFeature *GetFeatureByIndex(int) const;
344 : IVFKFeature *GetFeature(GIntBig);
345 : void AddFeature(IVFKFeature *);
346 :
347 : void ResetReading(int iIdx = -1);
348 : IVFKFeature *GetNextFeature();
349 : IVFKFeature *GetPreviousFeature();
350 : IVFKFeature *GetFirstFeature();
351 : IVFKFeature *GetLastFeature();
352 : int SetNextFeature(const IVFKFeature *);
353 :
354 : OGRwkbGeometryType SetGeometryType(bool = false);
355 : OGRwkbGeometryType GetGeometryType() const;
356 :
357 : int LoadGeometry();
358 :
359 : virtual OGRErr LoadProperties() = 0;
360 : virtual OGRErr CleanProperties() = 0;
361 :
362 1039 : IVFKReader *GetReader() const
363 : {
364 1039 : return m_poReader;
365 : }
366 :
367 : int GetRecordCount(RecordType = RecordValid) const;
368 : void SetIncRecordCount(RecordType);
369 : };
370 :
371 : /************************************************************************/
372 : /* VFKDataBlock */
373 : /************************************************************************/
374 : class VFKDataBlock : public IVFKDataBlock
375 : {
376 : private:
377 : int LoadGeometryPoint() override;
378 : int LoadGeometryLineStringSBP() override;
379 : int LoadGeometryLineStringHP() override;
380 : int LoadGeometryPolygon() override;
381 :
382 : public:
383 0 : VFKDataBlock(const char *pszName, const IVFKReader *poReader)
384 0 : : IVFKDataBlock(pszName, poReader)
385 : {
386 0 : }
387 :
388 : VFKFeature *GetFeature(int, GUIntBig, VFKFeatureList *poList = nullptr);
389 : VFKFeatureList GetFeatures(int, GUIntBig);
390 : VFKFeatureList GetFeatures(int, int, GUIntBig);
391 :
392 : GIntBig GetFeatureCount(const char *, const char *);
393 :
394 0 : OGRErr LoadProperties() override
395 : {
396 0 : return OGRERR_UNSUPPORTED_OPERATION;
397 : }
398 :
399 0 : OGRErr CleanProperties() override
400 : {
401 0 : return OGRERR_UNSUPPORTED_OPERATION;
402 : }
403 : };
404 :
405 : /************************************************************************/
406 : /* VFKDataBlockSQLite */
407 : /************************************************************************/
408 : class VFKDataBlockSQLite : public IVFKDataBlock
409 : {
410 : private:
411 : sqlite3_stmt *m_hStmt;
412 :
413 : bool SetGeometryLineString(VFKFeatureSQLite *, OGRLineString *, bool &,
414 : const char *, std::vector<int> &, int &);
415 :
416 : int LoadGeometryPoint() override;
417 : int LoadGeometryLineStringSBP() override;
418 : int LoadGeometryLineStringHP() override;
419 : int LoadGeometryPolygon() override;
420 :
421 : bool LoadGeometryFromDB();
422 : OGRErr SaveGeometryToDB(const OGRGeometry *, int);
423 :
424 : OGRErr LoadProperties() override;
425 : OGRErr CleanProperties() override;
426 :
427 : static bool IsRingClosed(const OGRLinearRing *);
428 : void UpdateVfkBlocks(int);
429 : void UpdateFID(GIntBig, const std::vector<int> &);
430 :
431 : friend class VFKFeatureSQLite;
432 :
433 : public:
434 : VFKDataBlockSQLite(const char *, const IVFKReader *);
435 :
436 : const char *GetKey() const;
437 : IVFKFeature *GetFeature(GIntBig);
438 : VFKFeatureSQLite *GetFeature(const char *, GUIntBig, bool = false);
439 : VFKFeatureSQLite *GetFeature(const char **, GUIntBig *, int, bool = false);
440 : VFKFeatureSQLiteList GetFeatures(const char **, GUIntBig *, int);
441 :
442 : int GetGeometrySQLType() const;
443 :
444 : OGRErr AddGeometryColumn() const;
445 : };
446 :
447 : /************************************************************************/
448 : /* IVFKReader */
449 : /************************************************************************/
450 : class IVFKReader
451 : {
452 : private:
453 : virtual void AddInfo(const char *) = 0;
454 :
455 : protected:
456 : virtual IVFKDataBlock *CreateDataBlock(const char *) = 0;
457 : virtual void AddDataBlock(IVFKDataBlock * = nullptr,
458 : const char * = nullptr) = 0;
459 : virtual OGRErr AddFeature(IVFKDataBlock * = nullptr,
460 : VFKFeature * = nullptr) = 0;
461 :
462 : public:
463 : virtual ~IVFKReader();
464 :
465 : virtual const char *GetFilename() const = 0;
466 :
467 : virtual const char *GetEncoding() const = 0;
468 : virtual bool IsSpatial() const = 0;
469 : virtual bool IsPreProcessed() const = 0;
470 : virtual bool IsValid() const = 0;
471 : virtual bool HasFileField() const = 0;
472 : virtual int ReadDataBlocks(bool = false) = 0;
473 : virtual int ReadDataRecords(IVFKDataBlock * = nullptr) = 0;
474 : virtual int LoadGeometry() = 0;
475 :
476 : virtual int GetDataBlockCount() const = 0;
477 : virtual IVFKDataBlock *GetDataBlock(int) const = 0;
478 : virtual IVFKDataBlock *GetDataBlock(const char *) const = 0;
479 :
480 : virtual const char *GetInfo(const char *) = 0;
481 : };
482 :
483 : IVFKReader *CreateVFKReader(const GDALOpenInfo *);
484 :
485 : #endif // GDAL_OGR_VFK_VFKREADER_H_INCLUDED
|