Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: OpenGIS Simple Features Reference Implementation 4 : * Purpose: Streaming parser for GeoJSON-like FeatureCollection 5 : * Author: Even Rouault <even.rouault at spatialys.com> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2017, Even Rouault <even.rouault at spatialys.com> 9 : * 10 : * Permission is hereby granted, free of charge, to any person obtaining a 11 : * copy of this software and associated documentation files (the "Software"), 12 : * to deal in the Software without restriction, including without limitation 13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 : * and/or sell copies of the Software, and to permit persons to whom the 15 : * Software is furnished to do so, subject to the following conditions: 16 : * 17 : * The above copyright notice and this permission notice shall be included 18 : * in all copies or substantial portions of the Software. 19 : * 20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 : * DEALINGS IN THE SOFTWARE. 27 : ****************************************************************************/ 28 : 29 : #ifndef OGRJSONCOLLECTIONSTREAMING_PARSER_H_INCLUDED 30 : #define OGRJSONCOLLECTIONSTREAMING_PARSER_H_INCLUDED 31 : 32 : #include "cpl_json_streaming_parser.h" 33 : 34 : #include <json.h> // JSON-C 35 : 36 : /************************************************************************/ 37 : /* OGRJSONCollectionStreamingParser */ 38 : /************************************************************************/ 39 : 40 : /** Streaming parser for GeoJSON-like FeatureCollection */ 41 : class OGRJSONCollectionStreamingParser CPL_NON_FINAL 42 : : public CPLJSonStreamingParser 43 : { 44 : bool m_bFirstPass = false; 45 : 46 : int m_nDepth = 0; 47 : bool m_bInFeatures = false; 48 : bool m_bCanEasilyAppend = false; 49 : bool m_bInFeaturesArray = false; 50 : bool m_bInCoordinates = false; 51 : bool m_bInType = false; 52 : bool m_bIsTypeKnown = false; 53 : bool m_bIsFeatureCollection = false; 54 : json_object *m_poRootObj = nullptr; 55 : size_t m_nRootObjMemEstimate = 0; 56 : json_object *m_poCurObj = nullptr; 57 : size_t m_nCurObjMemEstimate = 0; 58 : GUIntBig m_nTotalOGRFeatureMemEstimate = 0; 59 : bool m_bKeySet = false; 60 : std::string m_osCurKey{}; 61 : std::vector<json_object *> m_apoCurObj{}; 62 : std::vector<bool> m_abFirstMember{}; 63 : bool m_bStoreNativeData = false; 64 : std::string m_osJson{}; 65 : size_t m_nMaxObjectSize = 0; 66 : 67 : bool m_bStartFeature = false; 68 : bool m_bEndFeature = false; 69 : 70 : void AppendObject(json_object *poNewObj); 71 : 72 : CPL_DISALLOW_COPY_ASSIGN(OGRJSONCollectionStreamingParser) 73 : 74 : protected: 75 103 : inline bool IsFirstPass() const 76 : { 77 103 : return m_bFirstPass; 78 : } 79 : 80 : virtual void GotFeature(json_object *poObj, bool bFirstPass, 81 : const std::string &osJson) = 0; 82 : virtual void TooComplex() = 0; 83 : 84 : public: 85 : OGRJSONCollectionStreamingParser(bool bFirstPass, bool bStoreNativeData, 86 : size_t nMaxObjectSize); 87 : ~OGRJSONCollectionStreamingParser(); 88 : 89 : virtual void String(const char * /*pszValue*/, size_t) override; 90 : virtual void Number(const char * /*pszValue*/, size_t) override; 91 : virtual void Boolean(bool b) override; 92 : virtual void Null() override; 93 : 94 : virtual void StartObject() override; 95 : virtual void EndObject() override; 96 : virtual void StartObjectMember(const char * /*pszKey*/, size_t) override; 97 : 98 : virtual void StartArray() override; 99 : virtual void EndArray() override; 100 : virtual void StartArrayMember() override; 101 : 102 : virtual void Exception(const char * /*pszMessage*/) override; 103 : 104 : json_object *StealRootObject(); 105 : 106 709 : inline bool IsTypeKnown() const 107 : { 108 709 : return m_bIsTypeKnown; 109 : } 110 : 111 707 : inline bool IsFeatureCollection() const 112 : { 113 707 : return m_bIsFeatureCollection; 114 : } 115 : 116 241 : inline GUIntBig GetTotalOGRFeatureMemEstimate() const 117 : { 118 241 : return m_nTotalOGRFeatureMemEstimate; 119 : } 120 : 121 241 : inline bool CanEasilyAppend() const 122 : { 123 241 : return m_bCanEasilyAppend; 124 : } 125 : 126 1668 : inline void ResetFeatureDetectionState() 127 : { 128 1668 : m_bStartFeature = false; 129 1668 : m_bEndFeature = false; 130 1668 : } 131 : 132 1668 : inline bool IsStartFeature() const 133 : { 134 1668 : return m_bStartFeature; 135 : } 136 : 137 1650 : inline bool IsEndFeature() const 138 : { 139 1650 : return m_bEndFeature; 140 : } 141 : }; 142 : 143 : #endif // OGRJSONCOLLECTIONSTREAMING_PARSER_H_INCLUDED