LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/jsonfg - ogrjsonfgstreamingparser.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 33 37 89.2 %
Date: 2025-10-17 19:28:35 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         363 : static size_t OGRJSONFGStreamingParserGetMaxObjectSize()
      20             : {
      21             :     const double dfTmp =
      22         363 :         CPLAtof(CPLGetConfigOption("OGR_JSONFG_MAX_OBJ_SIZE", "200"));
      23         363 :     return dfTmp > 0 ? static_cast<size_t>(dfTmp * 1024 * 1024) : 0;
      24             : }
      25             : 
      26             : /************************************************************************/
      27             : /*                     OGRJSONFGStreamingParser()                       */
      28             : /************************************************************************/
      29             : 
      30         363 : OGRJSONFGStreamingParser::OGRJSONFGStreamingParser(OGRJSONFGReader &oReader,
      31             :                                                    bool bFirstPass,
      32         363 :                                                    bool bHasTopLevelMeasures)
      33             :     : OGRJSONCollectionStreamingParser(
      34             :           bFirstPass, /*bStoreNativeData=*/false,
      35             :           OGRJSONFGStreamingParserGetMaxObjectSize()),
      36         363 :       m_oReader(oReader)
      37             : {
      38         363 :     m_bHasTopLevelMeasures = bHasTopLevelMeasures;
      39         363 : }
      40             : 
      41             : /************************************************************************/
      42             : /*                   ~OGRJSONFGStreamingParser()                        */
      43             : /************************************************************************/
      44             : 
      45             : OGRJSONFGStreamingParser::~OGRJSONFGStreamingParser() = default;
      46             : 
      47             : /************************************************************************/
      48             : /*                OGRJSONFGStreamingParser::Clone()                     */
      49             : /************************************************************************/
      50             : 
      51         107 : std::unique_ptr<OGRJSONFGStreamingParser> OGRJSONFGStreamingParser::Clone()
      52             : {
      53             :     auto poRet = std::make_unique<OGRJSONFGStreamingParser>(
      54         107 :         m_oReader, IsFirstPass(), m_bHasTopLevelMeasures);
      55         107 :     poRet->m_osRequestedLayer = m_osRequestedLayer;
      56         107 :     return poRet;
      57             : }
      58             : 
      59             : /************************************************************************/
      60             : /*                          GetNextFeature()                            */
      61             : /************************************************************************/
      62             : 
      63             : std::pair<std::unique_ptr<OGRFeature>, OGRLayer *>
      64         630 : OGRJSONFGStreamingParser::GetNextFeature()
      65             : {
      66         630 :     if (m_nCurFeatureIdx < m_apoFeatures.size())
      67             :     {
      68         862 :         auto poRet = std::move(m_apoFeatures[m_nCurFeatureIdx]);
      69         431 :         m_apoFeatures[m_nCurFeatureIdx].first = nullptr;
      70         431 :         m_apoFeatures[m_nCurFeatureIdx].second = nullptr;
      71         431 :         m_nCurFeatureIdx++;
      72         431 :         return poRet;
      73             :     }
      74         199 :     m_nCurFeatureIdx = 0;
      75         199 :     m_apoFeatures.clear();
      76         199 :     return std::pair(nullptr, nullptr);
      77             : }
      78             : 
      79             : /************************************************************************/
      80             : /*                          AnalyzeFeature()                            */
      81             : /************************************************************************/
      82             : 
      83         794 : void OGRJSONFGStreamingParser::GotFeature(json_object *poObj, bool bFirstPass,
      84             :                                           const std::string & /*osJson*/)
      85             : {
      86         794 :     if (bFirstPass)
      87             :     {
      88         201 :         m_oReader.GenerateLayerDefnFromFeature(poObj);
      89             :     }
      90             :     else
      91             :     {
      92         593 :         OGRJSONFGStreamedLayer *poStreamedLayer = nullptr;
      93         593 :         auto poFeat = m_oReader.ReadFeature(poObj, m_osRequestedLayer.c_str(),
      94         593 :                                             m_bHasTopLevelMeasures, nullptr,
      95        1186 :                                             &poStreamedLayer);
      96         593 :         if (poFeat)
      97             :         {
      98         590 :             CPLAssert(poStreamedLayer);
      99             :             m_apoFeatures.emplace_back(
     100         590 :                 std::pair(std::move(poFeat), poStreamedLayer));
     101             :         }
     102             :     }
     103         794 : }
     104             : 
     105             : /************************************************************************/
     106             : /*                            TooComplex()                              */
     107             : /************************************************************************/
     108             : 
     109           0 : void OGRJSONFGStreamingParser::TooComplex()
     110             : {
     111           0 :     if (!ExceptionOccurred())
     112           0 :         EmitException("JSON object too complex/large. You may define the "
     113             :                       "OGR_JSONFG_MAX_OBJ_SIZE configuration option to "
     114             :                       "a value in megabytes to allow "
     115             :                       "for larger features, or 0 to remove any size limit.");
     116           0 : }

Generated by: LCOV version 1.14