Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GDAL 4 : * Purpose: "write" step of "mdim pipeline" 5 : * Author: Even Rouault <even dot rouault at spatialys.com> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2026, Even Rouault <even dot rouault at spatialys.com> 9 : * 10 : * SPDX-License-Identifier: MIT 11 : ****************************************************************************/ 12 : 13 : #include "gdalalg_mdim_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 : /* GDALMdimWriteAlgorithm::GDALMdimWriteAlgorithm() */ 23 : /************************************************************************/ 24 : 25 6 : GDALMdimWriteAlgorithm::GDALMdimWriteAlgorithm() 26 : : GDALMdimPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, 27 6 : ConstructorOptions()) 28 : { 29 6 : AddMdimOutputArgs(/* hiddenForCLI = */ false); 30 6 : } 31 : 32 : /************************************************************************/ 33 : /* GDALMdimWriteAlgorithm::RunStep() */ 34 : /************************************************************************/ 35 : 36 5 : bool GDALMdimWriteAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt) 37 : { 38 5 : auto pfnProgress = ctxt.m_pfnProgress; 39 5 : auto pProgressData = ctxt.m_pProgressData; 40 5 : auto poSrcDS = m_inputDataset[0].GetDatasetRef(); 41 5 : CPLAssert(poSrcDS); 42 5 : CPLAssert(!m_outputDataset.GetDatasetRef()); 43 : 44 5 : if (m_format == "stream") 45 : { 46 1 : m_outputDataset.Set(poSrcDS); 47 1 : return true; 48 : } 49 : 50 8 : CPLStringList aosOptions; 51 4 : if (!m_overwrite) 52 : { 53 4 : aosOptions.AddString("--no-overwrite"); 54 : } 55 4 : if (!m_format.empty()) 56 : { 57 0 : aosOptions.AddString("-of"); 58 0 : aosOptions.AddString(m_format.c_str()); 59 : } 60 4 : for (const auto &co : m_creationOptions) 61 : { 62 0 : aosOptions.AddString("-co"); 63 0 : aosOptions.AddString(co.c_str()); 64 : } 65 : 66 : GDALMultiDimTranslateOptions *psOptions = 67 4 : GDALMultiDimTranslateOptionsNew(aosOptions.List(), nullptr); 68 4 : GDALMultiDimTranslateOptionsSetProgress(psOptions, pfnProgress, 69 : pProgressData); 70 : 71 : // Backup error state since GDALMultiDimTranslate() resets it multiple times 72 4 : const auto nLastErrorNum = CPLGetLastErrorNo(); 73 4 : const auto nLastErrorType = CPLGetLastErrorType(); 74 8 : const std::string osLastErrorMsg = CPLGetLastErrorMsg(); 75 4 : const auto nLastErrorCounter = CPLGetErrorCounter(); 76 : 77 4 : GDALDatasetH hSrcDS = GDALDataset::ToHandle(poSrcDS); 78 4 : auto poRetDS = GDALDataset::FromHandle( 79 4 : GDALMultiDimTranslate(m_outputDataset.GetName().c_str(), nullptr, 1, 80 : &hSrcDS, psOptions, nullptr)); 81 4 : GDALMultiDimTranslateOptionsFree(psOptions); 82 : 83 4 : if (nLastErrorCounter > 0 && CPLGetErrorCounter() == 0) 84 : { 85 0 : CPLErrorSetState(nLastErrorType, nLastErrorNum, osLastErrorMsg.c_str(), 86 : &nLastErrorCounter); 87 : } 88 : 89 4 : if (!poRetDS) 90 1 : return false; 91 : 92 3 : m_outputDataset.Set(std::unique_ptr<GDALDataset>(poRetDS)); 93 : 94 3 : return true; 95 : } 96 : 97 : //! @endcond