Coverage Report

Created: 2026-09-14 06:50

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