Coverage Report

Created: 2025-06-22 06:59

/src/gdal/ogr/ogrsf_frmts/geojson/ogrgeojsonreader.h
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Project:  OpenGIS Simple Features Reference Implementation
4
 * Purpose:  Defines GeoJSON reader within OGR OGRGeoJSON Driver.
5
 * Author:   Mateusz Loskot, mateusz@loskot.net
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2007, Mateusz Loskot
9
 * Copyright (c) 2010-2013, Even Rouault <even dot rouault at spatialys.com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
#ifndef OGR_GEOJSONREADER_H_INCLUDED
14
#define OGR_GEOJSONREADER_H_INCLUDED
15
16
#include "cpl_json_header.h"
17
#include "cpl_string.h"
18
#include "ogr_core.h"
19
#include "ogrsf_frmts.h"
20
21
#include "ogrgeojsonutils.h"
22
#include "directedacyclicgraph.hpp"
23
24
#include <utility>
25
#include <map>
26
#include <set>
27
#include <vector>
28
29
/************************************************************************/
30
/*                         FORWARD DECLARATIONS                         */
31
/************************************************************************/
32
33
class OGRGeometry;
34
class OGRRawPoint;
35
class OGRPoint;
36
class OGRMultiPoint;
37
class OGRLineString;
38
class OGRMultiLineString;
39
class OGRLinearRing;
40
class OGRPolygon;
41
class OGRMultiPolygon;
42
class OGRGeometryCollection;
43
class OGRFeature;
44
class OGRGeoJSONLayer;
45
class OGRSpatialReference;
46
47
/************************************************************************/
48
/*                        OGRGeoJSONBaseReader                          */
49
/************************************************************************/
50
51
class OGRGeoJSONBaseReader
52
{
53
  public:
54
    OGRGeoJSONBaseReader();
55
56
    void SetPreserveGeometryType(bool bPreserve);
57
    void SetSkipAttributes(bool bSkip);
58
    void SetFlattenNestedAttributes(bool bFlatten, char chSeparator);
59
    void SetStoreNativeData(bool bStoreNativeData);
60
    void SetArrayAsString(bool bArrayAsString);
61
    void SetDateAsString(bool bDateAsString);
62
63
    enum class ForeignMemberProcessing
64
    {
65
        AUTO,
66
        ALL,
67
        NONE,
68
        STAC,
69
    };
70
71
    void
72
    SetForeignMemberProcessing(ForeignMemberProcessing eForeignMemberProcessing)
73
0
    {
74
0
        eForeignMemberProcessing_ = eForeignMemberProcessing;
75
0
    }
76
77
    bool GenerateFeatureDefn(
78
        std::map<std::string, int> &oMapFieldNameToIdx,
79
        std::vector<std::unique_ptr<OGRFieldDefn>> &apoFieldDefn,
80
        gdal::DirectedAcyclicGraph<int, std::string> &dag, OGRLayer *poLayer,
81
        json_object *poObj);
82
    void FinalizeLayerDefn(OGRLayer *poLayer, CPLString &osFIDColumn);
83
84
    OGRGeometry *ReadGeometry(json_object *poObj,
85
                              OGRSpatialReference *poLayerSRS);
86
    OGRFeature *ReadFeature(OGRLayer *poLayer, json_object *poObj,
87
                            const char *pszSerializedObj);
88
89
    bool ExtentRead() const;
90
91
    OGREnvelope3D GetExtent3D() const;
92
93
  protected:
94
    bool bGeometryPreserve_ = true;
95
    bool bAttributesSkip_ = false;
96
    bool bFlattenNestedAttributes_ = false;
97
    char chNestedAttributeSeparator_ = 0;
98
    bool bStoreNativeData_ = false;
99
    bool bArrayAsString_ = false;
100
    bool bDateAsString_ = false;
101
    ForeignMemberProcessing eForeignMemberProcessing_ =
102
        ForeignMemberProcessing::AUTO;
103
104
  private:
105
    std::set<int> aoSetUndeterminedTypeFields_;
106
107
    // bFlatten... is a tri-state boolean with -1 being unset.
108
    int bFlattenGeocouchSpatiallistFormat = -1;
109
110
    bool bFoundGeocouchId = false;
111
    bool bFoundRev = false;
112
    bool bFoundTypeFeature = false;
113
    bool bIsGeocouchSpatiallistFormat = false;
114
    bool bFeatureLevelIdAsAttribute_ = false;
115
    bool bFeatureLevelIdAsFID_ = false;
116
    bool m_bNeedFID64 = false;
117
118
    bool m_bFirstGeometry = true;
119
    OGREnvelope3D m_oEnvelope3D;
120
    // Becomes true when extent has been read from data
121
    bool m_bExtentRead = false;
122
    OGRwkbGeometryType m_eLayerGeomType = wkbUnknown;
123
124
    CPL_DISALLOW_COPY_ASSIGN(OGRGeoJSONBaseReader)
125
};
126
127
/************************************************************************/
128
/*                           OGRGeoJSONReader                           */
129
/************************************************************************/
130
131
class OGRGeoJSONDataSource;
132
class OGRGeoJSONReaderStreamingParser;
133
134
class OGRGeoJSONReader : public OGRGeoJSONBaseReader
135
{
136
  public:
137
    OGRGeoJSONReader();
138
    ~OGRGeoJSONReader();
139
140
    OGRErr Parse(const char *pszText);
141
    void ReadLayers(OGRGeoJSONDataSource *poDS);
142
    void ReadLayer(OGRGeoJSONDataSource *poDS, const char *pszName,
143
                   json_object *poObj);
144
    bool FirstPassReadLayer(OGRGeoJSONDataSource *poDS, VSILFILE *fp,
145
                            bool &bTryStandardReading);
146
147
    json_object *GetJSonObject()
148
0
    {
149
0
        return poGJObject_;
150
0
    }
151
152
    void ResetReading();
153
    OGRFeature *GetNextFeature(OGRGeoJSONLayer *poLayer);
154
    OGRFeature *GetFeature(OGRGeoJSONLayer *poLayer, GIntBig nFID);
155
    bool IngestAll(OGRGeoJSONLayer *poLayer);
156
157
    VSILFILE *GetFP()
158
0
    {
159
0
        return fp_;
160
0
    }
161
162
    bool CanEasilyAppend() const
163
0
    {
164
0
        return bCanEasilyAppend_;
165
0
    }
166
167
    bool FCHasBBOX() const
168
0
    {
169
0
        return bFCHasBBOX_;
170
0
    }
171
172
  private:
173
    friend class OGRGeoJSONReaderStreamingParser;
174
175
    json_object *poGJObject_;
176
    OGRGeoJSONReaderStreamingParser *poStreamingParser_;
177
    bool bFirstSeg_;
178
    bool bJSonPLikeWrapper_;
179
    VSILFILE *fp_;
180
    bool bCanEasilyAppend_;
181
    bool bFCHasBBOX_;
182
    bool bOriginalIdModifiedEmitted_ = false;
183
184
    size_t nBufferSize_;
185
    GByte *pabyBuffer_;
186
187
    GIntBig nTotalFeatureCount_;
188
    GUIntBig nTotalOGRFeatureMemEstimate_;
189
190
    std::map<GIntBig, std::pair<vsi_l_offset, vsi_l_offset>>
191
        oMapFIDToOffsetSize_;
192
    //
193
    // Copy operations not supported.
194
    //
195
    CPL_DISALLOW_COPY_ASSIGN(OGRGeoJSONReader)
196
197
    //
198
    // Translation utilities.
199
    //
200
    bool GenerateLayerDefn(OGRGeoJSONLayer *poLayer, json_object *poGJObject);
201
202
    static bool AddFeature(OGRGeoJSONLayer *poLayer, OGRGeometry *poGeometry);
203
    static bool AddFeature(OGRGeoJSONLayer *poLayer, OGRFeature *poFeature);
204
205
    void ReadFeatureCollection(OGRGeoJSONLayer *poLayer, json_object *poObj);
206
    size_t SkipPrologEpilogAndUpdateJSonPLikeWrapper(size_t nRead);
207
};
208
209
void OGRGeoJSONGenerateFeatureDefnDealWithID(
210
    json_object *poObj, json_object *poObjProps, int &nPrevFieldIdx,
211
    std::map<std::string, int> &oMapFieldNameToIdx,
212
    std::vector<std::unique_ptr<OGRFieldDefn>> &apoFieldDefn,
213
    gdal::DirectedAcyclicGraph<int, std::string> &dag,
214
    bool &bFeatureLevelIdAsFID, bool &bFeatureLevelIdAsAttribute,
215
    bool &bNeedFID64);
