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