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