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