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
|
/******************************************************************************
*
* Project: GDAL
* Purpose: "select" 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_select.h"
#include "gdal_priv.h"
#include "gdal_utils.h"
//! @cond Doxygen_Suppress
#ifndef _
#define _(x) (x)
#endif
/************************************************************************/
/* GDALRasterSelectAlgorithm::GDALRasterSelectAlgorithm() */
/************************************************************************/
GDALRasterSelectAlgorithm::GDALRasterSelectAlgorithm(bool standaloneStep)
: GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
standaloneStep)
{
{
auto &arg =
AddArg("band", 'b',
_("Band(s) (1-based index, 'mask' or 'mask:<band>')"),
&m_bands)
.SetPositional()
.SetRequired()
.SetMinCount(1);
arg.AddValidationAction(
[&arg]()
{
const auto &val = arg.Get<std::vector<std::string>>();
for (const auto &v : val)
{
if (!STARTS_WITH(v.c_str(), "mask") &&
!(CPLGetValueType(v.c_str()) == CPL_VALUE_INTEGER &&
atoi(v.c_str()) >= 1))
{
CPLError(CE_Failure, CPLE_AppDefined,
"Invalid band specification.");
return false;
}
}
return true;
});
}
{
auto &arg = AddArg(
"mask", 0,
_("Mask band (1-based index, 'mask', 'mask:<band>' or 'none')"),
&m_mask);
arg.AddValidationAction(
[&arg]()
{
const auto &v = arg.Get<std::string>();
if (!STARTS_WITH(v.c_str(), "mask") &&
!EQUAL(v.c_str(), "none") &&
!(CPLGetValueType(v.c_str()) == CPL_VALUE_INTEGER &&
atoi(v.c_str()) >= 1))
{
CPLError(CE_Failure, CPLE_AppDefined,
"Invalid mask band specification.");
return false;
}
return true;
});
}
}
/************************************************************************/
/* GDALRasterSelectAlgorithm::RunStep() */
/************************************************************************/
bool GDALRasterSelectAlgorithm::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");
for (const std::string &v : m_bands)
{
aosOptions.AddString("-b");
aosOptions.AddString(CPLString(v).replaceAll(':', ',').c_str());
}
if (!m_mask.empty())
{
aosOptions.AddString("-mask");
aosOptions.AddString(CPLString(m_mask).replaceAll(':', ',').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;
}
GDALRasterSelectAlgorithmStandalone::~GDALRasterSelectAlgorithmStandalone() =
default;
//! @endcond
|