|
| 1 | +/****************************************************************************** |
| 2 | + * |
| 3 | + * Project: GDAL |
| 4 | + * Purpose: gdal "raster index" subcommand |
| 5 | + * Author: Even Rouault <even dot rouault at spatialys.com> |
| 6 | + * |
| 7 | + ****************************************************************************** |
| 8 | + * Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com> |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: MIT |
| 11 | + ****************************************************************************/ |
| 12 | + |
| 13 | +#include "gdalalg_raster_index.h" |
| 14 | + |
| 15 | +#include "cpl_conv.h" |
| 16 | +#include "gdal_priv.h" |
| 17 | +#include "gdal_utils_priv.h" |
| 18 | +#include "ogrsf_frmts.h" |
| 19 | + |
| 20 | +//! @cond Doxygen_Suppress |
| 21 | + |
| 22 | +#ifndef _ |
| 23 | +#define _(x) (x) |
| 24 | +#endif |
| 25 | + |
| 26 | +/************************************************************************/ |
| 27 | +/* GDALRasterIndexAlgorithm::GDALRasterIndexAlgorithm() */ |
| 28 | +/************************************************************************/ |
| 29 | + |
| 30 | +GDALRasterIndexAlgorithm::GDALRasterIndexAlgorithm() |
| 31 | + : GDALVectorOutputAbstractAlgorithm(NAME, DESCRIPTION, HELP_URL) |
| 32 | +{ |
| 33 | + AddProgressArg(); |
| 34 | + AddInputDatasetArg(&m_inputDatasets, GDAL_OF_RASTER) |
| 35 | + .SetAutoOpenDataset(false); |
| 36 | + GDALVectorOutputAbstractAlgorithm::AddAllOutputArgs(); |
| 37 | + |
| 38 | + AddCommonOptions(); |
| 39 | + |
| 40 | + AddArg("source-crs-field-name", 0, |
| 41 | + _("Name of the field to store the CRS of each dataset"), |
| 42 | + &m_sourceCrsName) |
| 43 | + .SetMinCharCount(1); |
| 44 | + AddArg("source-crs-format", 0, |
| 45 | + _("Format in which the CRS of each dataset must be written"), |
| 46 | + &m_sourceCrsFormat) |
| 47 | + .SetMinCharCount(1) |
| 48 | + .SetDefault(m_sourceCrsFormat) |
| 49 | + .SetChoices("auto", "WKT", "EPSG", "PROJ"); |
| 50 | +} |
| 51 | + |
| 52 | +/************************************************************************/ |
| 53 | +/* GDALRasterIndexAlgorithm::GDALRasterIndexAlgorithm() */ |
| 54 | +/************************************************************************/ |
| 55 | + |
| 56 | +GDALRasterIndexAlgorithm::GDALRasterIndexAlgorithm( |
| 57 | + const std::string &name, const std::string &description, |
| 58 | + const std::string &helpURL) |
| 59 | + : GDALVectorOutputAbstractAlgorithm(name, description, helpURL) |
| 60 | +{ |
| 61 | +} |
| 62 | + |
| 63 | +/************************************************************************/ |
| 64 | +/* GDALRasterIndexAlgorithm::AddCommonOptions() */ |
| 65 | +/************************************************************************/ |
| 66 | + |
| 67 | +void GDALRasterIndexAlgorithm::AddCommonOptions() |
| 68 | +{ |
| 69 | + AddArg("recursive", 0, |
| 70 | + _("Whether input directories should be explored recursively."), |
| 71 | + &m_recursive); |
| 72 | + AddArg("filename-filter", 0, |
| 73 | + _("Pattern that the filenames in input directories should follow " |
| 74 | + "('*' and '?' wildcard)"), |
| 75 | + &m_filenameFilter); |
| 76 | + AddArg("min-pixel-size", 0, |
| 77 | + _("Minimum pixel size in term of geospatial extent per pixel " |
| 78 | + "(resolution) that a raster should have to be selected."), |
| 79 | + &m_minPixelSize) |
| 80 | + .SetMinValueExcluded(0); |
| 81 | + AddArg("max-pixel-size", 0, |
| 82 | + _("Maximum pixel size in term of geospatial extent per pixel " |
| 83 | + "(resolution) that a raster should have to be selected."), |
| 84 | + &m_maxPixelSize) |
| 85 | + .SetMinValueExcluded(0); |
| 86 | + AddArg("location-name", 0, _("Name of the field with the raster path"), |
| 87 | + &m_locationName) |
| 88 | + .SetDefault(m_locationName) |
| 89 | + .SetMinCharCount(1); |
| 90 | + AddArg("absolute-path", 0, |
| 91 | + _("Whether the path to the input datasets should be stored as an " |
| 92 | + "absolute path"), |
| 93 | + &m_writeAbsolutePaths); |
| 94 | + AddArg("dst-crs", 0, _("Destination CRS"), &m_crs) |
| 95 | + .SetIsCRSArg() |
| 96 | + .AddHiddenAlias("t_srs"); |
| 97 | + |
| 98 | + { |
| 99 | + auto &arg = |
| 100 | + AddArg("metadata", 0, _("Add dataset metadata item"), &m_metadata) |
| 101 | + .SetMetaVar("<KEY>=<VALUE>"); |
| 102 | + arg.AddValidationAction([this, &arg]() |
| 103 | + { return ValidateKeyValue(arg); }); |
| 104 | + arg.AddHiddenAlias("mo"); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/************************************************************************/ |
| 109 | +/* GDALRasterIndexAlgorithm::RunImpl() */ |
| 110 | +/************************************************************************/ |
| 111 | + |
| 112 | +bool GDALRasterIndexAlgorithm::RunImpl(GDALProgressFunc pfnProgress, |
| 113 | + void *pProgressData) |
| 114 | +{ |
| 115 | + CPLStringList aosSources; |
| 116 | + for (auto &srcDS : m_inputDatasets) |
| 117 | + { |
| 118 | + if (srcDS.GetDatasetRef()) |
| 119 | + { |
| 120 | + ReportError( |
| 121 | + CE_Failure, CPLE_IllegalArg, |
| 122 | + "Input datasets must be provided by name, not as object"); |
| 123 | + return false; |
| 124 | + } |
| 125 | + aosSources.push_back(srcDS.GetName()); |
| 126 | + } |
| 127 | + |
| 128 | + auto setupRet = SetupOutputDataset(); |
| 129 | + if (!setupRet.outDS) |
| 130 | + return false; |
| 131 | + |
| 132 | + if (!SetDefaultOutputLayerNameIfNeeded(setupRet.outDS)) |
| 133 | + return false; |
| 134 | + |
| 135 | + CPLStringList aosOptions; |
| 136 | + if (m_recursive) |
| 137 | + { |
| 138 | + aosOptions.push_back("-recursive"); |
| 139 | + } |
| 140 | + for (const std::string &s : m_filenameFilter) |
| 141 | + { |
| 142 | + aosOptions.push_back("-filename_filter"); |
| 143 | + aosOptions.push_back(s); |
| 144 | + } |
| 145 | + if (m_minPixelSize > 0) |
| 146 | + { |
| 147 | + aosOptions.push_back("-min_pixel_size"); |
| 148 | + aosOptions.push_back(CPLSPrintf("%.17g", m_minPixelSize)); |
| 149 | + } |
| 150 | + if (m_maxPixelSize > 0) |
| 151 | + { |
| 152 | + aosOptions.push_back("-max_pixel_size"); |
| 153 | + aosOptions.push_back(CPLSPrintf("%.17g", m_maxPixelSize)); |
| 154 | + } |
| 155 | + |
| 156 | + if (!m_outputLayerName.empty()) |
| 157 | + { |
| 158 | + aosOptions.push_back("-lyr_name"); |
| 159 | + aosOptions.push_back(m_outputLayerName); |
| 160 | + } |
| 161 | + |
| 162 | + aosOptions.push_back("-tileindex"); |
| 163 | + aosOptions.push_back(m_locationName); |
| 164 | + |
| 165 | + if (m_writeAbsolutePaths) |
| 166 | + { |
| 167 | + aosOptions.push_back("-write_absolute_path"); |
| 168 | + } |
| 169 | + if (m_crs.empty()) |
| 170 | + { |
| 171 | + if (m_sourceCrsName.empty()) |
| 172 | + aosOptions.push_back("-skip_different_projection"); |
| 173 | + } |
| 174 | + else |
| 175 | + { |
| 176 | + aosOptions.push_back("-t_srs"); |
| 177 | + aosOptions.push_back(m_crs); |
| 178 | + } |
| 179 | + if (!m_sourceCrsName.empty()) |
| 180 | + { |
| 181 | + aosOptions.push_back("-src_srs_name"); |
| 182 | + aosOptions.push_back(m_sourceCrsName); |
| 183 | + |
| 184 | + aosOptions.push_back("-src_srs_format"); |
| 185 | + aosOptions.push_back(CPLString(m_sourceCrsFormat).toupper()); |
| 186 | + } |
| 187 | + |
| 188 | + for (const std::string &s : m_metadata) |
| 189 | + { |
| 190 | + aosOptions.push_back("-mo"); |
| 191 | + aosOptions.push_back(s); |
| 192 | + } |
| 193 | + |
| 194 | + if (!AddExtraOptions(aosOptions)) |
| 195 | + return false; |
| 196 | + |
| 197 | + std::unique_ptr<GDALTileIndexOptions, decltype(&GDALTileIndexOptionsFree)> |
| 198 | + options(GDALTileIndexOptionsNew(aosOptions.List(), nullptr), |
| 199 | + GDALTileIndexOptionsFree); |
| 200 | + |
| 201 | + if (options) |
| 202 | + { |
| 203 | + GDALTileIndexOptionsSetProgress(options.get(), pfnProgress, |
| 204 | + pProgressData); |
| 205 | + } |
| 206 | + |
| 207 | + const bool ret = |
| 208 | + options && GDALTileIndexInternal(m_outputDataset.GetName().c_str(), |
| 209 | + GDALDataset::ToHandle(setupRet.outDS), |
| 210 | + OGRLayer::ToHandle(setupRet.layer), |
| 211 | + aosSources.size(), aosSources.List(), |
| 212 | + options.get(), nullptr) != nullptr; |
| 213 | + |
| 214 | + if (ret && setupRet.newDS) |
| 215 | + { |
| 216 | + m_outputDataset.Set(std::move(setupRet.newDS)); |
| 217 | + } |
| 218 | + |
| 219 | + return ret; |
| 220 | +} |
| 221 | + |
| 222 | +//! @endcond |
0 commit comments