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