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 365 : GDALVectorWriteAlgorithm::GDALVectorWriteAlgorithm() 30 : : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, 31 365 : /* standaloneStep =*/false) 32 : { 33 365 : AddVectorOutputArgs(/* hiddenForCLI = */ false, 34 : /* shortNameOutputLayerAllowed=*/true); 35 365 : } 36 : 37 : /************************************************************************/ 38 : /* GDALVectorWriteAlgorithm::RunStep() */ 39 : /************************************************************************/ 40 : 41 148 : bool GDALVectorWriteAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt) 42 : { 43 148 : auto pfnProgress = ctxt.m_pfnProgress; 44 148 : auto pProgressData = ctxt.m_pProgressData; 45 148 : auto poSrcDS = m_inputDataset[0].GetDatasetRef(); 46 148 : CPLAssert(poSrcDS); 47 : 48 148 : if (m_format == "stream") 49 : { 50 18 : m_outputDataset.Set(poSrcDS); 51 18 : return true; 52 : } 53 : 54 260 : CPLStringList aosOptions; 55 130 : aosOptions.AddString("--invoked-from-gdal-vector-convert"); 56 130 : if (!m_overwrite) 57 : { 58 121 : aosOptions.AddString("--no-overwrite"); 59 : } 60 130 : if (m_overwriteLayer) 61 : { 62 3 : aosOptions.AddString("-overwrite"); 63 : } 64 130 : if (m_appendLayer) 65 : { 66 3 : aosOptions.AddString("-append"); 67 : } 68 130 : if (m_upsert) 69 : { 70 2 : aosOptions.AddString("-upsert"); 71 : } 72 130 : if (!m_format.empty()) 73 : { 74 73 : aosOptions.AddString("-of"); 75 73 : aosOptions.AddString(m_format.c_str()); 76 : } 77 132 : for (const auto &co : m_creationOptions) 78 : { 79 2 : aosOptions.AddString("-dsco"); 80 2 : aosOptions.AddString(co.c_str()); 81 : } 82 134 : for (const auto &co : m_layerCreationOptions) 83 : { 84 4 : aosOptions.AddString("-lco"); 85 4 : aosOptions.AddString(co.c_str()); 86 : } 87 130 : if (!m_outputLayerName.empty()) 88 : { 89 5 : aosOptions.AddString("-nln"); 90 5 : aosOptions.AddString(m_outputLayerName.c_str()); 91 : } 92 130 : if (pfnProgress && pfnProgress != GDALDummyProgress) 93 : { 94 8 : aosOptions.AddString("-progress"); 95 : } 96 130 : if (m_skipErrors) 97 : { 98 2 : aosOptions.AddString("-skipfailures"); 99 : } 100 : 101 130 : GDALDataset *poRetDS = nullptr; 102 : GDALDatasetH hOutDS = 103 130 : GDALDataset::ToHandle(m_outputDataset.GetDatasetRef()); 104 : GDALVectorTranslateOptions *psOptions = 105 130 : GDALVectorTranslateOptionsNew(aosOptions.List(), nullptr); 106 130 : if (psOptions) 107 : { 108 130 : GDALVectorTranslateOptionsSetProgress(psOptions, pfnProgress, 109 : pProgressData); 110 : 111 130 : GDALDatasetH hSrcDS = GDALDataset::ToHandle(poSrcDS); 112 130 : poRetDS = GDALDataset::FromHandle( 113 130 : GDALVectorTranslate(m_outputDataset.GetName().c_str(), hOutDS, 1, 114 : &hSrcDS, psOptions, nullptr)); 115 130 : GDALVectorTranslateOptionsFree(psOptions); 116 : } 117 130 : if (!poRetDS) 118 3 : return false; 119 : 120 127 : if (!hOutDS) 121 : { 122 117 : m_outputDataset.Set(std::unique_ptr<GDALDataset>(poRetDS)); 123 : } 124 : 125 127 : return true; 126 : } 127 : 128 : //! @endcond