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 551 : GDALRasterWriteAlgorithm::GDALRasterWriteAlgorithm() 26 : : GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, 27 551 : /* standaloneStep =*/false) 28 : { 29 551 : AddRasterOutputArgs(/* hiddenForCLI = */ false); 30 551 : } 31 : 32 : /************************************************************************/ 33 : /* GDALRasterWriteAlgorithm::RunStep() */ 34 : /************************************************************************/ 35 : 36 244 : bool GDALRasterWriteAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt) 37 : { 38 244 : auto pfnProgress = ctxt.m_pfnProgress; 39 244 : auto pProgressData = ctxt.m_pProgressData; 40 244 : auto poSrcDS = m_inputDataset[0].GetDatasetRef(); 41 244 : CPLAssert(poSrcDS); 42 244 : CPLAssert(!m_outputDataset.GetDatasetRef()); 43 : 44 244 : if (m_format == "stream") 45 : { 46 34 : m_outputDataset.Set(poSrcDS); 47 34 : return true; 48 : } 49 : 50 420 : CPLStringList aosOptions; 51 210 : if (!m_overwrite) 52 : { 53 194 : aosOptions.AddString("--no-overwrite"); 54 : } 55 210 : if (m_appendRaster) 56 : { 57 2 : aosOptions.AddString("-co"); 58 2 : aosOptions.AddString("APPEND_SUBDATASET=YES"); 59 : } 60 210 : if (!m_format.empty()) 61 : { 62 106 : aosOptions.AddString("-of"); 63 106 : aosOptions.AddString(m_format.c_str()); 64 : } 65 227 : for (const auto &co : m_creationOptions) 66 : { 67 17 : aosOptions.AddString("-co"); 68 17 : aosOptions.AddString(co.c_str()); 69 : } 70 : 71 : GDALTranslateOptions *psOptions = 72 210 : GDALTranslateOptionsNew(aosOptions.List(), nullptr); 73 210 : GDALTranslateOptionsSetProgress(psOptions, pfnProgress, pProgressData); 74 : 75 : // Backup error state since GDALTranslate() resets it multiple times 76 210 : const auto nLastErrorNum = CPLGetLastErrorNo(); 77 210 : const auto nLastErrorType = CPLGetLastErrorType(); 78 420 : const std::string osLastErrorMsg = CPLGetLastErrorMsg(); 79 210 : const auto nLastErrorCounter = CPLGetErrorCounter(); 80 : 81 210 : GDALDatasetH hSrcDS = GDALDataset::ToHandle(poSrcDS); 82 210 : auto poRetDS = GDALDataset::FromHandle(GDALTranslate( 83 210 : m_outputDataset.GetName().c_str(), hSrcDS, psOptions, nullptr)); 84 210 : GDALTranslateOptionsFree(psOptions); 85 : 86 210 : if (nLastErrorCounter > 0 && CPLGetErrorCounter() == 0) 87 : { 88 9 : CPLErrorSetState(nLastErrorType, nLastErrorNum, osLastErrorMsg.c_str(), 89 : &nLastErrorCounter); 90 : } 91 : 92 210 : if (!poRetDS) 93 4 : return false; 94 : 95 206 : m_outputDataset.Set(std::unique_ptr<GDALDataset>(poRetDS)); 96 : 97 206 : return true; 98 : } 99 : 100 : //! @endcond