LCOV - code coverage report
Current view: top level - apps - gdalalg_vector_info.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 56 56 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 info" 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_info.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             : /*            GDALVectorInfoAlgorithm::GDALVectorInfoAlgorithm()        */
      27             : /************************************************************************/
      28             : 
      29          35 : GDALVectorInfoAlgorithm::GDALVectorInfoAlgorithm()
      30          35 :     : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
      31             : {
      32          35 :     AddOutputFormatArg(&m_format).SetDefault("json").SetChoices("json", "text");
      33          35 :     AddOpenOptionsArg(&m_openOptions);
      34          35 :     AddInputFormatsArg(&m_inputFormats)
      35          70 :         .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_VECTOR});
      36          35 :     AddInputDatasetArg(&m_dataset, GDAL_OF_VECTOR);
      37          35 :     AddLayerNameArg(&m_layerNames).SetMutualExclusionGroup("layer-sql");
      38             :     AddArg("features", 0,
      39             :            _("List all features (beware of RAM consumption on large layers)"),
      40          35 :            &m_listFeatures);
      41             :     AddArg("sql", 0,
      42             :            _("Execute the indicated SQL statement and return the result"),
      43          70 :            &m_sql)
      44          35 :         .SetReadFromFileAtSyntaxAllowed()
      45          70 :         .SetMetaVar("<statement>|@<filename>")
      46          35 :         .SetRemoveSQLCommentsEnabled()
      47          35 :         .SetMutualExclusionGroup("layer-sql");
      48             :     AddArg("where", 0,
      49             :            _("Attribute query in a restricted form of the queries used in the "
      50             :              "SQL WHERE statement"),
      51          70 :            &m_where)
      52          35 :         .SetReadFromFileAtSyntaxAllowed()
      53          70 :         .SetMetaVar("<WHERE>|@<filename>")
      54          35 :         .SetRemoveSQLCommentsEnabled();
      55          35 :     AddArg("dialect", 0, _("SQL dialect"), &m_dialect);
      56             :     AddArg(GDAL_ARG_NAME_UPDATE, 0, _("Open the dataset in update mode"),
      57          35 :            &m_update);
      58          35 :     AddOutputStringArg(&m_output);
      59             :     AddArg("stdout", 0,
      60             :            _("Directly output on stdout (format=text mode only). If enabled, "
      61             :              "output-string will be empty"),
      62          70 :            &m_stdout)
      63          35 :         .SetHiddenForCLI();
      64          35 : }
      65             : 
      66             : /************************************************************************/
      67             : /*                  GDALVectorInfoAlgorithm::RunImpl()                  */
      68             : /************************************************************************/
      69             : 
      70          16 : bool GDALVectorInfoAlgorithm::RunImpl(GDALProgressFunc, void *)
      71             : {
      72          16 :     CPLAssert(m_dataset.GetDatasetRef());
      73             : 
      74          32 :     CPLStringList aosOptions;
      75          16 :     if (m_format == "json")
      76             :     {
      77           9 :         aosOptions.AddString("-json");
      78             :     }
      79             :     else
      80             :     {
      81           7 :         aosOptions.AddString("-al");
      82           7 :         if (!m_listFeatures)
      83           7 :             aosOptions.AddString("-so");
      84             :     }
      85          16 :     if (m_listFeatures)
      86             :     {
      87           2 :         aosOptions.AddString("-features");
      88             :     }
      89          16 :     if (!m_sql.empty())
      90             :     {
      91           2 :         aosOptions.AddString("-sql");
      92           2 :         aosOptions.AddString(m_sql.c_str());
      93             :     }
      94          16 :     if (!m_where.empty())
      95             :     {
      96           2 :         aosOptions.AddString("-where");
      97           2 :         aosOptions.AddString(m_where.c_str());
      98             :     }
      99          16 :     if (!m_dialect.empty())
     100             :     {
     101           2 :         aosOptions.AddString("-dialect");
     102           2 :         aosOptions.AddString(m_dialect.c_str());
     103             :     }
     104          16 :     if (m_stdout)
     105             :     {
     106           1 :         aosOptions.AddString("-stdout");
     107             :     }
     108             : 
     109             :     // Must be last, as positional
     110          16 :     aosOptions.AddString("dummy");
     111          18 :     for (const std::string &name : m_layerNames)
     112           2 :         aosOptions.AddString(name.c_str());
     113             : 
     114             :     GDALVectorInfoOptions *psInfo =
     115          16 :         GDALVectorInfoOptionsNew(aosOptions.List(), nullptr);
     116          16 :     char *ret = GDALVectorInfo(GDALDataset::ToHandle(m_dataset.GetDatasetRef()),
     117             :                                psInfo);
     118          16 :     GDALVectorInfoOptionsFree(psInfo);
     119          16 :     if (!ret)
     120           1 :         return false;
     121             : 
     122          15 :     m_output = ret;
     123          15 :     CPLFree(ret);
     124             : 
     125          15 :     return true;
     126             : }
     127             : 
     128             : //! @endcond

Generated by: LCOV version 1.14