1 : /******************************************************************************
2 : * $Id: vfkreader.h 25721 2013-03-09 16:21:46Z martinl $
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-2010, 2012-2013, 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 :
57 : /************************************************************************/
58 : /* VFKProperty */
59 : /************************************************************************/
60 : class CPL_DLL VFKProperty
61 : {
62 : private:
63 : bool m_bIsNull;
64 :
65 : int m_nValue;
66 : double m_dValue;
67 : CPLString m_strValue;
68 :
69 : public:
70 : VFKProperty();
71 : explicit VFKProperty(int);
72 : explicit VFKProperty(double);
73 : explicit VFKProperty(const char*);
74 : explicit VFKProperty(CPLString const&);
75 : virtual ~VFKProperty();
76 :
77 : VFKProperty(VFKProperty const& other);
78 : VFKProperty& operator=(VFKProperty const& other);
79 :
80 652 : bool IsNull() const { return m_bIsNull; }
81 139 : int GetValueI() const { return m_nValue; }
82 26 : double GetValueD() const { return m_dValue; }
83 307 : const char *GetValueS() const { return m_strValue.c_str(); }
84 : };
85 :
86 : /************************************************************************/
87 : /* IVFKFeature */
88 : /************************************************************************/
89 : class CPL_DLL IVFKFeature
90 : {
91 : protected:
92 : IVFKDataBlock *m_poDataBlock;
93 : long m_nFID;
94 : OGRwkbGeometryType m_nGeometryType;
95 : bool m_bGeometry;
96 : bool m_bValid;
97 : OGRGeometry *m_paGeom;
98 :
99 : virtual bool LoadGeometryPoint() = 0;
100 : virtual bool LoadGeometryLineStringSBP() = 0;
101 : virtual bool LoadGeometryLineStringHP() = 0;
102 : virtual bool LoadGeometryPolygon() = 0;
103 :
104 : public:
105 : IVFKFeature(IVFKDataBlock *);
106 : virtual ~IVFKFeature();
107 :
108 189 : long GetFID() const { return m_nFID; }
109 : void SetFID(long);
110 : void SetGeometryType(OGRwkbGeometryType);
111 :
112 : bool IsValid() const { return m_bValid; }
113 :
114 : IVFKDataBlock *GetDataBlock() const { return m_poDataBlock; }
115 29 : OGRwkbGeometryType GetGeometryType() const { return m_nGeometryType; }
116 : bool SetGeometry(OGRGeometry *);
117 : OGRGeometry *GetGeometry();
118 :
119 : bool LoadGeometry();
120 : virtual OGRErr LoadProperties(OGRFeature *) = 0;
121 : };
122 :
123 : /************************************************************************/
124 : /* VFKFeature */
125 : /************************************************************************/
126 : class CPL_DLL VFKFeature : public IVFKFeature
127 53 : {
128 : private:
129 : typedef std::vector<VFKProperty> VFKPropertyList;
130 :
131 : VFKPropertyList m_propertyList;
132 :
133 : bool SetProperty(int, const char *);
134 :
135 : friend class VFKFeatureSQLite;
136 :
137 : bool LoadGeometryPoint();
138 : bool LoadGeometryLineStringSBP();
139 : bool LoadGeometryLineStringHP();
140 : bool LoadGeometryPolygon();
141 :
142 : public:
143 : VFKFeature(IVFKDataBlock *, long);
144 :
145 : bool SetProperties(const char *);
146 : const VFKProperty *GetProperty(int) const;
147 : const VFKProperty *GetProperty(const char *) const;
148 :
149 : OGRErr LoadProperties(OGRFeature *);
150 :
151 : bool AppendLineToRing(int, const OGRLineString *);
152 : };
153 :
154 : /************************************************************************/
155 : /* VFKFeatureSQLite */
156 : /************************************************************************/
157 : class CPL_DLL VFKFeatureSQLite : public IVFKFeature
158 80 : {
159 : private:
160 : int m_iRowId; /* rowid in DB */
161 : sqlite3_stmt *m_hStmt;
162 :
163 : bool LoadGeometryPoint();
164 : bool LoadGeometryLineStringSBP();
165 : bool LoadGeometryLineStringHP();
166 : bool LoadGeometryPolygon();
167 :
168 : OGRErr SetFIDFromDB();
169 : OGRErr ExecuteSQL(const char *);
170 : void FinalizeSQL();
171 :
172 : public:
173 : VFKFeatureSQLite(IVFKDataBlock *);
174 : VFKFeatureSQLite(IVFKDataBlock *, int, long);
175 : VFKFeatureSQLite(const VFKFeature *);
176 :
177 : OGRErr LoadProperties(OGRFeature *);
178 : };
179 :
180 : /************************************************************************/
181 : /* VFKPropertyDefn */
182 : /************************************************************************/
183 : class CPL_DLL VFKPropertyDefn
184 : {
185 : private:
186 : char *m_pszName;
187 :
188 : char *m_pszType;
189 : char *m_pszEncoding;
190 : OGRFieldType m_eFType;
191 :
192 : int m_nWidth;
193 : int m_nPrecision;
194 :
195 : public:
196 : VFKPropertyDefn(const char*, const char *, bool);
197 : virtual ~VFKPropertyDefn();
198 :
199 1729 : const char *GetName() const { return m_pszName; }
200 2300 : int GetWidth() const { return m_nWidth; }
201 1210 : int GetPrecision() const { return m_nPrecision; }
202 2274 : OGRFieldType GetType() const { return m_eFType; }
203 : CPLString GetTypeSQL() const;
204 307 : GBool IsIntBig() const { return m_pszType[0] == 'N'; }
205 307 : const char *GetEncoding() const { return m_pszEncoding; }
206 : };
207 :
208 : /************************************************************************/
209 : /* IVFKDataBlock */
210 : /************************************************************************/
211 : class CPL_DLL IVFKDataBlock
212 : {
213 : private:
214 : IVFKFeature **m_papoFeature;
215 :
216 : int m_nPropertyCount;
217 : VFKPropertyDefn **m_papoProperty;
218 :
219 : int AddProperty(const char *, const char *);
220 :
221 : protected:
222 : typedef std::vector<OGRPoint> PointList;
223 : typedef std::vector<PointList *> PointListArray;
224 :
225 : char *m_pszName;
226 : bool m_bGeometry;
227 :
228 : OGRwkbGeometryType m_nGeometryType;
229 : bool m_bGeometryPerBlock;
230 :
231 : int m_nFeatureCount;
232 : int m_iNextFeature;
233 :
234 : IVFKReader *m_poReader;
235 :
236 : bool AppendLineToRing(PointListArray *, const OGRLineString *, bool, bool = FALSE);
237 : int LoadData();
238 :
239 : virtual int LoadGeometryPoint() = 0;
240 : virtual int LoadGeometryLineStringSBP() = 0;
241 : virtual int LoadGeometryLineStringHP() = 0;
242 : virtual int LoadGeometryPolygon() = 0;
243 :
244 : public:
245 : IVFKDataBlock(const char *, const IVFKReader *);
246 : virtual ~IVFKDataBlock();
247 :
248 4447 : const char *GetName() const { return m_pszName; }
249 :
250 4267 : int GetPropertyCount() const { return m_nPropertyCount; }
251 : VFKPropertyDefn *GetProperty(int) const;
252 : void SetProperties(const char *);
253 : int GetPropertyIndex(const char *) const;
254 :
255 : int GetFeatureCount();
256 : void SetFeatureCount(int, bool = FALSE);
257 : IVFKFeature *GetFeatureByIndex(int) const;
258 : IVFKFeature *GetFeature(long);
259 : void AddFeature(IVFKFeature *);
260 :
261 : void ResetReading(int iIdx = -1);
262 : IVFKFeature *GetNextFeature();
263 : IVFKFeature *GetPreviousFeature();
264 : IVFKFeature *GetFirstFeature();
265 : IVFKFeature *GetLastFeature();
266 : int SetNextFeature(const IVFKFeature *);
267 :
268 : OGRwkbGeometryType SetGeometryType();
269 : OGRwkbGeometryType GetGeometryType() const;
270 :
271 : int LoadGeometry();
272 :
273 15 : IVFKReader *GetReader() const { return m_poReader; }
274 : };
275 :
276 : /************************************************************************/
277 : /* VFKDataBlock */
278 : /************************************************************************/
279 : class CPL_DLL VFKDataBlock : public IVFKDataBlock
280 0 : {
281 : private:
282 : int LoadGeometryPoint();
283 : int LoadGeometryLineStringSBP();
284 : int LoadGeometryLineStringHP();
285 : int LoadGeometryPolygon();
286 :
287 : public:
288 0 : VFKDataBlock(const char *pszName, const IVFKReader *poReader) : IVFKDataBlock(pszName, poReader) {}
289 :
290 : VFKFeature *GetFeature(int, GUIntBig, VFKFeatureList* = NULL);
291 : VFKFeatureList GetFeatures(int, GUIntBig);
292 : VFKFeatureList GetFeatures(int, int, GUIntBig);
293 :
294 : int GetFeatureCount(const char *, const char *);
295 : };
296 :
297 : /************************************************************************/
298 : /* VFKDataBlockSQLite */
299 : /************************************************************************/
300 : class CPL_DLL VFKDataBlockSQLite : public IVFKDataBlock
301 122 : {
302 : private:
303 : int LoadGeometryPoint();
304 : int LoadGeometryLineStringSBP();
305 : int LoadGeometryLineStringHP();
306 : int LoadGeometryPolygon();
307 :
308 : bool LoadGeometryFromDB();
309 : OGRErr SaveGeometryToDB(const OGRGeometry *, int);
310 :
311 : bool IsRingClosed(const OGRLinearRing *);
312 : void UpdateVfkBlocks(int);
313 : void UpdateFID(long int, std::vector<int>);
314 :
315 : public:
316 122 : VFKDataBlockSQLite(const char *pszName, const IVFKReader *poReader) : IVFKDataBlock(pszName, poReader) {}
317 :
318 : const char *GetKey() const;
319 : IVFKFeature *GetFeature(long);
320 : VFKFeatureSQLite *GetFeature(const char *, GUIntBig, bool = FALSE);
321 : VFKFeatureSQLite *GetFeature(const char **, GUIntBig *, int, bool = FALSE);
322 : VFKFeatureSQLiteList GetFeatures(const char **, GUIntBig *, int);
323 : };
324 :
325 : /************************************************************************/
326 : /* IVFKReader */
327 : /************************************************************************/
328 : class CPL_DLL IVFKReader
329 2 : {
330 : private:
331 : virtual void AddInfo(const char *) = 0;
332 :
333 : protected:
334 : virtual IVFKDataBlock *CreateDataBlock(const char *) = 0;
335 : virtual void AddDataBlock(IVFKDataBlock * = NULL, const char * = NULL) = 0;
336 : virtual OGRErr AddFeature(IVFKDataBlock * = NULL, VFKFeature * = NULL) = 0;
337 :
338 : public:
339 : virtual ~IVFKReader();
340 :
341 : virtual bool IsLatin2() const = 0;
342 : virtual bool IsSpatial() const = 0;
343 : virtual int ReadDataBlocks() = 0;
344 : virtual int ReadDataRecords(IVFKDataBlock *) = 0;
345 : virtual int LoadGeometry() = 0;
346 :
347 : virtual int GetDataBlockCount() const = 0;
348 : virtual IVFKDataBlock *GetDataBlock(int) const = 0;
349 : virtual IVFKDataBlock *GetDataBlock(const char *) const = 0;
350 :
351 : virtual const char *GetInfo(const char *) = 0;
352 : };
353 :
354 : IVFKReader *CreateVFKReader(const char *);
355 :
356 : #endif // GDAL_OGR_VFK_VFKREADER_H_INCLUDED
|