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