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