216
217
void OGRGeoJSONReaderSetField(OGRLayer *poLayer, OGRFeature *poFeature,
218
                              int nField, const char *pszAttrPrefix,
219
                              json_object *poVal, bool bFlattenNestedAttributes,
220
                              char chNestedAttributeSeparator);
221
void OGRGeoJSONReaderAddOrUpdateField(
222
    std::vector<int> &retIndices,
223
    std::map<std::string, int> &oMapFieldNameToIdx,
224
    std::vector<std::unique_ptr<OGRFieldDefn>> &apoFieldDefn,
225
    const char *pszKey, json_object *poVal, bool bFlattenNestedAttributes,
226
    char chNestedAttributeSeparator, bool bArrayAsString, bool bDateAsString,
227
    std::set<int> &aoSetUndeterminedTypeFields);
228
229
/************************************************************************/
230
/*                 GeoJSON Parsing Utilities                            */
231
/************************************************************************/
232
233
bool OGRGeoJSONUpdateLayerGeomType(bool &bFirstGeom,
234
                                   OGRwkbGeometryType eGeomType,
235
                                   OGRwkbGeometryType &eLayerGeomType);
236
237
// Get the 3D extent from the geometry coordinates of a feature
238
bool OGRGeoJSONGetExtent3D(json_object *poObj, OGREnvelope3D *poEnvelope);
239
240
/************************************************************************/
241
/*                          OGRESRIJSONReader                           */
242
/************************************************************************/
243
244
class OGRESRIJSONReader
245
{
246
  public:
247
    OGRESRIJSONReader();
248
    ~OGRESRIJSONReader();
249
250
    OGRErr Parse(const char *pszText);
251
    void ReadLayers(OGRGeoJSONDataSource *poDS, GeoJSONSourceType eSourceType);
252
253
    json_object *GetJSonObject()
254
0
    {
255
0
        return poGJObject_;
256
0
    }
257
258
  private:
259
    json_object *poGJObject_;
260
    OGRGeoJSONLayer *poLayer_;
261
262
    //
263
    // Copy operations not supported.
264
    //
265
    OGRESRIJSONReader(OGRESRIJSONReader const &);
266
    OGRESRIJSONReader &operator=(OGRESRIJSONReader const &);
267
268
    //
269
    // Translation utilities.
270
    //
271
    bool GenerateLayerDefn();
272
    bool ParseField(json_object *poObj);
273
    bool AddFeature(OGRFeature *poFeature);
274
275
    OGRFeature *ReadFeature(json_object *poObj);
276
    OGRGeoJSONLayer *ReadFeatureCollection(json_object *poObj);
277
};
278
279
/************************************************************************/
280
/*                          OGRTopoJSONReader                           */
281
/************************************************************************/
282
283
class OGRTopoJSONReader
284
{
285
  public:
286
    OGRTopoJSONReader();
287
    ~OGRTopoJSONReader();
288
289
    OGRErr Parse(const char *pszText, bool bLooseIdentification);
290
    void ReadLayers(OGRGeoJSONDataSource *poDS);
291
292
  private:
293
    json_object *poGJObject_;
294
295
    //
296
    // Copy operations not supported.
297
    //
298
    OGRTopoJSONReader(OGRTopoJSONReader const &);
299
    OGRTopoJSONReader &operator=(OGRTopoJSONReader const &);
300
};
301
302
#endif /* OGR_GEOJSONUTILS_H_INCLUDED */