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
|
/******************************************************************************
*
* Project: GDAL
* Purpose: "resize" 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_resize.h"
#include "gdal_priv.h"
#include "gdal_utils.h"
//! @cond Doxygen_Suppress
#ifndef _
#define _(x) (x)
#endif
/************************************************************************/
/* GDALRasterResizeAlgorithm::GDALRasterResizeAlgorithm() */
/************************************************************************/
GDALRasterResizeAlgorithm::GDALRasterResizeAlgorithm(bool standaloneStep)
: GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
standaloneStep)
{
AddArg("resolution", 0, _("Target resolution (in destination CRS units)"),
&m_resolution)
.SetMinCount(2)
.SetMaxCount(2)
.SetMinValueExcluded(0)
.SetRepeatedArgAllowed(false)
.SetDisplayHintAboutRepetition(false)
.SetMetaVar("<xres>,<yres>")
.SetMutualExclusionGroup("resolution-size");
AddArg("size", 0,
_("Target size in pixels (or percentage if using '%' suffix)"),
&m_size)
.SetMinCount(2)
.SetMaxCount(2)
.SetRequired()
.SetMinValueIncluded(0)
.SetRepeatedArgAllowed(false)
.SetDisplayHintAboutRepetition(false)
.SetMetaVar("<width[%]>,<height[%]>")
.SetMutualExclusionGroup("resolution-size")
.AddValidationAction(
[this]()
{
for (const auto &s : m_size)
{
char *endptr = nullptr;
const double val = CPLStrtod(s.c_str(), &endptr);
bool ok = false;
if (endptr == s.c_str() + s.size())
{
if (val >= 0 && val <= INT_MAX &&
static_cast<int>(val) == val)
{
ok = true;
}
}
else if (endptr && ((endptr[0] == ' ' && endptr[1] == '%' &&
endptr + 2 == s.c_str() + s.size()) ||
(endptr[0] == '%' &&
endptr + 1 == s.c_str() + s.size())))
{
if (val >= 0)
{
ok = true;
}
}
if (!ok)
{
ReportError(CE_Failure, CPLE_IllegalArg,
"Invalid size value: %s'", s.c_str());
return false;
}
}
return true;
});
AddArg("resampling", 'r', _("Resampling method"), &m_resampling)
.SetChoices("nearest", "bilinear", "cubic", "cubicspline", "lanczos",
"average", "mode")
.SetDefault("nearest")
.SetHiddenChoices("near");
}
/************************************************************************/
/* GDALRasterResizeAlgorithm::RunStep() */
/************************************************************************/
bool GDALRasterResizeAlgorithm::RunStep(GDALPipelineStepRunContext &)
{
const 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_size.empty())
{
aosOptions.AddString("-outsize");
aosOptions.AddString(m_size[0]);
aosOptions.AddString(m_size[1]);
}
if (!m_resolution.empty())
{
aosOptions.AddString("-tr");
aosOptions.AddString(CPLSPrintf("%.17g", m_resolution[0]));
aosOptions.AddString(CPLSPrintf("%.17g", m_resolution[1]));
}
if (!m_resampling.empty())
{
aosOptions.AddString("-r");
aosOptions.AddString(m_resampling.c_str());
}
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;
}
GDALRasterResizeAlgorithmStandalone::~GDALRasterResizeAlgorithmStandalone() =
default;
//! @endcond
|