LCOV - code coverage report
Current view: top level - apps - gdalalg_vector_convert.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 61 61 100.0 %
Date: 2025-06-19 12:30:01 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  gdal "vector convert" subcommand
       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_convert.h"
      14             : 
      15             : #include "cpl_conv.h"
      16             : #include "gdal_priv.h"
      17             : #include "gdal_utils.h"
      18             : 
      19             : //! @cond Doxygen_Suppress
      20             : 
      21             : #ifndef _
      22             : #define _(x) (x)
      23             : #endif
      24             : 
      25             : /************************************************************************/
      26             : /*          GDALVectorConvertAlgorithm::GDALVectorConvertAlgorithm()    */
      27             : /************************************************************************/
      28             : 
      29          29 : GDALVectorConvertAlgorithm::GDALVectorConvertAlgorithm()
      30          29 :     : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
      31             : {
      32          29 :     AddProgressArg();
      33          29 :     AddOutputFormatArg(&m_outputFormat)
      34             :         .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES,
      35          87 :                          {GDAL_DCAP_VECTOR, GDAL_DCAP_CREATE});
      36          29 :     AddOpenOptionsArg(&m_openOptions);
      37          29 :     AddInputFormatsArg(&m_inputFormats)
      38          58 :         .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_VECTOR});
      39          29 :     AddInputDatasetArg(&m_inputDataset, GDAL_OF_VECTOR);
      40          29 :     AddOutputDatasetArg(&m_outputDataset, GDAL_OF_VECTOR)
      41          29 :         .SetDatasetInputFlags(GADV_NAME | GADV_OBJECT);
      42          29 :     AddCreationOptionsArg(&m_creationOptions);
      43          29 :     AddLayerCreationOptionsArg(&m_layerCreationOptions);
      44          29 :     AddOverwriteArg(&m_overwrite);
      45          29 :     AddUpdateArg(&m_update);
      46          29 :     AddOverwriteLayerArg(&m_overwriteLayer);
      47          29 :     AddAppendLayerArg(&m_appendLayer);
      48          58 :     AddArg("input-layer", 'l', _("Input layer name(s)"), &m_inputLayerNames)
      49          29 :         .AddAlias("layer");
      50          58 :     AddArg("output-layer", 0, _("Output layer name"), &m_outputLayerName)
      51          29 :         .AddHiddenAlias("nln");  // For ogr2ogr nostalgic people
      52          29 : }
      53             : 
      54             : /************************************************************************/
      55             : /*                  GDALVectorConvertAlgorithm::RunImpl()               */
      56             : /************************************************************************/
      57             : 
      58          14 : bool GDALVectorConvertAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
      59             :                                          void *pProgressData)
      60             : {
      61          14 :     CPLAssert(m_inputDataset.GetDatasetRef());
      62             : 
      63          28 :     CPLStringList aosOptions;
      64          14 :     aosOptions.AddString("--invoked-from-gdal-vector-convert");
      65          14 :     if (!m_overwrite)
      66             :     {
      67          11 :         aosOptions.AddString("--no-overwrite");
      68             :     }
      69          14 :     if (m_overwriteLayer)
      70             :     {
      71           1 :         aosOptions.AddString("-overwrite");
      72             :     }
      73          14 :     if (m_appendLayer)
      74             :     {
      75           1 :         aosOptions.AddString("-append");
      76             :     }
      77          14 :     if (!m_outputFormat.empty())
      78             :     {
      79           6 :         aosOptions.AddString("-of");
      80           6 :         aosOptions.AddString(m_outputFormat.c_str());
      81             :     }
      82          15 :     for (const auto &co : m_creationOptions)
      83             :     {
      84           1 :         aosOptions.AddString("-dsco");
      85           1 :         aosOptions.AddString(co.c_str());
      86             :     }
      87          17 :     for (const auto &co : m_layerCreationOptions)
      88             :     {
      89           3 :         aosOptions.AddString("-lco");
      90           3 :         aosOptions.AddString(co.c_str());
      91             :     }
      92          14 :     if (!m_outputLayerName.empty())
      93             :     {
      94           2 :         aosOptions.AddString("-nln");
      95           2 :         aosOptions.AddString(m_outputLayerName.c_str());
      96             :     }
      97          14 :     if (pfnProgress && pfnProgress != GDALDummyProgress)
      98             :     {
      99           1 :         aosOptions.AddString("-progress");
     100             :     }
     101             : 
     102             :     // Must be last, as positional
     103          15 :     for (const auto &name : m_inputLayerNames)
     104             :     {
     105           1 :         aosOptions.AddString(name.c_str());
     106             :     }
     107             : 
     108             :     std::unique_ptr<GDALVectorTranslateOptions,
     109             :                     decltype(&GDALVectorTranslateOptionsFree)>
     110             :         psOptions(GDALVectorTranslateOptionsNew(aosOptions.List(), nullptr),
     111          14 :                   GDALVectorTranslateOptionsFree);
     112          14 :     bool bOK = false;
     113          14 :     if (psOptions)
     114             :     {
     115          14 :         GDALVectorTranslateOptionsSetProgress(psOptions.get(), pfnProgress,
     116             :                                               pProgressData);
     117             :         GDALDatasetH hOutDS =
     118          14 :             GDALDataset::ToHandle(m_outputDataset.GetDatasetRef());
     119             :         GDALDatasetH hSrcDS =
     120          14 :             GDALDataset::ToHandle(m_inputDataset.GetDatasetRef());
     121          14 :         auto poRetDS = GDALDataset::FromHandle(
     122          14 :             GDALVectorTranslate(m_outputDataset.GetName().c_str(), hOutDS, 1,
     123          14 :                                 &hSrcDS, psOptions.get(), nullptr));
     124          14 :         bOK = poRetDS != nullptr;
     125          14 :         if (!hOutDS)
     126             :         {
     127          11 :             m_outputDataset.Set(std::unique_ptr<GDALDataset>(poRetDS));
     128             :         }
     129             :     }
     130             : 
     131          28 :     return bOK;
     132             : }
     133             : 
     134             : //! @endcond

Generated by: LCOV version 1.14