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