LCOV - code coverage report
Current view: top level - apps - gdalalg_vector_convert.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 62 62 100.0 %
Date: 2025-01-18 12:42:00 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          22 : GDALVectorConvertAlgorithm::GDALVectorConvertAlgorithm()
      30          22 :     : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
      31             : {
      32          22 :     AddProgressArg();
      33          22 :     AddOutputFormatArg(&m_outputFormat)
      34             :         .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES,
      35          66 :                          {GDAL_DCAP_VECTOR, GDAL_DCAP_CREATE});
      36          22 :     AddOpenOptionsArg(&m_openOptions);
      37          22 :     AddInputFormatsArg(&m_inputFormats)
      38          44 :         .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_VECTOR});
      39          22 :     AddInputDatasetArg(&m_inputDataset, GDAL_OF_VECTOR);
      40          22 :     AddOutputDatasetArg(&m_outputDataset, GDAL_OF_VECTOR);
      41          22 :     m_outputDataset.SetInputFlags(GADV_NAME | GADV_OBJECT);
      42          22 :     AddCreationOptionsArg(&m_creationOptions);
      43          22 :     AddLayerCreationOptionsArg(&m_layerCreationOptions);
      44          22 :     AddOverwriteArg(&m_overwrite);
      45          22 :     AddUpdateArg(&m_update);
      46             :     AddArg("overwrite-layer", 0,
      47             :            _("Whether overwriting existing layer is allowed"),
      48          44 :            &m_overwriteLayer)
      49          22 :         .SetDefault(false);
      50             :     AddArg("append", 0, _("Whether appending to existing layer is allowed"),
      51          44 :            &m_appendLayer)
      52          22 :         .SetDefault(false);
      53          44 :     AddArg("input-layer", 'l', _("Input layer name(s)"), &m_inputLayerNames)
      54          22 :         .AddAlias("layer");
      55          44 :     AddArg("output-layer", 0, _("Output layer name"), &m_outputLayerName)
      56          22 :         .AddHiddenAlias("nln");  // For ogr2ogr nostalgic people
      57          22 : }
      58             : 
      59             : /************************************************************************/
      60             : /*                  GDALVectorConvertAlgorithm::RunImpl()               */
      61             : /************************************************************************/
      62             : 
      63          11 : bool GDALVectorConvertAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
      64             :                                          void *pProgressData)
      65             : {
      66          11 :     CPLAssert(m_inputDataset.GetDatasetRef());
      67             : 
      68          22 :     CPLStringList aosOptions;
      69          11 :     aosOptions.AddString("--invoked-from-gdal-vector-convert");
      70          11 :     if (!m_overwrite)
      71             :     {
      72          10 :         aosOptions.AddString("--no-overwrite");
      73             :     }
      74          11 :     if (m_overwriteLayer)
      75             :     {
      76           1 :         aosOptions.AddString("-overwrite");
      77             :     }
      78          11 :     if (m_appendLayer)
      79             :     {
      80           1 :         aosOptions.AddString("-append");
      81             :     }
      82          11 :     if (!m_outputFormat.empty())
      83             :     {
      84           2 :         aosOptions.AddString("-of");
      85           2 :         aosOptions.AddString(m_outputFormat.c_str());
      86             :     }
      87          12 :     for (const auto &co : m_creationOptions)
      88             :     {
      89           1 :         aosOptions.AddString("-dsco");
      90           1 :         aosOptions.AddString(co.c_str());
      91             :     }
      92          12 :     for (const auto &co : m_layerCreationOptions)
      93             :     {
      94           1 :         aosOptions.AddString("-lco");
      95           1 :         aosOptions.AddString(co.c_str());
      96             :     }
      97          11 :     if (!m_outputLayerName.empty())
      98             :     {
      99           2 :         aosOptions.AddString("-nln");
     100           2 :         aosOptions.AddString(m_outputLayerName.c_str());
     101             :     }
     102          11 :     if (pfnProgress && pfnProgress != GDALDummyProgress)
     103             :     {
     104           1 :         aosOptions.AddString("-progress");
     105             :     }
     106             : 
     107             :     // Must be last, as positional
     108          12 :     for (const auto &name : m_inputLayerNames)
     109             :     {
     110           1 :         aosOptions.AddString(name.c_str());
     111             :     }
     112             : 
     113             :     GDALVectorTranslateOptions *psOptions =
     114          11 :         GDALVectorTranslateOptionsNew(aosOptions.List(), nullptr);
     115             : 
     116          11 :     GDALVectorTranslateOptionsSetProgress(psOptions, pfnProgress,
     117             :                                           pProgressData);
     118             : 
     119             :     GDALDatasetH hOutDS =
     120          11 :         GDALDataset::ToHandle(m_outputDataset.GetDatasetRef());
     121          11 :     GDALDatasetH hSrcDS = GDALDataset::ToHandle(m_inputDataset.GetDatasetRef());
     122          11 :     auto poRetDS = GDALDataset::FromHandle(
     123          11 :         GDALVectorTranslate(m_outputDataset.GetName().c_str(), hOutDS, 1,
     124             :                             &hSrcDS, psOptions, nullptr));
     125          11 :     GDALVectorTranslateOptionsFree(psOptions);
     126          11 :     if (!poRetDS)
     127           2 :         return false;
     128             : 
     129           9 :     if (!hOutDS)
     130             :     {
     131           8 :         m_outputDataset.Set(std::unique_ptr<GDALDataset>(poRetDS));
     132             :     }
     133             : 
     134           9 :     return true;
     135             : }
     136             : 
     137             : //! @endcond

Generated by: LCOV version 1.14