File: gdalalg_raster_scale.cpp

package info (click to toggle)
gdal 3.12.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 92,396 kB
  • sloc: cpp: 1,224,305; ansic: 206,456; python: 26,284; java: 6,001; xml: 4,769; sh: 3,869; cs: 2,513; yacc: 1,306; makefile: 214
file content (148 lines) | stat: -rw-r--r-- 4,919 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/******************************************************************************
 *
 * Project:  GDAL
 * Purpose:  "scale" step of "raster pipeline"
 * Author:   Even Rouault <even dot rouault at spatialys.com>
 *
 ******************************************************************************
 * Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com>
 *
 * SPDX-License-Identifier: MIT
 ****************************************************************************/

#include "gdalalg_raster_scale.h"

#include "gdal_priv.h"
#include "gdal_utils.h"

#include <cmath>

//! @cond Doxygen_Suppress

#ifndef _
#define _(x) (x)
#endif

/************************************************************************/
/*         GDALRasterScaleAlgorithm::GDALRasterScaleAlgorithm()        */
/************************************************************************/

GDALRasterScaleAlgorithm::GDALRasterScaleAlgorithm(bool standaloneStep)
    : GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
                                      standaloneStep)
{
    AddOutputDataTypeArg(&m_type);
    AddBandArg(&m_band,
               _("Select band to restrict the scaling (1-based index)"));
    AddArg("src-min", 0, _("Minimum value of the source range"), &m_srcMin);
    AddArg("src-max", 0, _("Maximum value of the source range"), &m_srcMax);
    AddArg("dst-min", 0, _("Minimum value of the destination range"),
           &m_dstMin);
    AddArg("dst-max", 0, _("Maximum value of the destination range"),
           &m_dstMax);
    AddArg("exponent", 0,
           _("Exponent to apply non-linear scaling with a power function"),
           &m_exponent);
    AddArg("no-clip", 0, _("Do not clip input values to [srcmin, srcmax]"),
           &m_noClip);
}

/************************************************************************/
/*               GDALRasterScaleAlgorithm::RunStep()                    */
/************************************************************************/

bool GDALRasterScaleAlgorithm::RunStep(GDALPipelineStepRunContext &)
{
    auto poSrcDS = m_inputDataset[0].GetDatasetRef();
    CPLAssert(poSrcDS);
    CPLAssert(m_outputDataset.GetName().empty());
    CPLAssert(!m_outputDataset.GetDatasetRef());

    CPLStringList aosOptions;
    aosOptions.AddString("-of");
    aosOptions.AddString("VRT");
    if (!m_type.empty())
    {
        aosOptions.AddString("-ot");
        aosOptions.AddString(m_type.c_str());
    }
    aosOptions.AddString(m_band > 0 ? CPLSPrintf("-scale_%d", m_band)
                                    : "-scale");
    if (!std::isnan(m_srcMin))
    {
        if (std::isnan(m_srcMax))
        {
            ReportError(CE_Failure, CPLE_AppDefined,
                        "src-max must be specified when src-min is specified");
            return false;
        }
        aosOptions.AddString(CPLSPrintf("%.17g", m_srcMin));
        aosOptions.AddString(CPLSPrintf("%.17g", m_srcMax));
    }
    else if (!std::isnan(m_srcMax))
    {
        ReportError(CE_Failure, CPLE_AppDefined,
                    "src-min must be specified when src-max is specified");
        return false;
    }

    if (!std::isnan(m_dstMin))
    {
        if (std::isnan(m_dstMax))
        {
            ReportError(CE_Failure, CPLE_AppDefined,
                        "dst-max must be specified when dst-min is specified");
            return false;
        }
        if (std::isnan(m_srcMin))
        {
            aosOptions.AddString("NaN");
            aosOptions.AddString("NaN");
        }
        aosOptions.AddString(CPLSPrintf("%.17g", m_dstMin));
        aosOptions.AddString(CPLSPrintf("%.17g", m_dstMax));
    }
    else if (!std::isnan(m_dstMax))
    {
        ReportError(CE_Failure, CPLE_AppDefined,
                    "dst-min must be specified when dst-max is specified");
        return false;
    }

    if (!std::isnan(m_exponent))
    {
        aosOptions.AddString(m_band > 0 ? CPLSPrintf("-exponent_%d", m_band)
                                        : "-exponent");
        aosOptions.AddString(CPLSPrintf("%.17g", m_exponent));
    }
    else if (!m_noClip)
    {
        aosOptions.AddString(m_band > 0 ? CPLSPrintf("-exponent_%d", m_band)
                                        : "-exponent");
        aosOptions.AddString("1");
    }

    if (m_noClip)
    {
        aosOptions.AddString("--no-clip");
    }

    GDALTranslateOptions *psOptions =
        GDALTranslateOptionsNew(aosOptions.List(), nullptr);

    auto poOutDS = std::unique_ptr<GDALDataset>(GDALDataset::FromHandle(
        GDALTranslate("", GDALDataset::ToHandle(poSrcDS), psOptions, nullptr)));
    GDALTranslateOptionsFree(psOptions);
    const bool bRet = poOutDS != nullptr;
    if (poOutDS)
    {
        m_outputDataset.Set(std::move(poOutDS));
    }

    return bRet;
}

GDALRasterScaleAlgorithmStandalone::~GDALRasterScaleAlgorithmStandalone() =
    default;

//! @endcond