Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GDAL 4 : * Purpose: "write" step of "raster 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_raster_write.h" 14 : 15 : #include "cpl_string.h" 16 : #include "gdal_utils.h" 17 : #include "gdal_priv.h" 18 : 19 : //! @cond Doxygen_Suppress 20 : 21 : /************************************************************************/ 22 : /* GDALRasterWriteAlgorithm::GDALRasterWriteAlgorithm() */ 23 : /************************************************************************/ 24 : 25 98 : GDALRasterWriteAlgorithm::GDALRasterWriteAlgorithm() 26 : : GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, 27 98 : /* standaloneStep =*/false) 28 : { 29 98 : AddOutputArgs(/* hiddenForCLI = */ false); 30 98 : } 31 : 32 : /************************************************************************/ 33 : /* GDALRasterWriteAlgorithm::RunStep() */ 34 : /************************************************************************/ 35 : 36 64 : bool GDALRasterWriteAlgorithm::RunStep(GDALProgressFunc pfnProgress, 37 : void *pProgressData) 38 : { 39 64 : CPLAssert(m_inputDataset.GetDatasetRef()); 40 64 : CPLAssert(!m_outputDataset.GetDatasetRef()); 41 : 42 64 : if (m_format == "stream") 43 : { 44 4 : m_outputDataset.Set(m_inputDataset.GetDatasetRef()); 45 4 : return true; 46 : } 47 : 48 120 : CPLStringList aosOptions; 49 60 : if (!m_overwrite) 50 : { 51 48 : aosOptions.AddString("--no-overwrite"); 52 : } 53 60 : if (!m_format.empty()) 54 : { 55 30 : aosOptions.AddString("-of"); 56 30 : aosOptions.AddString(m_format.c_str()); 57 : } 58 61 : for (const auto &co : m_creationOptions) 59 : { 60 1 : aosOptions.AddString("-co"); 61 1 : aosOptions.AddString(co.c_str()); 62 : } 63 : 64 : GDALTranslateOptions *psOptions = 65 60 : GDALTranslateOptionsNew(aosOptions.List(), nullptr); 66 60 : GDALTranslateOptionsSetProgress(psOptions, pfnProgress, pProgressData); 67 : 68 60 : GDALDatasetH hSrcDS = GDALDataset::ToHandle(m_inputDataset.GetDatasetRef()); 69 60 : auto poRetDS = GDALDataset::FromHandle(GDALTranslate( 70 60 : m_outputDataset.GetName().c_str(), hSrcDS, psOptions, nullptr)); 71 60 : GDALTranslateOptionsFree(psOptions); 72 60 : if (!poRetDS) 73 1 : return false; 74 : 75 59 : m_outputDataset.Set(std::unique_ptr<GDALDataset>(poRetDS)); 76 : 77 59 : return true; 78 : } 79 : 80 : //! @endcond