LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/jsonfg - ogrjsonfgstreamingparser.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 31 35 88.6 %
Date: 2024-11-21 22:18:42 Functions: 5 6 83.3 %

          Line data    Source code
       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         291 : static size_t OGRJSONFGStreamingParserGetMaxObjectSize()
      20             : {
      21             :     const double dfTmp =
      22         291 :         CPLAtof(CPLGetConfigOption("OGR_JSONFG_MAX_OBJ_SIZE", "200"));
      23         291 :     return dfTmp > 0 ? static_cast<size_t>(dfTmp * 1024 * 1024) : 0;
      24             : }
      25             : 
      26             : /************************************************************************/
      27             : /*                     OGRJSONFGStreamingParser()                       */
      28             : /************************************************************************/
      29             : 
      30         291 : OGRJSONFGStreamingParser::OGRJSONFGStreamingParser(OGRJSONFGReader &oReader,
      31         291 :                                                    bool bFirstPass)
      32             :     : OGRJSONCollectionStreamingParser(
      33             :           bFirstPass, /*bStoreNativeData=*/false,
      34             :           OGRJSONFGStreamingParserGetMaxObjectSize()),
      35         291 :       m_oReader(oReader)
      36             : {
      37         291 : }
      38             : 
      39             : /************************************************************************/
      40             : /*                   ~OGRJSONFGStreamingParser()                        */
      41             : /************************************************************************/
      42             : 
      43             : OGRJSONFGStreamingParser::~OGRJSONFGStreamingParser() = default;
      44             : 
      45             : /************************************************************************/
      46             : /*                OGRJSONFGStreamingParser::Clone()                     */
      47             : /************************************************************************/
      48             : 
      49         103 : std::unique_ptr<OGRJSONFGStreamingParser> OGRJSONFGStreamingParser::Clone()
      50             : {
      51             :     auto poRet =
      52         103 :         std::make_unique<OGRJSONFGStreamingParser>(m_oReader, IsFirstPass());
      53         103 :     poRet->m_osRequestedLayer = m_osRequestedLayer;
      54         103 :     return poRet;
      55             : }
      56             : 
      57             : /************************************************************************/
      58             : /*                          GetNextFeature()                            */
      59             : /************************************************************************/
      60             : 
      61             : std::pair<std::unique_ptr<OGRFeature>, OGRLayer *>
      62         543 : OGRJSONFGStreamingParser::GetNextFeature()
      63             : {
      64         543 :     if (m_nCurFeatureIdx < m_apoFeatures.size())
      65             :     {
      66         776 :         auto poRet = std::move(m_apoFeatures[m_nCurFeatureIdx]);
      67         388 :         m_apoFeatures[m_nCurFeatureIdx].first = nullptr;
      68         388 :         m_apoFeatures[m_nCurFeatureIdx].second = nullptr;
      69         388 :         m_nCurFeatureIdx++;
      70         388 :         return poRet;
      71             :     }
      72         155 :     m_nCurFeatureIdx = 0;
      73         155 :     m_apoFeatures.clear();
      74         155 :     return std::pair(nullptr, nullptr);
      75             : }
      76             : 
      77             : /************************************************************************/
      78             : /*                          AnalyzeFeature()                            */
      79             : /************************************************************************/
      80             : 
      81         708 : void OGRJSONFGStreamingParser::GotFeature(json_object *poObj, bool bFirstPass,
      82             :                                           const std::string & /*osJson*/)
      83             : {
      84         708 :     if (bFirstPass)
      85             :     {
      86         167 :         m_oReader.GenerateLayerDefnFromFeature(poObj);
      87             :     }
      88             :     else
      89             :     {
      90         541 :         OGRJSONFGStreamedLayer *poStreamedLayer = nullptr;
      91         541 :         auto poFeat = m_oReader.ReadFeature(poObj, m_osRequestedLayer.c_str(),
      92        1082 :                                             nullptr, &poStreamedLayer);
      93         541 :         if (poFeat)
      94             :         {
      95         538 :             CPLAssert(poStreamedLayer);
      96             :             m_apoFeatures.emplace_back(
      97         538 :                 std::pair(std::move(poFeat), poStreamedLayer));
      98             :         }
      99             :     }
     100         708 : }
     101             : 
     102             : /************************************************************************/
     103             : /*                            TooComplex()                              */
     104             : /************************************************************************/
     105             : 
     106           0 : void OGRJSONFGStreamingParser::TooComplex()
     107             : {
     108           0 :     if (!ExceptionOccurred())
     109           0 :         EmitException("JSON object too complex/large. You may define the "
     110             :                       "OGR_JSONFG_MAX_OBJ_SIZE configuration option to "
     111             :                       "a value in megabytes to allow "
     112             :                       "for larger features, or 0 to remove any size limit.");
     113           0 : }

Generated by: LCOV version 1.14