LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/geojson - ogr_geojson.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 32 35 91.4 %
Date: 2024-05-03 15:49:35 Functions: 14 15 93.3 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  * $Id$
       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, OGRSpatialReference *poSRS,
      66             :                     OGRwkbGeometryType eGType, OGRGeoJSONDataSource *poDS,
      67             :                     OGRGeoJSONReader *poReader);
      68             :     virtual ~OGRGeoJSONLayer();
      69             : 
      70             :     //
      71             :     // OGRLayer Interface
      72             :     //
      73             :     virtual const char *GetFIDColumn() override;
      74             :     virtual int TestCapability(const char *pszCap) override;
      75             : 
      76             :     virtual OGRErr SyncToDisk() override;
      77             : 
      78             :     virtual void ResetReading() override;
      79             :     virtual OGRFeature *GetNextFeature() override;
      80             :     virtual OGRFeature *GetFeature(GIntBig nFID) override;
      81             :     virtual GIntBig GetFeatureCount(int bForce) override;
      82             : 
      83             :     OGRErr ISetFeature(OGRFeature *poFeature) override;
      84             :     OGRErr ICreateFeature(OGRFeature *poFeature) override;
      85             :     virtual OGRErr DeleteFeature(GIntBig nFID) override;
      86             :     virtual OGRErr CreateField(const OGRFieldDefn *poField,
      87             :                                int bApproxOK = TRUE) override;
      88             :     virtual OGRErr DeleteField(int iField) override;
      89             :     virtual OGRErr ReorderFields(int *panMap) override;
      90             :     virtual OGRErr AlterFieldDefn(int iField, OGRFieldDefn *poNewFieldDefn,
      91             :                                   int nFlags) override;
      92             :     virtual OGRErr CreateGeomField(const OGRGeomFieldDefn *poGeomField,
      93             :                                    int bApproxOK = TRUE) override;
      94             :     virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce = TRUE) override;
      95             :     virtual OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent,
      96             :                              int bForce = TRUE) override;
      97             :     virtual OGRErr GetExtent3D(int iGeomField, OGREnvelope3D *psExtent3D,
      98             :                                int bForce = TRUE) override;
      99             : 
     100             :     GDALDataset *GetDataset() override;
     101             : 
     102             :     //
     103             :     // OGRGeoJSONLayer Interface
     104             :     //
     105             :     void SetFIDColumn(const char *pszFIDColumn);
     106             :     void AddFeature(OGRFeature *poFeature);
     107             :     void DetectGeometryType();
     108             : 
     109        2830 :     void IncFeatureCount()
     110             :     {
     111        2830 :         nTotalFeatureCount_++;
     112        2830 :     }
     113             : 
     114           3 :     void UnsetReader()
     115             :     {
     116           3 :         poReader_ = nullptr;
     117           3 :     }
     118             : 
     119           0 :     void InvalidateFeatureCount()
     120             :     {
     121           0 :         nTotalFeatureCount_ = -1;
     122           0 :     }
     123             : 
     124         332 :     void SetWriteOptions(const OGRGeoJSONWriteOptions &options)
     125             :     {
     126         332 :         oWriteOptions_ = options;
     127         332 :     }
     128             : 
     129             :   private:
     130             :     OGRGeoJSONDataSource *poDS_;
     131             :     OGRGeoJSONReader *poReader_;
     132             :     bool bHasAppendedFeatures_;
     133             :     CPLString sFIDColumn_;
     134             :     bool bOriginalIdModified_;
     135             :     GIntBig nTotalFeatureCount_;
     136             :     GIntBig nFeatureReadSinceReset_ = 0;
     137             : 
     138             :     //! Write options used by ICreateFeature() in append scenarios
     139             :     OGRGeoJSONWriteOptions oWriteOptions_;
     140             : 
     141             :     bool IngestAll();
     142             :     void TerminateAppendSession();
     143             : 
     144             :     CPL_DISALLOW_COPY_ASSIGN(OGRGeoJSONLayer)
     145             : };
     146             : 
     147             : /************************************************************************/
     148             : /*                         OGRGeoJSONWriteLayer                         */
     149             : /************************************************************************/
     150             : 
     151             : class OGRGeoJSONWriteLayer final : public OGRLayer
     152             : {
     153             :   public:
     154             :     OGRGeoJSONWriteLayer(const char *pszName, OGRwkbGeometryType eGType,
     155             :                          CSLConstList papszOptions, bool bWriteFC_BBOXIn,
     156             :                          OGRCoordinateTransformation *poCT,
     157             :                          OGRGeoJSONDataSource *poDS);
     158             :     ~OGRGeoJSONWriteLayer();
     159             : 
     160             :     //
     161             :     // OGRLayer Interface
     162             :     //
     163        2652 :     OGRFeatureDefn *GetLayerDefn() override
     164             :     {
     165        2652 :         return poFeatureDefn_;
     166             :     }
     167             : 
     168           4 :     OGRSpatialReference *GetSpatialRef() override
     169             :     {
     170           4 :         return nullptr;
     171             :     }
     172             : 
     173          16 :     void ResetReading() override
     174             :     {
     175          16 :     }
     176             : 
     177          16 :     OGRFeature *GetNextFeature() override
     178             :     {
     179          16 :         return nullptr;
     180             :     }
     181             : 
     182             :     OGRErr ICreateFeature(OGRFeature *poFeature) override;
     183             :     OGRErr CreateField(const OGRFieldDefn *poField, int bApproxOK) override;
     184             :     int TestCapability(const char *pszCap) override;
     185             :     OGRErr GetExtent(OGREnvelope *psExtent, int bForce) override;
     186             : 
     187           3 :     OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent, int bForce) override
     188             :     {
     189             :         return iGeomField == 0
     190           3 :                    ? OGRGeoJSONWriteLayer::GetExtent(psExtent, bForce)
     191           3 :                    : OGRERR_FAILURE;
     192             :     }
     193             : 
     194             :     OGRErr SyncToDisk() override;
     195             : 
     196             :     GDALDataset *GetDataset() override;
     197             : 
     198             :   private:
     199             :     OGRGeoJSONDataSource *poDS_;
     200             :     OGRFeatureDefn *poFeatureDefn_;
     201             :     int nOutCounter_;
     202             :     /** Offset at which the '] }' terminating sequence has already been
     203             :      * written by SyncToDisk(). 0 if it has not been written.
     204             :      */
     205             :     vsi_l_offset m_nPositionBeforeFCClosed = 0;
     206             : 
     207             :     bool bWriteBBOX;
     208             :     bool bBBOX3D;
     209             :     bool bWriteFC_BBOX;
     210             :     OGREnvelope3D sEnvelopeLayer;
     211             : 
     212             :     int nSignificantFigures_;
     213             : 
     214             :     bool bRFC7946_;
     215             :     bool bWrapDateLine_ = false;
     216             :     std::string osForeignMembers_{};
     217             :     OGRCoordinateTransformation *poCT_;
     218             :     OGRGeometryFactory::TransformWithOptionsCache oTransformCache_;
     219             :     OGRGeoJSONWriteOptions oWriteOptions_;
     220             : 
     221             :     CPL_DISALLOW_COPY_ASSIGN(OGRGeoJSONWriteLayer)
     222             : 
     223             :     void FinishWriting();
     224             : };
     225             : 
     226             : /************************************************************************/
     227             : /*                           OGRGeoJSONDataSource                       */
     228             : /************************************************************************/
     229             : 
     230             : class OGRGeoJSONDataSource final : public OGRDataSource
     231             : {
     232             :   public:
     233             :     OGRGeoJSONDataSource();
     234             :     virtual ~OGRGeoJSONDataSource();
     235             : 
     236             :     //
     237             :     // OGRDataSource Interface
     238             :     //
     239             :     int Open(GDALOpenInfo *poOpenInfo, GeoJSONSourceType nSrcType,
     240             :              const char *pszJSonFlavor);
     241             :     const char *GetName() override;
     242             :     int GetLayerCount() override;
     243             :     OGRLayer *GetLayer(int nLayer) override;
     244             :     OGRLayer *ICreateLayer(const char *pszName,
     245             :                            const OGRGeomFieldDefn *poGeomFieldDefn,
     246             :                            CSLConstList papszOptions) override;
     247             :     int TestCapability(const char *pszCap) override;
     248             : 
     249             :     void AddLayer(OGRGeoJSONLayer *poLayer);
     250             : 
     251             :     //
     252             :     // OGRGeoJSONDataSource Interface
     253             :     //
     254             :     int Create(const char *pszName, char **papszOptions);
     255             : 
     256        1202 :     VSILFILE *GetOutputFile() const
     257             :     {
     258        1202 :         return fpOut_;
     259             :     }
     260             : 
     261             :     enum GeometryTranslation
     262             :     {
     263             :         eGeometryPreserve,
     264             :         eGeometryAsCollection,
     265             :     };
     266             : 
     267             :     void SetGeometryTranslation(GeometryTranslation type);
     268             : 
     269             :     enum AttributesTranslation
     270             :     {
     271             :         eAttributesPreserve,
     272             :         eAttributesSkip
     273             :     };
     274             : 
     275             :     void SetAttributesTranslation(AttributesTranslation type);
     276             : 
     277         177 :     int GetFpOutputIsSeekable() const
     278             :     {
     279         177 :         return bFpOutputIsSeekable_;
     280             :     }
     281             : 
     282          23 :     int GetBBOXInsertLocation() const
     283             :     {
     284          23 :         return nBBOXInsertLocation_;
     285             :     }
     286             : 
     287         428 :     int HasOtherPages() const
     288             :     {
     289         428 :         return bOtherPages_;
     290             :     }
     291             : 
     292         434 :     bool IsUpdatable() const
     293             :     {
     294         434 :         return bUpdatable_;
     295             :     }
     296             : 
     297          44 :     const CPLString &GetJSonFlavor() const
     298             :     {
     299          44 :         return osJSonFlavor_;
     300             :     }
     301             : 
     302             :     virtual CPLErr FlushCache(bool bAtClosing) override;
     303             : 
     304             :     CPLErr Close() override;
     305             : 
     306             :     static const size_t SPACE_FOR_BBOX = 130;
     307             : 
     308             :   private:
     309             :     //
     310             :     // Private data members
     311             :     //
     312             :     char *pszName_;
     313             :     char *pszGeoData_;
     314             :     vsi_l_offset nGeoDataLen_;
     315             :     OGRGeoJSONLayer **papoLayers_;
     316             :     OGRGeoJSONWriteLayer **papoLayersWriter_;
     317             :     int nLayers_;
     318             :     VSILFILE *fpOut_;
     319             : 
     320             :     //
     321             :     // Translation/Creation control flags
     322             :     //
     323             :     GeometryTranslation flTransGeom_;
     324             :     AttributesTranslation flTransAttrs_;
     325             :     bool bOtherPages_;  // ESRI Feature Service specific.
     326             : 
     327             :     bool bFpOutputIsSeekable_;
     328             :     int nBBOXInsertLocation_;
     329             : 
     330             :     bool bUpdatable_;
     331             : 
     332             :     CPLString osJSonFlavor_;
     333             : 
     334             :     //
     335             :     // Private utility functions
     336             :     //
     337             :     bool Clear();
     338             :     int ReadFromFile(GDALOpenInfo *poOpenInfo, const char *pszUnprefixed);
     339             :     int ReadFromService(GDALOpenInfo *poOpenInfo, const char *pszSource);
     340             :     void LoadLayers(GDALOpenInfo *poOpenInfo, GeoJSONSourceType nSrcType,
     341             :                     const char *pszUnprefixed, const char *pszJSonFlavor);
     342             :     void SetOptionsOnReader(GDALOpenInfo *poOpenInfo,
     343             :                             OGRGeoJSONReader *poReader);
     344             :     void CheckExceededTransferLimit(json_object *poObj);
     345             :     void RemoveJSonPStuff();
     346             : 
     347             :     CPL_DISALLOW_COPY_ASSIGN(OGRGeoJSONDataSource)
     348             : };
     349             : 
     350             : #endif  // OGR_GEOJSON_H_INCLUDED

Generated by: LCOV version 1.14