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