LCOV - code coverage report
Current view: top level - apps - gdalalg_vector_write.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 49 50 98.0 %
Date: 2025-08-01 10:10:57 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  "write" step of "vector pipeline"
       5             :  * Author:   Even Rouault <even dot rouault at spatialys.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "gdalalg_vector_write.h"
      14             : 
      15             : #include "cpl_string.h"
      16             : #include "gdal_utils.h"
      17             : #include "gdal_priv.h"
      18             : 
      19             : #ifndef _
      20             : #define _(x) (x)
      21             : #endif
      22             : 
      23             : //! @cond Doxygen_Suppress
      24             : 
      25             : /************************************************************************/
      26             : /*          GDALVectorWriteAlgorithm::GDALVectorWriteAlgorithm()        */
      27             : /************************************************************************/
      28             : 
      29         256 : GDALVectorWriteAlgorithm::GDALVectorWriteAlgorithm()
      30             :     : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
      31         256 :                                       /* standaloneStep =*/false)
      32             : {
      33         256 :     AddVectorOutputArgs(/* hiddenForCLI = */ false,
      34             :                         /* shortNameOutputLayerAllowed=*/true);
      35             :     AddArg("skip-errors", 0, _("Skip errors when writing features"),
      36         512 :            &m_skipErrors)
      37         256 :         .AddHiddenAlias("skip-failures");  // For ogr2ogr nostalgic people
      38         256 : }
      39             : 
      40             : /************************************************************************/
      41             : /*                  GDALVectorWriteAlgorithm::RunStep()                 */
      42             : /************************************************************************/
      43             : 
      44         109 : bool GDALVectorWriteAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt)
      45             : {
      46         109 :     auto pfnProgress = ctxt.m_pfnProgress;
      47         109 :     auto pProgressData = ctxt.m_pProgressData;
      48         109 :     auto poSrcDS = m_inputDataset[0].GetDatasetRef();
      49         109 :     CPLAssert(poSrcDS);
      50             : 
      51         109 :     if (m_format == "stream")
      52             :     {
      53           8 :         m_outputDataset.Set(poSrcDS);
      54           8 :         return true;
      55             :     }
      56             : 
      57         202 :     CPLStringList aosOptions;
      58         101 :     aosOptions.AddString("--invoked-from-gdal-vector-convert");
      59         101 :     if (!m_overwrite)
      60             :     {
      61          95 :         aosOptions.AddString("--no-overwrite");
      62             :     }
      63         101 :     if (m_overwriteLayer)
      64             :     {
      65           2 :         aosOptions.AddString("-overwrite");
      66             :     }
      67         101 :     if (m_appendLayer)
      68             :     {
      69           2 :         aosOptions.AddString("-append");
      70             :     }
      71         101 :     if (!m_format.empty())
      72             :     {
      73          58 :         aosOptions.AddString("-of");
      74          58 :         aosOptions.AddString(m_format.c_str());
      75             :     }
      76         102 :     for (const auto &co : m_creationOptions)
      77             :     {
      78           1 :         aosOptions.AddString("-dsco");
      79           1 :         aosOptions.AddString(co.c_str());
      80             :     }
      81         102 :     for (const auto &co : m_layerCreationOptions)
      82             :     {
      83           1 :         aosOptions.AddString("-lco");
      84           1 :         aosOptions.AddString(co.c_str());
      85             :     }
      86         101 :     if (!m_outputLayerName.empty())
      87             :     {
      88           3 :         aosOptions.AddString("-nln");
      89           3 :         aosOptions.AddString(m_outputLayerName.c_str());
      90             :     }
      91         101 :     if (pfnProgress && pfnProgress != GDALDummyProgress)
      92             :     {
      93           6 :         aosOptions.AddString("-progress");
      94             :     }
      95         101 :     if (m_skipErrors)
      96             :     {
      97           1 :         aosOptions.AddString("-skipfailures");
      98             :     }
      99             : 
     100             :     GDALVectorTranslateOptions *psOptions =
     101         101 :         GDALVectorTranslateOptionsNew(aosOptions.List(), nullptr);
     102         101 :     GDALVectorTranslateOptionsSetProgress(psOptions, pfnProgress,
     103             :                                           pProgressData);
     104             : 
     105             :     GDALDatasetH hOutDS =
     106         101 :         GDALDataset::ToHandle(m_outputDataset.GetDatasetRef());
     107         101 :     GDALDatasetH hSrcDS = GDALDataset::ToHandle(poSrcDS);
     108         101 :     auto poRetDS = GDALDataset::FromHandle(
     109         101 :         GDALVectorTranslate(m_outputDataset.GetName().c_str(), hOutDS, 1,
     110             :                             &hSrcDS, psOptions, nullptr));
     111         101 :     GDALVectorTranslateOptionsFree(psOptions);
     112         101 :     if (!poRetDS)
     113           0 :         return false;
     114             : 
     115         101 :     if (!hOutDS)
     116             :     {
     117          95 :         m_outputDataset.Set(std::unique_ptr<GDALDataset>(poRetDS));
     118             :     }
     119             : 
     120         101 :     return true;
     121             : }
     122             : 
     123             : //! @endcond

Generated by: LCOV version 1.14