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