Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GDAL 4 : * Purpose: gdal "vector export-schema" subcommand 5 : * Author: Alessandro Pasotti <elpaso at itopen dot it> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2026, Alessandro Pasotti <elpaso at itopen dot it> 9 : * 10 : * SPDX-License-Identifier: MIT 11 : ****************************************************************************/ 12 : 13 : #include "gdalalg_vector_export_schema.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 : /* GDALVectorExportSchemaAlgorithm::GDALVectorExportSchemaAlgorithm() */ 27 : /************************************************************************/ 28 : 29 80 : GDALVectorExportSchemaAlgorithm::GDALVectorExportSchemaAlgorithm( 30 80 : bool standaloneStep) 31 : : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, 32 0 : ConstructorOptions() 33 80 : .SetStandaloneStep(standaloneStep) 34 80 : .SetInputDatasetMaxCount(1) 35 160 : .SetAddDefaultArguments(false)) 36 : { 37 80 : AddOpenOptionsArg(&m_openOptions).SetHiddenForCLI(!standaloneStep); 38 80 : AddInputFormatsArg(&m_inputFormats) 39 240 : .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_VECTOR}) 40 80 : .SetHiddenForCLI(!standaloneStep); 41 : 42 : auto &datasetArg = 43 80 : AddInputDatasetArg(&m_inputDataset, GDAL_OF_VECTOR).AddAlias("dataset"); 44 80 : if (!standaloneStep) 45 35 : datasetArg.SetHidden(); 46 80 : auto &layerArg = AddLayerNameArg(&m_layerNames).AddAlias("layer"); 47 80 : SetAutoCompleteFunctionForLayerName(layerArg, datasetArg); 48 80 : AddOutputStringArg(&m_output); 49 80 : AddStdoutArg(&m_stdout); 50 80 : } 51 : 52 : /************************************************************************/ 53 : /* GDALVectorExportSchemaAlgorithm::RunStep() */ 54 : /************************************************************************/ 55 : 56 3 : bool GDALVectorExportSchemaAlgorithm::RunStep(GDALPipelineStepRunContext &) 57 : { 58 3 : CPLAssert(m_inputDataset.size() == 1); 59 3 : auto poSrcDS = m_inputDataset[0].GetDatasetRef(); 60 3 : CPLAssert(poSrcDS); 61 : 62 6 : CPLStringList aosOptions; 63 : 64 3 : aosOptions.AddString("-schema"); 65 3 : aosOptions.AddString("--cli"); 66 : 67 : // Must be last, as positional 68 3 : aosOptions.AddString("dummy"); 69 : 70 3 : for (const std::string &name : m_layerNames) 71 0 : aosOptions.AddString(name.c_str()); 72 : 73 3 : if (m_layerNames.empty()) 74 : { 75 3 : aosOptions.AddString("-al"); 76 : } 77 : 78 : GDALVectorInfoOptions *psInfo = 79 3 : GDALVectorInfoOptionsNew(aosOptions.List(), nullptr); 80 : 81 3 : char *ret = GDALVectorInfo(GDALDataset::ToHandle(poSrcDS), psInfo); 82 3 : GDALVectorInfoOptionsFree(psInfo); 83 3 : if (!ret) 84 0 : return false; 85 : 86 3 : m_output = ret; 87 3 : CPLFree(ret); 88 : 89 3 : return true; 90 : } 91 : 92 : GDALVectorExportSchemaAlgorithmStandalone:: 93 : ~GDALVectorExportSchemaAlgorithmStandalone() = default; 94 : 95 : //! @endcond