1 : /******************************************************************************
2 : * $Id: vfkreader.h 24216 2012-04-09 11:26:40Z 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 158 : 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 58 : 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 : void 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 : void 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 106 : {
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(const VFKFeature *);
174 :
175 : OGRErr LoadProperties(OGRFeature *);
176 : };
177 :
178 : #endif
179 :
180 : /************************************************************************/
181 : /* VFKPropertyDefn */
182 : /************************************************************************/
183 : class CPL_DLL VFKPropertyDefn
184 : {
185 : private:
186 : char *m_pszName;
187 :
188 : char *m_pszType;
189 : OGRFieldType m_eFType;
190 :
191 : int m_nWidth;
192 : int m_nPrecision;
193 :
194 : public:
195 : VFKPropertyDefn(const char*, const char *);
196 : virtual ~VFKPropertyDefn();
197 :
198 1959 : const char *GetName() const { return m_pszName; }
199 2300 : int GetWidth() const { return m_nWidth; }
200 1210 : int GetPrecision() const { return m_nPrecision; }
201 2274 : OGRFieldType GetType() const { return m_eFType; }
202 : CPLString GetTypeSQL() const;
203 307 : GBool IsIntBig() const { return m_pszType[0] == 'N'; }
204 : };
205 :
206 : /************************************************************************/
207 : /* IVFKDataBlock */
208 : /************************************************************************/
209 : class CPL_DLL IVFKDataBlock
210 : {
211 : private:
212 : int m_nPropertyCount;
213 : VFKPropertyDefn **m_papoProperty;
214 :
215 : IVFKFeature **m_papoFeature;
216 :
217 : long m_nFID;
218 :
219 : OGRwkbGeometryType m_nGeometryType;
220 : bool m_bGeometryPerBlock;
221 :
222 : int AddProperty(const char *, const char *);
223 :
224 : protected:
225 : typedef std::vector<OGRPoint> PointList;
226 : typedef std::vector<PointList *> PointListArray;
227 :
228 : char *m_pszName;
229 : bool m_bGeometry;
230 :
231 : int m_nFeatureCount;
232 : int m_iNextFeature;
233 :
234 : IVFKReader *m_poReader;
235 :
236 : bool AppendLineToRing(PointListArray *, const OGRLineString *, bool);
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 4564 : const char *GetName() const { return m_pszName; }
249 :
250 4423 : int GetPropertyCount() const { return m_nPropertyCount; }
251 : VFKPropertyDefn *GetProperty(int) const;
252 : void SetProperties(const char *);
253 : int GetPropertyIndex(const char *) const;
254 :
255 114 : int GetFeatureCount() const { return m_nFeatureCount; }
256 : void SetFeatureCount(int, int = 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 : long GetMaxFID();
272 : long SetMaxFID(long);
273 :
274 : int LoadGeometry();
275 :
276 83 : IVFKReader *GetReader() const { return m_poReader; }
277 : };
278 :
279 : /************************************************************************/
280 : /* VFKDataBlock */
281 : /************************************************************************/
282 : class CPL_DLL VFKDataBlock : public IVFKDataBlock
283 0 : {
284 : private:
285 : int LoadGeometryPoint();
286 : int LoadGeometryLineStringSBP();
287 : int LoadGeometryLineStringHP();
288 : int LoadGeometryPolygon();
289 :
290 : public:
291 0 : VFKDataBlock(const char *pszName, const IVFKReader *poReader) : IVFKDataBlock(pszName, poReader) {}
292 :
293 : VFKFeature *GetFeature(int, GUIntBig, VFKFeatureList* = NULL);
294 : VFKFeatureList GetFeatures(int, GUIntBig);
295 : VFKFeatureList GetFeatures(int, int, GUIntBig);
296 :
297 : int GetFeatureCount(const char *, const char *);
298 : };
299 :
300 : #ifdef HAVE_SQLITE
301 : /************************************************************************/
302 : /* VFKDataBlockSQLite */
303 : /************************************************************************/
304 : class CPL_DLL VFKDataBlockSQLite : public IVFKDataBlock
305 122 : {
306 : private:
307 : int LoadGeometryPoint();
308 : int LoadGeometryLineStringSBP();
309 : int LoadGeometryLineStringHP();
310 : int LoadGeometryPolygon();
311 :
312 : public:
313 122 : VFKDataBlockSQLite(const char *pszName, const IVFKReader *poReader) : IVFKDataBlock(pszName, poReader) {}
314 :
315 : VFKFeatureSQLite *GetFeature(const char *, GUIntBig);
316 : VFKFeatureSQLite *GetFeature(const char **, GUIntBig *, int);
317 : VFKFeatureSQLiteList GetFeatures(const char **, GUIntBig *, int);
318 : };
319 : #endif
320 :
321 : /************************************************************************/
322 : /* IVFKReader */
323 : /************************************************************************/
324 : class CPL_DLL IVFKReader
325 2 : {
326 : private:
327 : virtual void AddInfo(const char *) = 0;
328 :
329 : protected:
330 : virtual IVFKDataBlock *CreateDataBlock(const char *) = 0;
331 : virtual void AddDataBlock(IVFKDataBlock * = NULL, const char * = NULL) = 0;
332 : virtual void AddFeature(IVFKDataBlock * = NULL, VFKFeature * = NULL) = 0;
333 :
334 : public:
335 : virtual ~IVFKReader();
336 :
337 : virtual int ReadDataBlocks() = 0;
338 : virtual int ReadDataRecords(IVFKDataBlock *) = 0;
339 : virtual int LoadGeometry() = 0;
340 :
341 : virtual int GetDataBlockCount() const = 0;
342 : virtual IVFKDataBlock *GetDataBlock(int) const = 0;
343 : virtual IVFKDataBlock *GetDataBlock(const char *) const = 0;
344 :
345 : virtual const char *GetInfo(const char *) = 0;
346 : };
347 :
348 : IVFKReader *CreateVFKReader(const char *);
349 :
350 : #endif // GDAL_OGR_VFK_VFKREADER_H_INCLUDED
|