/src/gdal/ogr/ogrsf_frmts/jsonfg/ogrjsonfgstreamingparser.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: OpenGIS Simple Features Reference Implementation |
4 | | * Purpose: Implementation of OGC Features and Geometries JSON (JSON-FG) |
5 | | * Author: Even Rouault <even.rouault at spatialys.com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2023, Even Rouault <even.rouault at spatialys.com> |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #include "ogr_jsonfg.h" |
14 | | |
15 | | /************************************************************************/ |
16 | | /* OGRJSONFGStreamingParserGetMaxObjectSize() */ |
17 | | /************************************************************************/ |
18 | | |
19 | | static size_t OGRJSONFGStreamingParserGetMaxObjectSize() |
20 | 165 | { |
21 | 165 | const double dfTmp = |
22 | 165 | CPLAtof(CPLGetConfigOption("OGR_JSONFG_MAX_OBJ_SIZE", "200")); |
23 | 165 | return dfTmp > 0 ? static_cast<size_t>(dfTmp * 1024 * 1024) : 0; |
24 | 165 | } |
25 | | |
26 | | /************************************************************************/ |
27 | | /* OGRJSONFGStreamingParser() */ |
28 | | /************************************************************************/ |
29 | | |
30 | | OGRJSONFGStreamingParser::OGRJSONFGStreamingParser(OGRJSONFGReader &oReader, |
31 | | bool bFirstPass) |
32 | 165 | : OGRJSONCollectionStreamingParser( |
33 | 165 | bFirstPass, /*bStoreNativeData=*/false, |
34 | 165 | OGRJSONFGStreamingParserGetMaxObjectSize()), |
35 | 165 | m_oReader(oReader) |
36 | 165 | { |
37 | 165 | } |
38 | | |
39 | | /************************************************************************/ |
40 | | /* ~OGRJSONFGStreamingParser() */ |
41 | | /************************************************************************/ |
42 | | |
43 | 165 | OGRJSONFGStreamingParser::~OGRJSONFGStreamingParser() = default; |
44 | | |
45 | | /************************************************************************/ |
46 | | /* OGRJSONFGStreamingParser::Clone() */ |
47 | | /************************************************************************/ |
48 | | |
49 | | std::unique_ptr<OGRJSONFGStreamingParser> OGRJSONFGStreamingParser::Clone() |
50 | 0 | { |
51 | 0 | auto poRet = |
52 | 0 | std::make_unique<OGRJSONFGStreamingParser>(m_oReader, IsFirstPass()); |
53 | 0 | poRet->m_osRequestedLayer = m_osRequestedLayer; |
54 | 0 | return poRet; |
55 | 0 | } |
56 | | |
57 | | /************************************************************************/ |
58 | | /* GetNextFeature() */ |
59 | | /************************************************************************/ |
60 | | |
61 | | std::pair<std::unique_ptr<OGRFeature>, OGRLayer *> |
62 | | OGRJSONFGStreamingParser::GetNextFeature() |
63 | 32 | { |
64 | 32 | if (m_nCurFeatureIdx < m_apoFeatures.size()) |
65 | 11 | { |
66 | 11 | auto poRet = std::move(m_apoFeatures[m_nCurFeatureIdx]); |
67 | 11 | m_apoFeatures[m_nCurFeatureIdx].first = nullptr; |
68 | 11 | m_apoFeatures[m_nCurFeatureIdx].second = nullptr; |
69 | 11 | m_nCurFeatureIdx++; |
70 | 11 | return poRet; |
71 | 11 | } |
72 | 21 | m_nCurFeatureIdx = 0; |
73 | 21 | m_apoFeatures.clear(); |
74 | 21 | return std::pair(nullptr, nullptr); |
75 | 32 | } |
76 | | |
77 | | /************************************************************************/ |
78 | | /* AnalyzeFeature() */ |
79 | | /************************************************************************/ |
80 | | |
81 | | void OGRJSONFGStreamingParser::GotFeature(json_object *poObj, bool bFirstPass, |
82 | | const std::string & /*osJson*/) |
83 | 252 | { |
84 | 252 | if (bFirstPass) |
85 | 241 | { |
86 | 241 | m_oReader.GenerateLayerDefnFromFeature(poObj); |
87 | 241 | } |
88 | 11 | else |
89 | 11 | { |
90 | 11 | OGRJSONFGStreamedLayer *poStreamedLayer = nullptr; |
91 | 11 | auto poFeat = m_oReader.ReadFeature(poObj, m_osRequestedLayer.c_str(), |
92 | 11 | nullptr, &poStreamedLayer); |
93 | 11 | if (poFeat) |
94 | 11 | { |
95 | 11 | CPLAssert(poStreamedLayer); |
96 | 11 | m_apoFeatures.emplace_back( |
97 | 11 | std::pair(std::move(poFeat), poStreamedLayer)); |
98 | 11 | } |
99 | 11 | } |
100 | 252 | } |
101 | | |
102 | | /************************************************************************/ |
103 | | /* TooComplex() */ |
104 | | /************************************************************************/ |
105 | | |
106 | | void OGRJSONFGStreamingParser::TooComplex() |
107 | 0 | { |
108 | 0 | if (!ExceptionOccurred()) |
109 | 0 | EmitException("JSON object too complex/large. You may define the " |
110 | 0 | "OGR_JSONFG_MAX_OBJ_SIZE configuration option to " |
111 | 0 | "a value in megabytes to allow " |
112 | 0 | "for larger features, or 0 to remove any size limit."); |
113 | 0 | } |