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