|
| 1 | +/****************************************************************************** |
| 2 | + * |
| 3 | + * Project: GDAL |
| 4 | + * Purpose: "gdal raster fillnodata" standalone command |
| 5 | + * Author: Alessandro Pasotti <elpaso at itopen dot it> |
| 6 | + * |
| 7 | + ****************************************************************************** |
| 8 | + * Copyright (c) 2025, Alessandro Pasotti <elpaso at itopen dot it> |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: MIT |
| 11 | + ****************************************************************************/ |
| 12 | + |
| 13 | +#include "gdalalg_raster_fillnodata.h" |
| 14 | + |
| 15 | +#include "gdal_priv.h" |
| 16 | +#include "gdal_utils.h" |
| 17 | +#include "gdal_alg.h" |
| 18 | + |
| 19 | +#include <algorithm> |
| 20 | + |
| 21 | +//! @cond Doxygen_Suppress |
| 22 | + |
| 23 | +#ifndef _ |
| 24 | +#define _(x) (x) |
| 25 | +#endif |
| 26 | + |
| 27 | +/************************************************************************/ |
| 28 | +/* GDALRasterFillNodataAlgorithm::GDALRasterFillNodataAlgorithm() */ |
| 29 | +/************************************************************************/ |
| 30 | + |
| 31 | +GDALRasterFillNodataAlgorithm::GDALRasterFillNodataAlgorithm() noexcept |
| 32 | + : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL) |
| 33 | +{ |
| 34 | + |
| 35 | + AddProgressArg(); |
| 36 | + |
| 37 | + AddInputFormatsArg(&m_inputFormats) |
| 38 | + .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_RASTER}); |
| 39 | + AddInputDatasetArg(&m_inputDataset, GDAL_OF_RASTER); |
| 40 | + AddOutputDatasetArg(&m_outputDataset, GDAL_OF_RASTER); |
| 41 | + AddOutputFormatArg(&m_format, /* bStreamAllowed = */ false, |
| 42 | + /* bGDALGAllowed = */ false) |
| 43 | + .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, |
| 44 | + {GDAL_DCAP_CREATE, GDAL_DCAP_RASTER}); |
| 45 | + |
| 46 | + AddCreationOptionsArg(&m_creationOptions); |
| 47 | + AddOverwriteArg(&m_overwrite); |
| 48 | + |
| 49 | + AddBandArg(&m_band).SetDefault(m_band); |
| 50 | + |
| 51 | + AddArg("max-distance", 'd', |
| 52 | + _("The maximum distance (in pixels) that the algorithm will search " |
| 53 | + "out for values to interpolate."), |
| 54 | + &m_maxDistance) |
| 55 | + .SetDefault(m_maxDistance) |
| 56 | + .SetMetaVar("MAX_DISTANCE"); |
| 57 | + |
| 58 | + AddArg("smoothing-iterations", 's', |
| 59 | + _("The number of 3x3 average filter smoothing iterations to run " |
| 60 | + "after the interpolation to dampen artifacts. The default is zero " |
| 61 | + "smoothing iterations."), |
| 62 | + &m_smoothingIterations) |
| 63 | + .SetDefault(m_smoothingIterations) |
| 64 | + .SetMetaVar("SMOOTHING_ITERATIONS"); |
| 65 | + |
| 66 | + auto &mask{AddArg("mask", 0, |
| 67 | + _("Use the first band of the specified file as a " |
| 68 | + "validity mask (zero is invalid, non-zero is valid)."), |
| 69 | + &m_maskDataset)}; |
| 70 | + |
| 71 | + SetAutoCompleteFunctionForFilename(mask, GDAL_OF_RASTER); |
| 72 | + |
| 73 | + mask.AddValidationAction( |
| 74 | + [&mask]() |
| 75 | + { |
| 76 | + // Check if the mask dataset is a valid raster dataset descriptor |
| 77 | + // Try to open the dataset as a raster |
| 78 | + std::unique_ptr<GDALDataset> poDS( |
| 79 | + GDALDataset::Open(mask.Get<std::string>().c_str(), |
| 80 | + GDAL_OF_RASTER, nullptr, nullptr, nullptr)); |
| 81 | + return static_cast<bool>(poDS); |
| 82 | + }); |
| 83 | + |
| 84 | + AddArg("strategy", 0, |
| 85 | + _("By default, pixels are interpolated using an inverse distance " |
| 86 | + "weighting (invdist). It is also possible to choose a nearest " |
| 87 | + "neighbour (nearest) strategy."), |
| 88 | + &m_strategy) |
| 89 | + .SetDefault(m_strategy) |
| 90 | + .SetChoices("invdist", "nearest"); |
| 91 | +} |
| 92 | + |
| 93 | +/************************************************************************/ |
| 94 | +/* GDALRasterFillNodataAlgorithm::RunImpl() */ |
| 95 | +/************************************************************************/ |
| 96 | + |
| 97 | +bool GDALRasterFillNodataAlgorithm::RunImpl(GDALProgressFunc pfnProgress, |
| 98 | + void *pProgressData) |
| 99 | +{ |
| 100 | + |
| 101 | + VSIStatBufL sStat; |
| 102 | + if (!m_overwrite && !m_outputDataset.GetName().empty() && |
| 103 | + (VSIStatL(m_outputDataset.GetName().c_str(), &sStat) == 0 || |
| 104 | + std::unique_ptr<GDALDataset>( |
| 105 | + GDALDataset::Open(m_outputDataset.GetName().c_str())))) |
| 106 | + { |
| 107 | + ReportError(CE_Failure, CPLE_AppDefined, |
| 108 | + "File '%s' already exists. Specify the --overwrite " |
| 109 | + "option to overwrite it.", |
| 110 | + m_outputDataset.GetName().c_str()); |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + CPLStringList aosOptions; |
| 115 | + aosOptions.AddString("-of"); |
| 116 | + aosOptions.AddString(m_format.c_str()); |
| 117 | + aosOptions.AddString("-b"); |
| 118 | + aosOptions.AddString(CPLSPrintf("%d", m_band)); |
| 119 | + |
| 120 | + for (const auto &co : m_creationOptions) |
| 121 | + { |
| 122 | + aosOptions.AddString("-co"); |
| 123 | + aosOptions.AddString(co.c_str()); |
| 124 | + } |
| 125 | + |
| 126 | + GDALTranslateOptions *psOptions = |
| 127 | + GDALTranslateOptionsNew(aosOptions.List(), nullptr); |
| 128 | + |
| 129 | + GDALDatasetH hSrcDS = GDALDataset::ToHandle(m_inputDataset.GetDatasetRef()); |
| 130 | + auto poRetDS = |
| 131 | + std::unique_ptr<GDALDataset>(GDALDataset::FromHandle(GDALTranslate( |
| 132 | + m_outputDataset.GetName().c_str(), hSrcDS, psOptions, nullptr))); |
| 133 | + GDALTranslateOptionsFree(psOptions); |
| 134 | + |
| 135 | + if (!poRetDS) |
| 136 | + { |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + GDALRasterBand *maskBand{nullptr}; |
| 141 | + if (m_maskDataset.GetDatasetRef()) |
| 142 | + { |
| 143 | + maskBand = m_maskDataset.GetDatasetRef()->GetRasterBand(1); |
| 144 | + if (!maskBand) |
| 145 | + { |
| 146 | + ReportError(CE_Failure, CPLE_AppDefined, "Cannot get mask band."); |
| 147 | + return false; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + GDALRasterBand *dstBand{poRetDS->GetRasterBand(1)}; |
| 152 | + // Prepare options to pass to GDALFillNodata |
| 153 | + CPLStringList aosFillOptions; |
| 154 | + |
| 155 | + if (EQUAL(m_strategy.c_str(), "nearest")) |
| 156 | + aosFillOptions.AddNameValue("INTERPOLATION", "NEAREST"); |
| 157 | + else |
| 158 | + aosFillOptions.AddNameValue("INTERPOLATION", |
| 159 | + "INV_DIST"); // default strategy |
| 160 | + |
| 161 | + auto retVal{GDALFillNodata( |
| 162 | + dstBand, maskBand, m_maxDistance, 0, m_smoothingIterations, |
| 163 | + aosFillOptions.List(), m_progressBarRequested ? pfnProgress : nullptr, |
| 164 | + m_progressBarRequested ? pProgressData : nullptr)}; |
| 165 | + if (retVal != CE_None) |
| 166 | + { |
| 167 | + ReportError(CE_Failure, CPLE_AppDefined, "Cannot run fillNodata."); |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + poRetDS->FlushCache(); |
| 172 | + |
| 173 | + m_outputDataset.Set(std::move(poRetDS)); |
| 174 | + |
| 175 | + return true; |
| 176 | +} |
| 177 | + |
| 178 | +//! @endcond |
0 commit comments