1 /******************************************************************************
2  * $Id: ogr_geojson.h 77ae389de75448298d20c8bf9b27fbccb257bd39 2020-02-25 23:27:58 +0100 Even Rouault $
3  *
4  * Project:  OpenGIS Simple Features Reference Implementation
5  * Purpose:  Definitions of OGR OGRGeoJSON driver types.
6  * Author:   Mateusz Loskot, mateusz@loskot.net
7  *
8  ******************************************************************************
9  * Copyright (c) 2007, Mateusz Loskot
10  * Copyright (c) 2010-2013, Even Rouault <even dot rouault at spatialys.com>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  ****************************************************************************/
30 
31 #ifndef OGR_GEOJSON_H_INCLUDED
32 #define OGR_GEOJSON_H_INCLUDED
33 
34 #include "cpl_port.h"
35 #include <ogrsf_frmts.h>
36 #include "../mem/ogr_mem.h"
37 
38 #include <cstdio>
39 #include <vector>  // Used by OGRGeoJSONLayer.
40 #include "ogrgeojsonutils.h"
41 #include "ogrgeojsonwriter.h"
42 
43 class OGRGeoJSONDataSource;
44 
45 GDALDataset* OGRGeoJSONDriverOpenInternal( GDALOpenInfo* poOpenInfo,
46                                            GeoJSONSourceType nSrcType,
47                                            const char* pszJSonFlavor );
48 void OGRGeoJSONDriverStoreContent( const char* pszSource, char* pszText );
49 char* OGRGeoJSONDriverStealStoredContent( const char* pszSource );
50 
51 /************************************************************************/
52 /*                           OGRGeoJSONLayer                            */
53 /************************************************************************/
54 
55 class OGRGeoJSONReader;
56 
57 class OGRGeoJSONLayer final: public OGRMemLayer
58 {
59     friend class OGRGeoJSONDataSource;
60 
61   public:
62     static const char* const DefaultName;
63     static const OGRwkbGeometryType DefaultGeometryType;
64 
65     OGRGeoJSONLayer( const char* pszName,
66                      OGRSpatialReference* poSRS,
67                      OGRwkbGeometryType eGType,
68                      OGRGeoJSONDataSource* poDS,
69                      OGRGeoJSONReader* poReader);
70     virtual ~OGRGeoJSONLayer();
71 
72     //
73     // OGRLayer Interface
74     //
75     virtual const char* GetFIDColumn() override;
76     virtual int         TestCapability( const char * pszCap ) override;
77 
78     virtual OGRErr      SyncToDisk() override;
79 
80     virtual void        ResetReading() override;
81     virtual OGRFeature* GetNextFeature() override;
82     virtual OGRFeature* GetFeature(GIntBig nFID) override;
83     virtual GIntBig     GetFeatureCount(int bForce) override;
84 
85     OGRErr              ISetFeature( OGRFeature *poFeature ) override;
86     OGRErr              ICreateFeature( OGRFeature *poFeature ) override;
87     virtual OGRErr      DeleteFeature( GIntBig nFID ) override;
88     virtual OGRErr      CreateField( OGRFieldDefn *poField,
89                                      int bApproxOK = TRUE ) override;
90     virtual OGRErr      DeleteField( int iField ) override;
91     virtual OGRErr      ReorderFields( int* panMap ) override;
92     virtual OGRErr      AlterFieldDefn( int iField,
93                                         OGRFieldDefn* poNewFieldDefn,
94                                         int nFlags ) override;
95     virtual OGRErr      CreateGeomField( OGRGeomFieldDefn *poGeomField,
96                                          int bApproxOK = TRUE ) override;
97 
98     //
99     // OGRGeoJSONLayer Interface
100     //
101     void SetFIDColumn( const char* pszFIDColumn );
102     void AddFeature( OGRFeature* poFeature );
103     void DetectGeometryType();
IncFeatureCount()104     void IncFeatureCount() { nTotalFeatureCount_++; }
UnsetReader()105     void UnsetReader() { poReader_ = nullptr; }
InvalidateFeatureCount()106     void InvalidateFeatureCount() { nTotalFeatureCount_ = -1; }
107 
108   private:
109     OGRGeoJSONDataSource* poDS_;
110     OGRGeoJSONReader* poReader_;
111     bool bHasAppendedFeatures_;
112     CPLString sFIDColumn_;
113     bool bUpdated_;
114     bool bOriginalIdModified_;
115     GIntBig nTotalFeatureCount_;
116     GIntBig nFeatureReadSinceReset_ = 0;
117     GIntBig nNextFID_;
118 
119     bool IngestAll();
120     void TerminateAppendSession();
121 };
122 
123 /************************************************************************/
124 /*                         OGRGeoJSONWriteLayer                         */
125 /************************************************************************/
126 
127 class OGRGeoJSONWriteLayer final: public OGRLayer
128 {
129   public:
130     OGRGeoJSONWriteLayer( const char* pszName,
131                           OGRwkbGeometryType eGType,
132                           char** papszOptions,
133                           bool bWriteFC_BBOXIn,
134                           OGRCoordinateTransformation* poCT,
135                           OGRGeoJSONDataSource* poDS );
136     ~OGRGeoJSONWriteLayer();
137 
138     //
139     // OGRLayer Interface
140     //
GetLayerDefn()141     OGRFeatureDefn* GetLayerDefn() override { return poFeatureDefn_; }
GetSpatialRef()142     OGRSpatialReference* GetSpatialRef() override { return nullptr; }
143 
ResetReading()144     void ResetReading() override { }
GetNextFeature()145     OGRFeature* GetNextFeature() override { return nullptr; }
146     OGRErr ICreateFeature( OGRFeature* poFeature ) override;
147     OGRErr CreateField( OGRFieldDefn* poField, int bApproxOK ) override;
148     int TestCapability( const char* pszCap ) override;
149     OGRErr GetExtent(OGREnvelope *psExtent, int bForce) override;
GetExtent(int iGeomField,OGREnvelope * psExtent,int bForce)150     OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent, int bForce) override
151         { return iGeomField == 0 ? OGRGeoJSONWriteLayer::GetExtent(psExtent, bForce) : OGRERR_FAILURE; }
152 
153   private:
154     OGRGeoJSONDataSource* poDS_;
155     OGRFeatureDefn* poFeatureDefn_;
156     int nOutCounter_;
157 
158     bool bWriteBBOX;
159     bool bBBOX3D;
160     bool bWriteFC_BBOX;
161     OGREnvelope3D sEnvelopeLayer;
162 
163     int nCoordPrecision_;
164     int nSignificantFigures_;
165 
166     bool bRFC7946_;
167     OGRCoordinateTransformation* poCT_;
168     OGRGeometryFactory::TransformWithOptionsCache oTransformCache_;
169     OGRGeoJSONWriteOptions oWriteOptions_;
170 };
171 
172 /************************************************************************/
173 /*                           OGRGeoJSONDataSource                       */
174 /************************************************************************/
175 
176 class OGRGeoJSONDataSource final: public OGRDataSource
177 {
178   public:
179     OGRGeoJSONDataSource();
180     virtual ~OGRGeoJSONDataSource();
181 
182     //
183     // OGRDataSource Interface
184     //
185     int Open( GDALOpenInfo* poOpenInfo,
186               GeoJSONSourceType nSrcType,
187               const char* pszJSonFlavor );
188     const char* GetName() override;
189     int GetLayerCount() override;
190     OGRLayer* GetLayer( int nLayer ) override;
191     OGRLayer* ICreateLayer( const char* pszName,
192                             OGRSpatialReference* poSRS = nullptr,
193                             OGRwkbGeometryType eGType = wkbUnknown,
194                             char** papszOptions = nullptr ) override;
195     int TestCapability( const char* pszCap ) override;
196 
197     void AddLayer( OGRGeoJSONLayer* poLayer );
198 
199     //
200     // OGRGeoJSONDataSource Interface
201     //
202     int Create( const char* pszName, char** papszOptions );
GetOutputFile()203     VSILFILE* GetOutputFile() const { return fpOut_; }
204 
205     enum GeometryTranslation
206     {
207         eGeometryPreserve,
208         eGeometryAsCollection,
209     };
210 
211     void SetGeometryTranslation( GeometryTranslation type );
212 
213     enum AttributesTranslation
214     {
215         eAttributesPreserve,
216         eAttributesSkip
217     };
218 
219     void SetAttributesTranslation( AttributesTranslation type );
220 
GetFpOutputIsSeekable()221     int  GetFpOutputIsSeekable() const { return bFpOutputIsSeekable_; }
GetBBOXInsertLocation()222     int  GetBBOXInsertLocation() const { return nBBOXInsertLocation_; }
HasOtherPages()223     int  HasOtherPages() const { return bOtherPages_; }
IsUpdatable()224     bool IsUpdatable() const { return bUpdatable_; }
GetJSonFlavor()225     const CPLString& GetJSonFlavor() const { return osJSonFlavor_; }
226 
227     virtual void        FlushCache() override;
228 
229     static const size_t SPACE_FOR_BBOX = 130;
230 
231   private:
232     //
233     // Private data members
234     //
235     char* pszName_;
236     char* pszGeoData_;
237     vsi_l_offset nGeoDataLen_;
238     OGRGeoJSONLayer** papoLayers_;
239     OGRGeoJSONWriteLayer** papoLayersWriter_;
240     int nLayers_;
241     VSILFILE* fpOut_;
242 
243     //
244     // Translation/Creation control flags
245     //
246     GeometryTranslation flTransGeom_;
247     AttributesTranslation flTransAttrs_;
248     bool bOtherPages_;  // ESRI Feature Service specific.
249 
250     bool bFpOutputIsSeekable_;
251     int nBBOXInsertLocation_;
252 
253     bool bUpdatable_;
254 
255     CPLString osJSonFlavor_;
256 
257     //
258     // Private utility functions
259     //
260     void Clear();
261     int ReadFromFile( GDALOpenInfo* poOpenInfo, const char* pszUnprefixed );
262     int ReadFromService( GDALOpenInfo* poOpenInfo, const char* pszSource );
263     void LoadLayers(GDALOpenInfo* poOpenInfo,
264                     GeoJSONSourceType nSrcType,
265                     const char* pszUnprefixed,
266                     const char* pszJSonFlavor);
267     void SetOptionsOnReader(GDALOpenInfo* poOpenInfo,
268                             OGRGeoJSONReader* poReader);
269     void CheckExceededTransferLimit( json_object* poObj );
270     void RemoveJSonPStuff();
271 };
272 
273 #endif  // OGR_GEOJSON_H_INCLUDED
274