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