Coverage Report

Created: 2025-07-23 09:13

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