/src/gdal/gcore/gdalalgorithm.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GDAL |
4 | | * Purpose: GDALAlgorithm class |
5 | | * Author: Even Rouault <even dot rouault at spatialys.com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com> |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #include "cpl_port.h" |
14 | | #include "cpl_conv.h" |
15 | | #include "cpl_error.h" |
16 | | #include "cpl_json.h" |
17 | | #include "cpl_levenshtein.h" |
18 | | #include "cpl_minixml.h" |
19 | | #include "cpl_multiproc.h" |
20 | | |
21 | | #include "gdalalgorithm.h" |
22 | | #include "gdal_priv.h" |
23 | | #include "ogrsf_frmts.h" |
24 | | #include "ogr_spatialref.h" |
25 | | #include "vrtdataset.h" |
26 | | |
27 | | #include <algorithm> |
28 | | #include <cassert> |
29 | | #include <cerrno> |
30 | | #include <cmath> |
31 | | #include <cstdlib> |
32 | | #include <limits> |
33 | | #include <map> |
34 | | #include <string_view> |
35 | | |
36 | | #ifndef _ |
37 | 0 | #define _(x) (x) |
38 | | #endif |
39 | | |
40 | | constexpr const char *GDAL_ARG_NAME_INPUT_FORMAT = "input-format"; |
41 | | |
42 | | constexpr const char *GDAL_ARG_NAME_OUTPUT_DATA_TYPE = "output-data-type"; |
43 | | |
44 | | constexpr const char *GDAL_ARG_NAME_OPEN_OPTION = "open-option"; |
45 | | |
46 | | constexpr const char *GDAL_ARG_NAME_BAND = "band"; |
47 | | |
48 | | //! @cond Doxygen_Suppress |
49 | | struct GDALAlgorithmArgHS |
50 | | { |
51 | | GDALAlgorithmArg *ptr = nullptr; |
52 | | |
53 | 0 | explicit GDALAlgorithmArgHS(GDALAlgorithmArg *arg) : ptr(arg) |
54 | 0 | { |
55 | 0 | } |
56 | | }; |
57 | | |
58 | | //! @endcond |
59 | | |
60 | | //! @cond Doxygen_Suppress |
61 | | struct GDALArgDatasetValueHS |
62 | | { |
63 | | GDALArgDatasetValue val{}; |
64 | | GDALArgDatasetValue *ptr = nullptr; |
65 | | |
66 | 0 | GDALArgDatasetValueHS() : ptr(&val) |
67 | 0 | { |
68 | 0 | } |
69 | | |
70 | 0 | explicit GDALArgDatasetValueHS(GDALArgDatasetValue *arg) : ptr(arg) |
71 | 0 | { |
72 | 0 | } |
73 | | |
74 | | GDALArgDatasetValueHS(const GDALArgDatasetValueHS &) = delete; |
75 | | GDALArgDatasetValueHS &operator=(const GDALArgDatasetValueHS &) = delete; |
76 | | }; |
77 | | |
78 | | //! @endcond |
79 | | |
80 | | /************************************************************************/ |
81 | | /* GDALAlgorithmArgTypeIsList() */ |
82 | | /************************************************************************/ |
83 | | |
84 | | bool GDALAlgorithmArgTypeIsList(GDALAlgorithmArgType type) |
85 | 0 | { |
86 | 0 | switch (type) |
87 | 0 | { |
88 | 0 | case GAAT_BOOLEAN: |
89 | 0 | case GAAT_STRING: |
90 | 0 | case GAAT_INTEGER: |
91 | 0 | case GAAT_REAL: |
92 | 0 | case GAAT_DATASET: |
93 | 0 | break; |
94 | | |
95 | 0 | case GAAT_STRING_LIST: |
96 | 0 | case GAAT_INTEGER_LIST: |
97 | 0 | case GAAT_REAL_LIST: |
98 | 0 | case GAAT_DATASET_LIST: |
99 | 0 | return true; |
100 | 0 | } |
101 | | |
102 | 0 | return false; |
103 | 0 | } |
104 | | |
105 | | /************************************************************************/ |
106 | | /* GDALAlgorithmArgTypeName() */ |
107 | | /************************************************************************/ |
108 | | |
109 | | const char *GDALAlgorithmArgTypeName(GDALAlgorithmArgType type) |
110 | 0 | { |
111 | 0 | switch (type) |
112 | 0 | { |
113 | 0 | case GAAT_BOOLEAN: |
114 | 0 | break; |
115 | 0 | case GAAT_STRING: |
116 | 0 | return "string"; |
117 | 0 | case GAAT_INTEGER: |
118 | 0 | return "integer"; |
119 | 0 | case GAAT_REAL: |
120 | 0 | return "real"; |
121 | 0 | case GAAT_DATASET: |
122 | 0 | return "dataset"; |
123 | 0 | case GAAT_STRING_LIST: |
124 | 0 | return "string_list"; |
125 | 0 | case GAAT_INTEGER_LIST: |
126 | 0 | return "integer_list"; |
127 | 0 | case GAAT_REAL_LIST: |
128 | 0 | return "real_list"; |
129 | 0 | case GAAT_DATASET_LIST: |
130 | 0 | return "dataset_list"; |
131 | 0 | } |
132 | | |
133 | 0 | return "boolean"; |
134 | 0 | } |
135 | | |
136 | | /************************************************************************/ |
137 | | /* GDALAlgorithmArgDatasetTypeName() */ |
138 | | /************************************************************************/ |
139 | | |
140 | | std::string GDALAlgorithmArgDatasetTypeName(GDALArgDatasetType type) |
141 | 0 | { |
142 | 0 | std::string ret; |
143 | 0 | if ((type & GDAL_OF_RASTER) != 0) |
144 | 0 | ret = "raster"; |
145 | 0 | if ((type & GDAL_OF_VECTOR) != 0) |
146 | 0 | { |
147 | 0 | if (!ret.empty()) |
148 | 0 | { |
149 | 0 | if ((type & GDAL_OF_MULTIDIM_RASTER) != 0) |
150 | 0 | ret += ", "; |
151 | 0 | else |
152 | 0 | ret += " or "; |
153 | 0 | } |
154 | 0 | ret += "vector"; |
155 | 0 | } |
156 | 0 | if ((type & GDAL_OF_MULTIDIM_RASTER) != 0) |
157 | 0 | { |
158 | 0 | if (!ret.empty()) |
159 | 0 | { |
160 | 0 | ret += " or "; |
161 | 0 | } |
162 | 0 | ret += "multidimensional raster"; |
163 | 0 | } |
164 | 0 | return ret; |
165 | 0 | } |
166 | | |
167 | | /************************************************************************/ |
168 | | /* GDALAlgorithmArgDecl() */ |
169 | | /************************************************************************/ |
170 | | |
171 | | // cppcheck-suppress uninitMemberVar |
172 | | GDALAlgorithmArgDecl::GDALAlgorithmArgDecl(const std::string &longName, |
173 | | char chShortName, |
174 | | const std::string &description, |
175 | | GDALAlgorithmArgType type) |
176 | 0 | : m_longName(longName), |
177 | 0 | m_shortName(chShortName ? std::string(&chShortName, 1) : std::string()), |
178 | 0 | m_description(description), m_type(type), |
179 | 0 | m_metaVar(CPLString(m_type == GAAT_BOOLEAN ? std::string() : longName) |
180 | 0 | .toupper()), |
181 | 0 | m_maxCount(GDALAlgorithmArgTypeIsList(type) ? UNBOUNDED : 1) |
182 | 0 | { |
183 | 0 | if (m_type == GAAT_BOOLEAN) |
184 | 0 | { |
185 | 0 | m_defaultValue = false; |
186 | 0 | } |
187 | 0 | } |
188 | | |
189 | | /************************************************************************/ |
190 | | /* GDALAlgorithmArgDecl::SetMinCount() */ |
191 | | /************************************************************************/ |
192 | | |
193 | | GDALAlgorithmArgDecl &GDALAlgorithmArgDecl::SetMinCount(int count) |
194 | 0 | { |
195 | 0 | if (!GDALAlgorithmArgTypeIsList(m_type)) |
196 | 0 | { |
197 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
198 | 0 | "SetMinCount() illegal on scalar argument '%s'", |
199 | 0 | GetName().c_str()); |
200 | 0 | } |
201 | 0 | else |
202 | 0 | { |
203 | 0 | m_minCount = count; |
204 | 0 | } |
205 | 0 | return *this; |
206 | 0 | } |
207 | | |
208 | | /************************************************************************/ |
209 | | /* GDALAlgorithmArgDecl::SetMaxCount() */ |
210 | | /************************************************************************/ |
211 | | |
212 | | GDALAlgorithmArgDecl &GDALAlgorithmArgDecl::SetMaxCount(int count) |
213 | 0 | { |
214 | 0 | if (!GDALAlgorithmArgTypeIsList(m_type)) |
215 | 0 | { |
216 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
217 | 0 | "SetMaxCount() illegal on scalar argument '%s'", |
218 | 0 | GetName().c_str()); |
219 | 0 | } |
220 | 0 | else |
221 | 0 | { |
222 | 0 | m_maxCount = count; |
223 | 0 | } |
224 | 0 | return *this; |
225 | 0 | } |
226 | | |
227 | | /************************************************************************/ |
228 | | /* GDALAlgorithmArg::~GDALAlgorithmArg() */ |
229 | | /************************************************************************/ |
230 | | |
231 | 0 | GDALAlgorithmArg::~GDALAlgorithmArg() = default; |
232 | | |
233 | | /************************************************************************/ |
234 | | /* GDALAlgorithmArg::Set() */ |
235 | | /************************************************************************/ |
236 | | |
237 | | bool GDALAlgorithmArg::Set(bool value) |
238 | 0 | { |
239 | 0 | if (m_decl.GetType() != GAAT_BOOLEAN) |
240 | 0 | { |
241 | 0 | CPLError( |
242 | 0 | CE_Failure, CPLE_AppDefined, |
243 | 0 | "Calling Set(bool) on argument '%s' of type %s is not supported", |
244 | 0 | GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType())); |
245 | 0 | return false; |
246 | 0 | } |
247 | 0 | return SetInternal(value); |
248 | 0 | } |
249 | | |
250 | | bool GDALAlgorithmArg::ProcessString(std::string &value) const |
251 | 0 | { |
252 | 0 | if (m_decl.IsReadFromFileAtSyntaxAllowed() && !value.empty() && |
253 | 0 | value.front() == '@') |
254 | 0 | { |
255 | 0 | GByte *pabyData = nullptr; |
256 | 0 | if (VSIIngestFile(nullptr, value.c_str() + 1, &pabyData, nullptr, |
257 | 0 | 1024 * 1024)) |
258 | 0 | { |
259 | | // Remove UTF-8 BOM |
260 | 0 | size_t offset = 0; |
261 | 0 | if (pabyData[0] == 0xEF && pabyData[1] == 0xBB && |
262 | 0 | pabyData[2] == 0xBF) |
263 | 0 | { |
264 | 0 | offset = 3; |
265 | 0 | } |
266 | 0 | value = reinterpret_cast<const char *>(pabyData + offset); |
267 | 0 | VSIFree(pabyData); |
268 | 0 | } |
269 | 0 | else |
270 | 0 | { |
271 | 0 | return false; |
272 | 0 | } |
273 | 0 | } |
274 | | |
275 | 0 | if (m_decl.IsRemoveSQLCommentsEnabled()) |
276 | 0 | value = CPLRemoveSQLComments(value); |
277 | |
|
278 | 0 | return true; |
279 | 0 | } |
280 | | |
281 | | bool GDALAlgorithmArg::Set(const std::string &value) |
282 | 0 | { |
283 | 0 | switch (m_decl.GetType()) |
284 | 0 | { |
285 | 0 | case GAAT_BOOLEAN: |
286 | 0 | if (EQUAL(value.c_str(), "1") || EQUAL(value.c_str(), "TRUE") || |
287 | 0 | EQUAL(value.c_str(), "YES") || EQUAL(value.c_str(), "ON")) |
288 | 0 | { |
289 | 0 | return Set(true); |
290 | 0 | } |
291 | 0 | else if (EQUAL(value.c_str(), "0") || |
292 | 0 | EQUAL(value.c_str(), "FALSE") || |
293 | 0 | EQUAL(value.c_str(), "NO") || EQUAL(value.c_str(), "OFF")) |
294 | 0 | { |
295 | 0 | return Set(false); |
296 | 0 | } |
297 | 0 | break; |
298 | | |
299 | 0 | case GAAT_INTEGER: |
300 | 0 | case GAAT_INTEGER_LIST: |
301 | 0 | { |
302 | 0 | errno = 0; |
303 | 0 | char *endptr = nullptr; |
304 | 0 | const auto v = std::strtoll(value.c_str(), &endptr, 10); |
305 | 0 | if (errno == 0 && v >= INT_MIN && v <= INT_MAX && |
306 | 0 | endptr == value.c_str() + value.size()) |
307 | 0 | { |
308 | 0 | if (m_decl.GetType() == GAAT_INTEGER) |
309 | 0 | return Set(static_cast<int>(v)); |
310 | 0 | else |
311 | 0 | return Set(std::vector<int>{static_cast<int>(v)}); |
312 | 0 | } |
313 | 0 | break; |
314 | 0 | } |
315 | | |
316 | 0 | case GAAT_REAL: |
317 | 0 | case GAAT_REAL_LIST: |
318 | 0 | { |
319 | 0 | char *endptr = nullptr; |
320 | 0 | const double v = CPLStrtod(value.c_str(), &endptr); |
321 | 0 | if (endptr == value.c_str() + value.size()) |
322 | 0 | { |
323 | 0 | if (m_decl.GetType() == GAAT_REAL) |
324 | 0 | return Set(v); |
325 | 0 | else |
326 | 0 | return Set(std::vector<double>{v}); |
327 | 0 | } |
328 | 0 | break; |
329 | 0 | } |
330 | | |
331 | 0 | case GAAT_STRING: |
332 | 0 | break; |
333 | | |
334 | 0 | case GAAT_STRING_LIST: |
335 | 0 | return Set(std::vector<std::string>{value}); |
336 | | |
337 | 0 | case GAAT_DATASET: |
338 | 0 | return SetDatasetName(value); |
339 | | |
340 | 0 | case GAAT_DATASET_LIST: |
341 | 0 | { |
342 | 0 | std::vector<GDALArgDatasetValue> v; |
343 | 0 | v.resize(1); |
344 | 0 | v[0].Set(value); |
345 | 0 | return Set(std::move(v)); |
346 | 0 | } |
347 | 0 | } |
348 | | |
349 | 0 | if (m_decl.GetType() != GAAT_STRING) |
350 | 0 | { |
351 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
352 | 0 | "Calling Set(std::string) on argument '%s' of type %s is not " |
353 | 0 | "supported", |
354 | 0 | GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType())); |
355 | 0 | return false; |
356 | 0 | } |
357 | | |
358 | 0 | std::string newValue(value); |
359 | 0 | return ProcessString(newValue) && SetInternal(newValue); |
360 | 0 | } |
361 | | |
362 | | bool GDALAlgorithmArg::Set(int value) |
363 | 0 | { |
364 | 0 | if (m_decl.GetType() == GAAT_BOOLEAN) |
365 | 0 | { |
366 | 0 | if (value == 1) |
367 | 0 | return Set(true); |
368 | 0 | else if (value == 0) |
369 | 0 | return Set(false); |
370 | 0 | } |
371 | 0 | else if (m_decl.GetType() == GAAT_REAL) |
372 | 0 | { |
373 | 0 | return Set(static_cast<double>(value)); |
374 | 0 | } |
375 | 0 | else if (m_decl.GetType() == GAAT_STRING) |
376 | 0 | { |
377 | 0 | return Set(std::to_string(value)); |
378 | 0 | } |
379 | 0 | else if (m_decl.GetType() == GAAT_INTEGER_LIST) |
380 | 0 | { |
381 | 0 | return Set(std::vector<int>{value}); |
382 | 0 | } |
383 | 0 | else if (m_decl.GetType() == GAAT_REAL_LIST) |
384 | 0 | { |
385 | 0 | return Set(std::vector<double>{static_cast<double>(value)}); |
386 | 0 | } |
387 | 0 | else if (m_decl.GetType() == GAAT_STRING_LIST) |
388 | 0 | { |
389 | 0 | return Set(std::vector<std::string>{std::to_string(value)}); |
390 | 0 | } |
391 | | |
392 | 0 | if (m_decl.GetType() != GAAT_INTEGER) |
393 | 0 | { |
394 | 0 | CPLError( |
395 | 0 | CE_Failure, CPLE_AppDefined, |
396 | 0 | "Calling Set(int) on argument '%s' of type %s is not supported", |
397 | 0 | GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType())); |
398 | 0 | return false; |
399 | 0 | } |
400 | 0 | return SetInternal(value); |
401 | 0 | } |
402 | | |
403 | | bool GDALAlgorithmArg::Set(double value) |
404 | 0 | { |
405 | 0 | if (m_decl.GetType() == GAAT_INTEGER && value >= INT_MIN && |
406 | 0 | value <= INT_MAX && static_cast<int>(value) == value) |
407 | 0 | { |
408 | 0 | return Set(static_cast<int>(value)); |
409 | 0 | } |
410 | 0 | else if (m_decl.GetType() == GAAT_STRING) |
411 | 0 | { |
412 | 0 | return Set(std::to_string(value)); |
413 | 0 | } |
414 | 0 | else if (m_decl.GetType() == GAAT_INTEGER_LIST && value >= INT_MIN && |
415 | 0 | value <= INT_MAX && static_cast<int>(value) == value) |
416 | 0 | { |
417 | 0 | return Set(std::vector<int>{static_cast<int>(value)}); |
418 | 0 | } |
419 | 0 | else if (m_decl.GetType() == GAAT_REAL_LIST) |
420 | 0 | { |
421 | 0 | return Set(std::vector<double>{value}); |
422 | 0 | } |
423 | 0 | else if (m_decl.GetType() == GAAT_STRING_LIST) |
424 | 0 | { |
425 | 0 | return Set(std::vector<std::string>{std::to_string(value)}); |
426 | 0 | } |
427 | 0 | else if (m_decl.GetType() != GAAT_REAL) |
428 | 0 | { |
429 | 0 | CPLError( |
430 | 0 | CE_Failure, CPLE_AppDefined, |
431 | 0 | "Calling Set(double) on argument '%s' of type %s is not supported", |
432 | 0 | GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType())); |
433 | 0 | return false; |
434 | 0 | } |
435 | 0 | return SetInternal(value); |
436 | 0 | } |
437 | | |
438 | | static bool CheckCanSetDatasetObject(const GDALAlgorithmArg *arg) |
439 | 0 | { |
440 | 0 | if (arg->GetDatasetInputFlags() == GADV_NAME && |
441 | 0 | arg->GetDatasetOutputFlags() == GADV_OBJECT) |
442 | 0 | { |
443 | 0 | CPLError( |
444 | 0 | CE_Failure, CPLE_AppDefined, |
445 | 0 | "Dataset object '%s' is created by algorithm and cannot be set " |
446 | 0 | "as an input.", |
447 | 0 | arg->GetName().c_str()); |
448 | 0 | return false; |
449 | 0 | } |
450 | 0 | else if ((arg->GetDatasetInputFlags() & GADV_OBJECT) == 0) |
451 | 0 | { |
452 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
453 | 0 | "A dataset cannot be set as an input argument of '%s'.", |
454 | 0 | arg->GetName().c_str()); |
455 | 0 | return false; |
456 | 0 | } |
457 | | |
458 | 0 | return true; |
459 | 0 | } |
460 | | |
461 | | bool GDALAlgorithmArg::Set(GDALDataset *ds) |
462 | 0 | { |
463 | 0 | if (m_decl.GetType() != GAAT_DATASET) |
464 | 0 | { |
465 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
466 | 0 | "Calling Set(GDALDataset*, bool) on argument '%s' of type %s " |
467 | 0 | "is not supported", |
468 | 0 | GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType())); |
469 | 0 | return false; |
470 | 0 | } |
471 | 0 | if (!CheckCanSetDatasetObject(this)) |
472 | 0 | return false; |
473 | 0 | m_explicitlySet = true; |
474 | 0 | auto &val = *std::get<GDALArgDatasetValue *>(m_value); |
475 | 0 | val.Set(ds); |
476 | 0 | return RunAllActions(); |
477 | 0 | } |
478 | | |
479 | | bool GDALAlgorithmArg::Set(std::unique_ptr<GDALDataset> ds) |
480 | 0 | { |
481 | 0 | if (m_decl.GetType() != GAAT_DATASET) |
482 | 0 | { |
483 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
484 | 0 | "Calling Set(GDALDataset*, bool) on argument '%s' of type %s " |
485 | 0 | "is not supported", |
486 | 0 | GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType())); |
487 | 0 | return false; |
488 | 0 | } |
489 | 0 | if (!CheckCanSetDatasetObject(this)) |
490 | 0 | return false; |
491 | 0 | m_explicitlySet = true; |
492 | 0 | auto &val = *std::get<GDALArgDatasetValue *>(m_value); |
493 | 0 | val.Set(std::move(ds)); |
494 | 0 | return RunAllActions(); |
495 | 0 | } |
496 | | |
497 | | bool GDALAlgorithmArg::SetDatasetName(const std::string &name) |
498 | 0 | { |
499 | 0 | if (m_decl.GetType() != GAAT_DATASET) |
500 | 0 | { |
501 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
502 | 0 | "Calling SetDatasetName() on argument '%s' of type %s is " |
503 | 0 | "not supported", |
504 | 0 | GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType())); |
505 | 0 | return false; |
506 | 0 | } |
507 | 0 | m_explicitlySet = true; |
508 | 0 | std::get<GDALArgDatasetValue *>(m_value)->Set(name); |
509 | 0 | return RunAllActions(); |
510 | 0 | } |
511 | | |
512 | | bool GDALAlgorithmArg::SetFrom(const GDALArgDatasetValue &other) |
513 | 0 | { |
514 | 0 | if (m_decl.GetType() != GAAT_DATASET) |
515 | 0 | { |
516 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
517 | 0 | "Calling SetFrom() on argument '%s' of type %s is " |
518 | 0 | "not supported", |
519 | 0 | GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType())); |
520 | 0 | return false; |
521 | 0 | } |
522 | 0 | if (!CheckCanSetDatasetObject(this)) |
523 | 0 | return false; |
524 | 0 | m_explicitlySet = true; |
525 | 0 | std::get<GDALArgDatasetValue *>(m_value)->SetFrom(other); |
526 | 0 | return RunAllActions(); |
527 | 0 | } |
528 | | |
529 | | bool GDALAlgorithmArg::Set(const std::vector<std::string> &value) |
530 | 0 | { |
531 | 0 | if (m_decl.GetType() == GAAT_INTEGER_LIST) |
532 | 0 | { |
533 | 0 | std::vector<int> v_i; |
534 | 0 | for (const std::string &s : value) |
535 | 0 | { |
536 | 0 | errno = 0; |
537 | 0 | char *endptr = nullptr; |
538 | 0 | const auto v = std::strtoll(s.c_str(), &endptr, 10); |
539 | 0 | if (errno == 0 && v >= INT_MIN && v <= INT_MAX && |
540 | 0 | endptr == s.c_str() + s.size()) |
541 | 0 | { |
542 | 0 | v_i.push_back(static_cast<int>(v)); |
543 | 0 | } |
544 | 0 | else |
545 | 0 | { |
546 | 0 | break; |
547 | 0 | } |
548 | 0 | } |
549 | 0 | if (v_i.size() == value.size()) |
550 | 0 | return Set(v_i); |
551 | 0 | } |
552 | 0 | else if (m_decl.GetType() == GAAT_REAL_LIST) |
553 | 0 | { |
554 | 0 | std::vector<double> v_d; |
555 | 0 | for (const std::string &s : value) |
556 | 0 | { |
557 | 0 | char *endptr = nullptr; |
558 | 0 | const double v = CPLStrtod(s.c_str(), &endptr); |
559 | 0 | if (endptr == s.c_str() + s.size()) |
560 | 0 | { |
561 | 0 | v_d.push_back(v); |
562 | 0 | } |
563 | 0 | else |
564 | 0 | { |
565 | 0 | break; |
566 | 0 | } |
567 | 0 | } |
568 | 0 | if (v_d.size() == value.size()) |
569 | 0 | return Set(v_d); |
570 | 0 | } |
571 | 0 | else if ((m_decl.GetType() == GAAT_INTEGER || |
572 | 0 | m_decl.GetType() == GAAT_REAL || |
573 | 0 | m_decl.GetType() == GAAT_STRING) && |
574 | 0 | value.size() == 1) |
575 | 0 | { |
576 | 0 | return Set(value[0]); |
577 | 0 | } |
578 | 0 | else if (m_decl.GetType() == GAAT_DATASET_LIST) |
579 | 0 | { |
580 | 0 | std::vector<GDALArgDatasetValue> dsVector; |
581 | 0 | for (const std::string &s : value) |
582 | 0 | dsVector.emplace_back(s); |
583 | 0 | return Set(std::move(dsVector)); |
584 | 0 | } |
585 | | |
586 | 0 | if (m_decl.GetType() != GAAT_STRING_LIST) |
587 | 0 | { |
588 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
589 | 0 | "Calling Set(const std::vector<std::string> &) on argument " |
590 | 0 | "'%s' of type %s is not supported", |
591 | 0 | GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType())); |
592 | 0 | return false; |
593 | 0 | } |
594 | | |
595 | 0 | if (m_decl.IsReadFromFileAtSyntaxAllowed() || |
596 | 0 | m_decl.IsRemoveSQLCommentsEnabled()) |
597 | 0 | { |
598 | 0 | std::vector<std::string> newValue(value); |
599 | 0 | for (auto &s : newValue) |
600 | 0 | { |
601 | 0 | if (!ProcessString(s)) |
602 | 0 | return false; |
603 | 0 | } |
604 | 0 | return SetInternal(newValue); |
605 | 0 | } |
606 | 0 | else |
607 | 0 | { |
608 | 0 | return SetInternal(value); |
609 | 0 | } |
610 | 0 | } |
611 | | |
612 | | bool GDALAlgorithmArg::Set(const std::vector<int> &value) |
613 | 0 | { |
614 | 0 | if (m_decl.GetType() == GAAT_REAL_LIST) |
615 | 0 | { |
616 | 0 | std::vector<double> v_d; |
617 | 0 | for (int i : value) |
618 | 0 | v_d.push_back(i); |
619 | 0 | return Set(v_d); |
620 | 0 | } |
621 | 0 | else if (m_decl.GetType() == GAAT_STRING_LIST) |
622 | 0 | { |
623 | 0 | std::vector<std::string> v_s; |
624 | 0 | for (int i : value) |
625 | 0 | v_s.push_back(std::to_string(i)); |
626 | 0 | return Set(v_s); |
627 | 0 | } |
628 | 0 | else if ((m_decl.GetType() == GAAT_INTEGER || |
629 | 0 | m_decl.GetType() == GAAT_REAL || |
630 | 0 | m_decl.GetType() == GAAT_STRING) && |
631 | 0 | value.size() == 1) |
632 | 0 | { |
633 | 0 | return Set(value[0]); |
634 | 0 | } |
635 | | |
636 | 0 | if (m_decl.GetType() != GAAT_INTEGER_LIST) |
637 | 0 | { |
638 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
639 | 0 | "Calling Set(const std::vector<int> &) on argument '%s' of " |
640 | 0 | "type %s is not supported", |
641 | 0 | GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType())); |
642 | 0 | return false; |
643 | 0 | } |
644 | 0 | return SetInternal(value); |
645 | 0 | } |
646 | | |
647 | | bool GDALAlgorithmArg::Set(const std::vector<double> &value) |
648 | 0 | { |
649 | 0 | if (m_decl.GetType() == GAAT_INTEGER_LIST) |
650 | 0 | { |
651 | 0 | std::vector<int> v_i; |
652 | 0 | for (double d : value) |
653 | 0 | { |
654 | 0 | if (d >= INT_MIN && d <= INT_MAX && static_cast<int>(d) == d) |
655 | 0 | { |
656 | 0 | v_i.push_back(static_cast<int>(d)); |
657 | 0 | } |
658 | 0 | else |
659 | 0 | { |
660 | 0 | break; |
661 | 0 | } |
662 | 0 | } |
663 | 0 | if (v_i.size() == value.size()) |
664 | 0 | return Set(v_i); |
665 | 0 | } |
666 | 0 | else if (m_decl.GetType() == GAAT_STRING_LIST) |
667 | 0 | { |
668 | 0 | std::vector<std::string> v_s; |
669 | 0 | for (double d : value) |
670 | 0 | v_s.push_back(std::to_string(d)); |
671 | 0 | return Set(v_s); |
672 | 0 | } |
673 | 0 | else if ((m_decl.GetType() == GAAT_INTEGER || |
674 | 0 | m_decl.GetType() == GAAT_REAL || |
675 | 0 | m_decl.GetType() == GAAT_STRING) && |
676 | 0 | value.size() == 1) |
677 | 0 | { |
678 | 0 | return Set(value[0]); |
679 | 0 | } |
680 | | |
681 | 0 | if (m_decl.GetType() != GAAT_REAL_LIST) |
682 | 0 | { |
683 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
684 | 0 | "Calling Set(const std::vector<double> &) on argument '%s' of " |
685 | 0 | "type %s is not supported", |
686 | 0 | GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType())); |
687 | 0 | return false; |
688 | 0 | } |
689 | 0 | return SetInternal(value); |
690 | 0 | } |
691 | | |
692 | | bool GDALAlgorithmArg::Set(std::vector<GDALArgDatasetValue> &&value) |
693 | 0 | { |
694 | 0 | if (m_decl.GetType() != GAAT_DATASET_LIST) |
695 | 0 | { |
696 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
697 | 0 | "Calling Set(const std::vector<GDALArgDatasetValue> &&) on " |
698 | 0 | "argument '%s' of type %s is not supported", |
699 | 0 | GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType())); |
700 | 0 | return false; |
701 | 0 | } |
702 | 0 | m_explicitlySet = true; |
703 | 0 | *std::get<std::vector<GDALArgDatasetValue> *>(m_value) = std::move(value); |
704 | 0 | return RunAllActions(); |
705 | 0 | } |
706 | | |
707 | | GDALAlgorithmArg & |
708 | | GDALAlgorithmArg::operator=(std::unique_ptr<GDALDataset> value) |
709 | 0 | { |
710 | 0 | Set(std::move(value)); |
711 | 0 | return *this; |
712 | 0 | } |
713 | | |
714 | | bool GDALAlgorithmArg::Set(const OGRSpatialReference &value) |
715 | 0 | { |
716 | 0 | const char *const apszOptions[] = {"FORMAT=WKT2_2019", nullptr}; |
717 | 0 | return Set(value.exportToWkt(apszOptions)); |
718 | 0 | } |
719 | | |
720 | | bool GDALAlgorithmArg::SetFrom(const GDALAlgorithmArg &other) |
721 | 0 | { |
722 | 0 | if (m_decl.GetType() != other.GetType()) |
723 | 0 | { |
724 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
725 | 0 | "Calling SetFrom() on argument '%s' of type %s whereas " |
726 | 0 | "other argument type is %s is not supported", |
727 | 0 | GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()), |
728 | 0 | GDALAlgorithmArgTypeName(other.GetType())); |
729 | 0 | return false; |
730 | 0 | } |
731 | | |
732 | 0 | switch (m_decl.GetType()) |
733 | 0 | { |
734 | 0 | case GAAT_BOOLEAN: |
735 | 0 | *std::get<bool *>(m_value) = *std::get<bool *>(other.m_value); |
736 | 0 | break; |
737 | 0 | case GAAT_STRING: |
738 | 0 | *std::get<std::string *>(m_value) = |
739 | 0 | *std::get<std::string *>(other.m_value); |
740 | 0 | break; |
741 | 0 | case GAAT_INTEGER: |
742 | 0 | *std::get<int *>(m_value) = *std::get<int *>(other.m_value); |
743 | 0 | break; |
744 | 0 | case GAAT_REAL: |
745 | 0 | *std::get<double *>(m_value) = *std::get<double *>(other.m_value); |
746 | 0 | break; |
747 | 0 | case GAAT_DATASET: |
748 | 0 | return SetFrom(other.Get<GDALArgDatasetValue>()); |
749 | 0 | case GAAT_STRING_LIST: |
750 | 0 | *std::get<std::vector<std::string> *>(m_value) = |
751 | 0 | *std::get<std::vector<std::string> *>(other.m_value); |
752 | 0 | break; |
753 | 0 | case GAAT_INTEGER_LIST: |
754 | 0 | *std::get<std::vector<int> *>(m_value) = |
755 | 0 | *std::get<std::vector<int> *>(other.m_value); |
756 | 0 | break; |
757 | 0 | case GAAT_REAL_LIST: |
758 | 0 | *std::get<std::vector<double> *>(m_value) = |
759 | 0 | *std::get<std::vector<double> *>(other.m_value); |
760 | 0 | break; |
761 | 0 | case GAAT_DATASET_LIST: |
762 | 0 | { |
763 | 0 | std::get<std::vector<GDALArgDatasetValue> *>(m_value)->clear(); |
764 | 0 | for (const auto &val : |
765 | 0 | *std::get<std::vector<GDALArgDatasetValue> *>(other.m_value)) |
766 | 0 | { |
767 | 0 | GDALArgDatasetValue v; |
768 | 0 | v.SetFrom(val); |
769 | 0 | std::get<std::vector<GDALArgDatasetValue> *>(m_value) |
770 | 0 | ->push_back(std::move(v)); |
771 | 0 | } |
772 | 0 | break; |
773 | 0 | } |
774 | 0 | } |
775 | 0 | m_explicitlySet = true; |
776 | 0 | return RunAllActions(); |
777 | 0 | } |
778 | | |
779 | | /************************************************************************/ |
780 | | /* GDALAlgorithmArg::RunAllActions() */ |
781 | | /************************************************************************/ |
782 | | |
783 | | bool GDALAlgorithmArg::RunAllActions() |
784 | 0 | { |
785 | 0 | if (!RunValidationActions()) |
786 | 0 | return false; |
787 | 0 | RunActions(); |
788 | 0 | return true; |
789 | 0 | } |
790 | | |
791 | | /************************************************************************/ |
792 | | /* GDALAlgorithmArg::RunActions() */ |
793 | | /************************************************************************/ |
794 | | |
795 | | void GDALAlgorithmArg::RunActions() |
796 | 0 | { |
797 | 0 | for (const auto &f : m_actions) |
798 | 0 | f(); |
799 | 0 | } |
800 | | |
801 | | /************************************************************************/ |
802 | | /* GDALAlgorithmArg::ValidateChoice() */ |
803 | | /************************************************************************/ |
804 | | |
805 | | // Returns the canonical value if matching a valid choice, or empty string |
806 | | // otherwise. |
807 | | std::string GDALAlgorithmArg::ValidateChoice(const std::string &value) const |
808 | 0 | { |
809 | 0 | for (const std::string &choice : GetChoices()) |
810 | 0 | { |
811 | 0 | if (EQUAL(value.c_str(), choice.c_str())) |
812 | 0 | { |
813 | 0 | return choice; |
814 | 0 | } |
815 | 0 | } |
816 | | |
817 | 0 | for (const std::string &choice : GetHiddenChoices()) |
818 | 0 | { |
819 | 0 | if (EQUAL(value.c_str(), choice.c_str())) |
820 | 0 | { |
821 | 0 | return choice; |
822 | 0 | } |
823 | 0 | } |
824 | | |
825 | 0 | std::string expected; |
826 | 0 | for (const auto &choice : GetChoices()) |
827 | 0 | { |
828 | 0 | if (!expected.empty()) |
829 | 0 | expected += ", "; |
830 | 0 | expected += '\''; |
831 | 0 | expected += choice; |
832 | 0 | expected += '\''; |
833 | 0 | } |
834 | 0 | if (m_owner && m_owner->IsCalledFromCommandLine() && value == "?") |
835 | 0 | { |
836 | 0 | return "?"; |
837 | 0 | } |
838 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
839 | 0 | "Invalid value '%s' for string argument '%s'. Should be " |
840 | 0 | "one among %s.", |
841 | 0 | value.c_str(), GetName().c_str(), expected.c_str()); |
842 | 0 | return std::string(); |
843 | 0 | } |
844 | | |
845 | | /************************************************************************/ |
846 | | /* GDALAlgorithmArg::ValidateIntRange() */ |
847 | | /************************************************************************/ |
848 | | |
849 | | bool GDALAlgorithmArg::ValidateIntRange(int val) const |
850 | 0 | { |
851 | 0 | bool ret = true; |
852 | |
|
853 | 0 | const auto [minVal, minValIsIncluded] = GetMinValue(); |
854 | 0 | if (!std::isnan(minVal)) |
855 | 0 | { |
856 | 0 | if (minValIsIncluded && val < minVal) |
857 | 0 | { |
858 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
859 | 0 | "Value of argument '%s' is %d, but should be >= %d", |
860 | 0 | GetName().c_str(), val, static_cast<int>(minVal)); |
861 | 0 | ret = false; |
862 | 0 | } |
863 | 0 | else if (!minValIsIncluded && val <= minVal) |
864 | 0 | { |
865 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
866 | 0 | "Value of argument '%s' is %d, but should be > %d", |
867 | 0 | GetName().c_str(), val, static_cast<int>(minVal)); |
868 | 0 | ret = false; |
869 | 0 | } |
870 | 0 | } |
871 | |
|
872 | 0 | const auto [maxVal, maxValIsIncluded] = GetMaxValue(); |
873 | 0 | if (!std::isnan(maxVal)) |
874 | 0 | { |
875 | |
|
876 | 0 | if (maxValIsIncluded && val > maxVal) |
877 | 0 | { |
878 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
879 | 0 | "Value of argument '%s' is %d, but should be <= %d", |
880 | 0 | GetName().c_str(), val, static_cast<int>(maxVal)); |
881 | 0 | ret = false; |
882 | 0 | } |
883 | 0 | else if (!maxValIsIncluded && val >= maxVal) |
884 | 0 | { |
885 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
886 | 0 | "Value of argument '%s' is %d, but should be < %d", |
887 | 0 | GetName().c_str(), val, static_cast<int>(maxVal)); |
888 | 0 | ret = false; |
889 | 0 | } |
890 | 0 | } |
891 | |
|
892 | 0 | return ret; |
893 | 0 | } |
894 | | |
895 | | /************************************************************************/ |
896 | | /* GDALAlgorithmArg::ValidateRealRange() */ |
897 | | /************************************************************************/ |
898 | | |
899 | | bool GDALAlgorithmArg::ValidateRealRange(double val) const |
900 | 0 | { |
901 | 0 | bool ret = true; |
902 | |
|
903 | 0 | const auto [minVal, minValIsIncluded] = GetMinValue(); |
904 | 0 | if (!std::isnan(minVal)) |
905 | 0 | { |
906 | 0 | if (minValIsIncluded && !(val >= minVal)) |
907 | 0 | { |
908 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
909 | 0 | "Value of argument '%s' is %g, but should be >= %g", |
910 | 0 | GetName().c_str(), val, minVal); |
911 | 0 | ret = false; |
912 | 0 | } |
913 | 0 | else if (!minValIsIncluded && !(val > minVal)) |
914 | 0 | { |
915 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
916 | 0 | "Value of argument '%s' is %g, but should be > %g", |
917 | 0 | GetName().c_str(), val, minVal); |
918 | 0 | ret = false; |
919 | 0 | } |
920 | 0 | } |
921 | |
|
922 | 0 | const auto [maxVal, maxValIsIncluded] = GetMaxValue(); |
923 | 0 | if (!std::isnan(maxVal)) |
924 | 0 | { |
925 | |
|
926 | 0 | if (maxValIsIncluded && !(val <= maxVal)) |
927 | 0 | { |
928 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
929 | 0 | "Value of argument '%s' is %g, but should be <= %g", |
930 | 0 | GetName().c_str(), val, maxVal); |
931 | 0 | ret = false; |
932 | 0 | } |
933 | 0 | else if (!maxValIsIncluded && !(val < maxVal)) |
934 | 0 | { |
935 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
936 | 0 | "Value of argument '%s' is %g, but should be < %g", |
937 | 0 | GetName().c_str(), val, maxVal); |
938 | 0 | ret = false; |
939 | 0 | } |
940 | 0 | } |
941 | |
|
942 | 0 | return ret; |
943 | 0 | } |
944 | | |
945 | | /************************************************************************/ |
946 | | /* GDALAlgorithmArg::RunValidationActions() */ |
947 | | /************************************************************************/ |
948 | | |
949 | | bool GDALAlgorithmArg::RunValidationActions() |
950 | 0 | { |
951 | 0 | bool ret = true; |
952 | |
|
953 | 0 | if (GetType() == GAAT_STRING && !GetChoices().empty()) |
954 | 0 | { |
955 | 0 | auto &val = Get<std::string>(); |
956 | 0 | std::string validVal = ValidateChoice(val); |
957 | 0 | if (validVal.empty()) |
958 | 0 | ret = false; |
959 | 0 | else |
960 | 0 | val = std::move(validVal); |
961 | 0 | } |
962 | 0 | else if (GetType() == GAAT_STRING_LIST && !GetChoices().empty()) |
963 | 0 | { |
964 | 0 | auto &values = Get<std::vector<std::string>>(); |
965 | 0 | for (std::string &val : values) |
966 | 0 | { |
967 | 0 | std::string validVal = ValidateChoice(val); |
968 | 0 | if (validVal.empty()) |
969 | 0 | ret = false; |
970 | 0 | else |
971 | 0 | val = std::move(validVal); |
972 | 0 | } |
973 | 0 | } |
974 | |
|
975 | 0 | if (GetType() == GAAT_STRING) |
976 | 0 | { |
977 | 0 | const int nMinCharCount = GetMinCharCount(); |
978 | 0 | if (nMinCharCount > 0) |
979 | 0 | { |
980 | 0 | const auto &val = Get<std::string>(); |
981 | 0 | if (val.size() < static_cast<size_t>(nMinCharCount)) |
982 | 0 | { |
983 | 0 | CPLError( |
984 | 0 | CE_Failure, CPLE_IllegalArg, |
985 | 0 | "Value of argument '%s' is '%s', but should have at least " |
986 | 0 | "%d character(s)", |
987 | 0 | GetName().c_str(), val.c_str(), nMinCharCount); |
988 | 0 | ret = false; |
989 | 0 | } |
990 | 0 | } |
991 | 0 | } |
992 | 0 | else if (GetType() == GAAT_STRING_LIST) |
993 | 0 | { |
994 | 0 | const int nMinCharCount = GetMinCharCount(); |
995 | 0 | if (nMinCharCount > 0) |
996 | 0 | { |
997 | 0 | for (const auto &val : Get<std::vector<std::string>>()) |
998 | 0 | { |
999 | 0 | if (val.size() < static_cast<size_t>(nMinCharCount)) |
1000 | 0 | { |
1001 | 0 | CPLError( |
1002 | 0 | CE_Failure, CPLE_IllegalArg, |
1003 | 0 | "Value of argument '%s' is '%s', but should have at " |
1004 | 0 | "least %d character(s)", |
1005 | 0 | GetName().c_str(), val.c_str(), nMinCharCount); |
1006 | 0 | ret = false; |
1007 | 0 | } |
1008 | 0 | } |
1009 | 0 | } |
1010 | 0 | } |
1011 | 0 | else if (GetType() == GAAT_INTEGER) |
1012 | 0 | { |
1013 | 0 | ret = ValidateIntRange(Get<int>()) && ret; |
1014 | 0 | } |
1015 | 0 | else if (GetType() == GAAT_INTEGER_LIST) |
1016 | 0 | { |
1017 | 0 | for (int v : Get<std::vector<int>>()) |
1018 | 0 | ret = ValidateIntRange(v) && ret; |
1019 | 0 | } |
1020 | 0 | else if (GetType() == GAAT_REAL) |
1021 | 0 | { |
1022 | 0 | ret = ValidateRealRange(Get<double>()) && ret; |
1023 | 0 | } |
1024 | 0 | else if (GetType() == GAAT_REAL_LIST) |
1025 | 0 | { |
1026 | 0 | for (double v : Get<std::vector<double>>()) |
1027 | 0 | ret = ValidateRealRange(v) && ret; |
1028 | 0 | } |
1029 | |
|
1030 | 0 | for (const auto &f : m_validationActions) |
1031 | 0 | { |
1032 | 0 | if (!f()) |
1033 | 0 | ret = false; |
1034 | 0 | } |
1035 | |
|
1036 | 0 | return ret; |
1037 | 0 | } |
1038 | | |
1039 | | /************************************************************************/ |
1040 | | /* GDALAlgorithmArg::Serialize() */ |
1041 | | /************************************************************************/ |
1042 | | |
1043 | | bool GDALAlgorithmArg::Serialize(std::string &serializedArg) const |
1044 | 0 | { |
1045 | 0 | serializedArg.clear(); |
1046 | |
|
1047 | 0 | if (!IsExplicitlySet()) |
1048 | 0 | { |
1049 | 0 | return false; |
1050 | 0 | } |
1051 | | |
1052 | 0 | std::string ret = "--"; |
1053 | 0 | ret += GetName(); |
1054 | 0 | if (GetType() == GAAT_BOOLEAN) |
1055 | 0 | { |
1056 | 0 | serializedArg = std::move(ret); |
1057 | 0 | return true; |
1058 | 0 | } |
1059 | | |
1060 | 0 | const auto AppendString = [&ret](const std::string &str) |
1061 | 0 | { |
1062 | 0 | if (str.find('"') != std::string::npos || |
1063 | 0 | str.find(' ') != std::string::npos || |
1064 | 0 | str.find('\\') != std::string::npos || |
1065 | 0 | str.find(',') != std::string::npos) |
1066 | 0 | { |
1067 | 0 | ret += '"'; |
1068 | 0 | ret += |
1069 | 0 | CPLString(str).replaceAll('\\', "\\\\").replaceAll('"', "\\\""); |
1070 | 0 | ret += '"'; |
1071 | 0 | } |
1072 | 0 | else |
1073 | 0 | { |
1074 | 0 | ret += str; |
1075 | 0 | } |
1076 | 0 | }; |
1077 | |
|
1078 | 0 | ret += ' '; |
1079 | 0 | switch (GetType()) |
1080 | 0 | { |
1081 | 0 | case GAAT_BOOLEAN: |
1082 | 0 | break; |
1083 | 0 | case GAAT_STRING: |
1084 | 0 | { |
1085 | 0 | const auto &val = Get<std::string>(); |
1086 | 0 | AppendString(val); |
1087 | 0 | break; |
1088 | 0 | } |
1089 | 0 | case GAAT_INTEGER: |
1090 | 0 | { |
1091 | 0 | ret += CPLSPrintf("%d", Get<int>()); |
1092 | 0 | break; |
1093 | 0 | } |
1094 | 0 | case GAAT_REAL: |
1095 | 0 | { |
1096 | 0 | ret += CPLSPrintf("%.17g", Get<double>()); |
1097 | 0 | break; |
1098 | 0 | } |
1099 | 0 | case GAAT_DATASET: |
1100 | 0 | { |
1101 | 0 | const auto &val = Get<GDALArgDatasetValue>(); |
1102 | 0 | const auto &str = val.GetName(); |
1103 | 0 | if (str.empty()) |
1104 | 0 | { |
1105 | 0 | return false; |
1106 | 0 | } |
1107 | 0 | AppendString(str); |
1108 | 0 | break; |
1109 | 0 | } |
1110 | 0 | case GAAT_STRING_LIST: |
1111 | 0 | { |
1112 | 0 | const auto &vals = Get<std::vector<std::string>>(); |
1113 | 0 | for (size_t i = 0; i < vals.size(); ++i) |
1114 | 0 | { |
1115 | 0 | if (i > 0) |
1116 | 0 | ret += ','; |
1117 | 0 | AppendString(vals[i]); |
1118 | 0 | } |
1119 | 0 | break; |
1120 | 0 | } |
1121 | 0 | case GAAT_INTEGER_LIST: |
1122 | 0 | { |
1123 | 0 | const auto &vals = Get<std::vector<int>>(); |
1124 | 0 | for (size_t i = 0; i < vals.size(); ++i) |
1125 | 0 | { |
1126 | 0 | if (i > 0) |
1127 | 0 | ret += ','; |
1128 | 0 | ret += CPLSPrintf("%d", vals[i]); |
1129 | 0 | } |
1130 | 0 | break; |
1131 | 0 | } |
1132 | 0 | case GAAT_REAL_LIST: |
1133 | 0 | { |
1134 | 0 | const auto &vals = Get<std::vector<double>>(); |
1135 | 0 | for (size_t i = 0; i < vals.size(); ++i) |
1136 | 0 | { |
1137 | 0 | if (i > 0) |
1138 | 0 | ret += ','; |
1139 | 0 | ret += CPLSPrintf("%.17g", vals[i]); |
1140 | 0 | } |
1141 | 0 | break; |
1142 | 0 | } |
1143 | 0 | case GAAT_DATASET_LIST: |
1144 | 0 | { |
1145 | 0 | const auto &vals = Get<std::vector<GDALArgDatasetValue>>(); |
1146 | 0 | for (size_t i = 0; i < vals.size(); ++i) |
1147 | 0 | { |
1148 | 0 | if (i > 0) |
1149 | 0 | ret += ','; |
1150 | 0 | const auto &val = vals[i]; |
1151 | 0 | const auto &str = val.GetName(); |
1152 | 0 | if (str.empty()) |
1153 | 0 | { |
1154 | 0 | return false; |
1155 | 0 | } |
1156 | 0 | AppendString(str); |
1157 | 0 | } |
1158 | 0 | break; |
1159 | 0 | } |
1160 | 0 | } |
1161 | | |
1162 | 0 | serializedArg = std::move(ret); |
1163 | 0 | return true; |
1164 | 0 | } |
1165 | | |
1166 | | /************************************************************************/ |
1167 | | /* ~GDALInConstructionAlgorithmArg() */ |
1168 | | /************************************************************************/ |
1169 | | |
1170 | | GDALInConstructionAlgorithmArg::~GDALInConstructionAlgorithmArg() = default; |
1171 | | |
1172 | | /************************************************************************/ |
1173 | | /* GDALInConstructionAlgorithmArg::AddAlias() */ |
1174 | | /************************************************************************/ |
1175 | | |
1176 | | GDALInConstructionAlgorithmArg & |
1177 | | GDALInConstructionAlgorithmArg::AddAlias(const std::string &alias) |
1178 | 0 | { |
1179 | 0 | m_decl.AddAlias(alias); |
1180 | 0 | if (m_owner) |
1181 | 0 | m_owner->AddAliasFor(this, alias); |
1182 | 0 | return *this; |
1183 | 0 | } |
1184 | | |
1185 | | /************************************************************************/ |
1186 | | /* GDALInConstructionAlgorithmArg::AddHiddenAlias() */ |
1187 | | /************************************************************************/ |
1188 | | |
1189 | | GDALInConstructionAlgorithmArg & |
1190 | | GDALInConstructionAlgorithmArg::AddHiddenAlias(const std::string &alias) |
1191 | 0 | { |
1192 | 0 | m_decl.AddHiddenAlias(alias); |
1193 | 0 | if (m_owner) |
1194 | 0 | m_owner->AddAliasFor(this, alias); |
1195 | 0 | return *this; |
1196 | 0 | } |
1197 | | |
1198 | | /************************************************************************/ |
1199 | | /* GDALInConstructionAlgorithmArg::AddShortNameAlias() */ |
1200 | | /************************************************************************/ |
1201 | | |
1202 | | GDALInConstructionAlgorithmArg & |
1203 | | GDALInConstructionAlgorithmArg::AddShortNameAlias(char shortNameAlias) |
1204 | 0 | { |
1205 | 0 | m_decl.AddShortNameAlias(shortNameAlias); |
1206 | 0 | if (m_owner) |
1207 | 0 | m_owner->AddShortNameAliasFor(this, shortNameAlias); |
1208 | 0 | return *this; |
1209 | 0 | } |
1210 | | |
1211 | | /************************************************************************/ |
1212 | | /* GDALInConstructionAlgorithmArg::SetPositional() */ |
1213 | | /************************************************************************/ |
1214 | | |
1215 | | GDALInConstructionAlgorithmArg &GDALInConstructionAlgorithmArg::SetPositional() |
1216 | 0 | { |
1217 | 0 | m_decl.SetPositional(); |
1218 | 0 | if (m_owner) |
1219 | 0 | m_owner->SetPositional(this); |
1220 | 0 | return *this; |
1221 | 0 | } |
1222 | | |
1223 | | /************************************************************************/ |
1224 | | /* GDALArgDatasetValue::GDALArgDatasetValue() */ |
1225 | | /************************************************************************/ |
1226 | | |
1227 | | GDALArgDatasetValue::GDALArgDatasetValue(GDALDataset *poDS) |
1228 | 0 | : m_poDS(poDS), m_name(m_poDS ? m_poDS->GetDescription() : std::string()), |
1229 | 0 | m_nameSet(true) |
1230 | 0 | { |
1231 | 0 | if (m_poDS) |
1232 | 0 | m_poDS->Reference(); |
1233 | 0 | } |
1234 | | |
1235 | | /************************************************************************/ |
1236 | | /* GDALArgDatasetValue::Set() */ |
1237 | | /************************************************************************/ |
1238 | | |
1239 | | void GDALArgDatasetValue::Set(const std::string &name) |
1240 | 0 | { |
1241 | 0 | Close(); |
1242 | 0 | m_name = name; |
1243 | 0 | m_nameSet = true; |
1244 | 0 | if (m_ownerArg) |
1245 | 0 | m_ownerArg->NotifyValueSet(); |
1246 | 0 | } |
1247 | | |
1248 | | /************************************************************************/ |
1249 | | /* GDALArgDatasetValue::Set() */ |
1250 | | /************************************************************************/ |
1251 | | |
1252 | | void GDALArgDatasetValue::Set(std::unique_ptr<GDALDataset> poDS) |
1253 | 0 | { |
1254 | 0 | Close(); |
1255 | 0 | m_poDS = poDS.release(); |
1256 | 0 | m_name = m_poDS ? m_poDS->GetDescription() : std::string(); |
1257 | 0 | m_nameSet = true; |
1258 | 0 | if (m_ownerArg) |
1259 | 0 | m_ownerArg->NotifyValueSet(); |
1260 | 0 | } |
1261 | | |
1262 | | /************************************************************************/ |
1263 | | /* GDALArgDatasetValue::Set() */ |
1264 | | /************************************************************************/ |
1265 | | |
1266 | | void GDALArgDatasetValue::Set(GDALDataset *poDS) |
1267 | 0 | { |
1268 | 0 | Close(); |
1269 | 0 | m_poDS = poDS; |
1270 | 0 | if (m_poDS) |
1271 | 0 | m_poDS->Reference(); |
1272 | 0 | m_name = m_poDS ? m_poDS->GetDescription() : std::string(); |
1273 | 0 | m_nameSet = true; |
1274 | 0 | if (m_ownerArg) |
1275 | 0 | m_ownerArg->NotifyValueSet(); |
1276 | 0 | } |
1277 | | |
1278 | | /************************************************************************/ |
1279 | | /* GDALArgDatasetValue::SetFrom() */ |
1280 | | /************************************************************************/ |
1281 | | |
1282 | | void GDALArgDatasetValue::SetFrom(const GDALArgDatasetValue &other) |
1283 | 0 | { |
1284 | 0 | Close(); |
1285 | 0 | m_name = other.m_name; |
1286 | 0 | m_nameSet = other.m_nameSet; |
1287 | 0 | m_poDS = other.m_poDS; |
1288 | 0 | if (m_poDS) |
1289 | 0 | m_poDS->Reference(); |
1290 | 0 | } |
1291 | | |
1292 | | /************************************************************************/ |
1293 | | /* GDALArgDatasetValue::~GDALArgDatasetValue() */ |
1294 | | /************************************************************************/ |
1295 | | |
1296 | | GDALArgDatasetValue::~GDALArgDatasetValue() |
1297 | 0 | { |
1298 | 0 | Close(); |
1299 | 0 | } |
1300 | | |
1301 | | /************************************************************************/ |
1302 | | /* GDALArgDatasetValue::Close() */ |
1303 | | /************************************************************************/ |
1304 | | |
1305 | | bool GDALArgDatasetValue::Close() |
1306 | 0 | { |
1307 | 0 | bool ret = true; |
1308 | 0 | if (m_poDS && m_poDS->Dereference() == 0) |
1309 | 0 | { |
1310 | 0 | ret = m_poDS->Close() == CE_None; |
1311 | 0 | delete m_poDS; |
1312 | 0 | } |
1313 | 0 | m_poDS = nullptr; |
1314 | 0 | return ret; |
1315 | 0 | } |
1316 | | |
1317 | | /************************************************************************/ |
1318 | | /* GDALArgDatasetValue::operator=() */ |
1319 | | /************************************************************************/ |
1320 | | |
1321 | | GDALArgDatasetValue &GDALArgDatasetValue::operator=(GDALArgDatasetValue &&other) |
1322 | 0 | { |
1323 | 0 | Close(); |
1324 | 0 | m_poDS = other.m_poDS; |
1325 | 0 | m_name = other.m_name; |
1326 | 0 | m_nameSet = other.m_nameSet; |
1327 | 0 | other.m_poDS = nullptr; |
1328 | 0 | other.m_name.clear(); |
1329 | 0 | other.m_nameSet = false; |
1330 | 0 | return *this; |
1331 | 0 | } |
1332 | | |
1333 | | /************************************************************************/ |
1334 | | /* GDALArgDatasetValue::GetDataset() */ |
1335 | | /************************************************************************/ |
1336 | | |
1337 | | GDALDataset *GDALArgDatasetValue::GetDatasetIncreaseRefCount() |
1338 | 0 | { |
1339 | 0 | if (m_poDS) |
1340 | 0 | m_poDS->Reference(); |
1341 | 0 | return m_poDS; |
1342 | 0 | } |
1343 | | |
1344 | | /************************************************************************/ |
1345 | | /* GDALArgDatasetValue(GDALArgDatasetValue &&other) */ |
1346 | | /************************************************************************/ |
1347 | | |
1348 | | GDALArgDatasetValue::GDALArgDatasetValue(GDALArgDatasetValue &&other) |
1349 | 0 | : m_poDS(other.m_poDS), m_name(other.m_name), m_nameSet(other.m_nameSet) |
1350 | 0 | { |
1351 | 0 | other.m_poDS = nullptr; |
1352 | 0 | other.m_name.clear(); |
1353 | 0 | } |
1354 | | |
1355 | | /************************************************************************/ |
1356 | | /* GDALInConstructionAlgorithmArg::SetIsCRSArg() */ |
1357 | | /************************************************************************/ |
1358 | | |
1359 | | GDALInConstructionAlgorithmArg &GDALInConstructionAlgorithmArg::SetIsCRSArg( |
1360 | | bool noneAllowed, const std::vector<std::string> &specialValues) |
1361 | 0 | { |
1362 | 0 | if (GetType() != GAAT_STRING) |
1363 | 0 | { |
1364 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1365 | 0 | "SetIsCRSArg() can only be called on a String argument"); |
1366 | 0 | return *this; |
1367 | 0 | } |
1368 | 0 | AddValidationAction( |
1369 | 0 | [this, noneAllowed, specialValues]() |
1370 | 0 | { |
1371 | 0 | const std::string &osVal = |
1372 | 0 | static_cast<const GDALInConstructionAlgorithmArg *>(this) |
1373 | 0 | ->Get<std::string>(); |
1374 | 0 | if (osVal == "?" && m_owner && m_owner->IsCalledFromCommandLine()) |
1375 | 0 | return true; |
1376 | | |
1377 | 0 | if ((!noneAllowed || (osVal != "none" && osVal != "null")) && |
1378 | 0 | std::find(specialValues.begin(), specialValues.end(), osVal) == |
1379 | 0 | specialValues.end()) |
1380 | 0 | { |
1381 | 0 | OGRSpatialReference oSRS; |
1382 | 0 | if (oSRS.SetFromUserInput(osVal.c_str()) != OGRERR_NONE) |
1383 | 0 | { |
1384 | 0 | m_owner->ReportError(CE_Failure, CPLE_AppDefined, |
1385 | 0 | "Invalid value for '%s' argument", |
1386 | 0 | GetName().c_str()); |
1387 | 0 | return false; |
1388 | 0 | } |
1389 | 0 | } |
1390 | 0 | return true; |
1391 | 0 | }); |
1392 | |
|
1393 | 0 | SetAutoCompleteFunction( |
1394 | 0 | [this, noneAllowed, specialValues](const std::string ¤tValue) |
1395 | 0 | { |
1396 | 0 | bool bIsRaster = false; |
1397 | 0 | OGREnvelope sDatasetLongLatEnv; |
1398 | 0 | OGRSpatialReference oDSCRS; |
1399 | 0 | const char *pszCelestialBodyName = nullptr; |
1400 | 0 | if (GetName() == "dst-crs") |
1401 | 0 | { |
1402 | 0 | auto inputArg = m_owner->GetArg(GDAL_ARG_NAME_INPUT); |
1403 | 0 | if (inputArg && inputArg->GetType() == GAAT_DATASET_LIST) |
1404 | 0 | { |
1405 | 0 | auto &val = |
1406 | 0 | inputArg->Get<std::vector<GDALArgDatasetValue>>(); |
1407 | 0 | if (val.size() == 1) |
1408 | 0 | { |
1409 | 0 | CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler); |
1410 | 0 | auto poDS = std::unique_ptr<GDALDataset>( |
1411 | 0 | GDALDataset::Open(val[0].GetName().c_str())); |
1412 | 0 | if (poDS) |
1413 | 0 | { |
1414 | 0 | bIsRaster = poDS->GetRasterCount() != 0; |
1415 | 0 | const OGRSpatialReference *poCRS; |
1416 | 0 | if ((poCRS = poDS->GetSpatialRef()) != nullptr) |
1417 | 0 | { |
1418 | 0 | oDSCRS = *poCRS; |
1419 | 0 | } |
1420 | 0 | else if (poDS->GetLayerCount() >= 1) |
1421 | 0 | { |
1422 | 0 | if (auto poLayer = poDS->GetLayer(0)) |
1423 | 0 | { |
1424 | 0 | if ((poCRS = poLayer->GetSpatialRef()) != |
1425 | 0 | nullptr) |
1426 | 0 | oDSCRS = *poCRS; |
1427 | 0 | } |
1428 | 0 | } |
1429 | 0 | if (!oDSCRS.IsEmpty()) |
1430 | 0 | { |
1431 | 0 | pszCelestialBodyName = |
1432 | 0 | oDSCRS.GetCelestialBodyName(); |
1433 | |
|
1434 | 0 | if (!pszCelestialBodyName || |
1435 | 0 | !EQUAL(pszCelestialBodyName, "Earth")) |
1436 | 0 | { |
1437 | 0 | OGRSpatialReference oLongLat; |
1438 | 0 | oLongLat.CopyGeogCSFrom(&oDSCRS); |
1439 | 0 | oLongLat.SetAxisMappingStrategy( |
1440 | 0 | OAMS_TRADITIONAL_GIS_ORDER); |
1441 | 0 | poDS->GetExtent(&sDatasetLongLatEnv, |
1442 | 0 | &oLongLat); |
1443 | 0 | } |
1444 | 0 | else |
1445 | 0 | { |
1446 | 0 | poDS->GetExtentWGS84LongLat( |
1447 | 0 | &sDatasetLongLatEnv); |
1448 | 0 | } |
1449 | 0 | } |
1450 | 0 | } |
1451 | 0 | } |
1452 | 0 | } |
1453 | 0 | } |
1454 | |
|
1455 | 0 | const auto IsCRSCompatible = |
1456 | 0 | [bIsRaster, &sDatasetLongLatEnv, |
1457 | 0 | pszCelestialBodyName](const OSRCRSInfo *crsInfo) |
1458 | 0 | { |
1459 | 0 | if (!sDatasetLongLatEnv.IsInit()) |
1460 | 0 | return true; |
1461 | 0 | return crsInfo->eType != OSR_CRS_TYPE_VERTICAL && |
1462 | 0 | !(bIsRaster && |
1463 | 0 | crsInfo->eType == OSR_CRS_TYPE_GEOCENTRIC) && |
1464 | 0 | crsInfo->dfWestLongitudeDeg < |
1465 | 0 | crsInfo->dfEastLongitudeDeg && |
1466 | 0 | sDatasetLongLatEnv.MinX < crsInfo->dfEastLongitudeDeg && |
1467 | 0 | sDatasetLongLatEnv.MaxX > crsInfo->dfWestLongitudeDeg && |
1468 | 0 | sDatasetLongLatEnv.MinY < crsInfo->dfNorthLatitudeDeg && |
1469 | 0 | sDatasetLongLatEnv.MaxY > crsInfo->dfSouthLatitudeDeg && |
1470 | 0 | ((pszCelestialBodyName && |
1471 | 0 | crsInfo->pszCelestialBodyName && |
1472 | 0 | EQUAL(pszCelestialBodyName, |
1473 | 0 | crsInfo->pszCelestialBodyName)) || |
1474 | 0 | (!pszCelestialBodyName && |
1475 | 0 | !crsInfo->pszCelestialBodyName)); |
1476 | 0 | }; |
1477 | |
|
1478 | 0 | std::vector<std::string> oRet; |
1479 | 0 | if (noneAllowed) |
1480 | 0 | oRet.push_back("none"); |
1481 | 0 | oRet.insert(oRet.end(), specialValues.begin(), specialValues.end()); |
1482 | 0 | if (!currentValue.empty()) |
1483 | 0 | { |
1484 | 0 | const CPLStringList aosTokens( |
1485 | 0 | CSLTokenizeString2(currentValue.c_str(), ":", 0)); |
1486 | 0 | int nCount = 0; |
1487 | 0 | std::unique_ptr<OSRCRSInfo *, decltype(&OSRDestroyCRSInfoList)> |
1488 | 0 | pCRSList(OSRGetCRSInfoListFromDatabase(aosTokens[0], |
1489 | 0 | nullptr, &nCount), |
1490 | 0 | OSRDestroyCRSInfoList); |
1491 | 0 | std::string osCode; |
1492 | |
|
1493 | 0 | std::vector<const OSRCRSInfo *> candidates; |
1494 | 0 | for (int i = 0; i < nCount; ++i) |
1495 | 0 | { |
1496 | 0 | const auto *entry = (pCRSList.get())[i]; |
1497 | 0 | if (!entry->bDeprecated && IsCRSCompatible(entry)) |
1498 | 0 | { |
1499 | 0 | if (aosTokens.size() == 1 || |
1500 | 0 | STARTS_WITH(entry->pszCode, aosTokens[1])) |
1501 | 0 | { |
1502 | 0 | if (candidates.empty()) |
1503 | 0 | osCode = entry->pszCode; |
1504 | 0 | candidates.push_back(entry); |
1505 | 0 | } |
1506 | 0 | } |
1507 | 0 | } |
1508 | 0 | if (candidates.size() == 1) |
1509 | 0 | { |
1510 | 0 | oRet.push_back(std::move(osCode)); |
1511 | 0 | } |
1512 | 0 | else |
1513 | 0 | { |
1514 | 0 | if (sDatasetLongLatEnv.IsInit()) |
1515 | 0 | { |
1516 | 0 | std::sort( |
1517 | 0 | candidates.begin(), candidates.end(), |
1518 | 0 | [](const OSRCRSInfo *a, const OSRCRSInfo *b) |
1519 | 0 | { |
1520 | 0 | const double dfXa = |
1521 | 0 | a->dfWestLongitudeDeg > |
1522 | 0 | a->dfEastLongitudeDeg |
1523 | 0 | ? a->dfWestLongitudeDeg - |
1524 | 0 | a->dfEastLongitudeDeg |
1525 | 0 | : (180 - a->dfWestLongitudeDeg) + |
1526 | 0 | (a->dfEastLongitudeDeg - -180); |
1527 | 0 | const double dfYa = a->dfNorthLatitudeDeg - |
1528 | 0 | a->dfSouthLatitudeDeg; |
1529 | 0 | const double dfXb = |
1530 | 0 | b->dfWestLongitudeDeg > |
1531 | 0 | b->dfEastLongitudeDeg |
1532 | 0 | ? b->dfWestLongitudeDeg - |
1533 | 0 | b->dfEastLongitudeDeg |
1534 | 0 | : (180 - b->dfWestLongitudeDeg) + |
1535 | 0 | (b->dfEastLongitudeDeg - -180); |
1536 | 0 | const double dfYb = b->dfNorthLatitudeDeg - |
1537 | 0 | b->dfSouthLatitudeDeg; |
1538 | 0 | const double diffArea = |
1539 | 0 | dfXa * dfYa - dfXb * dfYb; |
1540 | 0 | if (diffArea < 0) |
1541 | 0 | return true; |
1542 | 0 | if (diffArea == 0) |
1543 | 0 | { |
1544 | 0 | if (std::string_view(a->pszName) == |
1545 | 0 | b->pszName) |
1546 | 0 | { |
1547 | 0 | if (a->eType == |
1548 | 0 | OSR_CRS_TYPE_GEOGRAPHIC_2D && |
1549 | 0 | b->eType != |
1550 | 0 | OSR_CRS_TYPE_GEOGRAPHIC_2D) |
1551 | 0 | return true; |
1552 | 0 | if (a->eType == |
1553 | 0 | OSR_CRS_TYPE_GEOGRAPHIC_3D && |
1554 | 0 | b->eType == OSR_CRS_TYPE_GEOCENTRIC) |
1555 | 0 | return true; |
1556 | 0 | return false; |
1557 | 0 | } |
1558 | 0 | return std::string_view(a->pszCode) < |
1559 | 0 | b->pszCode; |
1560 | 0 | } |
1561 | 0 | return false; |
1562 | 0 | }); |
1563 | 0 | } |
1564 | |
|
1565 | 0 | for (const auto *entry : candidates) |
1566 | 0 | { |
1567 | 0 | std::string val = std::string(entry->pszCode) |
1568 | 0 | .append(" -- ") |
1569 | 0 | .append(entry->pszName); |
1570 | 0 | if (entry->eType == OSR_CRS_TYPE_GEOGRAPHIC_2D) |
1571 | 0 | val.append(" (geographic 2D)"); |
1572 | 0 | else if (entry->eType == OSR_CRS_TYPE_GEOGRAPHIC_3D) |
1573 | 0 | val.append(" (geographic 3D)"); |
1574 | 0 | else if (entry->eType == OSR_CRS_TYPE_GEOCENTRIC) |
1575 | 0 | val.append(" (geocentric)"); |
1576 | 0 | oRet.push_back(std::move(val)); |
1577 | 0 | } |
1578 | 0 | } |
1579 | 0 | } |
1580 | 0 | if (currentValue.empty() || oRet.empty()) |
1581 | 0 | { |
1582 | 0 | const CPLStringList aosAuthorities( |
1583 | 0 | OSRGetAuthorityListFromDatabase()); |
1584 | 0 | for (const char *pszAuth : cpl::Iterate(aosAuthorities)) |
1585 | 0 | { |
1586 | 0 | int nCount = 0; |
1587 | 0 | OSRDestroyCRSInfoList(OSRGetCRSInfoListFromDatabase( |
1588 | 0 | pszAuth, nullptr, &nCount)); |
1589 | 0 | if (nCount) |
1590 | 0 | oRet.push_back(std::string(pszAuth).append(":")); |
1591 | 0 | } |
1592 | 0 | } |
1593 | 0 | return oRet; |
1594 | 0 | }); |
1595 | |
|
1596 | 0 | return *this; |
1597 | 0 | } |
1598 | | |
1599 | | /************************************************************************/ |
1600 | | /* GDALAlgorithm::GDALAlgorithm() */ |
1601 | | /************************************************************************/ |
1602 | | |
1603 | | GDALAlgorithm::GDALAlgorithm(const std::string &name, |
1604 | | const std::string &description, |
1605 | | const std::string &helpURL) |
1606 | 0 | : m_name(name), m_description(description), m_helpURL(helpURL), |
1607 | 0 | m_helpFullURL(!m_helpURL.empty() && m_helpURL[0] == '/' |
1608 | 0 | ? "https://gdal.org" + m_helpURL |
1609 | 0 | : m_helpURL) |
1610 | 0 | { |
1611 | 0 | AddArg("help", 'h', _("Display help message and exit"), &m_helpRequested) |
1612 | 0 | .SetOnlyForCLI() |
1613 | 0 | .SetCategory(GAAC_COMMON) |
1614 | 0 | .AddAction([this]() { m_specialActionRequested = true; }); |
1615 | 0 | AddArg("help-doc", 0, _("Display help message for use by documentation"), |
1616 | 0 | &m_helpDocRequested) |
1617 | 0 | .SetHidden() |
1618 | 0 | .AddAction([this]() { m_specialActionRequested = true; }); |
1619 | 0 | AddArg("json-usage", 0, _("Display usage as JSON document and exit"), |
1620 | 0 | &m_JSONUsageRequested) |
1621 | 0 | .SetOnlyForCLI() |
1622 | 0 | .SetCategory(GAAC_COMMON) |
1623 | 0 | .AddAction([this]() { m_specialActionRequested = true; }); |
1624 | 0 | AddArg("config", 0, _("Configuration option"), &m_dummyConfigOptions) |
1625 | 0 | .SetMetaVar("<KEY>=<VALUE>") |
1626 | 0 | .SetOnlyForCLI() |
1627 | 0 | .SetCategory(GAAC_COMMON) |
1628 | 0 | .AddAction( |
1629 | 0 | [this]() |
1630 | 0 | { |
1631 | 0 | ReportError( |
1632 | 0 | CE_Warning, CPLE_AppDefined, |
1633 | 0 | "Configuration options passed with the 'config' argument " |
1634 | 0 | "are ignored"); |
1635 | 0 | }); |
1636 | 0 | } |
1637 | | |
1638 | | /************************************************************************/ |
1639 | | /* GDALAlgorithm::~GDALAlgorithm() */ |
1640 | | /************************************************************************/ |
1641 | | |
1642 | 0 | GDALAlgorithm::~GDALAlgorithm() = default; |
1643 | | |
1644 | | /************************************************************************/ |
1645 | | /* GDALAlgorithm::ParseArgument() */ |
1646 | | /************************************************************************/ |
1647 | | |
1648 | | bool GDALAlgorithm::ParseArgument( |
1649 | | GDALAlgorithmArg *arg, const std::string &name, const std::string &value, |
1650 | | std::map< |
1651 | | GDALAlgorithmArg *, |
1652 | | std::variant<std::vector<std::string>, std::vector<int>, |
1653 | | std::vector<double>, std::vector<GDALArgDatasetValue>>> |
1654 | | &inConstructionValues) |
1655 | 0 | { |
1656 | 0 | const bool isListArg = GDALAlgorithmArgTypeIsList(arg->GetType()); |
1657 | 0 | if (arg->IsExplicitlySet() && !isListArg) |
1658 | 0 | { |
1659 | | // Hack for "gdal info" to be able to pass an opened raster dataset |
1660 | | // by "gdal raster info" to the "gdal vector info" algorithm. |
1661 | 0 | if (arg->SkipIfAlreadySet()) |
1662 | 0 | { |
1663 | 0 | arg->SetSkipIfAlreadySet(false); |
1664 | 0 | return true; |
1665 | 0 | } |
1666 | | |
1667 | 0 | ReportError(CE_Failure, CPLE_IllegalArg, |
1668 | 0 | "Argument '%s' has already been specified.", name.c_str()); |
1669 | 0 | return false; |
1670 | 0 | } |
1671 | | |
1672 | 0 | if (!arg->GetRepeatedArgAllowed() && |
1673 | 0 | cpl::contains(inConstructionValues, arg)) |
1674 | 0 | { |
1675 | 0 | ReportError(CE_Failure, CPLE_IllegalArg, |
1676 | 0 | "Argument '%s' has already been specified.", name.c_str()); |
1677 | 0 | return false; |
1678 | 0 | } |
1679 | | |
1680 | 0 | switch (arg->GetType()) |
1681 | 0 | { |
1682 | 0 | case GAAT_BOOLEAN: |
1683 | 0 | { |
1684 | 0 | if (value.empty() || value == "true") |
1685 | 0 | return arg->Set(true); |
1686 | 0 | else if (value == "false") |
1687 | 0 | return arg->Set(false); |
1688 | 0 | else |
1689 | 0 | { |
1690 | 0 | ReportError( |
1691 | 0 | CE_Failure, CPLE_IllegalArg, |
1692 | 0 | "Invalid value '%s' for boolean argument '%s'. Should be " |
1693 | 0 | "'true' or 'false'.", |
1694 | 0 | value.c_str(), name.c_str()); |
1695 | 0 | return false; |
1696 | 0 | } |
1697 | 0 | } |
1698 | | |
1699 | 0 | case GAAT_STRING: |
1700 | 0 | { |
1701 | 0 | return arg->Set(value); |
1702 | 0 | } |
1703 | | |
1704 | 0 | case GAAT_INTEGER: |
1705 | 0 | { |
1706 | 0 | errno = 0; |
1707 | 0 | char *endptr = nullptr; |
1708 | 0 | const auto val = std::strtol(value.c_str(), &endptr, 10); |
1709 | 0 | if (errno == 0 && endptr && |
1710 | 0 | endptr == value.c_str() + value.size() && val >= INT_MIN && |
1711 | 0 | val <= INT_MAX) |
1712 | 0 | { |
1713 | 0 | return arg->Set(static_cast<int>(val)); |
1714 | 0 | } |
1715 | 0 | else |
1716 | 0 | { |
1717 | 0 | ReportError(CE_Failure, CPLE_IllegalArg, |
1718 | 0 | "Expected integer value for argument '%s', " |
1719 | 0 | "but got '%s'.", |
1720 | 0 | name.c_str(), value.c_str()); |
1721 | 0 | return false; |
1722 | 0 | } |
1723 | 0 | } |
1724 | | |
1725 | 0 | case GAAT_REAL: |
1726 | 0 | { |
1727 | 0 | char *endptr = nullptr; |
1728 | 0 | double dfValue = CPLStrtod(value.c_str(), &endptr); |
1729 | 0 | if (endptr != value.c_str() + value.size()) |
1730 | 0 | { |
1731 | 0 | ReportError( |
1732 | 0 | CE_Failure, CPLE_IllegalArg, |
1733 | 0 | "Expected real value for argument '%s', but got '%s'.", |
1734 | 0 | name.c_str(), value.c_str()); |
1735 | 0 | return false; |
1736 | 0 | } |
1737 | 0 | return arg->Set(dfValue); |
1738 | 0 | } |
1739 | | |
1740 | 0 | case GAAT_DATASET: |
1741 | 0 | { |
1742 | 0 | return arg->SetDatasetName(value); |
1743 | 0 | } |
1744 | | |
1745 | 0 | case GAAT_STRING_LIST: |
1746 | 0 | { |
1747 | 0 | const CPLStringList aosTokens( |
1748 | 0 | arg->GetPackedValuesAllowed() |
1749 | 0 | ? CSLTokenizeString2(value.c_str(), ",", CSLT_HONOURSTRINGS) |
1750 | 0 | : CSLAddString(nullptr, value.c_str())); |
1751 | 0 | if (!cpl::contains(inConstructionValues, arg)) |
1752 | 0 | { |
1753 | 0 | inConstructionValues[arg] = std::vector<std::string>(); |
1754 | 0 | } |
1755 | 0 | auto &valueVector = |
1756 | 0 | std::get<std::vector<std::string>>(inConstructionValues[arg]); |
1757 | 0 | for (const char *v : aosTokens) |
1758 | 0 | { |
1759 | 0 | valueVector.push_back(v); |
1760 | 0 | } |
1761 | 0 | break; |
1762 | 0 | } |
1763 | | |
1764 | 0 | case GAAT_INTEGER_LIST: |
1765 | 0 | { |
1766 | 0 | const CPLStringList aosTokens( |
1767 | 0 | arg->GetPackedValuesAllowed() |
1768 | 0 | ? CSLTokenizeString2( |
1769 | 0 | value.c_str(), ",", |
1770 | 0 | CSLT_HONOURSTRINGS | CSLT_STRIPLEADSPACES | |
1771 | 0 | CSLT_STRIPENDSPACES | CSLT_ALLOWEMPTYTOKENS) |
1772 | 0 | : CSLAddString(nullptr, value.c_str())); |
1773 | 0 | if (!cpl::contains(inConstructionValues, arg)) |
1774 | 0 | { |
1775 | 0 | inConstructionValues[arg] = std::vector<int>(); |
1776 | 0 | } |
1777 | 0 | auto &valueVector = |
1778 | 0 | std::get<std::vector<int>>(inConstructionValues[arg]); |
1779 | 0 | for (const char *v : aosTokens) |
1780 | 0 | { |
1781 | 0 | errno = 0; |
1782 | 0 | char *endptr = nullptr; |
1783 | 0 | const auto val = std::strtol(v, &endptr, 10); |
1784 | 0 | if (errno == 0 && endptr && endptr == v + strlen(v) && |
1785 | 0 | val >= INT_MIN && val <= INT_MAX && strlen(v) > 0) |
1786 | 0 | { |
1787 | 0 | valueVector.push_back(static_cast<int>(val)); |
1788 | 0 | } |
1789 | 0 | else |
1790 | 0 | { |
1791 | 0 | ReportError( |
1792 | 0 | CE_Failure, CPLE_IllegalArg, |
1793 | 0 | "Expected list of integer value for argument '%s', " |
1794 | 0 | "but got '%s'.", |
1795 | 0 | name.c_str(), value.c_str()); |
1796 | 0 | return false; |
1797 | 0 | } |
1798 | 0 | } |
1799 | 0 | break; |
1800 | 0 | } |
1801 | | |
1802 | 0 | case GAAT_REAL_LIST: |
1803 | 0 | { |
1804 | 0 | const CPLStringList aosTokens( |
1805 | 0 | arg->GetPackedValuesAllowed() |
1806 | 0 | ? CSLTokenizeString2( |
1807 | 0 | value.c_str(), ",", |
1808 | 0 | CSLT_HONOURSTRINGS | CSLT_STRIPLEADSPACES | |
1809 | 0 | CSLT_STRIPENDSPACES | CSLT_ALLOWEMPTYTOKENS) |
1810 | 0 | : CSLAddString(nullptr, value.c_str())); |
1811 | 0 | if (!cpl::contains(inConstructionValues, arg)) |
1812 | 0 | { |
1813 | 0 | inConstructionValues[arg] = std::vector<double>(); |
1814 | 0 | } |
1815 | 0 | auto &valueVector = |
1816 | 0 | std::get<std::vector<double>>(inConstructionValues[arg]); |
1817 | 0 | for (const char *v : aosTokens) |
1818 | 0 | { |
1819 | 0 | char *endptr = nullptr; |
1820 | 0 | double dfValue = CPLStrtod(v, &endptr); |
1821 | 0 | if (strlen(v) == 0 || endptr != v + strlen(v)) |
1822 | 0 | { |
1823 | 0 | ReportError( |
1824 | 0 | CE_Failure, CPLE_IllegalArg, |
1825 | 0 | "Expected list of real value for argument '%s', " |
1826 | 0 | "but got '%s'.", |
1827 | 0 | name.c_str(), value.c_str()); |
1828 | 0 | return false; |
1829 | 0 | } |
1830 | 0 | valueVector.push_back(dfValue); |
1831 | 0 | } |
1832 | 0 | break; |
1833 | 0 | } |
1834 | | |
1835 | 0 | case GAAT_DATASET_LIST: |
1836 | 0 | { |
1837 | 0 | const CPLStringList aosTokens( |
1838 | 0 | CSLTokenizeString2(value.c_str(), ",", CSLT_HONOURSTRINGS)); |
1839 | 0 | if (!cpl::contains(inConstructionValues, arg)) |
1840 | 0 | { |
1841 | 0 | inConstructionValues[arg] = std::vector<GDALArgDatasetValue>(); |
1842 | 0 | } |
1843 | 0 | auto &valueVector = std::get<std::vector<GDALArgDatasetValue>>( |
1844 | 0 | inConstructionValues[arg]); |
1845 | 0 | for (const char *v : aosTokens) |
1846 | 0 | { |
1847 | 0 | valueVector.push_back(GDALArgDatasetValue(v)); |
1848 | 0 | } |
1849 | 0 | break; |
1850 | 0 | } |
1851 | 0 | } |
1852 | | |
1853 | 0 | return true; |
1854 | 0 | } |
1855 | | |
1856 | | /************************************************************************/ |
1857 | | /* GDALAlgorithm::ParseCommandLineArguments() */ |
1858 | | /************************************************************************/ |
1859 | | |
1860 | | bool GDALAlgorithm::ParseCommandLineArguments( |
1861 | | const std::vector<std::string> &args) |
1862 | 0 | { |
1863 | 0 | if (m_parsedSubStringAlreadyCalled) |
1864 | 0 | { |
1865 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
1866 | 0 | "ParseCommandLineArguments() can only be called once per " |
1867 | 0 | "instance."); |
1868 | 0 | return false; |
1869 | 0 | } |
1870 | 0 | m_parsedSubStringAlreadyCalled = true; |
1871 | | |
1872 | | // AWS like syntax supported too (not advertized) |
1873 | 0 | if (args.size() == 1 && args[0] == "help") |
1874 | 0 | { |
1875 | 0 | auto arg = GetArg("help"); |
1876 | 0 | assert(arg); |
1877 | 0 | arg->Set(true); |
1878 | 0 | arg->RunActions(); |
1879 | 0 | return true; |
1880 | 0 | } |
1881 | | |
1882 | 0 | if (HasSubAlgorithms()) |
1883 | 0 | { |
1884 | 0 | if (args.empty()) |
1885 | 0 | { |
1886 | 0 | ReportError(CE_Failure, CPLE_AppDefined, "Missing %s name.", |
1887 | 0 | m_callPath.size() == 1 ? "command" : "subcommand"); |
1888 | 0 | return false; |
1889 | 0 | } |
1890 | 0 | if (!args[0].empty() && args[0][0] == '-') |
1891 | 0 | { |
1892 | | // go on argument parsing |
1893 | 0 | } |
1894 | 0 | else |
1895 | 0 | { |
1896 | 0 | const auto nCounter = CPLGetErrorCounter(); |
1897 | 0 | m_selectedSubAlgHolder = InstantiateSubAlgorithm(args[0]); |
1898 | 0 | if (m_selectedSubAlgHolder) |
1899 | 0 | { |
1900 | 0 | m_selectedSubAlg = m_selectedSubAlgHolder.get(); |
1901 | 0 | m_selectedSubAlg->SetReferencePathForRelativePaths( |
1902 | 0 | m_referencePath); |
1903 | 0 | m_selectedSubAlg->m_executionForStreamOutput = |
1904 | 0 | m_executionForStreamOutput; |
1905 | 0 | m_selectedSubAlg->m_calledFromCommandLine = |
1906 | 0 | m_calledFromCommandLine; |
1907 | 0 | bool bRet = m_selectedSubAlg->ParseCommandLineArguments( |
1908 | 0 | std::vector<std::string>(args.begin() + 1, args.end())); |
1909 | 0 | m_selectedSubAlg->PropagateSpecialActionTo(this); |
1910 | 0 | return bRet; |
1911 | 0 | } |
1912 | 0 | else |
1913 | 0 | { |
1914 | 0 | if (!(CPLGetErrorCounter() == nCounter + 1 && |
1915 | 0 | strstr(CPLGetLastErrorMsg(), "Do you mean"))) |
1916 | 0 | { |
1917 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
1918 | 0 | "Unknown command: '%s'", args[0].c_str()); |
1919 | 0 | } |
1920 | 0 | return false; |
1921 | 0 | } |
1922 | 0 | } |
1923 | 0 | } |
1924 | | |
1925 | 0 | std::map< |
1926 | 0 | GDALAlgorithmArg *, |
1927 | 0 | std::variant<std::vector<std::string>, std::vector<int>, |
1928 | 0 | std::vector<double>, std::vector<GDALArgDatasetValue>>> |
1929 | 0 | inConstructionValues; |
1930 | |
|
1931 | 0 | std::vector<std::string> lArgs(args); |
1932 | 0 | bool helpValueRequested = false; |
1933 | 0 | for (size_t i = 0; i < lArgs.size(); /* incremented in loop */) |
1934 | 0 | { |
1935 | 0 | const auto &strArg = lArgs[i]; |
1936 | 0 | GDALAlgorithmArg *arg = nullptr; |
1937 | 0 | std::string name; |
1938 | 0 | std::string value; |
1939 | 0 | bool hasValue = false; |
1940 | 0 | if (m_calledFromCommandLine && cpl::ends_with(strArg, "=?")) |
1941 | 0 | helpValueRequested = true; |
1942 | 0 | if (strArg.size() >= 2 && strArg[0] == '-' && strArg[1] == '-') |
1943 | 0 | { |
1944 | 0 | const auto equalPos = strArg.find('='); |
1945 | 0 | name = (equalPos != std::string::npos) ? strArg.substr(0, equalPos) |
1946 | 0 | : strArg; |
1947 | 0 | const std::string nameWithoutDash = name.substr(2); |
1948 | 0 | const auto iterArg = m_mapLongNameToArg.find(nameWithoutDash); |
1949 | 0 | if (iterArg == m_mapLongNameToArg.end()) |
1950 | 0 | { |
1951 | 0 | const std::string bestCandidate = |
1952 | 0 | GetSuggestionForArgumentName(nameWithoutDash); |
1953 | 0 | if (!bestCandidate.empty()) |
1954 | 0 | { |
1955 | 0 | ReportError(CE_Failure, CPLE_IllegalArg, |
1956 | 0 | "Option '%s' is unknown. Do you mean '--%s'?", |
1957 | 0 | name.c_str(), bestCandidate.c_str()); |
1958 | 0 | } |
1959 | 0 | else |
1960 | 0 | { |
1961 | 0 | ReportError(CE_Failure, CPLE_IllegalArg, |
1962 | 0 | "Option '%s' is unknown.", name.c_str()); |
1963 | 0 | } |
1964 | 0 | return false; |
1965 | 0 | } |
1966 | 0 | arg = iterArg->second; |
1967 | 0 | if (equalPos != std::string::npos) |
1968 | 0 | { |
1969 | 0 | hasValue = true; |
1970 | 0 | value = strArg.substr(equalPos + 1); |
1971 | 0 | } |
1972 | 0 | } |
1973 | 0 | else if (strArg.size() >= 2 && strArg[0] == '-' && |
1974 | 0 | CPLGetValueType(strArg.c_str()) == CPL_VALUE_STRING) |
1975 | 0 | { |
1976 | 0 | for (size_t j = 1; j < strArg.size(); ++j) |
1977 | 0 | { |
1978 | 0 | name.clear(); |
1979 | 0 | name += strArg[j]; |
1980 | 0 | const auto iterArg = m_mapShortNameToArg.find(name); |
1981 | 0 | if (iterArg == m_mapShortNameToArg.end()) |
1982 | 0 | { |
1983 | 0 | const std::string nameWithoutDash = strArg.substr(1); |
1984 | 0 | if (m_mapLongNameToArg.find(nameWithoutDash) != |
1985 | 0 | m_mapLongNameToArg.end()) |
1986 | 0 | { |
1987 | 0 | ReportError(CE_Failure, CPLE_IllegalArg, |
1988 | 0 | "Short name option '%s' is unknown. Do you " |
1989 | 0 | "mean '--%s' (with leading double dash) ?", |
1990 | 0 | name.c_str(), nameWithoutDash.c_str()); |
1991 | 0 | } |
1992 | 0 | else |
1993 | 0 | { |
1994 | 0 | const std::string bestCandidate = |
1995 | 0 | GetSuggestionForArgumentName(nameWithoutDash); |
1996 | 0 | if (!bestCandidate.empty()) |
1997 | 0 | { |
1998 | 0 | ReportError( |
1999 | 0 | CE_Failure, CPLE_IllegalArg, |
2000 | 0 | "Short name option '%s' is unknown. Do you " |
2001 | 0 | "mean '--%s' (with leading double dash) ?", |
2002 | 0 | name.c_str(), bestCandidate.c_str()); |
2003 | 0 | } |
2004 | 0 | else |
2005 | 0 | { |
2006 | 0 | ReportError(CE_Failure, CPLE_IllegalArg, |
2007 | 0 | "Short name option '%s' is unknown.", |
2008 | 0 | name.c_str()); |
2009 | 0 | } |
2010 | 0 | } |
2011 | 0 | return false; |
2012 | 0 | } |
2013 | 0 | arg = iterArg->second; |
2014 | 0 | if (strArg.size() > 2) |
2015 | 0 | { |
2016 | 0 | if (arg->GetType() != GAAT_BOOLEAN) |
2017 | 0 | { |
2018 | 0 | ReportError(CE_Failure, CPLE_IllegalArg, |
2019 | 0 | "Invalid argument '%s'. Option '%s' is not " |
2020 | 0 | "a boolean option.", |
2021 | 0 | strArg.c_str(), name.c_str()); |
2022 | 0 | return false; |
2023 | 0 | } |
2024 | | |
2025 | 0 | if (!ParseArgument(arg, name, "true", inConstructionValues)) |
2026 | 0 | return false; |
2027 | 0 | } |
2028 | 0 | } |
2029 | 0 | if (strArg.size() > 2) |
2030 | 0 | { |
2031 | 0 | lArgs.erase(lArgs.begin() + i); |
2032 | 0 | continue; |
2033 | 0 | } |
2034 | 0 | } |
2035 | 0 | else |
2036 | 0 | { |
2037 | 0 | ++i; |
2038 | 0 | continue; |
2039 | 0 | } |
2040 | 0 | CPLAssert(arg); |
2041 | | |
2042 | 0 | if (arg && arg->GetType() == GAAT_BOOLEAN) |
2043 | 0 | { |
2044 | 0 | if (!hasValue) |
2045 | 0 | { |
2046 | 0 | hasValue = true; |
2047 | 0 | value = "true"; |
2048 | 0 | } |
2049 | 0 | } |
2050 | |
|
2051 | 0 | if (!hasValue) |
2052 | 0 | { |
2053 | 0 | if (i + 1 == lArgs.size()) |
2054 | 0 | { |
2055 | 0 | if (m_parseForAutoCompletion) |
2056 | 0 | { |
2057 | 0 | lArgs.erase(lArgs.begin() + i); |
2058 | 0 | break; |
2059 | 0 | } |
2060 | 0 | ReportError( |
2061 | 0 | CE_Failure, CPLE_IllegalArg, |
2062 | 0 | "Expected value for argument '%s', but ran short of tokens", |
2063 | 0 | name.c_str()); |
2064 | 0 | return false; |
2065 | 0 | } |
2066 | 0 | value = lArgs[i + 1]; |
2067 | 0 | lArgs.erase(lArgs.begin() + i + 1); |
2068 | 0 | } |
2069 | | |
2070 | 0 | if (arg && !ParseArgument(arg, name, value, inConstructionValues)) |
2071 | 0 | return false; |
2072 | | |
2073 | 0 | lArgs.erase(lArgs.begin() + i); |
2074 | 0 | } |
2075 | | |
2076 | 0 | if (m_specialActionRequested) |
2077 | 0 | { |
2078 | 0 | return true; |
2079 | 0 | } |
2080 | | |
2081 | 0 | const auto ProcessInConstructionValues = [&inConstructionValues]() |
2082 | 0 | { |
2083 | 0 | for (auto &[arg, value] : inConstructionValues) |
2084 | 0 | { |
2085 | 0 | if (arg->GetType() == GAAT_STRING_LIST) |
2086 | 0 | { |
2087 | 0 | if (!arg->Set(std::get<std::vector<std::string>>( |
2088 | 0 | inConstructionValues[arg]))) |
2089 | 0 | { |
2090 | 0 | return false; |
2091 | 0 | } |
2092 | 0 | } |
2093 | 0 | else if (arg->GetType() == GAAT_INTEGER_LIST) |
2094 | 0 | { |
2095 | 0 | if (!arg->Set( |
2096 | 0 | std::get<std::vector<int>>(inConstructionValues[arg]))) |
2097 | 0 | { |
2098 | 0 | return false; |
2099 | 0 | } |
2100 | 0 | } |
2101 | 0 | else if (arg->GetType() == GAAT_REAL_LIST) |
2102 | 0 | { |
2103 | 0 | if (!arg->Set(std::get<std::vector<double>>( |
2104 | 0 | inConstructionValues[arg]))) |
2105 | 0 | { |
2106 | 0 | return false; |
2107 | 0 | } |
2108 | 0 | } |
2109 | 0 | else if (arg->GetType() == GAAT_DATASET_LIST) |
2110 | 0 | { |
2111 | 0 | if (!arg->Set( |
2112 | 0 | std::move(std::get<std::vector<GDALArgDatasetValue>>( |
2113 | 0 | inConstructionValues[arg])))) |
2114 | 0 | { |
2115 | 0 | return false; |
2116 | 0 | } |
2117 | 0 | } |
2118 | 0 | } |
2119 | 0 | return true; |
2120 | 0 | }; |
2121 | | |
2122 | | // Process positional arguments that have not been set through their |
2123 | | // option name. |
2124 | 0 | size_t i = 0; |
2125 | 0 | size_t iCurPosArg = 0; |
2126 | | |
2127 | | // Special case for <INPUT> <AUXILIARY>... <OUTPUT> |
2128 | 0 | if (m_positionalArgs.size() == 3 && |
2129 | 0 | (m_positionalArgs[0]->IsRequired() || |
2130 | 0 | m_positionalArgs[0]->GetMinCount() == 1) && |
2131 | 0 | m_positionalArgs[0]->GetMaxCount() == 1 && |
2132 | 0 | (m_positionalArgs[1]->IsRequired() || |
2133 | 0 | m_positionalArgs[1]->GetMinCount() == 1) && |
2134 | | /* Second argument may have several occurrences */ |
2135 | 0 | m_positionalArgs[1]->GetMaxCount() >= 1 && |
2136 | 0 | (m_positionalArgs[2]->IsRequired() || |
2137 | 0 | m_positionalArgs[2]->GetMinCount() == 1) && |
2138 | 0 | m_positionalArgs[2]->GetMaxCount() == 1 && |
2139 | 0 | !m_positionalArgs[0]->IsExplicitlySet() && |
2140 | 0 | !m_positionalArgs[1]->IsExplicitlySet() && |
2141 | 0 | !m_positionalArgs[2]->IsExplicitlySet()) |
2142 | 0 | { |
2143 | 0 | if (lArgs.size() - i < 3) |
2144 | 0 | { |
2145 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
2146 | 0 | "Not enough positional values."); |
2147 | 0 | return false; |
2148 | 0 | } |
2149 | 0 | bool ok = ParseArgument(m_positionalArgs[0], |
2150 | 0 | m_positionalArgs[0]->GetName().c_str(), |
2151 | 0 | lArgs[i], inConstructionValues); |
2152 | 0 | if (ok) |
2153 | 0 | { |
2154 | 0 | ++i; |
2155 | 0 | for (; i + 1 < lArgs.size() && ok; ++i) |
2156 | 0 | { |
2157 | 0 | ok = ParseArgument(m_positionalArgs[1], |
2158 | 0 | m_positionalArgs[1]->GetName().c_str(), |
2159 | 0 | lArgs[i], inConstructionValues); |
2160 | 0 | } |
2161 | 0 | } |
2162 | 0 | if (ok) |
2163 | 0 | { |
2164 | 0 | ok = ParseArgument(m_positionalArgs[2], |
2165 | 0 | m_positionalArgs[2]->GetName().c_str(), lArgs[i], |
2166 | 0 | inConstructionValues); |
2167 | 0 | ++i; |
2168 | 0 | } |
2169 | 0 | if (!ok) |
2170 | 0 | { |
2171 | 0 | ProcessInConstructionValues(); |
2172 | 0 | return false; |
2173 | 0 | } |
2174 | 0 | } |
2175 | | |
2176 | 0 | while (i < lArgs.size() && iCurPosArg < m_positionalArgs.size()) |
2177 | 0 | { |
2178 | 0 | GDALAlgorithmArg *arg = m_positionalArgs[iCurPosArg]; |
2179 | 0 | while (arg->IsExplicitlySet()) |
2180 | 0 | { |
2181 | 0 | ++iCurPosArg; |
2182 | 0 | if (iCurPosArg == m_positionalArgs.size()) |
2183 | 0 | break; |
2184 | 0 | arg = m_positionalArgs[iCurPosArg]; |
2185 | 0 | } |
2186 | 0 | if (iCurPosArg == m_positionalArgs.size()) |
2187 | 0 | { |
2188 | 0 | break; |
2189 | 0 | } |
2190 | 0 | if (GDALAlgorithmArgTypeIsList(arg->GetType()) && |
2191 | 0 | arg->GetMinCount() != arg->GetMaxCount()) |
2192 | 0 | { |
2193 | 0 | if (iCurPosArg == 0) |
2194 | 0 | { |
2195 | 0 | size_t nCountAtEnd = 0; |
2196 | 0 | for (size_t j = 1; j < m_positionalArgs.size(); j++) |
2197 | 0 | { |
2198 | 0 | const auto *otherArg = m_positionalArgs[j]; |
2199 | 0 | if (GDALAlgorithmArgTypeIsList(otherArg->GetType())) |
2200 | 0 | { |
2201 | 0 | if (otherArg->GetMinCount() != otherArg->GetMaxCount()) |
2202 | 0 | { |
2203 | 0 | ReportError( |
2204 | 0 | CE_Failure, CPLE_AppDefined, |
2205 | 0 | "Ambiguity in definition of positional " |
2206 | 0 | "argument " |
2207 | 0 | "'%s' given it has a varying number of values, " |
2208 | 0 | "but follows argument '%s' which also has a " |
2209 | 0 | "varying number of values", |
2210 | 0 | otherArg->GetName().c_str(), |
2211 | 0 | arg->GetName().c_str()); |
2212 | 0 | ProcessInConstructionValues(); |
2213 | 0 | return false; |
2214 | 0 | } |
2215 | 0 | nCountAtEnd += otherArg->GetMinCount(); |
2216 | 0 | } |
2217 | 0 | else |
2218 | 0 | { |
2219 | 0 | if (!otherArg->IsRequired()) |
2220 | 0 | { |
2221 | 0 | ReportError( |
2222 | 0 | CE_Failure, CPLE_AppDefined, |
2223 | 0 | "Ambiguity in definition of positional " |
2224 | 0 | "argument " |
2225 | 0 | "'%s', given it is not required but follows " |
2226 | 0 | "argument '%s' which has a varying number of " |
2227 | 0 | "values", |
2228 | 0 | otherArg->GetName().c_str(), |
2229 | 0 | arg->GetName().c_str()); |
2230 | 0 | ProcessInConstructionValues(); |
2231 | 0 | return false; |
2232 | 0 | } |
2233 | 0 | nCountAtEnd++; |
2234 | 0 | } |
2235 | 0 | } |
2236 | 0 | if (lArgs.size() < nCountAtEnd) |
2237 | 0 | { |
2238 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
2239 | 0 | "Not enough positional values."); |
2240 | 0 | ProcessInConstructionValues(); |
2241 | 0 | return false; |
2242 | 0 | } |
2243 | 0 | for (; i < lArgs.size() - nCountAtEnd; ++i) |
2244 | 0 | { |
2245 | 0 | if (!ParseArgument(arg, arg->GetName().c_str(), lArgs[i], |
2246 | 0 | inConstructionValues)) |
2247 | 0 | { |
2248 | 0 | ProcessInConstructionValues(); |
2249 | 0 | return false; |
2250 | 0 | } |
2251 | 0 | } |
2252 | 0 | } |
2253 | 0 | else if (iCurPosArg == m_positionalArgs.size() - 1) |
2254 | 0 | { |
2255 | 0 | for (; i < lArgs.size(); ++i) |
2256 | 0 | { |
2257 | 0 | if (!ParseArgument(arg, arg->GetName().c_str(), lArgs[i], |
2258 | 0 | inConstructionValues)) |
2259 | 0 | { |
2260 | 0 | ProcessInConstructionValues(); |
2261 | 0 | return false; |
2262 | 0 | } |
2263 | 0 | } |
2264 | 0 | } |
2265 | 0 | else |
2266 | 0 | { |
2267 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
2268 | 0 | "Ambiguity in definition of positional arguments: " |
2269 | 0 | "arguments with varying number of values must be " |
2270 | 0 | "first or last one."); |
2271 | 0 | return false; |
2272 | 0 | } |
2273 | 0 | } |
2274 | 0 | else |
2275 | 0 | { |
2276 | 0 | if (lArgs.size() - i < static_cast<size_t>(arg->GetMaxCount())) |
2277 | 0 | { |
2278 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
2279 | 0 | "Not enough positional values."); |
2280 | 0 | return false; |
2281 | 0 | } |
2282 | 0 | const size_t iMax = i + arg->GetMaxCount(); |
2283 | 0 | for (; i < iMax; ++i) |
2284 | 0 | { |
2285 | 0 | if (!ParseArgument(arg, arg->GetName().c_str(), lArgs[i], |
2286 | 0 | inConstructionValues)) |
2287 | 0 | { |
2288 | 0 | ProcessInConstructionValues(); |
2289 | 0 | return false; |
2290 | 0 | } |
2291 | 0 | } |
2292 | 0 | } |
2293 | 0 | ++iCurPosArg; |
2294 | 0 | } |
2295 | | |
2296 | 0 | if (i < lArgs.size()) |
2297 | 0 | { |
2298 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
2299 | 0 | "Positional values starting at '%s' are not expected.", |
2300 | 0 | lArgs[i].c_str()); |
2301 | 0 | return false; |
2302 | 0 | } |
2303 | | |
2304 | 0 | if (!ProcessInConstructionValues()) |
2305 | 0 | { |
2306 | 0 | return false; |
2307 | 0 | } |
2308 | | |
2309 | | // Skip to first unset positional argument. |
2310 | 0 | while (iCurPosArg < m_positionalArgs.size() && |
2311 | 0 | m_positionalArgs[iCurPosArg]->IsExplicitlySet()) |
2312 | 0 | { |
2313 | 0 | ++iCurPosArg; |
2314 | 0 | } |
2315 | | // Check if this positional argument is required. |
2316 | 0 | if (iCurPosArg < m_positionalArgs.size() && !helpValueRequested && |
2317 | 0 | (GDALAlgorithmArgTypeIsList(m_positionalArgs[iCurPosArg]->GetType()) |
2318 | 0 | ? m_positionalArgs[iCurPosArg]->GetMinCount() > 0 |
2319 | 0 | : m_positionalArgs[iCurPosArg]->IsRequired())) |
2320 | 0 | { |
2321 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
2322 | 0 | "Positional arguments starting at '%s' have not been " |
2323 | 0 | "specified.", |
2324 | 0 | m_positionalArgs[iCurPosArg]->GetMetaVar().c_str()); |
2325 | 0 | return false; |
2326 | 0 | } |
2327 | | |
2328 | 0 | if (m_calledFromCommandLine) |
2329 | 0 | { |
2330 | 0 | for (auto &arg : m_args) |
2331 | 0 | { |
2332 | 0 | if (arg->IsExplicitlySet() && |
2333 | 0 | ((arg->GetType() == GAAT_STRING && |
2334 | 0 | arg->Get<std::string>() == "?") || |
2335 | 0 | (arg->GetType() == GAAT_STRING_LIST && |
2336 | 0 | arg->Get<std::vector<std::string>>().size() == 1 && |
2337 | 0 | arg->Get<std::vector<std::string>>()[0] == "?"))) |
2338 | 0 | { |
2339 | 0 | { |
2340 | 0 | CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler); |
2341 | 0 | ValidateArguments(); |
2342 | 0 | } |
2343 | |
|
2344 | 0 | auto choices = arg->GetChoices(); |
2345 | 0 | if (choices.empty()) |
2346 | 0 | choices = arg->GetAutoCompleteChoices(std::string()); |
2347 | 0 | if (!choices.empty()) |
2348 | 0 | { |
2349 | 0 | if (choices.size() == 1) |
2350 | 0 | { |
2351 | 0 | ReportError( |
2352 | 0 | CE_Failure, CPLE_AppDefined, |
2353 | 0 | "Single potential value for argument '%s' is '%s'", |
2354 | 0 | arg->GetName().c_str(), choices.front().c_str()); |
2355 | 0 | } |
2356 | 0 | else |
2357 | 0 | { |
2358 | 0 | std::string msg("Potential values for argument '"); |
2359 | 0 | msg += arg->GetName(); |
2360 | 0 | msg += "' are:"; |
2361 | 0 | for (const auto &v : choices) |
2362 | 0 | { |
2363 | 0 | msg += "\n- "; |
2364 | 0 | msg += v; |
2365 | 0 | } |
2366 | 0 | ReportError(CE_Failure, CPLE_AppDefined, "%s", |
2367 | 0 | msg.c_str()); |
2368 | 0 | } |
2369 | 0 | return false; |
2370 | 0 | } |
2371 | 0 | } |
2372 | 0 | } |
2373 | 0 | } |
2374 | | |
2375 | 0 | return m_skipValidationInParseCommandLine || ValidateArguments(); |
2376 | 0 | } |
2377 | | |
2378 | | /************************************************************************/ |
2379 | | /* GDALAlgorithm::ReportError() */ |
2380 | | /************************************************************************/ |
2381 | | |
2382 | | //! @cond Doxygen_Suppress |
2383 | | void GDALAlgorithm::ReportError(CPLErr eErrClass, CPLErrorNum err_no, |
2384 | | const char *fmt, ...) const |
2385 | 0 | { |
2386 | 0 | va_list args; |
2387 | 0 | va_start(args, fmt); |
2388 | 0 | CPLError(eErrClass, err_no, "%s", |
2389 | 0 | std::string(m_name) |
2390 | 0 | .append(": ") |
2391 | 0 | .append(CPLString().vPrintf(fmt, args)) |
2392 | 0 | .c_str()); |
2393 | 0 | va_end(args); |
2394 | 0 | } |
2395 | | |
2396 | | //! @endcond |
2397 | | |
2398 | | /************************************************************************/ |
2399 | | /* GDALAlgorithm::ProcessDatasetArg() */ |
2400 | | /************************************************************************/ |
2401 | | |
2402 | | bool GDALAlgorithm::ProcessDatasetArg(GDALAlgorithmArg *arg, |
2403 | | GDALAlgorithm *algForOutput) |
2404 | 0 | { |
2405 | 0 | bool ret = true; |
2406 | |
|
2407 | 0 | const auto updateArg = algForOutput->GetArg(GDAL_ARG_NAME_UPDATE); |
2408 | 0 | const bool hasUpdateArg = updateArg && updateArg->GetType() == GAAT_BOOLEAN; |
2409 | 0 | const bool update = hasUpdateArg && updateArg->Get<bool>(); |
2410 | 0 | const auto overwriteArg = algForOutput->GetArg(GDAL_ARG_NAME_OVERWRITE); |
2411 | 0 | const bool overwrite = |
2412 | 0 | (arg->IsOutput() && overwriteArg && |
2413 | 0 | overwriteArg->GetType() == GAAT_BOOLEAN && overwriteArg->Get<bool>()); |
2414 | 0 | auto outputArg = algForOutput->GetArg(GDAL_ARG_NAME_OUTPUT); |
2415 | 0 | auto &val = [arg]() -> GDALArgDatasetValue & |
2416 | 0 | { |
2417 | 0 | if (arg->GetType() == GAAT_DATASET_LIST) |
2418 | 0 | return arg->Get<std::vector<GDALArgDatasetValue>>()[0]; |
2419 | 0 | else |
2420 | 0 | return arg->Get<GDALArgDatasetValue>(); |
2421 | 0 | }(); |
2422 | 0 | const bool onlyInputSpecifiedInUpdateAndOutputNotRequired = |
2423 | 0 | arg->GetName() == GDAL_ARG_NAME_INPUT && outputArg && |
2424 | 0 | !outputArg->IsExplicitlySet() && !outputArg->IsRequired() && update && |
2425 | 0 | !overwrite; |
2426 | 0 | if (!val.GetDatasetRef() && !val.IsNameSet()) |
2427 | 0 | { |
2428 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
2429 | 0 | "Argument '%s' has no dataset object or dataset name.", |
2430 | 0 | arg->GetName().c_str()); |
2431 | 0 | ret = false; |
2432 | 0 | } |
2433 | 0 | else if (val.GetDatasetRef() && !CheckCanSetDatasetObject(arg)) |
2434 | 0 | { |
2435 | 0 | return false; |
2436 | 0 | } |
2437 | 0 | else if (!val.GetDatasetRef() && arg->AutoOpenDataset() && |
2438 | 0 | (!arg->IsOutput() || (arg == outputArg && update && !overwrite) || |
2439 | 0 | onlyInputSpecifiedInUpdateAndOutputNotRequired)) |
2440 | 0 | { |
2441 | 0 | int flags = arg->GetDatasetType(); |
2442 | 0 | bool assignToOutputArg = false; |
2443 | | |
2444 | | // Check if input and output parameters point to the same |
2445 | | // filename (for vector datasets) |
2446 | 0 | if (arg->GetName() == GDAL_ARG_NAME_INPUT && update && !overwrite && |
2447 | 0 | outputArg && outputArg->GetType() == GAAT_DATASET) |
2448 | 0 | { |
2449 | 0 | auto &outputVal = outputArg->Get<GDALArgDatasetValue>(); |
2450 | 0 | if (!outputVal.GetDatasetRef() && |
2451 | 0 | outputVal.GetName() == val.GetName() && |
2452 | 0 | (outputArg->GetDatasetInputFlags() & GADV_OBJECT) != 0) |
2453 | 0 | { |
2454 | 0 | assignToOutputArg = true; |
2455 | 0 | flags |= GDAL_OF_UPDATE | GDAL_OF_VERBOSE_ERROR; |
2456 | 0 | } |
2457 | 0 | else if (onlyInputSpecifiedInUpdateAndOutputNotRequired) |
2458 | 0 | { |
2459 | 0 | if (updateArg->GetMutualExclusionGroup().empty() || |
2460 | 0 | outputArg->GetMutualExclusionGroup().empty() || |
2461 | 0 | updateArg->GetMutualExclusionGroup() != |
2462 | 0 | outputArg->GetMutualExclusionGroup()) |
2463 | 0 | { |
2464 | 0 | assignToOutputArg = true; |
2465 | 0 | } |
2466 | 0 | flags |= GDAL_OF_UPDATE | GDAL_OF_VERBOSE_ERROR; |
2467 | 0 | } |
2468 | 0 | } |
2469 | |
|
2470 | 0 | if (!arg->IsOutput() || arg->GetDatasetInputFlags() == GADV_NAME) |
2471 | 0 | flags |= GDAL_OF_VERBOSE_ERROR; |
2472 | 0 | if ((arg == outputArg || !outputArg) && update) |
2473 | 0 | flags |= GDAL_OF_UPDATE | GDAL_OF_VERBOSE_ERROR; |
2474 | |
|
2475 | 0 | const auto readOnlyArg = algForOutput->GetArg(GDAL_ARG_NAME_READ_ONLY); |
2476 | 0 | const bool readOnly = |
2477 | 0 | (readOnlyArg && readOnlyArg->GetType() == GAAT_BOOLEAN && |
2478 | 0 | readOnlyArg->Get<bool>()); |
2479 | 0 | if (readOnly) |
2480 | 0 | flags &= ~GDAL_OF_UPDATE; |
2481 | |
|
2482 | 0 | CPLStringList aosOpenOptions; |
2483 | 0 | CPLStringList aosAllowedDrivers; |
2484 | 0 | if (arg->IsInput()) |
2485 | 0 | { |
2486 | 0 | const auto ooArg = GetArg(GDAL_ARG_NAME_OPEN_OPTION); |
2487 | 0 | if (ooArg && ooArg->GetType() == GAAT_STRING_LIST) |
2488 | 0 | aosOpenOptions = |
2489 | 0 | CPLStringList(ooArg->Get<std::vector<std::string>>()); |
2490 | |
|
2491 | 0 | const auto ifArg = GetArg(GDAL_ARG_NAME_INPUT_FORMAT); |
2492 | 0 | if (ifArg && ifArg->GetType() == GAAT_STRING_LIST) |
2493 | 0 | aosAllowedDrivers = |
2494 | 0 | CPLStringList(ifArg->Get<std::vector<std::string>>()); |
2495 | 0 | } |
2496 | |
|
2497 | 0 | std::string osDatasetName = val.GetName(); |
2498 | 0 | if (!m_referencePath.empty()) |
2499 | 0 | { |
2500 | 0 | osDatasetName = GDALDataset::BuildFilename( |
2501 | 0 | osDatasetName.c_str(), m_referencePath.c_str(), true); |
2502 | 0 | } |
2503 | 0 | if (osDatasetName == "-" && (flags & GDAL_OF_UPDATE) == 0) |
2504 | 0 | osDatasetName = "/vsistdin/"; |
2505 | | |
2506 | | // Handle special case of overview delete in GTiff which would fail |
2507 | | // if it is COG without IGNORE_COG_LAYOUT_BREAK=YES open option. |
2508 | 0 | if ((flags & GDAL_OF_UPDATE) != 0 && m_callPath.size() == 4 && |
2509 | 0 | m_callPath[2] == "overview" && m_callPath[3] == "delete" && |
2510 | 0 | aosOpenOptions.FetchNameValue("IGNORE_COG_LAYOUT_BREAK") == nullptr) |
2511 | 0 | { |
2512 | 0 | CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler); |
2513 | 0 | GDALDriverH hDrv = |
2514 | 0 | GDALIdentifyDriver(osDatasetName.c_str(), nullptr); |
2515 | 0 | if (hDrv && EQUAL(GDALGetDescription(hDrv), "GTiff")) |
2516 | 0 | { |
2517 | | // Cleaning does not break COG layout |
2518 | 0 | aosOpenOptions.SetNameValue("IGNORE_COG_LAYOUT_BREAK", "YES"); |
2519 | 0 | } |
2520 | 0 | } |
2521 | |
|
2522 | 0 | auto poDS = |
2523 | 0 | GDALDataset::Open(osDatasetName.c_str(), flags, |
2524 | 0 | aosAllowedDrivers.List(), aosOpenOptions.List()); |
2525 | 0 | if (poDS) |
2526 | 0 | { |
2527 | 0 | if (assignToOutputArg) |
2528 | 0 | { |
2529 | | // Avoid opening twice the same datasource if it is both |
2530 | | // the input and output. |
2531 | | // Known to cause problems with at least FGdb, SQLite |
2532 | | // and GPKG drivers. See #4270 |
2533 | | // Restrict to those 3 drivers. For example it is known |
2534 | | // to break with the PG driver due to the way it |
2535 | | // manages transactions. |
2536 | 0 | auto poDriver = poDS->GetDriver(); |
2537 | 0 | if (poDriver && (EQUAL(poDriver->GetDescription(), "FileGDB") || |
2538 | 0 | EQUAL(poDriver->GetDescription(), "SQLite") || |
2539 | 0 | EQUAL(poDriver->GetDescription(), "GPKG"))) |
2540 | 0 | { |
2541 | 0 | outputArg->Get<GDALArgDatasetValue>().Set(poDS); |
2542 | 0 | } |
2543 | 0 | else if (onlyInputSpecifiedInUpdateAndOutputNotRequired) |
2544 | 0 | { |
2545 | 0 | outputArg->Get<GDALArgDatasetValue>().Set(poDS); |
2546 | 0 | } |
2547 | 0 | } |
2548 | 0 | val.Set(poDS); |
2549 | 0 | poDS->ReleaseRef(); |
2550 | 0 | } |
2551 | 0 | else |
2552 | 0 | { |
2553 | 0 | ret = false; |
2554 | 0 | } |
2555 | 0 | } |
2556 | 0 | else if (onlyInputSpecifiedInUpdateAndOutputNotRequired && |
2557 | 0 | val.GetDatasetRef()) |
2558 | 0 | { |
2559 | 0 | if (updateArg->GetMutualExclusionGroup().empty() || |
2560 | 0 | outputArg->GetMutualExclusionGroup().empty() || |
2561 | 0 | updateArg->GetMutualExclusionGroup() != |
2562 | 0 | outputArg->GetMutualExclusionGroup()) |
2563 | 0 | { |
2564 | 0 | outputArg->Get<GDALArgDatasetValue>().Set(val.GetDatasetRef()); |
2565 | 0 | } |
2566 | 0 | } |
2567 | | |
2568 | | // Deal with overwriting the output dataset |
2569 | 0 | if (ret && arg == outputArg && val.GetDatasetRef() == nullptr) |
2570 | 0 | { |
2571 | 0 | const auto appendArg = algForOutput->GetArg(GDAL_ARG_NAME_APPEND); |
2572 | 0 | const bool hasAppendArg = |
2573 | 0 | appendArg && appendArg->GetType() == GAAT_BOOLEAN; |
2574 | 0 | const bool append = (hasAppendArg && appendArg->Get<bool>()); |
2575 | 0 | if (!append) |
2576 | 0 | { |
2577 | | // If outputting to MEM, do not try to erase a real file of the same name! |
2578 | 0 | const auto outputFormatArg = |
2579 | 0 | algForOutput->GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT); |
2580 | 0 | if (!(outputFormatArg && |
2581 | 0 | outputFormatArg->GetType() == GAAT_STRING && |
2582 | 0 | (EQUAL(outputFormatArg->Get<std::string>().c_str(), "MEM") || |
2583 | 0 | EQUAL(outputFormatArg->Get<std::string>().c_str(), |
2584 | 0 | "stream") || |
2585 | 0 | EQUAL(outputFormatArg->Get<std::string>().c_str(), |
2586 | 0 | "Memory")))) |
2587 | 0 | { |
2588 | 0 | const char *pszType = ""; |
2589 | 0 | GDALDriver *poDriver = nullptr; |
2590 | 0 | if (!val.GetName().empty() && |
2591 | 0 | GDALDoesFileOrDatasetExist(val.GetName().c_str(), &pszType, |
2592 | 0 | &poDriver)) |
2593 | 0 | { |
2594 | 0 | if (!overwrite) |
2595 | 0 | { |
2596 | 0 | ReportError( |
2597 | 0 | CE_Failure, CPLE_AppDefined, |
2598 | 0 | "%s '%s' already exists. Specify the --overwrite " |
2599 | 0 | "option to overwrite it%s.", |
2600 | 0 | pszType, val.GetName().c_str(), |
2601 | 0 | hasAppendArg |
2602 | 0 | ? " or the --append option to append to it" |
2603 | 0 | : hasUpdateArg |
2604 | 0 | ? " or the --update option to update it" |
2605 | 0 | : ""); |
2606 | 0 | return false; |
2607 | 0 | } |
2608 | 0 | else if (EQUAL(pszType, "File")) |
2609 | 0 | { |
2610 | 0 | VSIUnlink(val.GetName().c_str()); |
2611 | 0 | } |
2612 | 0 | else if (EQUAL(pszType, "Directory")) |
2613 | 0 | { |
2614 | | // We don't want the user to accidentally erase a non-GDAL dataset |
2615 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
2616 | 0 | "Directory '%s' already exists, but is not " |
2617 | 0 | "recognized as a valid GDAL dataset. " |
2618 | 0 | "Please manually delete it before retrying", |
2619 | 0 | val.GetName().c_str()); |
2620 | 0 | return false; |
2621 | 0 | } |
2622 | 0 | else if (poDriver) |
2623 | 0 | { |
2624 | 0 | CPLStringList aosDrivers; |
2625 | 0 | aosDrivers.AddString(poDriver->GetDescription()); |
2626 | 0 | CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler); |
2627 | 0 | GDALDriver::QuietDelete(val.GetName().c_str(), |
2628 | 0 | aosDrivers.List()); |
2629 | 0 | } |
2630 | 0 | } |
2631 | 0 | } |
2632 | 0 | } |
2633 | 0 | } |
2634 | | |
2635 | 0 | return ret; |
2636 | 0 | } |
2637 | | |
2638 | | /************************************************************************/ |
2639 | | /* GDALAlgorithm::ValidateArguments() */ |
2640 | | /************************************************************************/ |
2641 | | |
2642 | | bool GDALAlgorithm::ValidateArguments() |
2643 | 0 | { |
2644 | 0 | if (m_selectedSubAlg) |
2645 | 0 | return m_selectedSubAlg->ValidateArguments(); |
2646 | | |
2647 | 0 | if (m_specialActionRequested) |
2648 | 0 | return true; |
2649 | | |
2650 | | // If only --output=format=MEM/stream is specified and not --output, |
2651 | | // then set empty name for --output. |
2652 | 0 | auto outputArg = GetArg(GDAL_ARG_NAME_OUTPUT); |
2653 | 0 | auto outputFormatArg = GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT); |
2654 | 0 | if (outputArg && outputFormatArg && outputFormatArg->IsExplicitlySet() && |
2655 | 0 | !outputArg->IsExplicitlySet() && |
2656 | 0 | outputFormatArg->GetType() == GAAT_STRING && |
2657 | 0 | (EQUAL(outputFormatArg->Get<std::string>().c_str(), "MEM") || |
2658 | 0 | EQUAL(outputFormatArg->Get<std::string>().c_str(), "stream")) && |
2659 | 0 | outputArg->GetType() == GAAT_DATASET && |
2660 | 0 | (outputArg->GetDatasetInputFlags() & GADV_NAME)) |
2661 | 0 | { |
2662 | 0 | outputArg->Get<GDALArgDatasetValue>().Set(""); |
2663 | 0 | } |
2664 | | |
2665 | | // The method may emit several errors if several constraints are not met. |
2666 | 0 | bool ret = true; |
2667 | 0 | std::map<std::string, std::string> mutualExclusionGroupUsed; |
2668 | 0 | for (auto &arg : m_args) |
2669 | 0 | { |
2670 | | // Check mutually exclusive arguments |
2671 | 0 | if (arg->IsExplicitlySet()) |
2672 | 0 | { |
2673 | 0 | const auto &mutualExclusionGroup = arg->GetMutualExclusionGroup(); |
2674 | 0 | if (!mutualExclusionGroup.empty()) |
2675 | 0 | { |
2676 | 0 | auto oIter = |
2677 | 0 | mutualExclusionGroupUsed.find(mutualExclusionGroup); |
2678 | 0 | if (oIter != mutualExclusionGroupUsed.end()) |
2679 | 0 | { |
2680 | 0 | ret = false; |
2681 | 0 | ReportError( |
2682 | 0 | CE_Failure, CPLE_AppDefined, |
2683 | 0 | "Argument '%s' is mutually exclusive with '%s'.", |
2684 | 0 | arg->GetName().c_str(), oIter->second.c_str()); |
2685 | 0 | } |
2686 | 0 | else |
2687 | 0 | { |
2688 | 0 | mutualExclusionGroupUsed[mutualExclusionGroup] = |
2689 | 0 | arg->GetName(); |
2690 | 0 | } |
2691 | 0 | } |
2692 | 0 | } |
2693 | |
|
2694 | 0 | if (arg->IsRequired() && !arg->IsExplicitlySet() && |
2695 | 0 | !arg->HasDefaultValue()) |
2696 | 0 | { |
2697 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
2698 | 0 | "Required argument '%s' has not been specified.", |
2699 | 0 | arg->GetName().c_str()); |
2700 | 0 | ret = false; |
2701 | 0 | } |
2702 | 0 | else if (arg->IsExplicitlySet() && arg->GetType() == GAAT_DATASET) |
2703 | 0 | { |
2704 | 0 | if (!ProcessDatasetArg(arg.get(), this)) |
2705 | 0 | ret = false; |
2706 | 0 | } |
2707 | 0 | else if (arg->IsExplicitlySet() && |
2708 | 0 | GDALAlgorithmArgTypeIsList(arg->GetType())) |
2709 | 0 | { |
2710 | 0 | int valueCount = 0; |
2711 | 0 | if (arg->GetType() == GAAT_STRING_LIST) |
2712 | 0 | { |
2713 | 0 | valueCount = static_cast<int>( |
2714 | 0 | arg->Get<std::vector<std::string>>().size()); |
2715 | 0 | } |
2716 | 0 | else if (arg->GetType() == GAAT_INTEGER_LIST) |
2717 | 0 | { |
2718 | 0 | valueCount = |
2719 | 0 | static_cast<int>(arg->Get<std::vector<int>>().size()); |
2720 | 0 | } |
2721 | 0 | else if (arg->GetType() == GAAT_REAL_LIST) |
2722 | 0 | { |
2723 | 0 | valueCount = |
2724 | 0 | static_cast<int>(arg->Get<std::vector<double>>().size()); |
2725 | 0 | } |
2726 | 0 | else if (arg->GetType() == GAAT_DATASET_LIST) |
2727 | 0 | { |
2728 | 0 | valueCount = static_cast<int>( |
2729 | 0 | arg->Get<std::vector<GDALArgDatasetValue>>().size()); |
2730 | 0 | } |
2731 | |
|
2732 | 0 | if (valueCount != arg->GetMinCount() && |
2733 | 0 | arg->GetMinCount() == arg->GetMaxCount()) |
2734 | 0 | { |
2735 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
2736 | 0 | "%d value%s been specified for argument '%s', " |
2737 | 0 | "whereas exactly %d %s expected.", |
2738 | 0 | valueCount, valueCount > 1 ? "s have" : " has", |
2739 | 0 | arg->GetName().c_str(), arg->GetMinCount(), |
2740 | 0 | arg->GetMinCount() > 1 ? "were" : "was"); |
2741 | 0 | ret = false; |
2742 | 0 | } |
2743 | 0 | else if (valueCount < arg->GetMinCount()) |
2744 | 0 | { |
2745 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
2746 | 0 | "Only %d value%s been specified for argument '%s', " |
2747 | 0 | "whereas at least %d %s expected.", |
2748 | 0 | valueCount, valueCount > 1 ? "s have" : " has", |
2749 | 0 | arg->GetName().c_str(), arg->GetMinCount(), |
2750 | 0 | arg->GetMinCount() > 1 ? "were" : "was"); |
2751 | 0 | ret = false; |
2752 | 0 | } |
2753 | 0 | else if (valueCount > arg->GetMaxCount()) |
2754 | 0 | { |
2755 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
2756 | 0 | "%d value%s been specified for argument '%s', " |
2757 | 0 | "whereas at most %d %s expected.", |
2758 | 0 | valueCount, valueCount > 1 ? "s have" : " has", |
2759 | 0 | arg->GetName().c_str(), arg->GetMaxCount(), |
2760 | 0 | arg->GetMaxCount() > 1 ? "were" : "was"); |
2761 | 0 | ret = false; |
2762 | 0 | } |
2763 | 0 | } |
2764 | |
|
2765 | 0 | if (arg->IsExplicitlySet() && arg->GetType() == GAAT_DATASET_LIST && |
2766 | 0 | arg->AutoOpenDataset()) |
2767 | 0 | { |
2768 | 0 | auto &listVal = arg->Get<std::vector<GDALArgDatasetValue>>(); |
2769 | 0 | if (listVal.size() == 1) |
2770 | 0 | { |
2771 | 0 | if (!ProcessDatasetArg(arg.get(), this)) |
2772 | 0 | ret = false; |
2773 | 0 | } |
2774 | 0 | else |
2775 | 0 | { |
2776 | 0 | for (auto &val : listVal) |
2777 | 0 | { |
2778 | 0 | if (!val.GetDatasetRef() && val.GetName().empty()) |
2779 | 0 | { |
2780 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
2781 | 0 | "Argument '%s' has no dataset object or " |
2782 | 0 | "dataset name.", |
2783 | 0 | arg->GetName().c_str()); |
2784 | 0 | ret = false; |
2785 | 0 | } |
2786 | 0 | else if (!val.GetDatasetRef()) |
2787 | 0 | { |
2788 | 0 | int flags = |
2789 | 0 | arg->GetDatasetType() | GDAL_OF_VERBOSE_ERROR; |
2790 | |
|
2791 | 0 | CPLStringList aosOpenOptions; |
2792 | 0 | CPLStringList aosAllowedDrivers; |
2793 | 0 | if (arg->GetName() == GDAL_ARG_NAME_INPUT) |
2794 | 0 | { |
2795 | 0 | const auto ooArg = |
2796 | 0 | GetArg(GDAL_ARG_NAME_OPEN_OPTION); |
2797 | 0 | if (ooArg && ooArg->GetType() == GAAT_STRING_LIST) |
2798 | 0 | { |
2799 | 0 | aosOpenOptions = CPLStringList( |
2800 | 0 | ooArg->Get<std::vector<std::string>>()); |
2801 | 0 | } |
2802 | |
|
2803 | 0 | const auto ifArg = |
2804 | 0 | GetArg(GDAL_ARG_NAME_INPUT_FORMAT); |
2805 | 0 | if (ifArg && ifArg->GetType() == GAAT_STRING_LIST) |
2806 | 0 | { |
2807 | 0 | aosAllowedDrivers = CPLStringList( |
2808 | 0 | ifArg->Get<std::vector<std::string>>()); |
2809 | 0 | } |
2810 | |
|
2811 | 0 | const auto updateArg = GetArg(GDAL_ARG_NAME_UPDATE); |
2812 | 0 | if (updateArg && |
2813 | 0 | updateArg->GetType() == GAAT_BOOLEAN && |
2814 | 0 | updateArg->Get<bool>()) |
2815 | 0 | { |
2816 | 0 | flags |= GDAL_OF_UPDATE; |
2817 | 0 | } |
2818 | 0 | } |
2819 | |
|
2820 | 0 | auto poDS = std::unique_ptr<GDALDataset>( |
2821 | 0 | GDALDataset::Open(val.GetName().c_str(), flags, |
2822 | 0 | aosAllowedDrivers.List(), |
2823 | 0 | aosOpenOptions.List())); |
2824 | 0 | if (poDS) |
2825 | 0 | { |
2826 | 0 | val.Set(std::move(poDS)); |
2827 | 0 | } |
2828 | 0 | else |
2829 | 0 | { |
2830 | 0 | ret = false; |
2831 | 0 | } |
2832 | 0 | } |
2833 | 0 | } |
2834 | 0 | } |
2835 | 0 | } |
2836 | 0 | } |
2837 | |
|
2838 | 0 | for (const auto &f : m_validationActions) |
2839 | 0 | { |
2840 | 0 | if (!f()) |
2841 | 0 | ret = false; |
2842 | 0 | } |
2843 | |
|
2844 | 0 | return ret; |
2845 | 0 | } |
2846 | | |
2847 | | /************************************************************************/ |
2848 | | /* GDALAlgorithm::InstantiateSubAlgorithm */ |
2849 | | /************************************************************************/ |
2850 | | |
2851 | | std::unique_ptr<GDALAlgorithm> |
2852 | | GDALAlgorithm::InstantiateSubAlgorithm(const std::string &name, |
2853 | | bool suggestionAllowed) const |
2854 | 0 | { |
2855 | 0 | auto ret = m_subAlgRegistry.Instantiate(name); |
2856 | 0 | auto childCallPath = m_callPath; |
2857 | 0 | childCallPath.push_back(name); |
2858 | 0 | if (!ret) |
2859 | 0 | { |
2860 | 0 | ret = GDALGlobalAlgorithmRegistry::GetSingleton() |
2861 | 0 | .InstantiateDeclaredSubAlgorithm(childCallPath); |
2862 | 0 | } |
2863 | 0 | if (ret) |
2864 | 0 | { |
2865 | 0 | ret->SetCallPath(childCallPath); |
2866 | 0 | } |
2867 | 0 | else if (suggestionAllowed) |
2868 | 0 | { |
2869 | 0 | std::string bestCandidate; |
2870 | 0 | size_t bestDistance = std::numeric_limits<size_t>::max(); |
2871 | 0 | for (const std::string &candidate : GetSubAlgorithmNames()) |
2872 | 0 | { |
2873 | 0 | const size_t distance = |
2874 | 0 | CPLLevenshteinDistance(name.c_str(), candidate.c_str(), |
2875 | 0 | /* transpositionAllowed = */ true); |
2876 | 0 | if (distance < bestDistance) |
2877 | 0 | { |
2878 | 0 | bestCandidate = candidate; |
2879 | 0 | bestDistance = distance; |
2880 | 0 | } |
2881 | 0 | else if (distance == bestDistance) |
2882 | 0 | { |
2883 | 0 | bestCandidate.clear(); |
2884 | 0 | } |
2885 | 0 | } |
2886 | 0 | if (!bestCandidate.empty() && bestDistance <= 2) |
2887 | 0 | { |
2888 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2889 | 0 | "Algorithm '%s' is unknown. Do you mean '%s'?", |
2890 | 0 | name.c_str(), bestCandidate.c_str()); |
2891 | 0 | } |
2892 | 0 | } |
2893 | 0 | return ret; |
2894 | 0 | } |
2895 | | |
2896 | | /************************************************************************/ |
2897 | | /* GDALAlgorithm::GetSuggestionForArgumentName() */ |
2898 | | /************************************************************************/ |
2899 | | |
2900 | | std::string |
2901 | | GDALAlgorithm::GetSuggestionForArgumentName(const std::string &osName) const |
2902 | 0 | { |
2903 | 0 | if (osName.size() >= 3) |
2904 | 0 | { |
2905 | 0 | std::string bestCandidate; |
2906 | 0 | size_t bestDistance = std::numeric_limits<size_t>::max(); |
2907 | 0 | for (const auto &[key, value] : m_mapLongNameToArg) |
2908 | 0 | { |
2909 | 0 | CPL_IGNORE_RET_VAL(value); |
2910 | 0 | const size_t distance = CPLLevenshteinDistance( |
2911 | 0 | osName.c_str(), key.c_str(), /* transpositionAllowed = */ true); |
2912 | 0 | if (distance < bestDistance) |
2913 | 0 | { |
2914 | 0 | bestCandidate = key; |
2915 | 0 | bestDistance = distance; |
2916 | 0 | } |
2917 | 0 | else if (distance == bestDistance) |
2918 | 0 | { |
2919 | 0 | bestCandidate.clear(); |
2920 | 0 | } |
2921 | 0 | } |
2922 | 0 | if (!bestCandidate.empty() && |
2923 | 0 | bestDistance <= (bestCandidate.size() >= 4U ? 2U : 1U)) |
2924 | 0 | { |
2925 | 0 | return bestCandidate; |
2926 | 0 | } |
2927 | 0 | } |
2928 | 0 | return std::string(); |
2929 | 0 | } |
2930 | | |
2931 | | /************************************************************************/ |
2932 | | /* GDALAlgorithm::GetArg() */ |
2933 | | /************************************************************************/ |
2934 | | |
2935 | | const GDALAlgorithmArg *GDALAlgorithm::GetArg(const std::string &osName, |
2936 | | bool suggestionAllowed) const |
2937 | 0 | { |
2938 | 0 | const auto nPos = osName.find_first_not_of('-'); |
2939 | 0 | if (nPos == std::string::npos) |
2940 | 0 | return nullptr; |
2941 | 0 | const std::string osKey = osName.substr(nPos); |
2942 | 0 | { |
2943 | 0 | const auto oIter = m_mapLongNameToArg.find(osKey); |
2944 | 0 | if (oIter != m_mapLongNameToArg.end()) |
2945 | 0 | return oIter->second; |
2946 | 0 | } |
2947 | 0 | { |
2948 | 0 | const auto oIter = m_mapShortNameToArg.find(osKey); |
2949 | 0 | if (oIter != m_mapShortNameToArg.end()) |
2950 | 0 | return oIter->second; |
2951 | 0 | } |
2952 | | |
2953 | 0 | if (suggestionAllowed) |
2954 | 0 | { |
2955 | 0 | const std::string bestCandidate = GetSuggestionForArgumentName(osName); |
2956 | 0 | ; |
2957 | 0 | if (!bestCandidate.empty()) |
2958 | 0 | { |
2959 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2960 | 0 | "Argument '%s' is unknown. Do you mean '%s'?", |
2961 | 0 | osName.c_str(), bestCandidate.c_str()); |
2962 | 0 | } |
2963 | 0 | } |
2964 | |
|
2965 | 0 | return nullptr; |
2966 | 0 | } |
2967 | | |
2968 | | /************************************************************************/ |
2969 | | /* GDALAlgorithm::AddAliasFor() */ |
2970 | | /************************************************************************/ |
2971 | | |
2972 | | //! @cond Doxygen_Suppress |
2973 | | void GDALAlgorithm::AddAliasFor(GDALInConstructionAlgorithmArg *arg, |
2974 | | const std::string &alias) |
2975 | 0 | { |
2976 | 0 | if (cpl::contains(m_mapLongNameToArg, alias)) |
2977 | 0 | { |
2978 | 0 | ReportError(CE_Failure, CPLE_AppDefined, "Name '%s' already declared.", |
2979 | 0 | alias.c_str()); |
2980 | 0 | } |
2981 | 0 | else |
2982 | 0 | { |
2983 | 0 | m_mapLongNameToArg[alias] = arg; |
2984 | 0 | } |
2985 | 0 | } |
2986 | | |
2987 | | //! @endcond |
2988 | | |
2989 | | /************************************************************************/ |
2990 | | /* GDALAlgorithm::AddShortNameAliasFor() */ |
2991 | | /************************************************************************/ |
2992 | | |
2993 | | //! @cond Doxygen_Suppress |
2994 | | void GDALAlgorithm::AddShortNameAliasFor(GDALInConstructionAlgorithmArg *arg, |
2995 | | char shortNameAlias) |
2996 | 0 | { |
2997 | 0 | std::string alias; |
2998 | 0 | alias += shortNameAlias; |
2999 | 0 | if (cpl::contains(m_mapShortNameToArg, alias)) |
3000 | 0 | { |
3001 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
3002 | 0 | "Short name '%s' already declared.", alias.c_str()); |
3003 | 0 | } |
3004 | 0 | else |
3005 | 0 | { |
3006 | 0 | m_mapShortNameToArg[alias] = arg; |
3007 | 0 | } |
3008 | 0 | } |
3009 | | |
3010 | | //! @endcond |
3011 | | |
3012 | | /************************************************************************/ |
3013 | | /* GDALAlgorithm::SetPositional() */ |
3014 | | /************************************************************************/ |
3015 | | |
3016 | | //! @cond Doxygen_Suppress |
3017 | | void GDALAlgorithm::SetPositional(GDALInConstructionAlgorithmArg *arg) |
3018 | 0 | { |
3019 | 0 | CPLAssert(std::find(m_positionalArgs.begin(), m_positionalArgs.end(), |
3020 | 0 | arg) == m_positionalArgs.end()); |
3021 | 0 | m_positionalArgs.push_back(arg); |
3022 | 0 | } |
3023 | | |
3024 | | //! @endcond |
3025 | | |
3026 | | /************************************************************************/ |
3027 | | /* GDALAlgorithm::HasSubAlgorithms() */ |
3028 | | /************************************************************************/ |
3029 | | |
3030 | | bool GDALAlgorithm::HasSubAlgorithms() const |
3031 | 0 | { |
3032 | 0 | if (!m_subAlgRegistry.empty()) |
3033 | 0 | return true; |
3034 | 0 | return !GDALGlobalAlgorithmRegistry::GetSingleton() |
3035 | 0 | .GetDeclaredSubAlgorithmNames(m_callPath) |
3036 | 0 | .empty(); |
3037 | 0 | } |
3038 | | |
3039 | | /************************************************************************/ |
3040 | | /* GDALAlgorithm::GetSubAlgorithmNames() */ |
3041 | | /************************************************************************/ |
3042 | | |
3043 | | std::vector<std::string> GDALAlgorithm::GetSubAlgorithmNames() const |
3044 | 0 | { |
3045 | 0 | std::vector<std::string> ret = m_subAlgRegistry.GetNames(); |
3046 | 0 | const auto other = GDALGlobalAlgorithmRegistry::GetSingleton() |
3047 | 0 | .GetDeclaredSubAlgorithmNames(m_callPath); |
3048 | 0 | ret.insert(ret.end(), other.begin(), other.end()); |
3049 | 0 | if (!other.empty()) |
3050 | 0 | std::sort(ret.begin(), ret.end()); |
3051 | 0 | return ret; |
3052 | 0 | } |
3053 | | |
3054 | | /************************************************************************/ |
3055 | | /* GDALAlgorithm::AddArg() */ |
3056 | | /************************************************************************/ |
3057 | | |
3058 | | GDALInConstructionAlgorithmArg & |
3059 | | GDALAlgorithm::AddArg(std::unique_ptr<GDALInConstructionAlgorithmArg> arg) |
3060 | 0 | { |
3061 | 0 | auto argRaw = arg.get(); |
3062 | 0 | const auto &longName = argRaw->GetName(); |
3063 | 0 | if (!longName.empty()) |
3064 | 0 | { |
3065 | 0 | if (longName[0] == '-') |
3066 | 0 | { |
3067 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
3068 | 0 | "Long name '%s' should not start with '-'", |
3069 | 0 | longName.c_str()); |
3070 | 0 | } |
3071 | 0 | if (longName.find('=') != std::string::npos) |
3072 | 0 | { |
3073 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
3074 | 0 | "Long name '%s' should not contain a '=' character", |
3075 | 0 | longName.c_str()); |
3076 | 0 | } |
3077 | 0 | if (cpl::contains(m_mapLongNameToArg, longName)) |
3078 | 0 | { |
3079 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
3080 | 0 | "Long name '%s' already declared", longName.c_str()); |
3081 | 0 | } |
3082 | 0 | m_mapLongNameToArg[longName] = argRaw; |
3083 | 0 | } |
3084 | 0 | const auto &shortName = argRaw->GetShortName(); |
3085 | 0 | if (!shortName.empty()) |
3086 | 0 | { |
3087 | 0 | if (shortName.size() != 1 || |
3088 | 0 | !((shortName[0] >= 'a' && shortName[0] <= 'z') || |
3089 | 0 | (shortName[0] >= 'A' && shortName[0] <= 'Z') || |
3090 | 0 | (shortName[0] >= '0' && shortName[0] <= '9'))) |
3091 | 0 | { |
3092 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
3093 | 0 | "Short name '%s' should be a single letter or digit", |
3094 | 0 | shortName.c_str()); |
3095 | 0 | } |
3096 | 0 | if (cpl::contains(m_mapShortNameToArg, shortName)) |
3097 | 0 | { |
3098 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
3099 | 0 | "Short name '%s' already declared", shortName.c_str()); |
3100 | 0 | } |
3101 | 0 | m_mapShortNameToArg[shortName] = argRaw; |
3102 | 0 | } |
3103 | 0 | m_args.emplace_back(std::move(arg)); |
3104 | 0 | return *( |
3105 | 0 | cpl::down_cast<GDALInConstructionAlgorithmArg *>(m_args.back().get())); |
3106 | 0 | } |
3107 | | |
3108 | | GDALInConstructionAlgorithmArg & |
3109 | | GDALAlgorithm::AddArg(const std::string &longName, char chShortName, |
3110 | | const std::string &helpMessage, bool *pValue) |
3111 | 0 | { |
3112 | 0 | return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>( |
3113 | 0 | this, |
3114 | 0 | GDALAlgorithmArgDecl(longName, chShortName, helpMessage, GAAT_BOOLEAN), |
3115 | 0 | pValue)); |
3116 | 0 | } |
3117 | | |
3118 | | GDALInConstructionAlgorithmArg & |
3119 | | GDALAlgorithm::AddArg(const std::string &longName, char chShortName, |
3120 | | const std::string &helpMessage, std::string *pValue) |
3121 | 0 | { |
3122 | 0 | return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>( |
3123 | 0 | this, |
3124 | 0 | GDALAlgorithmArgDecl(longName, chShortName, helpMessage, GAAT_STRING), |
3125 | 0 | pValue)); |
3126 | 0 | } |
3127 | | |
3128 | | GDALInConstructionAlgorithmArg & |
3129 | | GDALAlgorithm::AddArg(const std::string &longName, char chShortName, |
3130 | | const std::string &helpMessage, int *pValue) |
3131 | 0 | { |
3132 | 0 | return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>( |
3133 | 0 | this, |
3134 | 0 | GDALAlgorithmArgDecl(longName, chShortName, helpMessage, GAAT_INTEGER), |
3135 | 0 | pValue)); |
3136 | 0 | } |
3137 | | |
3138 | | GDALInConstructionAlgorithmArg & |
3139 | | GDALAlgorithm::AddArg(const std::string &longName, char chShortName, |
3140 | | const std::string &helpMessage, double *pValue) |
3141 | 0 | { |
3142 | 0 | return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>( |
3143 | 0 | this, |
3144 | 0 | GDALAlgorithmArgDecl(longName, chShortName, helpMessage, GAAT_REAL), |
3145 | 0 | pValue)); |
3146 | 0 | } |
3147 | | |
3148 | | GDALInConstructionAlgorithmArg & |
3149 | | GDALAlgorithm::AddArg(const std::string &longName, char chShortName, |
3150 | | const std::string &helpMessage, |
3151 | | GDALArgDatasetValue *pValue, GDALArgDatasetType type) |
3152 | 0 | { |
3153 | 0 | auto &arg = AddArg(std::make_unique<GDALInConstructionAlgorithmArg>( |
3154 | 0 | this, |
3155 | 0 | GDALAlgorithmArgDecl(longName, chShortName, |
3156 | 0 | helpMessage, GAAT_DATASET), |
3157 | 0 | pValue)) |
3158 | 0 | .SetDatasetType(type); |
3159 | 0 | pValue->SetOwnerArgument(&arg); |
3160 | 0 | return arg; |
3161 | 0 | } |
3162 | | |
3163 | | GDALInConstructionAlgorithmArg & |
3164 | | GDALAlgorithm::AddArg(const std::string &longName, char chShortName, |
3165 | | const std::string &helpMessage, |
3166 | | std::vector<std::string> *pValue) |
3167 | 0 | { |
3168 | 0 | return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>( |
3169 | 0 | this, |
3170 | 0 | GDALAlgorithmArgDecl(longName, chShortName, helpMessage, |
3171 | 0 | GAAT_STRING_LIST), |
3172 | 0 | pValue)); |
3173 | 0 | } |
3174 | | |
3175 | | GDALInConstructionAlgorithmArg & |
3176 | | GDALAlgorithm::AddArg(const std::string &longName, char chShortName, |
3177 | | const std::string &helpMessage, std::vector<int> *pValue) |
3178 | 0 | { |
3179 | 0 | return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>( |
3180 | 0 | this, |
3181 | 0 | GDALAlgorithmArgDecl(longName, chShortName, helpMessage, |
3182 | 0 | GAAT_INTEGER_LIST), |
3183 | 0 | pValue)); |
3184 | 0 | } |
3185 | | |
3186 | | GDALInConstructionAlgorithmArg & |
3187 | | GDALAlgorithm::AddArg(const std::string &longName, char chShortName, |
3188 | | const std::string &helpMessage, |
3189 | | std::vector<double> *pValue) |
3190 | 0 | { |
3191 | 0 | return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>( |
3192 | 0 | this, |
3193 | 0 | GDALAlgorithmArgDecl(longName, chShortName, helpMessage, |
3194 | 0 | GAAT_REAL_LIST), |
3195 | 0 | pValue)); |
3196 | 0 | } |
3197 | | |
3198 | | GDALInConstructionAlgorithmArg & |
3199 | | GDALAlgorithm::AddArg(const std::string &longName, char chShortName, |
3200 | | const std::string &helpMessage, |
3201 | | std::vector<GDALArgDatasetValue> *pValue, |
3202 | | GDALArgDatasetType type) |
3203 | 0 | { |
3204 | 0 | return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>( |
3205 | 0 | this, |
3206 | 0 | GDALAlgorithmArgDecl(longName, chShortName, helpMessage, |
3207 | 0 | GAAT_DATASET_LIST), |
3208 | 0 | pValue)) |
3209 | 0 | .SetDatasetType(type); |
3210 | 0 | } |
3211 | | |
3212 | | /************************************************************************/ |
3213 | | /* MsgOrDefault() */ |
3214 | | /************************************************************************/ |
3215 | | |
3216 | | inline const char *MsgOrDefault(const char *helpMessage, |
3217 | | const char *defaultMessage) |
3218 | 0 | { |
3219 | 0 | return helpMessage && helpMessage[0] ? helpMessage : defaultMessage; |
3220 | 0 | } |
3221 | | |
3222 | | /************************************************************************/ |
3223 | | /* GDALAlgorithm::SetAutoCompleteFunctionForFilename() */ |
3224 | | /************************************************************************/ |
3225 | | |
3226 | | /* static */ |
3227 | | void GDALAlgorithm::SetAutoCompleteFunctionForFilename( |
3228 | | GDALInConstructionAlgorithmArg &arg, GDALArgDatasetType type) |
3229 | 0 | { |
3230 | 0 | arg.SetAutoCompleteFunction( |
3231 | 0 | [type](const std::string ¤tValue) -> std::vector<std::string> |
3232 | 0 | { |
3233 | 0 | std::vector<std::string> oRet; |
3234 | |
|
3235 | 0 | { |
3236 | 0 | CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler); |
3237 | 0 | VSIStatBufL sStat; |
3238 | 0 | if (!currentValue.empty() && currentValue.back() != '/' && |
3239 | 0 | VSIStatL(currentValue.c_str(), &sStat) == 0) |
3240 | 0 | { |
3241 | 0 | return oRet; |
3242 | 0 | } |
3243 | 0 | } |
3244 | | |
3245 | 0 | auto poDM = GetGDALDriverManager(); |
3246 | 0 | std::set<std::string> oExtensions; |
3247 | 0 | if (type) |
3248 | 0 | { |
3249 | 0 | for (int i = 0; i < poDM->GetDriverCount(); ++i) |
3250 | 0 | { |
3251 | 0 | auto poDriver = poDM->GetDriver(i); |
3252 | 0 | if (((type & GDAL_OF_RASTER) != 0 && |
3253 | 0 | poDriver->GetMetadataItem(GDAL_DCAP_RASTER)) || |
3254 | 0 | ((type & GDAL_OF_VECTOR) != 0 && |
3255 | 0 | poDriver->GetMetadataItem(GDAL_DCAP_VECTOR)) || |
3256 | 0 | ((type & GDAL_OF_MULTIDIM_RASTER) != 0 && |
3257 | 0 | poDriver->GetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER))) |
3258 | 0 | { |
3259 | 0 | const char *pszExtensions = |
3260 | 0 | poDriver->GetMetadataItem(GDAL_DMD_EXTENSIONS); |
3261 | 0 | if (pszExtensions) |
3262 | 0 | { |
3263 | 0 | const CPLStringList aosExts( |
3264 | 0 | CSLTokenizeString2(pszExtensions, " ", 0)); |
3265 | 0 | for (const char *pszExt : cpl::Iterate(aosExts)) |
3266 | 0 | oExtensions.insert(CPLString(pszExt).tolower()); |
3267 | 0 | } |
3268 | 0 | } |
3269 | 0 | } |
3270 | 0 | } |
3271 | |
|
3272 | 0 | std::string osDir; |
3273 | 0 | const CPLStringList aosVSIPrefixes(VSIGetFileSystemsPrefixes()); |
3274 | 0 | std::string osPrefix; |
3275 | 0 | if (STARTS_WITH(currentValue.c_str(), "/vsi")) |
3276 | 0 | { |
3277 | 0 | for (const char *pszPrefix : cpl::Iterate(aosVSIPrefixes)) |
3278 | 0 | { |
3279 | 0 | if (STARTS_WITH(currentValue.c_str(), pszPrefix)) |
3280 | 0 | { |
3281 | 0 | osPrefix = pszPrefix; |
3282 | 0 | break; |
3283 | 0 | } |
3284 | 0 | } |
3285 | 0 | if (osPrefix.empty()) |
3286 | 0 | return aosVSIPrefixes; |
3287 | 0 | if (currentValue == osPrefix) |
3288 | 0 | osDir = osPrefix; |
3289 | 0 | } |
3290 | 0 | if (osDir.empty()) |
3291 | 0 | { |
3292 | 0 | osDir = CPLGetDirnameSafe(currentValue.c_str()); |
3293 | 0 | if (!osPrefix.empty() && osDir.size() < osPrefix.size()) |
3294 | 0 | osDir = std::move(osPrefix); |
3295 | 0 | } |
3296 | |
|
3297 | 0 | auto psDir = VSIOpenDir(osDir.c_str(), 0, nullptr); |
3298 | 0 | const std::string osSep = VSIGetDirectorySeparator(osDir.c_str()); |
3299 | 0 | if (currentValue.empty()) |
3300 | 0 | osDir.clear(); |
3301 | 0 | const std::string currentFilename = |
3302 | 0 | CPLGetFilename(currentValue.c_str()); |
3303 | 0 | if (psDir) |
3304 | 0 | { |
3305 | 0 | while (const VSIDIREntry *psEntry = VSIGetNextDirEntry(psDir)) |
3306 | 0 | { |
3307 | 0 | if ((currentFilename.empty() || |
3308 | 0 | STARTS_WITH(psEntry->pszName, |
3309 | 0 | currentFilename.c_str())) && |
3310 | 0 | strcmp(psEntry->pszName, ".") != 0 && |
3311 | 0 | strcmp(psEntry->pszName, "..") != 0 && |
3312 | 0 | (oExtensions.empty() || |
3313 | 0 | !strstr(psEntry->pszName, ".aux.xml"))) |
3314 | 0 | { |
3315 | 0 | if (oExtensions.empty() || |
3316 | 0 | cpl::contains( |
3317 | 0 | oExtensions, |
3318 | 0 | CPLString(CPLGetExtensionSafe(psEntry->pszName)) |
3319 | 0 | .tolower()) || |
3320 | 0 | VSI_ISDIR(psEntry->nMode)) |
3321 | 0 | { |
3322 | 0 | std::string osVal; |
3323 | 0 | if (osDir.empty() || osDir == ".") |
3324 | 0 | osVal = psEntry->pszName; |
3325 | 0 | else |
3326 | 0 | osVal = CPLFormFilenameSafe( |
3327 | 0 | osDir.c_str(), psEntry->pszName, nullptr); |
3328 | 0 | if (VSI_ISDIR(psEntry->nMode)) |
3329 | 0 | osVal += osSep; |
3330 | 0 | oRet.push_back(std::move(osVal)); |
3331 | 0 | } |
3332 | 0 | } |
3333 | 0 | } |
3334 | 0 | VSICloseDir(psDir); |
3335 | 0 | } |
3336 | 0 | return oRet; |
3337 | 0 | }); |
3338 | 0 | } |
3339 | | |
3340 | | /************************************************************************/ |
3341 | | /* GDALAlgorithm::AddInputDatasetArg() */ |
3342 | | /************************************************************************/ |
3343 | | |
3344 | | GDALInConstructionAlgorithmArg &GDALAlgorithm::AddInputDatasetArg( |
3345 | | GDALArgDatasetValue *pValue, GDALArgDatasetType type, |
3346 | | bool positionalAndRequired, const char *helpMessage) |
3347 | 0 | { |
3348 | 0 | auto &arg = AddArg( |
3349 | 0 | GDAL_ARG_NAME_INPUT, 'i', |
3350 | 0 | MsgOrDefault(helpMessage, |
3351 | 0 | CPLSPrintf("Input %s dataset", |
3352 | 0 | GDALAlgorithmArgDatasetTypeName(type).c_str())), |
3353 | 0 | pValue, type); |
3354 | 0 | if (positionalAndRequired) |
3355 | 0 | arg.SetPositional().SetRequired(); |
3356 | |
|
3357 | 0 | SetAutoCompleteFunctionForFilename(arg, type); |
3358 | |
|
3359 | 0 | AddValidationAction( |
3360 | 0 | [pValue]() |
3361 | 0 | { |
3362 | 0 | if (pValue->GetName() == "-") |
3363 | 0 | pValue->Set("/vsistdin/"); |
3364 | 0 | return true; |
3365 | 0 | }); |
3366 | |
|
3367 | 0 | return arg; |
3368 | 0 | } |
3369 | | |
3370 | | /************************************************************************/ |
3371 | | /* GDALAlgorithm::AddInputDatasetArg() */ |
3372 | | /************************************************************************/ |
3373 | | |
3374 | | GDALInConstructionAlgorithmArg &GDALAlgorithm::AddInputDatasetArg( |
3375 | | std::vector<GDALArgDatasetValue> *pValue, GDALArgDatasetType type, |
3376 | | bool positionalAndRequired, const char *helpMessage) |
3377 | 0 | { |
3378 | 0 | auto &arg = AddArg( |
3379 | 0 | GDAL_ARG_NAME_INPUT, 'i', |
3380 | 0 | MsgOrDefault(helpMessage, |
3381 | 0 | CPLSPrintf("Input %s datasets", |
3382 | 0 | GDALAlgorithmArgDatasetTypeName(type).c_str())), |
3383 | 0 | pValue, type); |
3384 | 0 | if (positionalAndRequired) |
3385 | 0 | arg.SetPositional().SetRequired(); |
3386 | |
|
3387 | 0 | AddValidationAction( |
3388 | 0 | [pValue]() |
3389 | 0 | { |
3390 | 0 | for (auto &val : *pValue) |
3391 | 0 | { |
3392 | 0 | if (val.GetName() == "-") |
3393 | 0 | val.Set("/vsistdin/"); |
3394 | 0 | } |
3395 | 0 | return true; |
3396 | 0 | }); |
3397 | 0 | return arg; |
3398 | 0 | } |
3399 | | |
3400 | | /************************************************************************/ |
3401 | | /* GDALAlgorithm::AddOutputDatasetArg() */ |
3402 | | /************************************************************************/ |
3403 | | |
3404 | | GDALInConstructionAlgorithmArg &GDALAlgorithm::AddOutputDatasetArg( |
3405 | | GDALArgDatasetValue *pValue, GDALArgDatasetType type, |
3406 | | bool positionalAndRequired, const char *helpMessage) |
3407 | 0 | { |
3408 | 0 | auto &arg = |
3409 | 0 | AddArg(GDAL_ARG_NAME_OUTPUT, 'o', |
3410 | 0 | MsgOrDefault( |
3411 | 0 | helpMessage, |
3412 | 0 | CPLSPrintf("Output %s dataset", |
3413 | 0 | GDALAlgorithmArgDatasetTypeName(type).c_str())), |
3414 | 0 | pValue, type) |
3415 | 0 | .SetIsInput(true) |
3416 | 0 | .SetIsOutput(true) |
3417 | 0 | .SetDatasetInputFlags(GADV_NAME) |
3418 | 0 | .SetDatasetOutputFlags(GADV_OBJECT); |
3419 | 0 | if (positionalAndRequired) |
3420 | 0 | arg.SetPositional().SetRequired(); |
3421 | |
|
3422 | 0 | AddValidationAction( |
3423 | 0 | [this, &arg, pValue]() |
3424 | 0 | { |
3425 | 0 | if (pValue->GetName() == "-") |
3426 | 0 | pValue->Set("/vsistdout/"); |
3427 | |
|
3428 | 0 | auto outputFormatArg = GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT); |
3429 | 0 | if (outputFormatArg && outputFormatArg->GetType() == GAAT_STRING && |
3430 | 0 | (!outputFormatArg->IsExplicitlySet() || |
3431 | 0 | outputFormatArg->Get<std::string>().empty()) && |
3432 | 0 | arg.IsExplicitlySet()) |
3433 | 0 | { |
3434 | 0 | const auto vrtCompatible = |
3435 | 0 | outputFormatArg->GetMetadataItem(GAAMDI_VRT_COMPATIBLE); |
3436 | 0 | if (vrtCompatible && !vrtCompatible->empty() && |
3437 | 0 | vrtCompatible->front() == "false" && |
3438 | 0 | EQUAL( |
3439 | 0 | CPLGetExtensionSafe(pValue->GetName().c_str()).c_str(), |
3440 | 0 | "VRT")) |
3441 | 0 | { |
3442 | 0 | ReportError( |
3443 | 0 | CE_Failure, CPLE_NotSupported, |
3444 | 0 | "VRT output is not supported.%s", |
3445 | 0 | outputFormatArg->GetDescription().find("GDALG") != |
3446 | 0 | std::string::npos |
3447 | 0 | ? " Consider using the GDALG driver instead (files " |
3448 | 0 | "with .gdalg.json extension)" |
3449 | 0 | : ""); |
3450 | 0 | return false; |
3451 | 0 | } |
3452 | 0 | else if (pValue->GetName().size() > strlen(".gdalg.json") && |
3453 | 0 | EQUAL(pValue->GetName() |
3454 | 0 | .substr(pValue->GetName().size() - |
3455 | 0 | strlen(".gdalg.json")) |
3456 | 0 | .c_str(), |
3457 | 0 | ".gdalg.json") && |
3458 | 0 | outputFormatArg->GetDescription().find("GDALG") == |
3459 | 0 | std::string::npos) |
3460 | 0 | { |
3461 | 0 | ReportError(CE_Failure, CPLE_NotSupported, |
3462 | 0 | "GDALG output is not supported"); |
3463 | 0 | return false; |
3464 | 0 | } |
3465 | 0 | } |
3466 | 0 | return true; |
3467 | 0 | }); |
3468 | |
|
3469 | 0 | return arg; |
3470 | 0 | } |
3471 | | |
3472 | | /************************************************************************/ |
3473 | | /* GDALAlgorithm::AddOverwriteArg() */ |
3474 | | /************************************************************************/ |
3475 | | |
3476 | | GDALInConstructionAlgorithmArg & |
3477 | | GDALAlgorithm::AddOverwriteArg(bool *pValue, const char *helpMessage) |
3478 | 0 | { |
3479 | 0 | return AddArg(GDAL_ARG_NAME_OVERWRITE, 0, |
3480 | 0 | MsgOrDefault( |
3481 | 0 | helpMessage, |
3482 | 0 | _("Whether overwriting existing output is allowed")), |
3483 | 0 | pValue) |
3484 | 0 | .SetDefault(false); |
3485 | 0 | } |
3486 | | |
3487 | | /************************************************************************/ |
3488 | | /* GDALAlgorithm::AddOverwriteLayerArg() */ |
3489 | | /************************************************************************/ |
3490 | | |
3491 | | GDALInConstructionAlgorithmArg & |
3492 | | GDALAlgorithm::AddOverwriteLayerArg(bool *pValue, const char *helpMessage) |
3493 | 0 | { |
3494 | 0 | AddValidationAction( |
3495 | 0 | [this] |
3496 | 0 | { |
3497 | 0 | auto updateArg = GetArg(GDAL_ARG_NAME_UPDATE); |
3498 | 0 | if (!(updateArg && updateArg->GetType() == GAAT_BOOLEAN)) |
3499 | 0 | { |
3500 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
3501 | 0 | "--update argument must exist for " |
3502 | 0 | "--overwrite-layer, even if hidden"); |
3503 | 0 | return false; |
3504 | 0 | } |
3505 | 0 | return true; |
3506 | 0 | }); |
3507 | 0 | return AddArg(GDAL_ARG_NAME_OVERWRITE_LAYER, 0, |
3508 | 0 | MsgOrDefault( |
3509 | 0 | helpMessage, |
3510 | 0 | _("Whether overwriting existing output is allowed")), |
3511 | 0 | pValue) |
3512 | 0 | .SetDefault(false) |
3513 | 0 | .AddAction( |
3514 | 0 | [this] |
3515 | 0 | { |
3516 | 0 | auto updateArg = GetArg(GDAL_ARG_NAME_UPDATE); |
3517 | 0 | if (updateArg && updateArg->GetType() == GAAT_BOOLEAN) |
3518 | 0 | { |
3519 | 0 | updateArg->Set(true); |
3520 | 0 | } |
3521 | 0 | }); |
3522 | 0 | } |
3523 | | |
3524 | | /************************************************************************/ |
3525 | | /* GDALAlgorithm::AddUpdateArg() */ |
3526 | | /************************************************************************/ |
3527 | | |
3528 | | GDALInConstructionAlgorithmArg & |
3529 | | GDALAlgorithm::AddUpdateArg(bool *pValue, const char *helpMessage) |
3530 | 0 | { |
3531 | 0 | return AddArg(GDAL_ARG_NAME_UPDATE, 0, |
3532 | 0 | MsgOrDefault( |
3533 | 0 | helpMessage, |
3534 | 0 | _("Whether to open existing dataset in update mode")), |
3535 | 0 | pValue) |
3536 | 0 | .SetDefault(false); |
3537 | 0 | } |
3538 | | |
3539 | | /************************************************************************/ |
3540 | | /* GDALAlgorithm::AddAppendLayerArg() */ |
3541 | | /************************************************************************/ |
3542 | | |
3543 | | GDALInConstructionAlgorithmArg & |
3544 | | GDALAlgorithm::AddAppendLayerArg(bool *pValue, const char *helpMessage) |
3545 | 0 | { |
3546 | 0 | AddValidationAction( |
3547 | 0 | [this] |
3548 | 0 | { |
3549 | 0 | auto updateArg = GetArg(GDAL_ARG_NAME_UPDATE); |
3550 | 0 | if (!(updateArg && updateArg->GetType() == GAAT_BOOLEAN)) |
3551 | 0 | { |
3552 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
3553 | 0 | "--update argument must exist for --append, even " |
3554 | 0 | "if hidden"); |
3555 | 0 | return false; |
3556 | 0 | } |
3557 | 0 | return true; |
3558 | 0 | }); |
3559 | 0 | return AddArg(GDAL_ARG_NAME_APPEND, 0, |
3560 | 0 | MsgOrDefault( |
3561 | 0 | helpMessage, |
3562 | 0 | _("Whether appending to existing layer is allowed")), |
3563 | 0 | pValue) |
3564 | 0 | .SetDefault(false) |
3565 | 0 | .AddAction( |
3566 | 0 | [this] |
3567 | 0 | { |
3568 | 0 | auto updateArg = GetArg(GDAL_ARG_NAME_UPDATE); |
3569 | 0 | if (updateArg && updateArg->GetType() == GAAT_BOOLEAN) |
3570 | 0 | { |
3571 | 0 | updateArg->Set(true); |
3572 | 0 | } |
3573 | 0 | }); |
3574 | 0 | } |
3575 | | |
3576 | | /************************************************************************/ |
3577 | | /* GDALAlgorithm::AddOptionsSuggestions() */ |
3578 | | /************************************************************************/ |
3579 | | |
3580 | | /* static */ |
3581 | | bool GDALAlgorithm::AddOptionsSuggestions(const char *pszXML, int datasetType, |
3582 | | const std::string ¤tValue, |
3583 | | std::vector<std::string> &oRet) |
3584 | 0 | { |
3585 | 0 | if (!pszXML) |
3586 | 0 | return false; |
3587 | 0 | CPLXMLTreeCloser poTree(CPLParseXMLString(pszXML)); |
3588 | 0 | if (!poTree) |
3589 | 0 | return false; |
3590 | | |
3591 | 0 | std::string typedOptionName = currentValue; |
3592 | 0 | const auto posEqual = typedOptionName.find('='); |
3593 | 0 | std::string typedValue; |
3594 | 0 | if (posEqual != 0 && posEqual != std::string::npos) |
3595 | 0 | { |
3596 | 0 | typedValue = currentValue.substr(posEqual + 1); |
3597 | 0 | typedOptionName.resize(posEqual); |
3598 | 0 | } |
3599 | |
|
3600 | 0 | for (const CPLXMLNode *psChild = poTree.get()->psChild; psChild; |
3601 | 0 | psChild = psChild->psNext) |
3602 | 0 | { |
3603 | 0 | const char *pszName = CPLGetXMLValue(psChild, "name", nullptr); |
3604 | 0 | if (pszName && typedOptionName == pszName && |
3605 | 0 | (strcmp(psChild->pszValue, "Option") == 0 || |
3606 | 0 | strcmp(psChild->pszValue, "Argument") == 0)) |
3607 | 0 | { |
3608 | 0 | const char *pszType = CPLGetXMLValue(psChild, "type", ""); |
3609 | 0 | const char *pszMin = CPLGetXMLValue(psChild, "min", nullptr); |
3610 | 0 | const char *pszMax = CPLGetXMLValue(psChild, "max", nullptr); |
3611 | 0 | if (EQUAL(pszType, "string-select")) |
3612 | 0 | { |
3613 | 0 | for (const CPLXMLNode *psChild2 = psChild->psChild; psChild2; |
3614 | 0 | psChild2 = psChild2->psNext) |
3615 | 0 | { |
3616 | 0 | if (EQUAL(psChild2->pszValue, "Value")) |
3617 | 0 | { |
3618 | 0 | oRet.push_back(CPLGetXMLValue(psChild2, "", "")); |
3619 | 0 | } |
3620 | 0 | } |
3621 | 0 | } |
3622 | 0 | else if (EQUAL(pszType, "boolean")) |
3623 | 0 | { |
3624 | 0 | if (typedValue == "YES" || typedValue == "NO") |
3625 | 0 | { |
3626 | 0 | oRet.push_back(currentValue); |
3627 | 0 | return true; |
3628 | 0 | } |
3629 | 0 | oRet.push_back("NO"); |
3630 | 0 | oRet.push_back("YES"); |
3631 | 0 | } |
3632 | 0 | else if (EQUAL(pszType, "int")) |
3633 | 0 | { |
3634 | 0 | if (pszMin && pszMax && atoi(pszMax) - atoi(pszMin) > 0 && |
3635 | 0 | atoi(pszMax) - atoi(pszMin) < 25) |
3636 | 0 | { |
3637 | 0 | const int nMax = atoi(pszMax); |
3638 | 0 | for (int i = atoi(pszMin); i <= nMax; ++i) |
3639 | 0 | oRet.push_back(std::to_string(i)); |
3640 | 0 | } |
3641 | 0 | } |
3642 | | |
3643 | 0 | if (oRet.empty()) |
3644 | 0 | { |
3645 | 0 | if (pszMin && pszMax) |
3646 | 0 | { |
3647 | 0 | oRet.push_back(std::string("##")); |
3648 | 0 | oRet.push_back(std::string("validity range: [") |
3649 | 0 | .append(pszMin) |
3650 | 0 | .append(",") |
3651 | 0 | .append(pszMax) |
3652 | 0 | .append("]")); |
3653 | 0 | } |
3654 | 0 | else if (pszMin) |
3655 | 0 | { |
3656 | 0 | oRet.push_back(std::string("##")); |
3657 | 0 | oRet.push_back( |
3658 | 0 | std::string("validity range: >= ").append(pszMin)); |
3659 | 0 | } |
3660 | 0 | else if (pszMax) |
3661 | 0 | { |
3662 | 0 | oRet.push_back(std::string("##")); |
3663 | 0 | oRet.push_back( |
3664 | 0 | std::string("validity range: <= ").append(pszMax)); |
3665 | 0 | } |
3666 | 0 | else if (const char *pszDescription = |
3667 | 0 | CPLGetXMLValue(psChild, "description", nullptr)) |
3668 | 0 | { |
3669 | 0 | oRet.push_back(std::string("##")); |
3670 | 0 | oRet.push_back(std::string("type: ") |
3671 | 0 | .append(pszType) |
3672 | 0 | .append(", description: ") |
3673 | 0 | .append(pszDescription)); |
3674 | 0 | } |
3675 | 0 | } |
3676 | |
|
3677 | 0 | return true; |
3678 | 0 | } |
3679 | 0 | } |
3680 | | |
3681 | 0 | for (const CPLXMLNode *psChild = poTree.get()->psChild; psChild; |
3682 | 0 | psChild = psChild->psNext) |
3683 | 0 | { |
3684 | 0 | const char *pszName = CPLGetXMLValue(psChild, "name", nullptr); |
3685 | 0 | if (pszName && (strcmp(psChild->pszValue, "Option") == 0 || |
3686 | 0 | strcmp(psChild->pszValue, "Argument") == 0)) |
3687 | 0 | { |
3688 | 0 | const char *pszScope = CPLGetXMLValue(psChild, "scope", nullptr); |
3689 | 0 | if (!pszScope || |
3690 | 0 | (EQUAL(pszScope, "raster") && |
3691 | 0 | (datasetType & GDAL_OF_RASTER) != 0) || |
3692 | 0 | (EQUAL(pszScope, "vector") && |
3693 | 0 | (datasetType & GDAL_OF_VECTOR) != 0)) |
3694 | 0 | { |
3695 | 0 | oRet.push_back(std::string(pszName).append("=")); |
3696 | 0 | } |
3697 | 0 | } |
3698 | 0 | } |
3699 | |
|
3700 | 0 | return false; |
3701 | 0 | } |
3702 | | |
3703 | | /************************************************************************/ |
3704 | | /* GDALAlgorithm::AddOpenOptionsArg() */ |
3705 | | /************************************************************************/ |
3706 | | |
3707 | | GDALInConstructionAlgorithmArg & |
3708 | | GDALAlgorithm::AddOpenOptionsArg(std::vector<std::string> *pValue, |
3709 | | const char *helpMessage) |
3710 | 0 | { |
3711 | 0 | auto &arg = AddArg(GDAL_ARG_NAME_OPEN_OPTION, 0, |
3712 | 0 | MsgOrDefault(helpMessage, _("Open options")), pValue) |
3713 | 0 | .AddAlias("oo") |
3714 | 0 | .SetMetaVar("<KEY>=<VALUE>") |
3715 | 0 | .SetPackedValuesAllowed(false) |
3716 | 0 | .SetCategory(GAAC_ADVANCED); |
3717 | |
|
3718 | 0 | arg.AddValidationAction([this, &arg]() |
3719 | 0 | { return ParseAndValidateKeyValue(arg); }); |
3720 | |
|
3721 | 0 | arg.SetAutoCompleteFunction( |
3722 | 0 | [this](const std::string ¤tValue) |
3723 | 0 | { |
3724 | 0 | std::vector<std::string> oRet; |
3725 | |
|
3726 | 0 | int datasetType = |
3727 | 0 | GDAL_OF_RASTER | GDAL_OF_VECTOR | GDAL_OF_MULTIDIM_RASTER; |
3728 | 0 | auto inputArg = GetArg(GDAL_ARG_NAME_INPUT); |
3729 | 0 | if (inputArg && (inputArg->GetType() == GAAT_DATASET || |
3730 | 0 | inputArg->GetType() == GAAT_DATASET_LIST)) |
3731 | 0 | { |
3732 | 0 | datasetType = inputArg->GetDatasetType(); |
3733 | 0 | } |
3734 | |
|
3735 | 0 | auto inputFormat = GetArg(GDAL_ARG_NAME_INPUT_FORMAT); |
3736 | 0 | if (inputFormat && inputFormat->GetType() == GAAT_STRING_LIST && |
3737 | 0 | inputFormat->IsExplicitlySet()) |
3738 | 0 | { |
3739 | 0 | const auto &aosAllowedDrivers = |
3740 | 0 | inputFormat->Get<std::vector<std::string>>(); |
3741 | 0 | if (aosAllowedDrivers.size() == 1) |
3742 | 0 | { |
3743 | 0 | auto poDriver = GetGDALDriverManager()->GetDriverByName( |
3744 | 0 | aosAllowedDrivers[0].c_str()); |
3745 | 0 | if (poDriver) |
3746 | 0 | { |
3747 | 0 | AddOptionsSuggestions( |
3748 | 0 | poDriver->GetMetadataItem(GDAL_DMD_OPENOPTIONLIST), |
3749 | 0 | datasetType, currentValue, oRet); |
3750 | 0 | } |
3751 | 0 | return oRet; |
3752 | 0 | } |
3753 | 0 | } |
3754 | | |
3755 | 0 | if (inputArg && inputArg->GetType() == GAAT_DATASET) |
3756 | 0 | { |
3757 | 0 | auto poDM = GetGDALDriverManager(); |
3758 | 0 | auto &datasetValue = inputArg->Get<GDALArgDatasetValue>(); |
3759 | 0 | const auto &osDSName = datasetValue.GetName(); |
3760 | 0 | const std::string osExt = CPLGetExtensionSafe(osDSName.c_str()); |
3761 | 0 | if (!osExt.empty()) |
3762 | 0 | { |
3763 | 0 | std::set<std::string> oVisitedExtensions; |
3764 | 0 | for (int i = 0; i < poDM->GetDriverCount(); ++i) |
3765 | 0 | { |
3766 | 0 | auto poDriver = poDM->GetDriver(i); |
3767 | 0 | if (((datasetType & GDAL_OF_RASTER) != 0 && |
3768 | 0 | poDriver->GetMetadataItem(GDAL_DCAP_RASTER)) || |
3769 | 0 | ((datasetType & GDAL_OF_VECTOR) != 0 && |
3770 | 0 | poDriver->GetMetadataItem(GDAL_DCAP_VECTOR)) || |
3771 | 0 | ((datasetType & GDAL_OF_MULTIDIM_RASTER) != 0 && |
3772 | 0 | poDriver->GetMetadataItem( |
3773 | 0 | GDAL_DCAP_MULTIDIM_RASTER))) |
3774 | 0 | { |
3775 | 0 | const char *pszExtensions = |
3776 | 0 | poDriver->GetMetadataItem(GDAL_DMD_EXTENSIONS); |
3777 | 0 | if (pszExtensions) |
3778 | 0 | { |
3779 | 0 | const CPLStringList aosExts( |
3780 | 0 | CSLTokenizeString2(pszExtensions, " ", 0)); |
3781 | 0 | for (const char *pszExt : cpl::Iterate(aosExts)) |
3782 | 0 | { |
3783 | 0 | if (EQUAL(pszExt, osExt.c_str()) && |
3784 | 0 | !cpl::contains(oVisitedExtensions, |
3785 | 0 | pszExt)) |
3786 | 0 | { |
3787 | 0 | oVisitedExtensions.insert(pszExt); |
3788 | 0 | if (AddOptionsSuggestions( |
3789 | 0 | poDriver->GetMetadataItem( |
3790 | 0 | GDAL_DMD_OPENOPTIONLIST), |
3791 | 0 | datasetType, currentValue, |
3792 | 0 | oRet)) |
3793 | 0 | { |
3794 | 0 | return oRet; |
3795 | 0 | } |
3796 | 0 | break; |
3797 | 0 | } |
3798 | 0 | } |
3799 | 0 | } |
3800 | 0 | } |
3801 | 0 | } |
3802 | 0 | } |
3803 | 0 | } |
3804 | | |
3805 | 0 | return oRet; |
3806 | 0 | }); |
3807 | |
|
3808 | 0 | return arg; |
3809 | 0 | } |
3810 | | |
3811 | | /************************************************************************/ |
3812 | | /* ValidateFormat() */ |
3813 | | /************************************************************************/ |
3814 | | |
3815 | | bool GDALAlgorithm::ValidateFormat(const GDALAlgorithmArg &arg, |
3816 | | bool bStreamAllowed, |
3817 | | bool bGDALGAllowed) const |
3818 | 0 | { |
3819 | 0 | if (arg.GetChoices().empty()) |
3820 | 0 | { |
3821 | 0 | const auto Validate = |
3822 | 0 | [this, &arg, bStreamAllowed, bGDALGAllowed](const std::string &val) |
3823 | 0 | { |
3824 | 0 | if (bStreamAllowed && EQUAL(val.c_str(), "stream")) |
3825 | 0 | return true; |
3826 | | |
3827 | 0 | if (EQUAL(val.c_str(), "GDALG") && |
3828 | 0 | arg.GetName() == GDAL_ARG_NAME_OUTPUT_FORMAT) |
3829 | 0 | { |
3830 | 0 | if (bGDALGAllowed) |
3831 | 0 | { |
3832 | 0 | return true; |
3833 | 0 | } |
3834 | 0 | else |
3835 | 0 | { |
3836 | 0 | ReportError(CE_Failure, CPLE_NotSupported, |
3837 | 0 | "GDALG output is not supported."); |
3838 | 0 | return false; |
3839 | 0 | } |
3840 | 0 | } |
3841 | | |
3842 | 0 | const auto vrtCompatible = |
3843 | 0 | arg.GetMetadataItem(GAAMDI_VRT_COMPATIBLE); |
3844 | 0 | if (vrtCompatible && !vrtCompatible->empty() && |
3845 | 0 | vrtCompatible->front() == "false" && EQUAL(val.c_str(), "VRT")) |
3846 | 0 | { |
3847 | 0 | ReportError(CE_Failure, CPLE_NotSupported, |
3848 | 0 | "VRT output is not supported.%s", |
3849 | 0 | bGDALGAllowed |
3850 | 0 | ? " Consider using the GDALG driver instead " |
3851 | 0 | "(files with .gdalg.json extension)." |
3852 | 0 | : ""); |
3853 | 0 | return false; |
3854 | 0 | } |
3855 | | |
3856 | 0 | auto hDriver = GDALGetDriverByName(val.c_str()); |
3857 | 0 | if (!hDriver) |
3858 | 0 | { |
3859 | 0 | auto poMissingDriver = |
3860 | 0 | GetGDALDriverManager()->GetHiddenDriverByName(val.c_str()); |
3861 | 0 | if (poMissingDriver) |
3862 | 0 | { |
3863 | 0 | const std::string msg = |
3864 | 0 | GDALGetMessageAboutMissingPluginDriver(poMissingDriver); |
3865 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
3866 | 0 | "Invalid value for argument '%s'. Driver '%s' " |
3867 | 0 | "not found but it known. However plugin %s", |
3868 | 0 | arg.GetName().c_str(), val.c_str(), |
3869 | 0 | msg.c_str()); |
3870 | 0 | } |
3871 | 0 | else |
3872 | 0 | { |
3873 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
3874 | 0 | "Invalid value for argument '%s'. Driver '%s' " |
3875 | 0 | "does not exist.", |
3876 | 0 | arg.GetName().c_str(), val.c_str()); |
3877 | 0 | } |
3878 | 0 | return false; |
3879 | 0 | } |
3880 | | |
3881 | 0 | const auto caps = arg.GetMetadataItem(GAAMDI_REQUIRED_CAPABILITIES); |
3882 | 0 | if (caps) |
3883 | 0 | { |
3884 | 0 | for (const std::string &cap : *caps) |
3885 | 0 | { |
3886 | 0 | const char *pszVal = |
3887 | 0 | GDALGetMetadataItem(hDriver, cap.c_str(), nullptr); |
3888 | 0 | if (!(pszVal && pszVal[0])) |
3889 | 0 | { |
3890 | 0 | if (cap == GDAL_DCAP_CREATECOPY && |
3891 | 0 | std::find(caps->begin(), caps->end(), |
3892 | 0 | GDAL_DCAP_RASTER) != caps->end() && |
3893 | 0 | GDALGetMetadataItem(hDriver, GDAL_DCAP_RASTER, |
3894 | 0 | nullptr) && |
3895 | 0 | GDALGetMetadataItem(hDriver, GDAL_DCAP_CREATE, |
3896 | 0 | nullptr)) |
3897 | 0 | { |
3898 | | // if it supports Create, it supports CreateCopy |
3899 | 0 | } |
3900 | 0 | else if (cap == GDAL_DMD_EXTENSIONS) |
3901 | 0 | { |
3902 | 0 | ReportError( |
3903 | 0 | CE_Failure, CPLE_AppDefined, |
3904 | 0 | "Invalid value for argument '%s'. Driver '%s' " |
3905 | 0 | "does " |
3906 | 0 | "not advertise any file format extension.", |
3907 | 0 | arg.GetName().c_str(), val.c_str()); |
3908 | 0 | return false; |
3909 | 0 | } |
3910 | 0 | else |
3911 | 0 | { |
3912 | 0 | ReportError( |
3913 | 0 | CE_Failure, CPLE_AppDefined, |
3914 | 0 | "Invalid value for argument '%s'. Driver '%s' " |
3915 | 0 | "does " |
3916 | 0 | "not expose the required '%s' capability.", |
3917 | 0 | arg.GetName().c_str(), val.c_str(), |
3918 | 0 | cap.c_str()); |
3919 | 0 | return false; |
3920 | 0 | } |
3921 | 0 | } |
3922 | 0 | } |
3923 | 0 | } |
3924 | 0 | return true; |
3925 | 0 | }; |
3926 | |
|
3927 | 0 | if (arg.GetType() == GAAT_STRING) |
3928 | 0 | { |
3929 | 0 | return Validate(arg.Get<std::string>()); |
3930 | 0 | } |
3931 | 0 | else if (arg.GetType() == GAAT_STRING_LIST) |
3932 | 0 | { |
3933 | 0 | for (const auto &val : arg.Get<std::vector<std::string>>()) |
3934 | 0 | { |
3935 | 0 | if (!Validate(val)) |
3936 | 0 | return false; |
3937 | 0 | } |
3938 | 0 | } |
3939 | 0 | } |
3940 | | |
3941 | 0 | return true; |
3942 | 0 | } |
3943 | | |
3944 | | /************************************************************************/ |
3945 | | /* FormatAutoCompleteFunction() */ |
3946 | | /************************************************************************/ |
3947 | | |
3948 | | /* static */ |
3949 | | std::vector<std::string> GDALAlgorithm::FormatAutoCompleteFunction( |
3950 | | const GDALAlgorithmArg &arg, bool /* bStreamAllowed */, bool bGDALGAllowed) |
3951 | 0 | { |
3952 | 0 | std::vector<std::string> res; |
3953 | 0 | auto poDM = GetGDALDriverManager(); |
3954 | 0 | const auto vrtCompatible = arg.GetMetadataItem(GAAMDI_VRT_COMPATIBLE); |
3955 | 0 | const auto caps = arg.GetMetadataItem(GAAMDI_REQUIRED_CAPABILITIES); |
3956 | 0 | for (int i = 0; i < poDM->GetDriverCount(); ++i) |
3957 | 0 | { |
3958 | 0 | auto poDriver = poDM->GetDriver(i); |
3959 | |
|
3960 | 0 | if (vrtCompatible && !vrtCompatible->empty() && |
3961 | 0 | vrtCompatible->front() == "false" && |
3962 | 0 | EQUAL(poDriver->GetDescription(), "VRT")) |
3963 | 0 | { |
3964 | | // do nothing |
3965 | 0 | } |
3966 | 0 | else if (caps) |
3967 | 0 | { |
3968 | 0 | bool ok = true; |
3969 | 0 | for (const std::string &cap : *caps) |
3970 | 0 | { |
3971 | 0 | if (cap == GDAL_ALG_DCAP_RASTER_OR_MULTIDIM_RASTER) |
3972 | 0 | { |
3973 | 0 | if (!poDriver->GetMetadataItem(GDAL_DCAP_RASTER) && |
3974 | 0 | !poDriver->GetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER)) |
3975 | 0 | { |
3976 | 0 | ok = false; |
3977 | 0 | break; |
3978 | 0 | } |
3979 | 0 | } |
3980 | 0 | else if (const char *pszVal = |
3981 | 0 | poDriver->GetMetadataItem(cap.c_str()); |
3982 | 0 | pszVal && pszVal[0]) |
3983 | 0 | { |
3984 | 0 | } |
3985 | 0 | else if (cap == GDAL_DCAP_CREATECOPY && |
3986 | 0 | (std::find(caps->begin(), caps->end(), |
3987 | 0 | GDAL_DCAP_RASTER) != caps->end() && |
3988 | 0 | poDriver->GetMetadataItem(GDAL_DCAP_RASTER)) && |
3989 | 0 | poDriver->GetMetadataItem(GDAL_DCAP_CREATE)) |
3990 | 0 | { |
3991 | | // if it supports Create, it supports CreateCopy |
3992 | 0 | } |
3993 | 0 | else |
3994 | 0 | { |
3995 | 0 | ok = false; |
3996 | 0 | break; |
3997 | 0 | } |
3998 | 0 | } |
3999 | 0 | if (ok) |
4000 | 0 | { |
4001 | 0 | res.push_back(poDriver->GetDescription()); |
4002 | 0 | } |
4003 | 0 | } |
4004 | 0 | } |
4005 | 0 | if (bGDALGAllowed) |
4006 | 0 | res.push_back("GDALG"); |
4007 | 0 | return res; |
4008 | 0 | } |
4009 | | |
4010 | | /************************************************************************/ |
4011 | | /* GDALAlgorithm::AddInputFormatsArg() */ |
4012 | | /************************************************************************/ |
4013 | | |
4014 | | GDALInConstructionAlgorithmArg & |
4015 | | GDALAlgorithm::AddInputFormatsArg(std::vector<std::string> *pValue, |
4016 | | const char *helpMessage) |
4017 | 0 | { |
4018 | 0 | auto &arg = AddArg(GDAL_ARG_NAME_INPUT_FORMAT, 0, |
4019 | 0 | MsgOrDefault(helpMessage, _("Input formats")), pValue) |
4020 | 0 | .AddAlias("if") |
4021 | 0 | .SetCategory(GAAC_ADVANCED); |
4022 | 0 | arg.AddValidationAction([this, &arg]() |
4023 | 0 | { return ValidateFormat(arg, false, false); }); |
4024 | 0 | arg.SetAutoCompleteFunction( |
4025 | 0 | [&arg](const std::string &) |
4026 | 0 | { return FormatAutoCompleteFunction(arg, false, false); }); |
4027 | 0 | return arg; |
4028 | 0 | } |
4029 | | |
4030 | | /************************************************************************/ |
4031 | | /* GDALAlgorithm::AddOutputFormatArg() */ |
4032 | | /************************************************************************/ |
4033 | | |
4034 | | GDALInConstructionAlgorithmArg & |
4035 | | GDALAlgorithm::AddOutputFormatArg(std::string *pValue, bool bStreamAllowed, |
4036 | | bool bGDALGAllowed, const char *helpMessage) |
4037 | 0 | { |
4038 | 0 | auto &arg = AddArg(GDAL_ARG_NAME_OUTPUT_FORMAT, 'f', |
4039 | 0 | MsgOrDefault(helpMessage, |
4040 | 0 | bGDALGAllowed |
4041 | 0 | ? _("Output format (\"GDALG\" allowed)") |
4042 | 0 | : _("Output format")), |
4043 | 0 | pValue) |
4044 | 0 | .AddAlias("of") |
4045 | 0 | .AddAlias("format"); |
4046 | 0 | arg.AddValidationAction( |
4047 | 0 | [this, &arg, bStreamAllowed, bGDALGAllowed]() |
4048 | 0 | { return ValidateFormat(arg, bStreamAllowed, bGDALGAllowed); }); |
4049 | 0 | arg.SetAutoCompleteFunction( |
4050 | 0 | [&arg, bStreamAllowed, bGDALGAllowed](const std::string &) { |
4051 | 0 | return FormatAutoCompleteFunction(arg, bStreamAllowed, |
4052 | 0 | bGDALGAllowed); |
4053 | 0 | }); |
4054 | 0 | return arg; |
4055 | 0 | } |
4056 | | |
4057 | | /************************************************************************/ |
4058 | | /* GDALAlgorithm::AddOutputDataTypeArg() */ |
4059 | | /************************************************************************/ |
4060 | | GDALInConstructionAlgorithmArg & |
4061 | | GDALAlgorithm::AddOutputDataTypeArg(std::string *pValue, |
4062 | | const char *helpMessage) |
4063 | 0 | { |
4064 | 0 | auto &arg = |
4065 | 0 | AddArg(GDAL_ARG_NAME_OUTPUT_DATA_TYPE, 0, |
4066 | 0 | MsgOrDefault(helpMessage, _("Output data type")), pValue) |
4067 | 0 | .AddAlias("ot") |
4068 | 0 | .AddAlias("datatype") |
4069 | 0 | .AddMetadataItem("type", {"GDALDataType"}) |
4070 | 0 | .SetChoices("Byte", "Int8", "UInt16", "Int16", "UInt32", "Int32", |
4071 | 0 | "UInt64", "Int64", "CInt16", "CInt32", "Float16", |
4072 | 0 | "Float32", "Float64", "CFloat32", "CFloat64"); |
4073 | 0 | return arg; |
4074 | 0 | } |
4075 | | |
4076 | | /************************************************************************/ |
4077 | | /* GDALAlgorithm::AddNodataArg() */ |
4078 | | /************************************************************************/ |
4079 | | |
4080 | | GDALInConstructionAlgorithmArg & |
4081 | | GDALAlgorithm::AddNodataArg(std::string *pValue, bool noneAllowed, |
4082 | | const std::string &optionName, |
4083 | | const char *helpMessage) |
4084 | 0 | { |
4085 | 0 | auto &arg = AddArg( |
4086 | 0 | optionName, 0, |
4087 | 0 | MsgOrDefault(helpMessage, |
4088 | 0 | noneAllowed |
4089 | 0 | ? _("Assign a specified nodata value to output bands " |
4090 | 0 | "('none', numeric value, 'nan', 'inf', '-inf')") |
4091 | 0 | : _("Assign a specified nodata value to output bands " |
4092 | 0 | "(numeric value, 'nan', 'inf', '-inf')")), |
4093 | 0 | pValue); |
4094 | 0 | arg.AddValidationAction( |
4095 | 0 | [this, pValue, noneAllowed, optionName]() |
4096 | 0 | { |
4097 | 0 | if (!(noneAllowed && EQUAL(pValue->c_str(), "none"))) |
4098 | 0 | { |
4099 | 0 | char *endptr = nullptr; |
4100 | 0 | CPLStrtod(pValue->c_str(), &endptr); |
4101 | 0 | if (endptr != pValue->c_str() + pValue->size()) |
4102 | 0 | { |
4103 | 0 | ReportError(CE_Failure, CPLE_IllegalArg, |
4104 | 0 | "Value of '%s' should be %sa " |
4105 | 0 | "numeric value, 'nan', 'inf' or '-inf'", |
4106 | 0 | optionName.c_str(), |
4107 | 0 | noneAllowed ? "'none', " : ""); |
4108 | 0 | return false; |
4109 | 0 | } |
4110 | 0 | } |
4111 | 0 | return true; |
4112 | 0 | }); |
4113 | 0 | return arg; |
4114 | 0 | } |
4115 | | |
4116 | | /************************************************************************/ |
4117 | | /* GDALAlgorithm::AddOutputStringArg() */ |
4118 | | /************************************************************************/ |
4119 | | |
4120 | | GDALInConstructionAlgorithmArg & |
4121 | | GDALAlgorithm::AddOutputStringArg(std::string *pValue, const char *helpMessage) |
4122 | 0 | { |
4123 | 0 | return AddArg( |
4124 | 0 | "output-string", 0, |
4125 | 0 | MsgOrDefault(helpMessage, |
4126 | 0 | _("Output string, in which the result is placed")), |
4127 | 0 | pValue) |
4128 | 0 | .SetHiddenForCLI() |
4129 | 0 | .SetIsInput(false) |
4130 | 0 | .SetIsOutput(true); |
4131 | 0 | } |
4132 | | |
4133 | | /************************************************************************/ |
4134 | | /* GDALAlgorithm::AddLayerNameArg() */ |
4135 | | /************************************************************************/ |
4136 | | |
4137 | | GDALInConstructionAlgorithmArg & |
4138 | | GDALAlgorithm::AddLayerNameArg(std::string *pValue, const char *helpMessage) |
4139 | 0 | { |
4140 | 0 | return AddArg("layer", 'l', MsgOrDefault(helpMessage, _("Layer name")), |
4141 | 0 | pValue); |
4142 | 0 | } |
4143 | | |
4144 | | /************************************************************************/ |
4145 | | /* GDALAlgorithm::AddLayerNameArg() */ |
4146 | | /************************************************************************/ |
4147 | | |
4148 | | GDALInConstructionAlgorithmArg & |
4149 | | GDALAlgorithm::AddLayerNameArg(std::vector<std::string> *pValue, |
4150 | | const char *helpMessage) |
4151 | 0 | { |
4152 | 0 | return AddArg("layer", 'l', MsgOrDefault(helpMessage, _("Layer name")), |
4153 | 0 | pValue); |
4154 | 0 | } |
4155 | | |
4156 | | /************************************************************************/ |
4157 | | /* GDALAlgorithm::AddGeometryTypeArg() */ |
4158 | | /************************************************************************/ |
4159 | | |
4160 | | GDALInConstructionAlgorithmArg & |
4161 | | GDALAlgorithm::AddGeometryTypeArg(std::string *pValue, const char *helpMessage) |
4162 | 0 | { |
4163 | 0 | return AddArg("geometry-type", 0, |
4164 | 0 | MsgOrDefault(helpMessage, _("Geometry type")), pValue) |
4165 | 0 | .SetAutoCompleteFunction( |
4166 | 0 | [](const std::string ¤tValue) |
4167 | 0 | { |
4168 | 0 | std::vector<std::string> oRet; |
4169 | 0 | for (const char *type : |
4170 | 0 | {"GEOMETRY", "POINT", "LINESTRING", "POLYGON", |
4171 | 0 | "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON", |
4172 | 0 | "GEOMETRYCOLLECTION", "CURVE", "CIRCULARSTRING", |
4173 | 0 | "COMPOUNDCURVE", "SURFACE", "CURVEPOLYGON", "MULTICURVE", |
4174 | 0 | "MULTISURFACE", "POLYHEDRALSURFACE", "TIN"}) |
4175 | 0 | { |
4176 | 0 | if (currentValue.empty() || |
4177 | 0 | STARTS_WITH(type, currentValue.c_str())) |
4178 | 0 | { |
4179 | 0 | oRet.push_back(type); |
4180 | 0 | oRet.push_back(std::string(type).append("Z")); |
4181 | 0 | oRet.push_back(std::string(type).append("M")); |
4182 | 0 | oRet.push_back(std::string(type).append("ZM")); |
4183 | 0 | } |
4184 | 0 | } |
4185 | 0 | return oRet; |
4186 | 0 | }) |
4187 | 0 | .AddValidationAction( |
4188 | 0 | [this, pValue]() |
4189 | 0 | { |
4190 | 0 | if (wkbFlatten(OGRFromOGCGeomType(pValue->c_str())) == |
4191 | 0 | wkbUnknown && |
4192 | 0 | !STARTS_WITH_CI(pValue->c_str(), "GEOMETRY")) |
4193 | 0 | { |
4194 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
4195 | 0 | "Invalid geometry type '%s'", pValue->c_str()); |
4196 | 0 | return false; |
4197 | 0 | } |
4198 | 0 | return true; |
4199 | 0 | }); |
4200 | 0 | } |
4201 | | |
4202 | | /************************************************************************/ |
4203 | | /* GDALAlgorithm::SetAutoCompleteFunctionForLayerName() */ |
4204 | | /************************************************************************/ |
4205 | | |
4206 | | /* static */ |
4207 | | void GDALAlgorithm::SetAutoCompleteFunctionForLayerName( |
4208 | | GDALInConstructionAlgorithmArg &layerArg, |
4209 | | GDALInConstructionAlgorithmArg &datasetArg) |
4210 | 0 | { |
4211 | 0 | CPLAssert(datasetArg.GetType() == GAAT_DATASET || |
4212 | 0 | datasetArg.GetType() == GAAT_DATASET_LIST); |
4213 | | |
4214 | 0 | layerArg.SetAutoCompleteFunction( |
4215 | 0 | [&datasetArg](const std::string ¤tValue) |
4216 | 0 | { |
4217 | 0 | std::vector<std::string> ret; |
4218 | 0 | CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler); |
4219 | 0 | GDALArgDatasetValue *dsVal = nullptr; |
4220 | 0 | if (datasetArg.GetType() == GAAT_DATASET) |
4221 | 0 | { |
4222 | 0 | dsVal = &(datasetArg.Get<GDALArgDatasetValue>()); |
4223 | 0 | } |
4224 | 0 | else |
4225 | 0 | { |
4226 | 0 | auto &val = datasetArg.Get<std::vector<GDALArgDatasetValue>>(); |
4227 | 0 | if (val.size() == 1) |
4228 | 0 | { |
4229 | 0 | dsVal = &val[0]; |
4230 | 0 | } |
4231 | 0 | } |
4232 | 0 | if (dsVal && !dsVal->GetName().empty()) |
4233 | 0 | { |
4234 | 0 | auto poDS = std::unique_ptr<GDALDataset>(GDALDataset::Open( |
4235 | 0 | dsVal->GetName().c_str(), GDAL_OF_VECTOR)); |
4236 | 0 | if (poDS) |
4237 | 0 | { |
4238 | 0 | for (auto &&poLayer : poDS->GetLayers()) |
4239 | 0 | { |
4240 | 0 | if (currentValue == poLayer->GetDescription()) |
4241 | 0 | { |
4242 | 0 | ret.clear(); |
4243 | 0 | ret.push_back(poLayer->GetDescription()); |
4244 | 0 | break; |
4245 | 0 | } |
4246 | 0 | ret.push_back(poLayer->GetDescription()); |
4247 | 0 | } |
4248 | 0 | } |
4249 | 0 | } |
4250 | 0 | return ret; |
4251 | 0 | }); |
4252 | 0 | } |
4253 | | |
4254 | | /************************************************************************/ |
4255 | | /* GDALAlgorithm::ValidateBandArg() */ |
4256 | | /************************************************************************/ |
4257 | | |
4258 | | bool GDALAlgorithm::ValidateBandArg() const |
4259 | 0 | { |
4260 | 0 | bool ret = true; |
4261 | 0 | const auto bandArg = GetArg(GDAL_ARG_NAME_BAND); |
4262 | 0 | const auto inputDatasetArg = GetArg(GDAL_ARG_NAME_INPUT, false); |
4263 | 0 | if (bandArg && bandArg->IsExplicitlySet() && inputDatasetArg && |
4264 | 0 | (inputDatasetArg->GetType() == GAAT_DATASET || |
4265 | 0 | inputDatasetArg->GetType() == GAAT_DATASET_LIST) && |
4266 | 0 | (inputDatasetArg->GetDatasetType() & GDAL_OF_RASTER) != 0) |
4267 | 0 | { |
4268 | 0 | const auto CheckBand = [this](const GDALDataset *poDS, int nBand) |
4269 | 0 | { |
4270 | 0 | if (nBand > poDS->GetRasterCount()) |
4271 | 0 | { |
4272 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
4273 | 0 | "Value of 'band' should be greater or equal than " |
4274 | 0 | "1 and less or equal than %d.", |
4275 | 0 | poDS->GetRasterCount()); |
4276 | 0 | return false; |
4277 | 0 | } |
4278 | 0 | return true; |
4279 | 0 | }; |
4280 | |
|
4281 | 0 | const auto ValidateForOneDataset = |
4282 | 0 | [&bandArg, &CheckBand](const GDALDataset *poDS) |
4283 | 0 | { |
4284 | 0 | bool l_ret = true; |
4285 | 0 | if (bandArg->GetType() == GAAT_INTEGER) |
4286 | 0 | { |
4287 | 0 | l_ret = CheckBand(poDS, bandArg->Get<int>()); |
4288 | 0 | } |
4289 | 0 | else if (bandArg->GetType() == GAAT_INTEGER_LIST) |
4290 | 0 | { |
4291 | 0 | for (int nBand : bandArg->Get<std::vector<int>>()) |
4292 | 0 | { |
4293 | 0 | l_ret = l_ret && CheckBand(poDS, nBand); |
4294 | 0 | } |
4295 | 0 | } |
4296 | 0 | return l_ret; |
4297 | 0 | }; |
4298 | |
|
4299 | 0 | if (inputDatasetArg->GetType() == GAAT_DATASET) |
4300 | 0 | { |
4301 | 0 | auto poDS = |
4302 | 0 | inputDatasetArg->Get<GDALArgDatasetValue>().GetDatasetRef(); |
4303 | 0 | if (poDS && !ValidateForOneDataset(poDS)) |
4304 | 0 | ret = false; |
4305 | 0 | } |
4306 | 0 | else |
4307 | 0 | { |
4308 | 0 | CPLAssert(inputDatasetArg->GetType() == GAAT_DATASET_LIST); |
4309 | 0 | for (auto &datasetValue : |
4310 | 0 | inputDatasetArg->Get<std::vector<GDALArgDatasetValue>>()) |
4311 | 0 | { |
4312 | 0 | auto poDS = datasetValue.GetDatasetRef(); |
4313 | 0 | if (poDS && !ValidateForOneDataset(poDS)) |
4314 | 0 | ret = false; |
4315 | 0 | } |
4316 | 0 | } |
4317 | 0 | } |
4318 | 0 | return ret; |
4319 | 0 | } |
4320 | | |
4321 | | /************************************************************************/ |
4322 | | /* GDALAlgorithm::RunPreStepPipelineValidations() */ |
4323 | | /************************************************************************/ |
4324 | | |
4325 | | bool GDALAlgorithm::RunPreStepPipelineValidations() const |
4326 | 0 | { |
4327 | 0 | return ValidateBandArg(); |
4328 | 0 | } |
4329 | | |
4330 | | /************************************************************************/ |
4331 | | /* GDALAlgorithm::AddBandArg() */ |
4332 | | /************************************************************************/ |
4333 | | |
4334 | | GDALInConstructionAlgorithmArg & |
4335 | | GDALAlgorithm::AddBandArg(int *pValue, const char *helpMessage) |
4336 | 0 | { |
4337 | 0 | AddValidationAction([this]() { return ValidateBandArg(); }); |
4338 | |
|
4339 | 0 | return AddArg(GDAL_ARG_NAME_BAND, 'b', |
4340 | 0 | MsgOrDefault(helpMessage, _("Input band (1-based index)")), |
4341 | 0 | pValue) |
4342 | 0 | .AddValidationAction( |
4343 | 0 | [pValue]() |
4344 | 0 | { |
4345 | 0 | if (*pValue <= 0) |
4346 | 0 | { |
4347 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
4348 | 0 | "Value of 'band' should greater or equal to 1."); |
4349 | 0 | return false; |
4350 | 0 | } |
4351 | 0 | return true; |
4352 | 0 | }); |
4353 | 0 | } |
4354 | | |
4355 | | /************************************************************************/ |
4356 | | /* GDALAlgorithm::AddBandArg() */ |
4357 | | /************************************************************************/ |
4358 | | |
4359 | | GDALInConstructionAlgorithmArg & |
4360 | | GDALAlgorithm::AddBandArg(std::vector<int> *pValue, const char *helpMessage) |
4361 | 0 | { |
4362 | 0 | AddValidationAction([this]() { return ValidateBandArg(); }); |
4363 | |
|
4364 | 0 | return AddArg(GDAL_ARG_NAME_BAND, 'b', |
4365 | 0 | MsgOrDefault(helpMessage, _("Input band(s) (1-based index)")), |
4366 | 0 | pValue) |
4367 | 0 | .AddValidationAction( |
4368 | 0 | [pValue]() |
4369 | 0 | { |
4370 | 0 | for (int val : *pValue) |
4371 | 0 | { |
4372 | 0 | if (val <= 0) |
4373 | 0 | { |
4374 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
4375 | 0 | "Value of 'band' should greater or equal " |
4376 | 0 | "to 1."); |
4377 | 0 | return false; |
4378 | 0 | } |
4379 | 0 | } |
4380 | 0 | return true; |
4381 | 0 | }); |
4382 | 0 | } |
4383 | | |
4384 | | /************************************************************************/ |
4385 | | /* ParseAndValidateKeyValue() */ |
4386 | | /************************************************************************/ |
4387 | | |
4388 | | bool GDALAlgorithm::ParseAndValidateKeyValue(GDALAlgorithmArg &arg) |
4389 | 0 | { |
4390 | 0 | const auto Validate = [this, &arg](const std::string &val) |
4391 | 0 | { |
4392 | 0 | if (val.find('=') == std::string::npos) |
4393 | 0 | { |
4394 | 0 | ReportError( |
4395 | 0 | CE_Failure, CPLE_AppDefined, |
4396 | 0 | "Invalid value for argument '%s'. <KEY>=<VALUE> expected", |
4397 | 0 | arg.GetName().c_str()); |
4398 | 0 | return false; |
4399 | 0 | } |
4400 | | |
4401 | 0 | return true; |
4402 | 0 | }; |
4403 | |
|
4404 | 0 | if (arg.GetType() == GAAT_STRING) |
4405 | 0 | { |
4406 | 0 | return Validate(arg.Get<std::string>()); |
4407 | 0 | } |
4408 | 0 | else if (arg.GetType() == GAAT_STRING_LIST) |
4409 | 0 | { |
4410 | 0 | std::vector<std::string> &vals = arg.Get<std::vector<std::string>>(); |
4411 | 0 | if (vals.size() == 1) |
4412 | 0 | { |
4413 | | // Try to split A=B,C=D into A=B and C=D if there is no ambiguity |
4414 | 0 | std::vector<std::string> newVals; |
4415 | 0 | std::string curToken; |
4416 | 0 | bool canSplitOnComma = true; |
4417 | 0 | char lastSep = 0; |
4418 | 0 | bool inString = false; |
4419 | 0 | bool equalFoundInLastToken = false; |
4420 | 0 | for (char c : vals[0]) |
4421 | 0 | { |
4422 | 0 | if (!inString && c == ',') |
4423 | 0 | { |
4424 | 0 | if (lastSep != '=' || !equalFoundInLastToken) |
4425 | 0 | { |
4426 | 0 | canSplitOnComma = false; |
4427 | 0 | break; |
4428 | 0 | } |
4429 | 0 | lastSep = c; |
4430 | 0 | newVals.push_back(curToken); |
4431 | 0 | curToken.clear(); |
4432 | 0 | equalFoundInLastToken = false; |
4433 | 0 | } |
4434 | 0 | else if (!inString && c == '=') |
4435 | 0 | { |
4436 | 0 | if (lastSep == '=') |
4437 | 0 | { |
4438 | 0 | canSplitOnComma = false; |
4439 | 0 | break; |
4440 | 0 | } |
4441 | 0 | equalFoundInLastToken = true; |
4442 | 0 | lastSep = c; |
4443 | 0 | curToken += c; |
4444 | 0 | } |
4445 | 0 | else if (c == '"') |
4446 | 0 | { |
4447 | 0 | inString = !inString; |
4448 | 0 | curToken += c; |
4449 | 0 | } |
4450 | 0 | else |
4451 | 0 | { |
4452 | 0 | curToken += c; |
4453 | 0 | } |
4454 | 0 | } |
4455 | 0 | if (canSplitOnComma && !inString && equalFoundInLastToken) |
4456 | 0 | { |
4457 | 0 | if (!curToken.empty()) |
4458 | 0 | newVals.emplace_back(std::move(curToken)); |
4459 | 0 | vals = std::move(newVals); |
4460 | 0 | } |
4461 | 0 | } |
4462 | |
|
4463 | 0 | for (const auto &val : vals) |
4464 | 0 | { |
4465 | 0 | if (!Validate(val)) |
4466 | 0 | return false; |
4467 | 0 | } |
4468 | 0 | } |
4469 | | |
4470 | 0 | return true; |
4471 | 0 | } |
4472 | | |
4473 | | /************************************************************************/ |
4474 | | /* IsGDALGOutput() */ |
4475 | | /************************************************************************/ |
4476 | | |
4477 | | bool GDALAlgorithm::IsGDALGOutput() const |
4478 | 0 | { |
4479 | 0 | bool isGDALGOutput = false; |
4480 | 0 | const auto outputFormatArg = GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT); |
4481 | 0 | const auto outputArg = GetArg(GDAL_ARG_NAME_OUTPUT); |
4482 | 0 | if (outputArg && outputArg->GetType() == GAAT_DATASET && |
4483 | 0 | outputArg->IsExplicitlySet()) |
4484 | 0 | { |
4485 | 0 | if (outputFormatArg && outputFormatArg->GetType() == GAAT_STRING && |
4486 | 0 | outputFormatArg->IsExplicitlySet()) |
4487 | 0 | { |
4488 | 0 | const auto &val = |
4489 | 0 | outputFormatArg->GDALAlgorithmArg::Get<std::string>(); |
4490 | 0 | isGDALGOutput = EQUAL(val.c_str(), "GDALG"); |
4491 | 0 | } |
4492 | 0 | else |
4493 | 0 | { |
4494 | 0 | const auto &filename = |
4495 | 0 | outputArg->GDALAlgorithmArg::Get<GDALArgDatasetValue>(); |
4496 | 0 | isGDALGOutput = |
4497 | 0 | filename.GetName().size() > strlen(".gdalg.json") && |
4498 | 0 | EQUAL(filename.GetName().c_str() + filename.GetName().size() - |
4499 | 0 | strlen(".gdalg.json"), |
4500 | 0 | ".gdalg.json"); |
4501 | 0 | } |
4502 | 0 | } |
4503 | 0 | return isGDALGOutput; |
4504 | 0 | } |
4505 | | |
4506 | | /************************************************************************/ |
4507 | | /* ProcessGDALGOutput() */ |
4508 | | /************************************************************************/ |
4509 | | |
4510 | | GDALAlgorithm::ProcessGDALGOutputRet GDALAlgorithm::ProcessGDALGOutput() |
4511 | 0 | { |
4512 | 0 | if (!SupportsStreamedOutput()) |
4513 | 0 | return ProcessGDALGOutputRet::NOT_GDALG; |
4514 | | |
4515 | 0 | if (IsGDALGOutput()) |
4516 | 0 | { |
4517 | 0 | const auto outputArg = GetArg(GDAL_ARG_NAME_OUTPUT); |
4518 | 0 | const auto &filename = |
4519 | 0 | outputArg->GDALAlgorithmArg::Get<GDALArgDatasetValue>().GetName(); |
4520 | 0 | VSIStatBufL sStat; |
4521 | 0 | if (VSIStatL(filename.c_str(), &sStat) == 0) |
4522 | 0 | { |
4523 | 0 | const auto overwriteArg = GetArg(GDAL_ARG_NAME_OVERWRITE); |
4524 | 0 | if (overwriteArg && overwriteArg->GetType() == GAAT_BOOLEAN) |
4525 | 0 | { |
4526 | 0 | if (!overwriteArg->GDALAlgorithmArg::Get<bool>()) |
4527 | 0 | { |
4528 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
4529 | 0 | "File '%s' already exists. Specify the " |
4530 | 0 | "--overwrite option to overwrite it.", |
4531 | 0 | filename.c_str()); |
4532 | 0 | return ProcessGDALGOutputRet::GDALG_ERROR; |
4533 | 0 | } |
4534 | 0 | } |
4535 | 0 | } |
4536 | | |
4537 | 0 | std::string osCommandLine; |
4538 | |
|
4539 | 0 | for (const auto &path : GDALAlgorithm::m_callPath) |
4540 | 0 | { |
4541 | 0 | if (!osCommandLine.empty()) |
4542 | 0 | osCommandLine += ' '; |
4543 | 0 | osCommandLine += path; |
4544 | 0 | } |
4545 | |
|
4546 | 0 | for (const auto &arg : GetArgs()) |
4547 | 0 | { |
4548 | 0 | if (arg->IsExplicitlySet() && |
4549 | 0 | arg->GetName() != GDAL_ARG_NAME_OUTPUT && |
4550 | 0 | arg->GetName() != GDAL_ARG_NAME_OUTPUT_FORMAT && |
4551 | 0 | arg->GetName() != GDAL_ARG_NAME_UPDATE && |
4552 | 0 | arg->GetName() != GDAL_ARG_NAME_OVERWRITE) |
4553 | 0 | { |
4554 | 0 | osCommandLine += ' '; |
4555 | 0 | std::string strArg; |
4556 | 0 | if (!arg->Serialize(strArg)) |
4557 | 0 | { |
4558 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
4559 | 0 | "Cannot serialize argument %s", |
4560 | 0 | arg->GetName().c_str()); |
4561 | 0 | return ProcessGDALGOutputRet::GDALG_ERROR; |
4562 | 0 | } |
4563 | 0 | osCommandLine += strArg; |
4564 | 0 | } |
4565 | 0 | } |
4566 | | |
4567 | 0 | osCommandLine += " --output-format stream --output streamed_dataset"; |
4568 | |
|
4569 | 0 | return SaveGDALG(filename, osCommandLine) |
4570 | 0 | ? ProcessGDALGOutputRet::GDALG_OK |
4571 | 0 | : ProcessGDALGOutputRet::GDALG_ERROR; |
4572 | 0 | } |
4573 | | |
4574 | 0 | return ProcessGDALGOutputRet::NOT_GDALG; |
4575 | 0 | } |
4576 | | |
4577 | | /************************************************************************/ |
4578 | | /* GDALAlgorithm::SaveGDALG() */ |
4579 | | /************************************************************************/ |
4580 | | |
4581 | | /* static */ bool GDALAlgorithm::SaveGDALG(const std::string &filename, |
4582 | | const std::string &commandLine) |
4583 | 0 | { |
4584 | 0 | CPLJSONDocument oDoc; |
4585 | 0 | oDoc.GetRoot().Add("type", "gdal_streamed_alg"); |
4586 | 0 | oDoc.GetRoot().Add("command_line", commandLine); |
4587 | 0 | oDoc.GetRoot().Add("gdal_version", GDALVersionInfo("VERSION_NUM")); |
4588 | |
|
4589 | 0 | return oDoc.Save(filename); |
4590 | 0 | } |
4591 | | |
4592 | | /************************************************************************/ |
4593 | | /* GDALAlgorithm::AddCreationOptionsArg() */ |
4594 | | /************************************************************************/ |
4595 | | |
4596 | | GDALInConstructionAlgorithmArg & |
4597 | | GDALAlgorithm::AddCreationOptionsArg(std::vector<std::string> *pValue, |
4598 | | const char *helpMessage) |
4599 | 0 | { |
4600 | 0 | auto &arg = AddArg("creation-option", 0, |
4601 | 0 | MsgOrDefault(helpMessage, _("Creation option")), pValue) |
4602 | 0 | .AddAlias("co") |
4603 | 0 | .SetMetaVar("<KEY>=<VALUE>") |
4604 | 0 | .SetPackedValuesAllowed(false); |
4605 | 0 | arg.AddValidationAction([this, &arg]() |
4606 | 0 | { return ParseAndValidateKeyValue(arg); }); |
4607 | |
|
4608 | 0 | arg.SetAutoCompleteFunction( |
4609 | 0 | [this](const std::string ¤tValue) |
4610 | 0 | { |
4611 | 0 | std::vector<std::string> oRet; |
4612 | |
|
4613 | 0 | int datasetType = |
4614 | 0 | GDAL_OF_RASTER | GDAL_OF_VECTOR | GDAL_OF_MULTIDIM_RASTER; |
4615 | 0 | auto outputArg = GetArg(GDAL_ARG_NAME_OUTPUT); |
4616 | 0 | if (outputArg && (outputArg->GetType() == GAAT_DATASET || |
4617 | 0 | outputArg->GetType() == GAAT_DATASET_LIST)) |
4618 | 0 | { |
4619 | 0 | datasetType = outputArg->GetDatasetType(); |
4620 | 0 | } |
4621 | |
|
4622 | 0 | auto outputFormat = GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT); |
4623 | 0 | if (outputFormat && outputFormat->GetType() == GAAT_STRING && |
4624 | 0 | outputFormat->IsExplicitlySet()) |
4625 | 0 | { |
4626 | 0 | auto poDriver = GetGDALDriverManager()->GetDriverByName( |
4627 | 0 | outputFormat->Get<std::string>().c_str()); |
4628 | 0 | if (poDriver) |
4629 | 0 | { |
4630 | 0 | AddOptionsSuggestions( |
4631 | 0 | poDriver->GetMetadataItem(GDAL_DMD_CREATIONOPTIONLIST), |
4632 | 0 | datasetType, currentValue, oRet); |
4633 | 0 | } |
4634 | 0 | return oRet; |
4635 | 0 | } |
4636 | | |
4637 | 0 | if (outputArg && outputArg->GetType() == GAAT_DATASET) |
4638 | 0 | { |
4639 | 0 | auto poDM = GetGDALDriverManager(); |
4640 | 0 | auto &datasetValue = outputArg->Get<GDALArgDatasetValue>(); |
4641 | 0 | const auto &osDSName = datasetValue.GetName(); |
4642 | 0 | const std::string osExt = CPLGetExtensionSafe(osDSName.c_str()); |
4643 | 0 | if (!osExt.empty()) |
4644 | 0 | { |
4645 | 0 | std::set<std::string> oVisitedExtensions; |
4646 | 0 | for (int i = 0; i < poDM->GetDriverCount(); ++i) |
4647 | 0 | { |
4648 | 0 | auto poDriver = poDM->GetDriver(i); |
4649 | 0 | if (((datasetType & GDAL_OF_RASTER) != 0 && |
4650 | 0 | poDriver->GetMetadataItem(GDAL_DCAP_RASTER)) || |
4651 | 0 | ((datasetType & GDAL_OF_VECTOR) != 0 && |
4652 | 0 | poDriver->GetMetadataItem(GDAL_DCAP_VECTOR)) || |
4653 | 0 | ((datasetType & GDAL_OF_MULTIDIM_RASTER) != 0 && |
4654 | 0 | poDriver->GetMetadataItem( |
4655 | 0 | GDAL_DCAP_MULTIDIM_RASTER))) |
4656 | 0 | { |
4657 | 0 | const char *pszExtensions = |
4658 | 0 | poDriver->GetMetadataItem(GDAL_DMD_EXTENSIONS); |
4659 | 0 | if (pszExtensions) |
4660 | 0 | { |
4661 | 0 | const CPLStringList aosExts( |
4662 | 0 | CSLTokenizeString2(pszExtensions, " ", 0)); |
4663 | 0 | for (const char *pszExt : cpl::Iterate(aosExts)) |
4664 | 0 | { |
4665 | 0 | if (EQUAL(pszExt, osExt.c_str()) && |
4666 | 0 | !cpl::contains(oVisitedExtensions, |
4667 | 0 | pszExt)) |
4668 | 0 | { |
4669 | 0 | oVisitedExtensions.insert(pszExt); |
4670 | 0 | if (AddOptionsSuggestions( |
4671 | 0 | poDriver->GetMetadataItem( |
4672 | 0 | GDAL_DMD_CREATIONOPTIONLIST), |
4673 | 0 | datasetType, currentValue, |
4674 | 0 | oRet)) |
4675 | 0 | { |
4676 | 0 | return oRet; |
4677 | 0 | } |
4678 | 0 | break; |
4679 | 0 | } |
4680 | 0 | } |
4681 | 0 | } |
4682 | 0 | } |
4683 | 0 | } |
4684 | 0 | } |
4685 | 0 | } |
4686 | | |
4687 | 0 | return oRet; |
4688 | 0 | }); |
4689 | |
|
4690 | 0 | return arg; |
4691 | 0 | } |
4692 | | |
4693 | | /************************************************************************/ |
4694 | | /* GDALAlgorithm::AddLayerCreationOptionsArg() */ |
4695 | | /************************************************************************/ |
4696 | | |
4697 | | GDALInConstructionAlgorithmArg & |
4698 | | GDALAlgorithm::AddLayerCreationOptionsArg(std::vector<std::string> *pValue, |
4699 | | const char *helpMessage) |
4700 | 0 | { |
4701 | 0 | auto &arg = |
4702 | 0 | AddArg("layer-creation-option", 0, |
4703 | 0 | MsgOrDefault(helpMessage, _("Layer creation option")), pValue) |
4704 | 0 | .AddAlias("lco") |
4705 | 0 | .SetMetaVar("<KEY>=<VALUE>") |
4706 | 0 | .SetPackedValuesAllowed(false); |
4707 | 0 | arg.AddValidationAction([this, &arg]() |
4708 | 0 | { return ParseAndValidateKeyValue(arg); }); |
4709 | |
|
4710 | 0 | arg.SetAutoCompleteFunction( |
4711 | 0 | [this](const std::string ¤tValue) |
4712 | 0 | { |
4713 | 0 | std::vector<std::string> oRet; |
4714 | |
|
4715 | 0 | auto outputFormat = GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT); |
4716 | 0 | if (outputFormat && outputFormat->GetType() == GAAT_STRING && |
4717 | 0 | outputFormat->IsExplicitlySet()) |
4718 | 0 | { |
4719 | 0 | auto poDriver = GetGDALDriverManager()->GetDriverByName( |
4720 | 0 | outputFormat->Get<std::string>().c_str()); |
4721 | 0 | if (poDriver) |
4722 | 0 | { |
4723 | 0 | AddOptionsSuggestions(poDriver->GetMetadataItem( |
4724 | 0 | GDAL_DS_LAYER_CREATIONOPTIONLIST), |
4725 | 0 | GDAL_OF_VECTOR, currentValue, oRet); |
4726 | 0 | } |
4727 | 0 | return oRet; |
4728 | 0 | } |
4729 | | |
4730 | 0 | auto outputArg = GetArg(GDAL_ARG_NAME_OUTPUT); |
4731 | 0 | if (outputArg && outputArg->GetType() == GAAT_DATASET) |
4732 | 0 | { |
4733 | 0 | auto poDM = GetGDALDriverManager(); |
4734 | 0 | auto &datasetValue = outputArg->Get<GDALArgDatasetValue>(); |
4735 | 0 | const auto &osDSName = datasetValue.GetName(); |
4736 | 0 | const std::string osExt = CPLGetExtensionSafe(osDSName.c_str()); |
4737 | 0 | if (!osExt.empty()) |
4738 | 0 | { |
4739 | 0 | std::set<std::string> oVisitedExtensions; |
4740 | 0 | for (int i = 0; i < poDM->GetDriverCount(); ++i) |
4741 | 0 | { |
4742 | 0 | auto poDriver = poDM->GetDriver(i); |
4743 | 0 | if (poDriver->GetMetadataItem(GDAL_DCAP_VECTOR)) |
4744 | 0 | { |
4745 | 0 | const char *pszExtensions = |
4746 | 0 | poDriver->GetMetadataItem(GDAL_DMD_EXTENSIONS); |
4747 | 0 | if (pszExtensions) |
4748 | 0 | { |
4749 | 0 | const CPLStringList aosExts( |
4750 | 0 | CSLTokenizeString2(pszExtensions, " ", 0)); |
4751 | 0 | for (const char *pszExt : cpl::Iterate(aosExts)) |
4752 | 0 | { |
4753 | 0 | if (EQUAL(pszExt, osExt.c_str()) && |
4754 | 0 | !cpl::contains(oVisitedExtensions, |
4755 | 0 | pszExt)) |
4756 | 0 | { |
4757 | 0 | oVisitedExtensions.insert(pszExt); |
4758 | 0 | if (AddOptionsSuggestions( |
4759 | 0 | poDriver->GetMetadataItem( |
4760 | 0 | GDAL_DS_LAYER_CREATIONOPTIONLIST), |
4761 | 0 | GDAL_OF_VECTOR, currentValue, |
4762 | 0 | oRet)) |
4763 | 0 | { |
4764 | 0 | return oRet; |
4765 | 0 | } |
4766 | 0 | break; |
4767 | 0 | } |
4768 | 0 | } |
4769 | 0 | } |
4770 | 0 | } |
4771 | 0 | } |
4772 | 0 | } |
4773 | 0 | } |
4774 | | |
4775 | 0 | return oRet; |
4776 | 0 | }); |
4777 | |
|
4778 | 0 | return arg; |
4779 | 0 | } |
4780 | | |
4781 | | /************************************************************************/ |
4782 | | /* GDALAlgorithm::AddBBOXArg() */ |
4783 | | /************************************************************************/ |
4784 | | |
4785 | | /** Add bbox=xmin,ymin,xmax,ymax argument. */ |
4786 | | GDALInConstructionAlgorithmArg & |
4787 | | GDALAlgorithm::AddBBOXArg(std::vector<double> *pValue, const char *helpMessage) |
4788 | 0 | { |
4789 | 0 | auto &arg = AddArg("bbox", 0, |
4790 | 0 | MsgOrDefault(helpMessage, |
4791 | 0 | _("Bounding box as xmin,ymin,xmax,ymax")), |
4792 | 0 | pValue) |
4793 | 0 | .SetRepeatedArgAllowed(false) |
4794 | 0 | .SetMinCount(4) |
4795 | 0 | .SetMaxCount(4) |
4796 | 0 | .SetDisplayHintAboutRepetition(false); |
4797 | 0 | arg.AddValidationAction( |
4798 | 0 | [&arg]() |
4799 | 0 | { |
4800 | 0 | const auto &val = arg.Get<std::vector<double>>(); |
4801 | 0 | CPLAssert(val.size() == 4); |
4802 | 0 | if (!(val[0] <= val[2]) || !(val[1] <= val[3])) |
4803 | 0 | { |
4804 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
4805 | 0 | "Value of 'bbox' should be xmin,ymin,xmax,ymax with " |
4806 | 0 | "xmin <= xmax and ymin <= ymax"); |
4807 | 0 | return false; |
4808 | 0 | } |
4809 | 0 | return true; |
4810 | 0 | }); |
4811 | 0 | return arg; |
4812 | 0 | } |
4813 | | |
4814 | | /************************************************************************/ |
4815 | | /* GDALAlgorithm::AddActiveLayerArg() */ |
4816 | | /************************************************************************/ |
4817 | | |
4818 | | GDALInConstructionAlgorithmArg & |
4819 | | GDALAlgorithm::AddActiveLayerArg(std::string *pValue, const char *helpMessage) |
4820 | 0 | { |
4821 | 0 | return AddArg("active-layer", 0, |
4822 | 0 | MsgOrDefault(helpMessage, |
4823 | 0 | _("Set active layer (if not specified, all)")), |
4824 | 0 | pValue); |
4825 | 0 | } |
4826 | | |
4827 | | /************************************************************************/ |
4828 | | /* GDALAlgorithm::AddNumThreadsArg() */ |
4829 | | /************************************************************************/ |
4830 | | |
4831 | | GDALInConstructionAlgorithmArg & |
4832 | | GDALAlgorithm::AddNumThreadsArg(int *pValue, std::string *pStrValue, |
4833 | | const char *helpMessage) |
4834 | 0 | { |
4835 | 0 | auto &arg = |
4836 | 0 | AddArg("num-threads", 'j', |
4837 | 0 | MsgOrDefault(helpMessage, _("Number of jobs (or ALL_CPUS)")), |
4838 | 0 | pStrValue); |
4839 | 0 | auto lambda = [this, &arg, pValue, pStrValue] |
4840 | 0 | { |
4841 | 0 | int nNumCPUs = std::max(1, CPLGetNumCPUs()); |
4842 | 0 | const char *pszThreads = |
4843 | 0 | CPLGetConfigOption("GDAL_NUM_THREADS", nullptr); |
4844 | 0 | if (pszThreads && !EQUAL(pszThreads, "ALL_CPUS")) |
4845 | 0 | { |
4846 | 0 | nNumCPUs = std::clamp(atoi(pszThreads), 1, nNumCPUs); |
4847 | 0 | } |
4848 | 0 | if (EQUAL(pStrValue->c_str(), "ALL_CPUS")) |
4849 | 0 | { |
4850 | 0 | *pValue = nNumCPUs; |
4851 | 0 | return true; |
4852 | 0 | } |
4853 | 0 | else |
4854 | 0 | { |
4855 | 0 | char *endptr = nullptr; |
4856 | 0 | const auto res = std::strtol(pStrValue->c_str(), &endptr, 10); |
4857 | 0 | if (endptr == pStrValue->c_str() + pStrValue->size() && res >= 0 && |
4858 | 0 | res <= INT_MAX) |
4859 | 0 | { |
4860 | 0 | *pValue = std::min(static_cast<int>(res), nNumCPUs); |
4861 | 0 | return true; |
4862 | 0 | } |
4863 | 0 | ReportError(CE_Failure, CPLE_IllegalArg, |
4864 | 0 | "Invalid value for '%s' argument", |
4865 | 0 | arg.GetName().c_str()); |
4866 | 0 | return false; |
4867 | 0 | } |
4868 | 0 | }; |
4869 | 0 | if (!pStrValue->empty()) |
4870 | 0 | { |
4871 | 0 | arg.SetDefault(*pStrValue); |
4872 | 0 | lambda(); |
4873 | 0 | } |
4874 | 0 | arg.AddValidationAction(std::move(lambda)); |
4875 | 0 | return arg; |
4876 | 0 | } |
4877 | | |
4878 | | /************************************************************************/ |
4879 | | /* GDALAlgorithm::AddAbsolutePathArg() */ |
4880 | | /************************************************************************/ |
4881 | | |
4882 | | GDALInConstructionAlgorithmArg & |
4883 | | GDALAlgorithm::AddAbsolutePathArg(bool *pValue, const char *helpMessage) |
4884 | 0 | { |
4885 | 0 | return AddArg( |
4886 | 0 | "absolute-path", 0, |
4887 | 0 | MsgOrDefault(helpMessage, _("Whether the path to the input dataset " |
4888 | 0 | "should be stored as an absolute path")), |
4889 | 0 | pValue); |
4890 | 0 | } |
4891 | | |
4892 | | /************************************************************************/ |
4893 | | /* GDALAlgorithm::AddPixelFunctionNameArg() */ |
4894 | | /************************************************************************/ |
4895 | | |
4896 | | GDALInConstructionAlgorithmArg & |
4897 | | GDALAlgorithm::AddPixelFunctionNameArg(std::string *pValue, |
4898 | | const char *helpMessage) |
4899 | 0 | { |
4900 | |
|
4901 | 0 | const auto pixelFunctionNames = |
4902 | 0 | VRTDerivedRasterBand::GetPixelFunctionNames(); |
4903 | 0 | return AddArg( |
4904 | 0 | "pixel-function", 0, |
4905 | 0 | MsgOrDefault( |
4906 | 0 | helpMessage, |
4907 | 0 | _("Specify a pixel function to calculate output value from " |
4908 | 0 | "overlapping inputs")), |
4909 | 0 | pValue) |
4910 | 0 | .SetChoices(pixelFunctionNames); |
4911 | 0 | } |
4912 | | |
4913 | | /************************************************************************/ |
4914 | | /* GDALAlgorithm::AddPixelFunctionArgsArg() */ |
4915 | | /************************************************************************/ |
4916 | | |
4917 | | GDALInConstructionAlgorithmArg & |
4918 | | GDALAlgorithm::AddPixelFunctionArgsArg(std::vector<std::string> *pValue, |
4919 | | const char *helpMessage) |
4920 | 0 | { |
4921 | 0 | auto &pixelFunctionArgArg = |
4922 | 0 | AddArg("pixel-function-arg", 0, |
4923 | 0 | MsgOrDefault( |
4924 | 0 | helpMessage, |
4925 | 0 | _("Specify argument(s) to pass to the pixel function")), |
4926 | 0 | pValue) |
4927 | 0 | .SetMetaVar("<NAME>=<VALUE>") |
4928 | 0 | .SetRepeatedArgAllowed(true); |
4929 | 0 | pixelFunctionArgArg.AddValidationAction( |
4930 | 0 | [this, &pixelFunctionArgArg]() |
4931 | 0 | { return ParseAndValidateKeyValue(pixelFunctionArgArg); }); |
4932 | |
|
4933 | 0 | pixelFunctionArgArg.SetAutoCompleteFunction( |
4934 | 0 | [this](const std::string ¤tValue) |
4935 | 0 | { |
4936 | 0 | std::string pixelFunction; |
4937 | 0 | const auto pixelFunctionArg = GetArg("pixel-function"); |
4938 | 0 | if (pixelFunctionArg && pixelFunctionArg->GetType() == GAAT_STRING) |
4939 | 0 | { |
4940 | 0 | pixelFunction = pixelFunctionArg->Get<std::string>(); |
4941 | 0 | } |
4942 | |
|
4943 | 0 | std::vector<std::string> ret; |
4944 | |
|
4945 | 0 | if (!pixelFunction.empty()) |
4946 | 0 | { |
4947 | 0 | const auto *pair = VRTDerivedRasterBand::GetPixelFunction( |
4948 | 0 | pixelFunction.c_str()); |
4949 | 0 | if (!pair) |
4950 | 0 | { |
4951 | 0 | ret.push_back("**"); |
4952 | | // Non printable UTF-8 space, to avoid autocompletion to pickup on 'd' |
4953 | 0 | ret.push_back(std::string("\xC2\xA0" |
4954 | 0 | "Invalid pixel function name")); |
4955 | 0 | } |
4956 | 0 | else if (pair->second.find("Argument name=") == |
4957 | 0 | std::string::npos) |
4958 | 0 | { |
4959 | 0 | ret.push_back("**"); |
4960 | | // Non printable UTF-8 space, to avoid autocompletion to pickup on 'd' |
4961 | 0 | ret.push_back( |
4962 | 0 | std::string( |
4963 | 0 | "\xC2\xA0" |
4964 | 0 | "No pixel function arguments for pixel function '") |
4965 | 0 | .append(pixelFunction) |
4966 | 0 | .append("'")); |
4967 | 0 | } |
4968 | 0 | else |
4969 | 0 | { |
4970 | 0 | AddOptionsSuggestions(pair->second.c_str(), 0, currentValue, |
4971 | 0 | ret); |
4972 | 0 | } |
4973 | 0 | } |
4974 | |
|
4975 | 0 | return ret; |
4976 | 0 | }); |
4977 | |
|
4978 | 0 | return pixelFunctionArgArg; |
4979 | 0 | } |
4980 | | |
4981 | | /************************************************************************/ |
4982 | | /* GDALAlgorithm::AddProgressArg() */ |
4983 | | /************************************************************************/ |
4984 | | |
4985 | | GDALInConstructionAlgorithmArg &GDALAlgorithm::AddProgressArg() |
4986 | 0 | { |
4987 | 0 | return AddArg("progress", 0, _("Display progress bar"), |
4988 | 0 | &m_progressBarRequested) |
4989 | 0 | .SetOnlyForCLI() |
4990 | 0 | .SetCategory(GAAC_COMMON); |
4991 | 0 | } |
4992 | | |
4993 | | /************************************************************************/ |
4994 | | /* GDALAlgorithm::Run() */ |
4995 | | /************************************************************************/ |
4996 | | |
4997 | | bool GDALAlgorithm::Run(GDALProgressFunc pfnProgress, void *pProgressData) |
4998 | 0 | { |
4999 | 0 | WarnIfDeprecated(); |
5000 | |
|
5001 | 0 | if (m_selectedSubAlg) |
5002 | 0 | { |
5003 | 0 | if (m_calledFromCommandLine) |
5004 | 0 | m_selectedSubAlg->m_calledFromCommandLine = true; |
5005 | 0 | return m_selectedSubAlg->Run(pfnProgress, pProgressData); |
5006 | 0 | } |
5007 | | |
5008 | 0 | if (m_helpRequested || m_helpDocRequested) |
5009 | 0 | { |
5010 | 0 | if (m_calledFromCommandLine) |
5011 | 0 | printf("%s", GetUsageForCLI(false).c_str()); /*ok*/ |
5012 | 0 | return true; |
5013 | 0 | } |
5014 | | |
5015 | 0 | if (m_JSONUsageRequested) |
5016 | 0 | { |
5017 | 0 | if (m_calledFromCommandLine) |
5018 | 0 | printf("%s", GetUsageAsJSON().c_str()); /*ok*/ |
5019 | 0 | return true; |
5020 | 0 | } |
5021 | | |
5022 | 0 | if (!ValidateArguments()) |
5023 | 0 | return false; |
5024 | | |
5025 | 0 | switch (ProcessGDALGOutput()) |
5026 | 0 | { |
5027 | 0 | case ProcessGDALGOutputRet::GDALG_ERROR: |
5028 | 0 | return false; |
5029 | | |
5030 | 0 | case ProcessGDALGOutputRet::GDALG_OK: |
5031 | 0 | return true; |
5032 | | |
5033 | 0 | case ProcessGDALGOutputRet::NOT_GDALG: |
5034 | 0 | break; |
5035 | 0 | } |
5036 | | |
5037 | 0 | if (m_executionForStreamOutput) |
5038 | 0 | { |
5039 | 0 | if (!CheckSafeForStreamOutput()) |
5040 | 0 | { |
5041 | 0 | return false; |
5042 | 0 | } |
5043 | 0 | } |
5044 | | |
5045 | 0 | return RunImpl(pfnProgress, pProgressData); |
5046 | 0 | } |
5047 | | |
5048 | | /************************************************************************/ |
5049 | | /* GDALAlgorithm::CheckSafeForStreamOutput() */ |
5050 | | /************************************************************************/ |
5051 | | |
5052 | | bool GDALAlgorithm::CheckSafeForStreamOutput() |
5053 | 0 | { |
5054 | 0 | const auto outputFormatArg = GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT); |
5055 | 0 | if (outputFormatArg && outputFormatArg->GetType() == GAAT_STRING) |
5056 | 0 | { |
5057 | 0 | const auto &val = outputFormatArg->GDALAlgorithmArg::Get<std::string>(); |
5058 | 0 | if (!EQUAL(val.c_str(), "stream")) |
5059 | 0 | { |
5060 | | // For security reasons, to avoid that reading a .gdalg.json file |
5061 | | // writes a file on the file system. |
5062 | 0 | ReportError( |
5063 | 0 | CE_Failure, CPLE_NotSupported, |
5064 | 0 | "in streamed execution, --format stream should be used"); |
5065 | 0 | return false; |
5066 | 0 | } |
5067 | 0 | } |
5068 | 0 | return true; |
5069 | 0 | } |
5070 | | |
5071 | | /************************************************************************/ |
5072 | | /* GDALAlgorithm::Finalize() */ |
5073 | | /************************************************************************/ |
5074 | | |
5075 | | bool GDALAlgorithm::Finalize() |
5076 | 0 | { |
5077 | 0 | bool ret = true; |
5078 | 0 | if (m_selectedSubAlg) |
5079 | 0 | ret = m_selectedSubAlg->Finalize(); |
5080 | |
|
5081 | 0 | for (auto &arg : m_args) |
5082 | 0 | { |
5083 | 0 | if (arg->GetType() == GAAT_DATASET) |
5084 | 0 | { |
5085 | 0 | ret = arg->Get<GDALArgDatasetValue>().Close() && ret; |
5086 | 0 | } |
5087 | 0 | else if (arg->GetType() == GAAT_DATASET_LIST) |
5088 | 0 | { |
5089 | 0 | for (auto &ds : arg->Get<std::vector<GDALArgDatasetValue>>()) |
5090 | 0 | { |
5091 | 0 | ret = ds.Close() && ret; |
5092 | 0 | } |
5093 | 0 | } |
5094 | 0 | } |
5095 | 0 | return ret; |
5096 | 0 | } |
5097 | | |
5098 | | /************************************************************************/ |
5099 | | /* GDALAlgorithm::GetArgNamesForCLI() */ |
5100 | | /************************************************************************/ |
5101 | | |
5102 | | std::pair<std::vector<std::pair<GDALAlgorithmArg *, std::string>>, size_t> |
5103 | | GDALAlgorithm::GetArgNamesForCLI() const |
5104 | 0 | { |
5105 | 0 | std::vector<std::pair<GDALAlgorithmArg *, std::string>> options; |
5106 | |
|
5107 | 0 | size_t maxOptLen = 0; |
5108 | 0 | for (const auto &arg : m_args) |
5109 | 0 | { |
5110 | 0 | if (arg->IsHidden() || arg->IsHiddenForCLI()) |
5111 | 0 | continue; |
5112 | 0 | std::string opt; |
5113 | 0 | bool addComma = false; |
5114 | 0 | if (!arg->GetShortName().empty()) |
5115 | 0 | { |
5116 | 0 | opt += '-'; |
5117 | 0 | opt += arg->GetShortName(); |
5118 | 0 | addComma = true; |
5119 | 0 | } |
5120 | 0 | for (char alias : arg->GetShortNameAliases()) |
5121 | 0 | { |
5122 | 0 | if (addComma) |
5123 | 0 | opt += ", "; |
5124 | 0 | opt += "-"; |
5125 | 0 | opt += alias; |
5126 | 0 | addComma = true; |
5127 | 0 | } |
5128 | 0 | for (const std::string &alias : arg->GetAliases()) |
5129 | 0 | { |
5130 | 0 | if (addComma) |
5131 | 0 | opt += ", "; |
5132 | 0 | opt += "--"; |
5133 | 0 | opt += alias; |
5134 | 0 | addComma = true; |
5135 | 0 | } |
5136 | 0 | if (!arg->GetName().empty()) |
5137 | 0 | { |
5138 | 0 | if (addComma) |
5139 | 0 | opt += ", "; |
5140 | 0 | opt += "--"; |
5141 | 0 | opt += arg->GetName(); |
5142 | 0 | } |
5143 | 0 | const auto &metaVar = arg->GetMetaVar(); |
5144 | 0 | if (!metaVar.empty()) |
5145 | 0 | { |
5146 | 0 | opt += ' '; |
5147 | 0 | if (metaVar.front() != '<') |
5148 | 0 | opt += '<'; |
5149 | 0 | opt += metaVar; |
5150 | 0 | if (metaVar.back() != '>') |
5151 | 0 | opt += '>'; |
5152 | 0 | } |
5153 | 0 | maxOptLen = std::max(maxOptLen, opt.size()); |
5154 | 0 | options.emplace_back(arg.get(), opt); |
5155 | 0 | } |
5156 | |
|
5157 | 0 | return std::make_pair(std::move(options), maxOptLen); |
5158 | 0 | } |
5159 | | |
5160 | | /************************************************************************/ |
5161 | | /* GDALAlgorithm::GetUsageForCLI() */ |
5162 | | /************************************************************************/ |
5163 | | |
5164 | | std::string |
5165 | | GDALAlgorithm::GetUsageForCLI(bool shortUsage, |
5166 | | const UsageOptions &usageOptions) const |
5167 | 0 | { |
5168 | 0 | if (m_selectedSubAlg) |
5169 | 0 | return m_selectedSubAlg->GetUsageForCLI(shortUsage, usageOptions); |
5170 | | |
5171 | 0 | std::string osRet(usageOptions.isPipelineStep ? "*" : "Usage:"); |
5172 | 0 | std::string osPath; |
5173 | 0 | for (const std::string &s : m_callPath) |
5174 | 0 | { |
5175 | 0 | if (!osPath.empty()) |
5176 | 0 | osPath += ' '; |
5177 | 0 | osPath += s; |
5178 | 0 | } |
5179 | 0 | osRet += ' '; |
5180 | 0 | osRet += osPath; |
5181 | |
|
5182 | 0 | bool hasNonPositionals = false; |
5183 | 0 | for (const auto &arg : m_args) |
5184 | 0 | { |
5185 | 0 | if (!arg->IsHidden() && !arg->IsHiddenForCLI() && !arg->IsPositional()) |
5186 | 0 | hasNonPositionals = true; |
5187 | 0 | } |
5188 | |
|
5189 | 0 | if (HasSubAlgorithms()) |
5190 | 0 | { |
5191 | 0 | if (m_callPath.size() == 1) |
5192 | 0 | { |
5193 | 0 | osRet += " <COMMAND>"; |
5194 | 0 | if (hasNonPositionals) |
5195 | 0 | osRet += " [OPTIONS]"; |
5196 | 0 | osRet += "\nwhere <COMMAND> is one of:\n"; |
5197 | 0 | } |
5198 | 0 | else |
5199 | 0 | { |
5200 | 0 | osRet += " <SUBCOMMAND>"; |
5201 | 0 | if (hasNonPositionals) |
5202 | 0 | osRet += " [OPTIONS]"; |
5203 | 0 | osRet += "\nwhere <SUBCOMMAND> is one of:\n"; |
5204 | 0 | } |
5205 | 0 | size_t maxNameLen = 0; |
5206 | 0 | for (const auto &subAlgName : GetSubAlgorithmNames()) |
5207 | 0 | { |
5208 | 0 | maxNameLen = std::max(maxNameLen, subAlgName.size()); |
5209 | 0 | } |
5210 | 0 | for (const auto &subAlgName : GetSubAlgorithmNames()) |
5211 | 0 | { |
5212 | 0 | auto subAlg = InstantiateSubAlgorithm(subAlgName); |
5213 | 0 | if (subAlg && !subAlg->IsHidden()) |
5214 | 0 | { |
5215 | 0 | const std::string &name(subAlg->GetName()); |
5216 | 0 | osRet += " - "; |
5217 | 0 | osRet += name; |
5218 | 0 | osRet += ": "; |
5219 | 0 | osRet.append(maxNameLen - name.size(), ' '); |
5220 | 0 | osRet += subAlg->GetDescription(); |
5221 | 0 | if (!subAlg->m_aliases.empty()) |
5222 | 0 | { |
5223 | 0 | bool first = true; |
5224 | 0 | for (const auto &alias : subAlg->GetAliases()) |
5225 | 0 | { |
5226 | 0 | if (alias == |
5227 | 0 | GDALAlgorithmRegistry::HIDDEN_ALIAS_SEPARATOR) |
5228 | 0 | break; |
5229 | 0 | if (first) |
5230 | 0 | osRet += " (alias: "; |
5231 | 0 | else |
5232 | 0 | osRet += ", "; |
5233 | 0 | osRet += alias; |
5234 | 0 | first = false; |
5235 | 0 | } |
5236 | 0 | if (!first) |
5237 | 0 | { |
5238 | 0 | osRet += ')'; |
5239 | 0 | } |
5240 | 0 | } |
5241 | 0 | osRet += '\n'; |
5242 | 0 | } |
5243 | 0 | } |
5244 | |
|
5245 | 0 | if (shortUsage && hasNonPositionals) |
5246 | 0 | { |
5247 | 0 | osRet += "\nTry '"; |
5248 | 0 | osRet += osPath; |
5249 | 0 | osRet += " --help' for help.\n"; |
5250 | 0 | } |
5251 | 0 | } |
5252 | 0 | else |
5253 | 0 | { |
5254 | 0 | if (!m_args.empty()) |
5255 | 0 | { |
5256 | 0 | if (hasNonPositionals) |
5257 | 0 | osRet += " [OPTIONS]"; |
5258 | 0 | for (const auto *arg : m_positionalArgs) |
5259 | 0 | { |
5260 | 0 | const bool optional = |
5261 | 0 | (!arg->IsRequired() && !(GetName() == "pipeline" && |
5262 | 0 | arg->GetName() == "pipeline")); |
5263 | 0 | osRet += ' '; |
5264 | 0 | if (optional) |
5265 | 0 | osRet += '['; |
5266 | 0 | const std::string &metavar = arg->GetMetaVar(); |
5267 | 0 | if (!metavar.empty() && metavar[0] == '<') |
5268 | 0 | { |
5269 | 0 | osRet += metavar; |
5270 | 0 | } |
5271 | 0 | else |
5272 | 0 | { |
5273 | 0 | osRet += '<'; |
5274 | 0 | osRet += metavar; |
5275 | 0 | osRet += '>'; |
5276 | 0 | } |
5277 | 0 | if (arg->GetType() == GAAT_DATASET_LIST && |
5278 | 0 | arg->GetMaxCount() > 1) |
5279 | 0 | { |
5280 | 0 | osRet += "..."; |
5281 | 0 | } |
5282 | 0 | if (optional) |
5283 | 0 | osRet += ']'; |
5284 | 0 | } |
5285 | 0 | } |
5286 | |
|
5287 | 0 | const size_t nLenFirstLine = osRet.size(); |
5288 | 0 | osRet += '\n'; |
5289 | 0 | if (usageOptions.isPipelineStep) |
5290 | 0 | { |
5291 | 0 | osRet.append(nLenFirstLine, '-'); |
5292 | 0 | osRet += '\n'; |
5293 | 0 | } |
5294 | |
|
5295 | 0 | if (shortUsage) |
5296 | 0 | { |
5297 | 0 | osRet += "Try '"; |
5298 | 0 | osRet += osPath; |
5299 | 0 | osRet += " --help' for help.\n"; |
5300 | 0 | return osRet; |
5301 | 0 | } |
5302 | | |
5303 | 0 | osRet += '\n'; |
5304 | 0 | osRet += m_description; |
5305 | 0 | osRet += '\n'; |
5306 | 0 | } |
5307 | | |
5308 | 0 | if (!m_args.empty() && !shortUsage) |
5309 | 0 | { |
5310 | 0 | std::vector<std::pair<GDALAlgorithmArg *, std::string>> options; |
5311 | 0 | size_t maxOptLen; |
5312 | 0 | std::tie(options, maxOptLen) = GetArgNamesForCLI(); |
5313 | 0 | if (usageOptions.maxOptLen) |
5314 | 0 | maxOptLen = usageOptions.maxOptLen; |
5315 | |
|
5316 | 0 | const auto OutputArg = |
5317 | 0 | [this, maxOptLen, &osRet](const GDALAlgorithmArg *arg, |
5318 | 0 | const std::string &opt) |
5319 | 0 | { |
5320 | 0 | osRet += " "; |
5321 | 0 | osRet += opt; |
5322 | 0 | osRet += " "; |
5323 | 0 | osRet.append(maxOptLen - opt.size(), ' '); |
5324 | 0 | osRet += arg->GetDescription(); |
5325 | |
|
5326 | 0 | const auto &choices = arg->GetChoices(); |
5327 | 0 | if (!choices.empty()) |
5328 | 0 | { |
5329 | 0 | osRet += ". "; |
5330 | 0 | osRet += arg->GetMetaVar(); |
5331 | 0 | osRet += '='; |
5332 | 0 | bool firstChoice = true; |
5333 | 0 | for (const auto &choice : choices) |
5334 | 0 | { |
5335 | 0 | if (!firstChoice) |
5336 | 0 | osRet += '|'; |
5337 | 0 | osRet += choice; |
5338 | 0 | firstChoice = false; |
5339 | 0 | } |
5340 | 0 | } |
5341 | |
|
5342 | 0 | if (arg->GetType() == GAAT_DATASET || |
5343 | 0 | arg->GetType() == GAAT_DATASET_LIST) |
5344 | 0 | { |
5345 | 0 | if (arg->GetDatasetInputFlags() == GADV_NAME && |
5346 | 0 | arg->GetDatasetOutputFlags() == GADV_OBJECT) |
5347 | 0 | { |
5348 | 0 | osRet += " (created by algorithm)"; |
5349 | 0 | } |
5350 | 0 | } |
5351 | |
|
5352 | 0 | if (arg->GetType() == GAAT_STRING && arg->HasDefaultValue()) |
5353 | 0 | { |
5354 | 0 | osRet += " (default: "; |
5355 | 0 | osRet += arg->GetDefault<std::string>(); |
5356 | 0 | osRet += ')'; |
5357 | 0 | } |
5358 | 0 | else if (arg->GetType() == GAAT_BOOLEAN && arg->HasDefaultValue()) |
5359 | 0 | { |
5360 | 0 | if (arg->GetDefault<bool>()) |
5361 | 0 | osRet += " (default: true)"; |
5362 | 0 | } |
5363 | 0 | else if (arg->GetType() == GAAT_INTEGER && arg->HasDefaultValue()) |
5364 | 0 | { |
5365 | 0 | osRet += " (default: "; |
5366 | 0 | osRet += CPLSPrintf("%d", arg->GetDefault<int>()); |
5367 | 0 | osRet += ')'; |
5368 | 0 | } |
5369 | 0 | else if (arg->GetType() == GAAT_REAL && arg->HasDefaultValue()) |
5370 | 0 | { |
5371 | 0 | osRet += " (default: "; |
5372 | 0 | osRet += CPLSPrintf("%g", arg->GetDefault<double>()); |
5373 | 0 | osRet += ')'; |
5374 | 0 | } |
5375 | 0 | else if (arg->GetType() == GAAT_STRING_LIST && |
5376 | 0 | arg->HasDefaultValue()) |
5377 | 0 | { |
5378 | 0 | const auto &defaultVal = |
5379 | 0 | arg->GetDefault<std::vector<std::string>>(); |
5380 | 0 | if (defaultVal.size() == 1) |
5381 | 0 | { |
5382 | 0 | osRet += " (default: "; |
5383 | 0 | osRet += defaultVal[0]; |
5384 | 0 | osRet += ')'; |
5385 | 0 | } |
5386 | 0 | } |
5387 | 0 | else if (arg->GetType() == GAAT_INTEGER_LIST && |
5388 | 0 | arg->HasDefaultValue()) |
5389 | 0 | { |
5390 | 0 | const auto &defaultVal = arg->GetDefault<std::vector<int>>(); |
5391 | 0 | if (defaultVal.size() == 1) |
5392 | 0 | { |
5393 | 0 | osRet += " (default: "; |
5394 | 0 | osRet += CPLSPrintf("%d", defaultVal[0]); |
5395 | 0 | osRet += ')'; |
5396 | 0 | } |
5397 | 0 | } |
5398 | 0 | else if (arg->GetType() == GAAT_REAL_LIST && arg->HasDefaultValue()) |
5399 | 0 | { |
5400 | 0 | const auto &defaultVal = arg->GetDefault<std::vector<double>>(); |
5401 | 0 | if (defaultVal.size() == 1) |
5402 | 0 | { |
5403 | 0 | osRet += " (default: "; |
5404 | 0 | osRet += CPLSPrintf("%g", defaultVal[0]); |
5405 | 0 | osRet += ')'; |
5406 | 0 | } |
5407 | 0 | } |
5408 | |
|
5409 | 0 | if (arg->GetDisplayHintAboutRepetition()) |
5410 | 0 | { |
5411 | 0 | if (arg->GetMinCount() > 0 && |
5412 | 0 | arg->GetMinCount() == arg->GetMaxCount()) |
5413 | 0 | { |
5414 | 0 | if (arg->GetMinCount() != 1) |
5415 | 0 | osRet += CPLSPrintf(" [%d values]", arg->GetMaxCount()); |
5416 | 0 | } |
5417 | 0 | else if (arg->GetMinCount() > 0 && |
5418 | 0 | arg->GetMaxCount() < GDALAlgorithmArgDecl::UNBOUNDED) |
5419 | 0 | { |
5420 | 0 | osRet += CPLSPrintf(" [%d..%d values]", arg->GetMinCount(), |
5421 | 0 | arg->GetMaxCount()); |
5422 | 0 | } |
5423 | 0 | else if (arg->GetMinCount() > 0) |
5424 | 0 | { |
5425 | 0 | osRet += CPLSPrintf(" [%d.. values]", arg->GetMinCount()); |
5426 | 0 | } |
5427 | 0 | else if (arg->GetMaxCount() > 1) |
5428 | 0 | { |
5429 | 0 | osRet += " [may be repeated]"; |
5430 | 0 | } |
5431 | 0 | } |
5432 | |
|
5433 | 0 | if (arg->IsRequired()) |
5434 | 0 | { |
5435 | 0 | osRet += " [required]"; |
5436 | 0 | } |
5437 | |
|
5438 | 0 | osRet += '\n'; |
5439 | |
|
5440 | 0 | const auto &mutualExclusionGroup = arg->GetMutualExclusionGroup(); |
5441 | 0 | if (!mutualExclusionGroup.empty()) |
5442 | 0 | { |
5443 | 0 | std::string otherArgs; |
5444 | 0 | for (const auto &otherArg : m_args) |
5445 | 0 | { |
5446 | 0 | if (otherArg->IsHidden() || otherArg->IsHiddenForCLI() || |
5447 | 0 | otherArg.get() == arg) |
5448 | 0 | continue; |
5449 | 0 | if (otherArg->GetMutualExclusionGroup() == |
5450 | 0 | mutualExclusionGroup) |
5451 | 0 | { |
5452 | 0 | if (!otherArgs.empty()) |
5453 | 0 | otherArgs += ", "; |
5454 | 0 | otherArgs += "--"; |
5455 | 0 | otherArgs += otherArg->GetName(); |
5456 | 0 | } |
5457 | 0 | } |
5458 | 0 | if (!otherArgs.empty()) |
5459 | 0 | { |
5460 | 0 | osRet += " "; |
5461 | 0 | osRet += " "; |
5462 | 0 | osRet.append(maxOptLen, ' '); |
5463 | 0 | osRet += "Mutually exclusive with "; |
5464 | 0 | osRet += otherArgs; |
5465 | 0 | osRet += '\n'; |
5466 | 0 | } |
5467 | 0 | } |
5468 | 0 | }; |
5469 | |
|
5470 | 0 | if (!m_positionalArgs.empty()) |
5471 | 0 | { |
5472 | 0 | osRet += "\nPositional arguments:\n"; |
5473 | 0 | for (const auto &[arg, opt] : options) |
5474 | 0 | { |
5475 | 0 | if (arg->IsPositional()) |
5476 | 0 | OutputArg(arg, opt); |
5477 | 0 | } |
5478 | 0 | } |
5479 | |
|
5480 | 0 | if (hasNonPositionals) |
5481 | 0 | { |
5482 | 0 | bool hasCommon = false; |
5483 | 0 | bool hasBase = false; |
5484 | 0 | bool hasAdvanced = false; |
5485 | 0 | bool hasEsoteric = false; |
5486 | 0 | std::vector<std::string> categories; |
5487 | 0 | for (const auto &iter : options) |
5488 | 0 | { |
5489 | 0 | const auto &arg = iter.first; |
5490 | 0 | if (!arg->IsPositional()) |
5491 | 0 | { |
5492 | 0 | const auto &category = arg->GetCategory(); |
5493 | 0 | if (category == GAAC_COMMON) |
5494 | 0 | { |
5495 | 0 | hasCommon = true; |
5496 | 0 | } |
5497 | 0 | else if (category == GAAC_BASE) |
5498 | 0 | { |
5499 | 0 | hasBase = true; |
5500 | 0 | } |
5501 | 0 | else if (category == GAAC_ADVANCED) |
5502 | 0 | { |
5503 | 0 | hasAdvanced = true; |
5504 | 0 | } |
5505 | 0 | else if (category == GAAC_ESOTERIC) |
5506 | 0 | { |
5507 | 0 | hasEsoteric = true; |
5508 | 0 | } |
5509 | 0 | else if (std::find(categories.begin(), categories.end(), |
5510 | 0 | category) == categories.end()) |
5511 | 0 | { |
5512 | 0 | categories.push_back(category); |
5513 | 0 | } |
5514 | 0 | } |
5515 | 0 | } |
5516 | 0 | if (hasAdvanced) |
5517 | 0 | categories.insert(categories.begin(), GAAC_ADVANCED); |
5518 | 0 | if (hasBase) |
5519 | 0 | categories.insert(categories.begin(), GAAC_BASE); |
5520 | 0 | if (hasCommon && !usageOptions.isPipelineStep) |
5521 | 0 | categories.insert(categories.begin(), GAAC_COMMON); |
5522 | 0 | if (hasEsoteric) |
5523 | 0 | categories.push_back(GAAC_ESOTERIC); |
5524 | |
|
5525 | 0 | for (const auto &category : categories) |
5526 | 0 | { |
5527 | 0 | osRet += "\n"; |
5528 | 0 | if (category != GAAC_BASE) |
5529 | 0 | { |
5530 | 0 | osRet += category; |
5531 | 0 | osRet += ' '; |
5532 | 0 | } |
5533 | 0 | osRet += "Options:\n"; |
5534 | 0 | for (const auto &[arg, opt] : options) |
5535 | 0 | { |
5536 | 0 | if (!arg->IsPositional() && arg->GetCategory() == category) |
5537 | 0 | OutputArg(arg, opt); |
5538 | 0 | } |
5539 | 0 | } |
5540 | 0 | } |
5541 | 0 | } |
5542 | |
|
5543 | 0 | if (!m_longDescription.empty()) |
5544 | 0 | { |
5545 | 0 | osRet += '\n'; |
5546 | 0 | osRet += m_longDescription; |
5547 | 0 | osRet += '\n'; |
5548 | 0 | } |
5549 | |
|
5550 | 0 | if (!m_helpDocRequested && !usageOptions.isPipelineMain) |
5551 | 0 | { |
5552 | 0 | if (!m_helpURL.empty()) |
5553 | 0 | { |
5554 | 0 | osRet += "\nFor more details, consult "; |
5555 | 0 | osRet += GetHelpFullURL(); |
5556 | 0 | osRet += '\n'; |
5557 | 0 | } |
5558 | 0 | osRet += GetUsageForCLIEnd(); |
5559 | 0 | } |
5560 | |
|
5561 | 0 | return osRet; |
5562 | 0 | } |
5563 | | |
5564 | | /************************************************************************/ |
5565 | | /* GDALAlgorithm::GetUsageForCLIEnd() */ |
5566 | | /************************************************************************/ |
5567 | | |
5568 | | //! @cond Doxygen_Suppress |
5569 | | std::string GDALAlgorithm::GetUsageForCLIEnd() const |
5570 | 0 | { |
5571 | 0 | std::string osRet; |
5572 | |
|
5573 | 0 | if (!m_callPath.empty() && m_callPath[0] == "gdal") |
5574 | 0 | { |
5575 | 0 | osRet += "\nWARNING: the gdal command is provisionally provided as an " |
5576 | 0 | "alternative interface to GDAL and OGR command line " |
5577 | 0 | "utilities.\nThe project reserves the right to modify, " |
5578 | 0 | "rename, reorganize, and change the behavior of the utility\n" |
5579 | 0 | "until it is officially frozen in a future feature release of " |
5580 | 0 | "GDAL.\n"; |
5581 | 0 | } |
5582 | 0 | return osRet; |
5583 | 0 | } |
5584 | | |
5585 | | //! @endcond |
5586 | | |
5587 | | /************************************************************************/ |
5588 | | /* GDALAlgorithm::GetUsageAsJSON() */ |
5589 | | /************************************************************************/ |
5590 | | |
5591 | | std::string GDALAlgorithm::GetUsageAsJSON() const |
5592 | 0 | { |
5593 | 0 | CPLJSONDocument oDoc; |
5594 | 0 | auto oRoot = oDoc.GetRoot(); |
5595 | |
|
5596 | 0 | if (m_displayInJSONUsage) |
5597 | 0 | { |
5598 | 0 | oRoot.Add("name", m_name); |
5599 | 0 | CPLJSONArray jFullPath; |
5600 | 0 | for (const std::string &s : m_callPath) |
5601 | 0 | { |
5602 | 0 | jFullPath.Add(s); |
5603 | 0 | } |
5604 | 0 | oRoot.Add("full_path", jFullPath); |
5605 | 0 | } |
5606 | |
|
5607 | 0 | oRoot.Add("description", m_description); |
5608 | 0 | if (!m_helpURL.empty()) |
5609 | 0 | { |
5610 | 0 | oRoot.Add("short_url", m_helpURL); |
5611 | 0 | oRoot.Add("url", GetHelpFullURL()); |
5612 | 0 | } |
5613 | |
|
5614 | 0 | CPLJSONArray jSubAlgorithms; |
5615 | 0 | for (const auto &subAlgName : GetSubAlgorithmNames()) |
5616 | 0 | { |
5617 | 0 | auto subAlg = InstantiateSubAlgorithm(subAlgName); |
5618 | 0 | if (subAlg && subAlg->m_displayInJSONUsage && !subAlg->IsHidden()) |
5619 | 0 | { |
5620 | 0 | CPLJSONDocument oSubDoc; |
5621 | 0 | CPL_IGNORE_RET_VAL(oSubDoc.LoadMemory(subAlg->GetUsageAsJSON())); |
5622 | 0 | jSubAlgorithms.Add(oSubDoc.GetRoot()); |
5623 | 0 | } |
5624 | 0 | } |
5625 | 0 | oRoot.Add("sub_algorithms", jSubAlgorithms); |
5626 | |
|
5627 | 0 | const auto ProcessArg = [](const GDALAlgorithmArg *arg) |
5628 | 0 | { |
5629 | 0 | CPLJSONObject jArg; |
5630 | 0 | jArg.Add("name", arg->GetName()); |
5631 | 0 | jArg.Add("type", GDALAlgorithmArgTypeName(arg->GetType())); |
5632 | 0 | jArg.Add("description", arg->GetDescription()); |
5633 | |
|
5634 | 0 | const auto &metaVar = arg->GetMetaVar(); |
5635 | 0 | if (!metaVar.empty() && metaVar != CPLString(arg->GetName()).toupper()) |
5636 | 0 | { |
5637 | 0 | if (metaVar.front() == '<' && metaVar.back() == '>' && |
5638 | 0 | metaVar.substr(1, metaVar.size() - 2).find('>') == |
5639 | 0 | std::string::npos) |
5640 | 0 | jArg.Add("metavar", metaVar.substr(1, metaVar.size() - 2)); |
5641 | 0 | else |
5642 | 0 | jArg.Add("metavar", metaVar); |
5643 | 0 | } |
5644 | |
|
5645 | 0 | const auto &choices = arg->GetChoices(); |
5646 | 0 | if (!choices.empty()) |
5647 | 0 | { |
5648 | 0 | CPLJSONArray jChoices; |
5649 | 0 | for (const auto &choice : choices) |
5650 | 0 | jChoices.Add(choice); |
5651 | 0 | jArg.Add("choices", jChoices); |
5652 | 0 | } |
5653 | 0 | if (arg->HasDefaultValue()) |
5654 | 0 | { |
5655 | 0 | switch (arg->GetType()) |
5656 | 0 | { |
5657 | 0 | case GAAT_BOOLEAN: |
5658 | 0 | jArg.Add("default", arg->GetDefault<bool>()); |
5659 | 0 | break; |
5660 | 0 | case GAAT_STRING: |
5661 | 0 | jArg.Add("default", arg->GetDefault<std::string>()); |
5662 | 0 | break; |
5663 | 0 | case GAAT_INTEGER: |
5664 | 0 | jArg.Add("default", arg->GetDefault<int>()); |
5665 | 0 | break; |
5666 | 0 | case GAAT_REAL: |
5667 | 0 | jArg.Add("default", arg->GetDefault<double>()); |
5668 | 0 | break; |
5669 | 0 | case GAAT_STRING_LIST: |
5670 | 0 | { |
5671 | 0 | const auto &val = |
5672 | 0 | arg->GetDefault<std::vector<std::string>>(); |
5673 | 0 | if (val.size() == 1) |
5674 | 0 | { |
5675 | 0 | jArg.Add("default", val[0]); |
5676 | 0 | } |
5677 | 0 | else |
5678 | 0 | { |
5679 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
5680 | 0 | "Unhandled default value for arg %s", |
5681 | 0 | arg->GetName().c_str()); |
5682 | 0 | } |
5683 | 0 | break; |
5684 | 0 | } |
5685 | 0 | case GAAT_INTEGER_LIST: |
5686 | 0 | { |
5687 | 0 | const auto &val = arg->GetDefault<std::vector<int>>(); |
5688 | 0 | if (val.size() == 1) |
5689 | 0 | { |
5690 | 0 | jArg.Add("default", val[0]); |
5691 | 0 | } |
5692 | 0 | else |
5693 | 0 | { |
5694 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
5695 | 0 | "Unhandled default value for arg %s", |
5696 | 0 | arg->GetName().c_str()); |
5697 | 0 | } |
5698 | 0 | break; |
5699 | 0 | } |
5700 | 0 | case GAAT_REAL_LIST: |
5701 | 0 | { |
5702 | 0 | const auto &val = arg->GetDefault<std::vector<double>>(); |
5703 | 0 | if (val.size() == 1) |
5704 | 0 | { |
5705 | 0 | jArg.Add("default", val[0]); |
5706 | 0 | } |
5707 | 0 | else |
5708 | 0 | { |
5709 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
5710 | 0 | "Unhandled default value for arg %s", |
5711 | 0 | arg->GetName().c_str()); |
5712 | 0 | } |
5713 | 0 | break; |
5714 | 0 | } |
5715 | 0 | case GAAT_DATASET: |
5716 | 0 | case GAAT_DATASET_LIST: |
5717 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
5718 | 0 | "Unhandled default value for arg %s", |
5719 | 0 | arg->GetName().c_str()); |
5720 | 0 | break; |
5721 | 0 | } |
5722 | 0 | } |
5723 | | |
5724 | 0 | const auto [minVal, minValIsIncluded] = arg->GetMinValue(); |
5725 | 0 | if (!std::isnan(minVal)) |
5726 | 0 | { |
5727 | 0 | if (arg->GetType() == GAAT_INTEGER || |
5728 | 0 | arg->GetType() == GAAT_INTEGER_LIST) |
5729 | 0 | jArg.Add("min_value", static_cast<int>(minVal)); |
5730 | 0 | else |
5731 | 0 | jArg.Add("min_value", minVal); |
5732 | 0 | jArg.Add("min_value_is_included", minValIsIncluded); |
5733 | 0 | } |
5734 | |
|
5735 | 0 | const auto [maxVal, maxValIsIncluded] = arg->GetMaxValue(); |
5736 | 0 | if (!std::isnan(maxVal)) |
5737 | 0 | { |
5738 | 0 | if (arg->GetType() == GAAT_INTEGER || |
5739 | 0 | arg->GetType() == GAAT_INTEGER_LIST) |
5740 | 0 | jArg.Add("max_value", static_cast<int>(maxVal)); |
5741 | 0 | else |
5742 | 0 | jArg.Add("max_value", maxVal); |
5743 | 0 | jArg.Add("max_value_is_included", maxValIsIncluded); |
5744 | 0 | } |
5745 | |
|
5746 | 0 | jArg.Add("required", arg->IsRequired()); |
5747 | 0 | if (GDALAlgorithmArgTypeIsList(arg->GetType())) |
5748 | 0 | { |
5749 | 0 | jArg.Add("packed_values_allowed", arg->GetPackedValuesAllowed()); |
5750 | 0 | jArg.Add("repeated_arg_allowed", arg->GetRepeatedArgAllowed()); |
5751 | 0 | jArg.Add("min_count", arg->GetMinCount()); |
5752 | 0 | jArg.Add("max_count", arg->GetMaxCount()); |
5753 | 0 | } |
5754 | 0 | jArg.Add("category", arg->GetCategory()); |
5755 | |
|
5756 | 0 | if (arg->GetType() == GAAT_DATASET || |
5757 | 0 | arg->GetType() == GAAT_DATASET_LIST) |
5758 | 0 | { |
5759 | 0 | { |
5760 | 0 | CPLJSONArray jAr; |
5761 | 0 | if (arg->GetDatasetType() & GDAL_OF_RASTER) |
5762 | 0 | jAr.Add("raster"); |
5763 | 0 | if (arg->GetDatasetType() & GDAL_OF_VECTOR) |
5764 | 0 | jAr.Add("vector"); |
5765 | 0 | if (arg->GetDatasetType() & GDAL_OF_MULTIDIM_RASTER) |
5766 | 0 | jAr.Add("multidim_raster"); |
5767 | 0 | jArg.Add("dataset_type", jAr); |
5768 | 0 | } |
5769 | |
|
5770 | 0 | const auto GetFlags = [](int flags) |
5771 | 0 | { |
5772 | 0 | CPLJSONArray jAr; |
5773 | 0 | if (flags & GADV_NAME) |
5774 | 0 | jAr.Add("name"); |
5775 | 0 | if (flags & GADV_OBJECT) |
5776 | 0 | jAr.Add("dataset"); |
5777 | 0 | return jAr; |
5778 | 0 | }; |
5779 | |
|
5780 | 0 | if (arg->IsInput()) |
5781 | 0 | { |
5782 | 0 | jArg.Add("input_flags", GetFlags(arg->GetDatasetInputFlags())); |
5783 | 0 | } |
5784 | 0 | if (arg->IsOutput()) |
5785 | 0 | { |
5786 | 0 | jArg.Add("output_flags", |
5787 | 0 | GetFlags(arg->GetDatasetOutputFlags())); |
5788 | 0 | } |
5789 | 0 | } |
5790 | |
|
5791 | 0 | const auto &mutualExclusionGroup = arg->GetMutualExclusionGroup(); |
5792 | 0 | if (!mutualExclusionGroup.empty()) |
5793 | 0 | { |
5794 | 0 | jArg.Add("mutual_exclusion_group", mutualExclusionGroup); |
5795 | 0 | } |
5796 | |
|
5797 | 0 | const auto &metadata = arg->GetMetadata(); |
5798 | 0 | if (!metadata.empty()) |
5799 | 0 | { |
5800 | 0 | CPLJSONObject jMetadata; |
5801 | 0 | for (const auto &[key, values] : metadata) |
5802 | 0 | { |
5803 | 0 | CPLJSONArray jValue; |
5804 | 0 | for (const auto &value : values) |
5805 | 0 | jValue.Add(value); |
5806 | 0 | jMetadata.Add(key, jValue); |
5807 | 0 | } |
5808 | 0 | jArg.Add("metadata", jMetadata); |
5809 | 0 | } |
5810 | |
|
5811 | 0 | return jArg; |
5812 | 0 | }; |
5813 | |
|
5814 | 0 | { |
5815 | 0 | CPLJSONArray jArgs; |
5816 | 0 | for (const auto &arg : m_args) |
5817 | 0 | { |
5818 | 0 | if (!arg->IsHidden() && !arg->IsOnlyForCLI() && arg->IsInput() && |
5819 | 0 | !arg->IsOutput()) |
5820 | 0 | jArgs.Add(ProcessArg(arg.get())); |
5821 | 0 | } |
5822 | 0 | oRoot.Add("input_arguments", jArgs); |
5823 | 0 | } |
5824 | |
|
5825 | 0 | { |
5826 | 0 | CPLJSONArray jArgs; |
5827 | 0 | for (const auto &arg : m_args) |
5828 | 0 | { |
5829 | 0 | if (!arg->IsHidden() && !arg->IsOnlyForCLI() && !arg->IsInput() && |
5830 | 0 | arg->IsOutput()) |
5831 | 0 | jArgs.Add(ProcessArg(arg.get())); |
5832 | 0 | } |
5833 | 0 | oRoot.Add("output_arguments", jArgs); |
5834 | 0 | } |
5835 | |
|
5836 | 0 | { |
5837 | 0 | CPLJSONArray jArgs; |
5838 | 0 | for (const auto &arg : m_args) |
5839 | 0 | { |
5840 | 0 | if (!arg->IsHidden() && !arg->IsOnlyForCLI() && arg->IsInput() && |
5841 | 0 | arg->IsOutput()) |
5842 | 0 | jArgs.Add(ProcessArg(arg.get())); |
5843 | 0 | } |
5844 | 0 | oRoot.Add("input_output_arguments", jArgs); |
5845 | 0 | } |
5846 | |
|
5847 | 0 | if (m_supportsStreamedOutput) |
5848 | 0 | { |
5849 | 0 | oRoot.Add("supports_streamed_output", true); |
5850 | 0 | } |
5851 | |
|
5852 | 0 | return oDoc.SaveAsString(); |
5853 | 0 | } |
5854 | | |
5855 | | /************************************************************************/ |
5856 | | /* GDALAlgorithm::GetAutoComplete() */ |
5857 | | /************************************************************************/ |
5858 | | |
5859 | | std::vector<std::string> |
5860 | | GDALAlgorithm::GetAutoComplete(std::vector<std::string> &args, |
5861 | | bool lastWordIsComplete, bool showAllOptions) |
5862 | 0 | { |
5863 | 0 | std::vector<std::string> ret; |
5864 | | |
5865 | | // Get inner-most algorithm |
5866 | 0 | std::unique_ptr<GDALAlgorithm> curAlgHolder; |
5867 | 0 | GDALAlgorithm *curAlg = this; |
5868 | 0 | while (!args.empty() && !args.front().empty() && args.front()[0] != '-') |
5869 | 0 | { |
5870 | 0 | auto subAlg = curAlg->InstantiateSubAlgorithm( |
5871 | 0 | args.front(), /* suggestionAllowed = */ false); |
5872 | 0 | if (!subAlg) |
5873 | 0 | break; |
5874 | 0 | if (args.size() == 1 && !lastWordIsComplete) |
5875 | 0 | { |
5876 | 0 | int nCount = 0; |
5877 | 0 | for (const auto &subAlgName : curAlg->GetSubAlgorithmNames()) |
5878 | 0 | { |
5879 | 0 | if (STARTS_WITH(subAlgName.c_str(), args.front().c_str())) |
5880 | 0 | nCount++; |
5881 | 0 | } |
5882 | 0 | if (nCount >= 2) |
5883 | 0 | { |
5884 | 0 | for (const std::string &subAlgName : |
5885 | 0 | curAlg->GetSubAlgorithmNames()) |
5886 | 0 | { |
5887 | 0 | subAlg = curAlg->InstantiateSubAlgorithm(subAlgName); |
5888 | 0 | if (subAlg && !subAlg->IsHidden()) |
5889 | 0 | ret.push_back(subAlg->GetName()); |
5890 | 0 | } |
5891 | 0 | return ret; |
5892 | 0 | } |
5893 | 0 | } |
5894 | 0 | showAllOptions = false; |
5895 | 0 | args.erase(args.begin()); |
5896 | 0 | curAlgHolder = std::move(subAlg); |
5897 | 0 | curAlg = curAlgHolder.get(); |
5898 | 0 | } |
5899 | 0 | if (curAlg != this) |
5900 | 0 | { |
5901 | 0 | return curAlg->GetAutoComplete(args, lastWordIsComplete, |
5902 | 0 | /* showAllOptions = */ false); |
5903 | 0 | } |
5904 | | |
5905 | 0 | std::string option; |
5906 | 0 | std::string value; |
5907 | 0 | ExtractLastOptionAndValue(args, option, value); |
5908 | |
|
5909 | 0 | if (option.empty() && !args.empty() && !args.back().empty() && |
5910 | 0 | args.back()[0] == '-') |
5911 | 0 | { |
5912 | 0 | const auto &lastArg = args.back(); |
5913 | | // List available options |
5914 | 0 | for (const auto &arg : GetArgs()) |
5915 | 0 | { |
5916 | 0 | if (arg->IsHidden() || arg->IsHiddenForCLI() || |
5917 | 0 | (!showAllOptions && |
5918 | 0 | (arg->GetName() == "help" || arg->GetName() == "config" || |
5919 | 0 | arg->GetName() == "version" || |
5920 | 0 | arg->GetName() == "json-usage"))) |
5921 | 0 | { |
5922 | 0 | continue; |
5923 | 0 | } |
5924 | 0 | if (!arg->GetShortName().empty()) |
5925 | 0 | { |
5926 | 0 | std::string str = std::string("-").append(arg->GetShortName()); |
5927 | 0 | if (lastArg == str) |
5928 | 0 | ret.push_back(std::move(str)); |
5929 | 0 | } |
5930 | 0 | if (lastArg != "-" && lastArg != "--") |
5931 | 0 | { |
5932 | 0 | for (const std::string &alias : arg->GetAliases()) |
5933 | 0 | { |
5934 | 0 | std::string str = std::string("--").append(alias); |
5935 | 0 | if (cpl::starts_with(str, lastArg)) |
5936 | 0 | ret.push_back(std::move(str)); |
5937 | 0 | } |
5938 | 0 | } |
5939 | 0 | if (!arg->GetName().empty()) |
5940 | 0 | { |
5941 | 0 | std::string str = std::string("--").append(arg->GetName()); |
5942 | 0 | if (cpl::starts_with(str, lastArg)) |
5943 | 0 | ret.push_back(std::move(str)); |
5944 | 0 | } |
5945 | 0 | } |
5946 | 0 | std::sort(ret.begin(), ret.end()); |
5947 | 0 | } |
5948 | 0 | else if (!option.empty()) |
5949 | 0 | { |
5950 | | // List possible choices for current option |
5951 | 0 | auto arg = GetArg(option); |
5952 | 0 | if (arg && arg->GetType() != GAAT_BOOLEAN) |
5953 | 0 | { |
5954 | 0 | ret = arg->GetChoices(); |
5955 | 0 | if (ret.empty()) |
5956 | 0 | { |
5957 | 0 | { |
5958 | 0 | CPLErrorStateBackuper oErrorQuieter(CPLQuietErrorHandler); |
5959 | 0 | SetParseForAutoCompletion(); |
5960 | 0 | CPL_IGNORE_RET_VAL(ParseCommandLineArguments(args)); |
5961 | 0 | } |
5962 | 0 | ret = arg->GetAutoCompleteChoices(value); |
5963 | 0 | } |
5964 | 0 | else |
5965 | 0 | { |
5966 | 0 | std::sort(ret.begin(), ret.end()); |
5967 | 0 | } |
5968 | 0 | if (!ret.empty() && ret.back() == value) |
5969 | 0 | { |
5970 | 0 | ret.clear(); |
5971 | 0 | } |
5972 | 0 | else if (ret.empty()) |
5973 | 0 | { |
5974 | 0 | ret.push_back("**"); |
5975 | | // Non printable UTF-8 space, to avoid autocompletion to pickup on 'd' |
5976 | 0 | ret.push_back(std::string("\xC2\xA0" |
5977 | 0 | "description: ") |
5978 | 0 | .append(arg->GetDescription())); |
5979 | 0 | } |
5980 | 0 | } |
5981 | 0 | } |
5982 | 0 | else |
5983 | 0 | { |
5984 | | // List possible sub-algorithms |
5985 | 0 | for (const std::string &subAlgName : GetSubAlgorithmNames()) |
5986 | 0 | { |
5987 | 0 | auto subAlg = InstantiateSubAlgorithm(subAlgName); |
5988 | 0 | if (subAlg && !subAlg->IsHidden()) |
5989 | 0 | ret.push_back(subAlg->GetName()); |
5990 | 0 | } |
5991 | 0 | if (!ret.empty()) |
5992 | 0 | { |
5993 | 0 | std::sort(ret.begin(), ret.end()); |
5994 | 0 | } |
5995 | | |
5996 | | // Try filenames |
5997 | 0 | if (ret.empty() && !args.empty()) |
5998 | 0 | { |
5999 | 0 | { |
6000 | 0 | CPLErrorStateBackuper oErrorQuieter(CPLQuietErrorHandler); |
6001 | 0 | SetParseForAutoCompletion(); |
6002 | 0 | CPL_IGNORE_RET_VAL(ParseCommandLineArguments(args)); |
6003 | 0 | } |
6004 | |
|
6005 | 0 | const std::string &lastArg = args.back(); |
6006 | 0 | GDALAlgorithmArg *arg = nullptr; |
6007 | 0 | for (const char *name : {GDAL_ARG_NAME_INPUT, "dataset", "filename", |
6008 | 0 | "like", "source", "destination"}) |
6009 | 0 | { |
6010 | 0 | if (!arg) |
6011 | 0 | { |
6012 | 0 | auto newArg = GetArg(name); |
6013 | 0 | if (newArg) |
6014 | 0 | { |
6015 | 0 | if (!newArg->IsExplicitlySet()) |
6016 | 0 | { |
6017 | 0 | arg = newArg; |
6018 | 0 | } |
6019 | 0 | else if (newArg->GetType() == GAAT_STRING || |
6020 | 0 | newArg->GetType() == GAAT_STRING_LIST || |
6021 | 0 | newArg->GetType() == GAAT_DATASET || |
6022 | 0 | newArg->GetType() == GAAT_DATASET_LIST) |
6023 | 0 | { |
6024 | 0 | VSIStatBufL sStat; |
6025 | 0 | if ((!lastArg.empty() && lastArg.back() == '/') || |
6026 | 0 | VSIStatL(lastArg.c_str(), &sStat) != 0) |
6027 | 0 | { |
6028 | 0 | arg = newArg; |
6029 | 0 | } |
6030 | 0 | } |
6031 | 0 | } |
6032 | 0 | } |
6033 | 0 | } |
6034 | 0 | if (arg) |
6035 | 0 | { |
6036 | 0 | ret = arg->GetAutoCompleteChoices(lastArg); |
6037 | 0 | } |
6038 | 0 | } |
6039 | 0 | } |
6040 | |
|
6041 | 0 | return ret; |
6042 | 0 | } |
6043 | | |
6044 | | /************************************************************************/ |
6045 | | /* GDALAlgorithm::ExtractLastOptionAndValue() */ |
6046 | | /************************************************************************/ |
6047 | | |
6048 | | void GDALAlgorithm::ExtractLastOptionAndValue(std::vector<std::string> &args, |
6049 | | std::string &option, |
6050 | | std::string &value) const |
6051 | 0 | { |
6052 | 0 | if (!args.empty() && !args.back().empty() && args.back()[0] == '-') |
6053 | 0 | { |
6054 | 0 | const auto nPosEqual = args.back().find('='); |
6055 | 0 | if (nPosEqual == std::string::npos) |
6056 | 0 | { |
6057 | | // Deal with "gdal ... --option" |
6058 | 0 | if (GetArg(args.back())) |
6059 | 0 | { |
6060 | 0 | option = args.back(); |
6061 | 0 | args.pop_back(); |
6062 | 0 | } |
6063 | 0 | } |
6064 | 0 | else |
6065 | 0 | { |
6066 | | // Deal with "gdal ... --option=<value>" |
6067 | 0 | if (GetArg(args.back().substr(0, nPosEqual))) |
6068 | 0 | { |
6069 | 0 | option = args.back().substr(0, nPosEqual); |
6070 | 0 | value = args.back().substr(nPosEqual + 1); |
6071 | 0 | args.pop_back(); |
6072 | 0 | } |
6073 | 0 | } |
6074 | 0 | } |
6075 | 0 | else if (args.size() >= 2 && !args[args.size() - 2].empty() && |
6076 | 0 | args[args.size() - 2][0] == '-') |
6077 | 0 | { |
6078 | | // Deal with "gdal ... --option <value>" |
6079 | 0 | auto arg = GetArg(args[args.size() - 2]); |
6080 | 0 | if (arg && arg->GetType() != GAAT_BOOLEAN) |
6081 | 0 | { |
6082 | 0 | option = args[args.size() - 2]; |
6083 | 0 | value = args.back(); |
6084 | 0 | args.pop_back(); |
6085 | 0 | } |
6086 | 0 | } |
6087 | |
|
6088 | 0 | const auto IsKeyValueOption = [](const std::string &osStr) |
6089 | 0 | { |
6090 | 0 | return osStr == "--co" || osStr == "--creation-option" || |
6091 | 0 | osStr == "--lco" || osStr == "--layer-creation-option" || |
6092 | 0 | osStr == "--oo" || osStr == "--open-option"; |
6093 | 0 | }; |
6094 | |
|
6095 | 0 | if (IsKeyValueOption(option)) |
6096 | 0 | { |
6097 | 0 | const auto nPosEqual = value.find('='); |
6098 | 0 | if (nPosEqual != std::string::npos) |
6099 | 0 | { |
6100 | 0 | value.resize(nPosEqual); |
6101 | 0 | } |
6102 | 0 | } |
6103 | 0 | } |
6104 | | |
6105 | | //! @cond Doxygen_Suppress |
6106 | | |
6107 | | /************************************************************************/ |
6108 | | /* GDALContainerAlgorithm::RunImpl() */ |
6109 | | /************************************************************************/ |
6110 | | |
6111 | | bool GDALContainerAlgorithm::RunImpl(GDALProgressFunc, void *) |
6112 | 0 | { |
6113 | 0 | return false; |
6114 | 0 | } |
6115 | | |
6116 | | //! @endcond |
6117 | | |
6118 | | /************************************************************************/ |
6119 | | /* GDALAlgorithmRelease() */ |
6120 | | /************************************************************************/ |
6121 | | |
6122 | | /** Release a handle to an algorithm. |
6123 | | * |
6124 | | * @since 3.11 |
6125 | | */ |
6126 | | void GDALAlgorithmRelease(GDALAlgorithmH hAlg) |
6127 | 0 | { |
6128 | 0 | delete hAlg; |
6129 | 0 | } |
6130 | | |
6131 | | /************************************************************************/ |
6132 | | /* GDALAlgorithmGetName() */ |
6133 | | /************************************************************************/ |
6134 | | |
6135 | | /** Return the algorithm name. |
6136 | | * |
6137 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6138 | | * @return algorithm name whose lifetime is bound to hAlg and which must not |
6139 | | * be freed. |
6140 | | * @since 3.11 |
6141 | | */ |
6142 | | const char *GDALAlgorithmGetName(GDALAlgorithmH hAlg) |
6143 | 0 | { |
6144 | 0 | VALIDATE_POINTER1(hAlg, __func__, nullptr); |
6145 | 0 | return hAlg->ptr->GetName().c_str(); |
6146 | 0 | } |
6147 | | |
6148 | | /************************************************************************/ |
6149 | | /* GDALAlgorithmGetDescription() */ |
6150 | | /************************************************************************/ |
6151 | | |
6152 | | /** Return the algorithm (short) description. |
6153 | | * |
6154 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6155 | | * @return algorithm description whose lifetime is bound to hAlg and which must |
6156 | | * not be freed. |
6157 | | * @since 3.11 |
6158 | | */ |
6159 | | const char *GDALAlgorithmGetDescription(GDALAlgorithmH hAlg) |
6160 | 0 | { |
6161 | 0 | VALIDATE_POINTER1(hAlg, __func__, nullptr); |
6162 | 0 | return hAlg->ptr->GetDescription().c_str(); |
6163 | 0 | } |
6164 | | |
6165 | | /************************************************************************/ |
6166 | | /* GDALAlgorithmGetLongDescription() */ |
6167 | | /************************************************************************/ |
6168 | | |
6169 | | /** Return the algorithm (longer) description. |
6170 | | * |
6171 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6172 | | * @return algorithm description whose lifetime is bound to hAlg and which must |
6173 | | * not be freed. |
6174 | | * @since 3.11 |
6175 | | */ |
6176 | | const char *GDALAlgorithmGetLongDescription(GDALAlgorithmH hAlg) |
6177 | 0 | { |
6178 | 0 | VALIDATE_POINTER1(hAlg, __func__, nullptr); |
6179 | 0 | return hAlg->ptr->GetLongDescription().c_str(); |
6180 | 0 | } |
6181 | | |
6182 | | /************************************************************************/ |
6183 | | /* GDALAlgorithmGetHelpFullURL() */ |
6184 | | /************************************************************************/ |
6185 | | |
6186 | | /** Return the algorithm full URL. |
6187 | | * |
6188 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6189 | | * @return algorithm URL whose lifetime is bound to hAlg and which must |
6190 | | * not be freed. |
6191 | | * @since 3.11 |
6192 | | */ |
6193 | | const char *GDALAlgorithmGetHelpFullURL(GDALAlgorithmH hAlg) |
6194 | 0 | { |
6195 | 0 | VALIDATE_POINTER1(hAlg, __func__, nullptr); |
6196 | 0 | return hAlg->ptr->GetHelpFullURL().c_str(); |
6197 | 0 | } |
6198 | | |
6199 | | /************************************************************************/ |
6200 | | /* GDALAlgorithmHasSubAlgorithms() */ |
6201 | | /************************************************************************/ |
6202 | | |
6203 | | /** Return whether the algorithm has sub-algorithms. |
6204 | | * |
6205 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6206 | | * @since 3.11 |
6207 | | */ |
6208 | | bool GDALAlgorithmHasSubAlgorithms(GDALAlgorithmH hAlg) |
6209 | 0 | { |
6210 | 0 | VALIDATE_POINTER1(hAlg, __func__, false); |
6211 | 0 | return hAlg->ptr->HasSubAlgorithms(); |
6212 | 0 | } |
6213 | | |
6214 | | /************************************************************************/ |
6215 | | /* GDALAlgorithmGetSubAlgorithmNames() */ |
6216 | | /************************************************************************/ |
6217 | | |
6218 | | /** Get the names of registered algorithms. |
6219 | | * |
6220 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6221 | | * @return a NULL terminated list of names, which must be destroyed with |
6222 | | * CSLDestroy() |
6223 | | * @since 3.11 |
6224 | | */ |
6225 | | char **GDALAlgorithmGetSubAlgorithmNames(GDALAlgorithmH hAlg) |
6226 | 0 | { |
6227 | 0 | VALIDATE_POINTER1(hAlg, __func__, nullptr); |
6228 | 0 | return CPLStringList(hAlg->ptr->GetSubAlgorithmNames()).StealList(); |
6229 | 0 | } |
6230 | | |
6231 | | /************************************************************************/ |
6232 | | /* GDALAlgorithmInstantiateSubAlgorithm() */ |
6233 | | /************************************************************************/ |
6234 | | |
6235 | | /** Instantiate an algorithm by its name (or its alias). |
6236 | | * |
6237 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6238 | | * @param pszSubAlgName Algorithm name. Must NOT be null. |
6239 | | * @return an handle to the algorithm (to be freed with GDALAlgorithmRelease), |
6240 | | * or NULL if the algorithm does not exist or another error occurred. |
6241 | | * @since 3.11 |
6242 | | */ |
6243 | | GDALAlgorithmH GDALAlgorithmInstantiateSubAlgorithm(GDALAlgorithmH hAlg, |
6244 | | const char *pszSubAlgName) |
6245 | 0 | { |
6246 | 0 | VALIDATE_POINTER1(hAlg, __func__, nullptr); |
6247 | 0 | VALIDATE_POINTER1(pszSubAlgName, __func__, nullptr); |
6248 | 0 | auto subAlg = hAlg->ptr->InstantiateSubAlgorithm(pszSubAlgName); |
6249 | 0 | return subAlg |
6250 | 0 | ? std::make_unique<GDALAlgorithmHS>(std::move(subAlg)).release() |
6251 | 0 | : nullptr; |
6252 | 0 | } |
6253 | | |
6254 | | /************************************************************************/ |
6255 | | /* GDALAlgorithmParseCommandLineArguments() */ |
6256 | | /************************************************************************/ |
6257 | | |
6258 | | /** Parse a command line argument, which does not include the algorithm |
6259 | | * name, to set the value of corresponding arguments. |
6260 | | * |
6261 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6262 | | * @param papszArgs NULL-terminated list of arguments, not including the algorithm name. |
6263 | | * @return true if successful, false otherwise |
6264 | | * @since 3.11 |
6265 | | */ |
6266 | | |
6267 | | bool GDALAlgorithmParseCommandLineArguments(GDALAlgorithmH hAlg, |
6268 | | CSLConstList papszArgs) |
6269 | 0 | { |
6270 | 0 | VALIDATE_POINTER1(hAlg, __func__, false); |
6271 | 0 | return hAlg->ptr->ParseCommandLineArguments(CPLStringList(papszArgs)); |
6272 | 0 | } |
6273 | | |
6274 | | /************************************************************************/ |
6275 | | /* GDALAlgorithmGetActualAlgorithm() */ |
6276 | | /************************************************************************/ |
6277 | | |
6278 | | /** Return the actual algorithm that is going to be invoked, when the |
6279 | | * current algorithm has sub-algorithms. |
6280 | | * |
6281 | | * Only valid after GDALAlgorithmParseCommandLineArguments() has been called. |
6282 | | * |
6283 | | * Note that the lifetime of the returned algorithm does not exceed the one of |
6284 | | * the hAlg instance that owns it. |
6285 | | * |
6286 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6287 | | * @return an handle to the algorithm (to be freed with GDALAlgorithmRelease). |
6288 | | * @since 3.11 |
6289 | | */ |
6290 | | GDALAlgorithmH GDALAlgorithmGetActualAlgorithm(GDALAlgorithmH hAlg) |
6291 | 0 | { |
6292 | 0 | VALIDATE_POINTER1(hAlg, __func__, nullptr); |
6293 | 0 | return GDALAlgorithmHS::FromRef(hAlg->ptr->GetActualAlgorithm()).release(); |
6294 | 0 | } |
6295 | | |
6296 | | /************************************************************************/ |
6297 | | /* GDALAlgorithmRun() */ |
6298 | | /************************************************************************/ |
6299 | | |
6300 | | /** Execute the algorithm, starting with ValidateArguments() and then |
6301 | | * calling RunImpl(). |
6302 | | * |
6303 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6304 | | * @param pfnProgress Progress callback. May be null. |
6305 | | * @param pProgressData Progress callback user data. May be null. |
6306 | | * @return true if successful, false otherwise |
6307 | | * @since 3.11 |
6308 | | */ |
6309 | | |
6310 | | bool GDALAlgorithmRun(GDALAlgorithmH hAlg, GDALProgressFunc pfnProgress, |
6311 | | void *pProgressData) |
6312 | 0 | { |
6313 | 0 | VALIDATE_POINTER1(hAlg, __func__, false); |
6314 | 0 | return hAlg->ptr->Run(pfnProgress, pProgressData); |
6315 | 0 | } |
6316 | | |
6317 | | /************************************************************************/ |
6318 | | /* GDALAlgorithmFinalize() */ |
6319 | | /************************************************************************/ |
6320 | | |
6321 | | /** Complete any pending actions, and return the final status. |
6322 | | * This is typically useful for algorithm that generate an output dataset. |
6323 | | * |
6324 | | * Note that this function does *NOT* release memory associated with the |
6325 | | * algorithm. GDALAlgorithmRelease() must still be called afterwards. |
6326 | | * |
6327 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6328 | | * @return true if successful, false otherwise |
6329 | | * @since 3.11 |
6330 | | */ |
6331 | | |
6332 | | bool GDALAlgorithmFinalize(GDALAlgorithmH hAlg) |
6333 | 0 | { |
6334 | 0 | VALIDATE_POINTER1(hAlg, __func__, false); |
6335 | 0 | return hAlg->ptr->Finalize(); |
6336 | 0 | } |
6337 | | |
6338 | | /************************************************************************/ |
6339 | | /* GDALAlgorithmGetUsageAsJSON() */ |
6340 | | /************************************************************************/ |
6341 | | |
6342 | | /** Return the usage of the algorithm as a JSON-serialized string. |
6343 | | * |
6344 | | * This can be used to dynamically generate interfaces to algorithms. |
6345 | | * |
6346 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6347 | | * @return a string that must be freed with CPLFree() |
6348 | | * @since 3.11 |
6349 | | */ |
6350 | | char *GDALAlgorithmGetUsageAsJSON(GDALAlgorithmH hAlg) |
6351 | 0 | { |
6352 | 0 | VALIDATE_POINTER1(hAlg, __func__, nullptr); |
6353 | 0 | return CPLStrdup(hAlg->ptr->GetUsageAsJSON().c_str()); |
6354 | 0 | } |
6355 | | |
6356 | | /************************************************************************/ |
6357 | | /* GDALAlgorithmGetArgNames() */ |
6358 | | /************************************************************************/ |
6359 | | |
6360 | | /** Return the list of available argument names. |
6361 | | * |
6362 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6363 | | * @return a NULL terminated list of names, which must be destroyed with |
6364 | | * CSLDestroy() |
6365 | | * @since 3.11 |
6366 | | */ |
6367 | | char **GDALAlgorithmGetArgNames(GDALAlgorithmH hAlg) |
6368 | 0 | { |
6369 | 0 | VALIDATE_POINTER1(hAlg, __func__, nullptr); |
6370 | 0 | CPLStringList list; |
6371 | 0 | for (const auto &arg : hAlg->ptr->GetArgs()) |
6372 | 0 | list.AddString(arg->GetName().c_str()); |
6373 | 0 | return list.StealList(); |
6374 | 0 | } |
6375 | | |
6376 | | /************************************************************************/ |
6377 | | /* GDALAlgorithmGetArg() */ |
6378 | | /************************************************************************/ |
6379 | | |
6380 | | /** Return an argument from its name. |
6381 | | * |
6382 | | * The lifetime of the returned object does not exceed the one of hAlg. |
6383 | | * |
6384 | | * @param hAlg Handle to an algorithm. Must NOT be null. |
6385 | | * @param pszArgName Argument name. Must NOT be null. |
6386 | | * @return an argument that must be released with GDALAlgorithmArgRelease(), |
6387 | | * or nullptr in case of error |
6388 | | * @since 3.11 |
6389 | | */ |
6390 | | GDALAlgorithmArgH GDALAlgorithmGetArg(GDALAlgorithmH hAlg, |
6391 | | const char *pszArgName) |
6392 | 0 | { |
6393 | 0 | VALIDATE_POINTER1(hAlg, __func__, nullptr); |
6394 | 0 | VALIDATE_POINTER1(pszArgName, __func__, nullptr); |
6395 | 0 | auto arg = hAlg->ptr->GetArg(pszArgName); |
6396 | 0 | if (!arg) |
6397 | 0 | return nullptr; |
6398 | 0 | return std::make_unique<GDALAlgorithmArgHS>(arg).release(); |
6399 | 0 | } |
6400 | | |
6401 | | /************************************************************************/ |
6402 | | /* GDALAlgorithmArgRelease() */ |
6403 | | /************************************************************************/ |
6404 | | |
6405 | | /** Release a handle to an argument. |
6406 | | * |
6407 | | * @since 3.11 |
6408 | | */ |
6409 | | void GDALAlgorithmArgRelease(GDALAlgorithmArgH hArg) |
6410 | 0 | { |
6411 | 0 | delete hArg; |
6412 | 0 | } |
6413 | | |
6414 | | /************************************************************************/ |
6415 | | /* GDALAlgorithmArgGetName() */ |
6416 | | /************************************************************************/ |
6417 | | |
6418 | | /** Return the name of an argument. |
6419 | | * |
6420 | | * @param hArg Handle to an argument. Must NOT be null. |
6421 | | * @return argument name whose lifetime is bound to hArg and which must not |
6422 | | * be freed. |
6423 | | * @since 3.11 |
6424 | | */ |
6425 | | const char *GDALAlgorithmArgGetName(GDALAlgorithmArgH hArg) |
6426 | 0 | { |
6427 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
6428 | 0 | return hArg->ptr->GetName().c_str(); |
6429 | 0 | } |
6430 | | |
6431 | | /************************************************************************/ |
6432 | | /* GDALAlgorithmArgGetType() */ |
6433 | | /************************************************************************/ |
6434 | | |
6435 | | /** Get the type of an argument |
6436 | | * |
6437 | | * @param hArg Handle to an argument. Must NOT be null. |
6438 | | * @since 3.11 |
6439 | | */ |
6440 | | GDALAlgorithmArgType GDALAlgorithmArgGetType(GDALAlgorithmArgH hArg) |
6441 | 0 | { |
6442 | 0 | VALIDATE_POINTER1(hArg, __func__, GAAT_STRING); |
6443 | 0 | return hArg->ptr->GetType(); |
6444 | 0 | } |
6445 | | |
6446 | | /************************************************************************/ |
6447 | | /* GDALAlgorithmArgGetDescription() */ |
6448 | | /************************************************************************/ |
6449 | | |
6450 | | /** Return the description of an argument. |
6451 | | * |
6452 | | * @param hArg Handle to an argument. Must NOT be null. |
6453 | | * @return argument descriptioin whose lifetime is bound to hArg and which must not |
6454 | | * be freed. |
6455 | | * @since 3.11 |
6456 | | */ |
6457 | | const char *GDALAlgorithmArgGetDescription(GDALAlgorithmArgH hArg) |
6458 | 0 | { |
6459 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
6460 | 0 | return hArg->ptr->GetDescription().c_str(); |
6461 | 0 | } |
6462 | | |
6463 | | /************************************************************************/ |
6464 | | /* GDALAlgorithmArgGetShortName() */ |
6465 | | /************************************************************************/ |
6466 | | |
6467 | | /** Return the short name, or empty string if there is none |
6468 | | * |
6469 | | * @param hArg Handle to an argument. Must NOT be null. |
6470 | | * @return short name whose lifetime is bound to hArg and which must not |
6471 | | * be freed. |
6472 | | * @since 3.11 |
6473 | | */ |
6474 | | const char *GDALAlgorithmArgGetShortName(GDALAlgorithmArgH hArg) |
6475 | 0 | { |
6476 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
6477 | 0 | return hArg->ptr->GetShortName().c_str(); |
6478 | 0 | } |
6479 | | |
6480 | | /************************************************************************/ |
6481 | | /* GDALAlgorithmArgGetAliases() */ |
6482 | | /************************************************************************/ |
6483 | | |
6484 | | /** Return the aliases (potentially none) |
6485 | | * |
6486 | | * @param hArg Handle to an argument. Must NOT be null. |
6487 | | * @return a NULL terminated list of names, which must be destroyed with |
6488 | | * CSLDestroy() |
6489 | | |
6490 | | * @since 3.11 |
6491 | | */ |
6492 | | char **GDALAlgorithmArgGetAliases(GDALAlgorithmArgH hArg) |
6493 | 0 | { |
6494 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
6495 | 0 | return CPLStringList(hArg->ptr->GetAliases()).StealList(); |
6496 | 0 | } |
6497 | | |
6498 | | /************************************************************************/ |
6499 | | /* GDALAlgorithmArgGetMetaVar() */ |
6500 | | /************************************************************************/ |
6501 | | |
6502 | | /** Return the "meta-var" hint. |
6503 | | * |
6504 | | * By default, the meta-var value is the long name of the argument in |
6505 | | * upper case. |
6506 | | * |
6507 | | * @param hArg Handle to an argument. Must NOT be null. |
6508 | | * @return meta-var hint whose lifetime is bound to hArg and which must not |
6509 | | * be freed. |
6510 | | * @since 3.11 |
6511 | | */ |
6512 | | const char *GDALAlgorithmArgGetMetaVar(GDALAlgorithmArgH hArg) |
6513 | 0 | { |
6514 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
6515 | 0 | return hArg->ptr->GetMetaVar().c_str(); |
6516 | 0 | } |
6517 | | |
6518 | | /************************************************************************/ |
6519 | | /* GDALAlgorithmArgGetCategory() */ |
6520 | | /************************************************************************/ |
6521 | | |
6522 | | /** Return the argument category |
6523 | | * |
6524 | | * GAAC_COMMON, GAAC_BASE, GAAC_ADVANCED, GAAC_ESOTERIC or a custom category. |
6525 | | * |
6526 | | * @param hArg Handle to an argument. Must NOT be null. |
6527 | | * @return category whose lifetime is bound to hArg and which must not |
6528 | | * be freed. |
6529 | | * @since 3.11 |
6530 | | */ |
6531 | | const char *GDALAlgorithmArgGetCategory(GDALAlgorithmArgH hArg) |
6532 | 0 | { |
6533 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
6534 | 0 | return hArg->ptr->GetCategory().c_str(); |
6535 | 0 | } |
6536 | | |
6537 | | /************************************************************************/ |
6538 | | /* GDALAlgorithmArgIsPositional() */ |
6539 | | /************************************************************************/ |
6540 | | |
6541 | | /** Return if the argument is a positional one. |
6542 | | * |
6543 | | * @param hArg Handle to an argument. Must NOT be null. |
6544 | | * @since 3.11 |
6545 | | */ |
6546 | | bool GDALAlgorithmArgIsPositional(GDALAlgorithmArgH hArg) |
6547 | 0 | { |
6548 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
6549 | 0 | return hArg->ptr->IsPositional(); |
6550 | 0 | } |
6551 | | |
6552 | | /************************************************************************/ |
6553 | | /* GDALAlgorithmArgIsRequired() */ |
6554 | | /************************************************************************/ |
6555 | | |
6556 | | /** Return whether the argument is required. Defaults to false. |
6557 | | * |
6558 | | * @param hArg Handle to an argument. Must NOT be null. |
6559 | | * @since 3.11 |
6560 | | */ |
6561 | | bool GDALAlgorithmArgIsRequired(GDALAlgorithmArgH hArg) |
6562 | 0 | { |
6563 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
6564 | 0 | return hArg->ptr->IsRequired(); |
6565 | 0 | } |
6566 | | |
6567 | | /************************************************************************/ |
6568 | | /* GDALAlgorithmArgGetMinCount() */ |
6569 | | /************************************************************************/ |
6570 | | |
6571 | | /** Return the minimum number of values for the argument. |
6572 | | * |
6573 | | * Defaults to 0. |
6574 | | * Only applies to list type of arguments. |
6575 | | * |
6576 | | * @param hArg Handle to an argument. Must NOT be null. |
6577 | | * @since 3.11 |
6578 | | */ |
6579 | | int GDALAlgorithmArgGetMinCount(GDALAlgorithmArgH hArg) |
6580 | 0 | { |
6581 | 0 | VALIDATE_POINTER1(hArg, __func__, 0); |
6582 | 0 | return hArg->ptr->GetMinCount(); |
6583 | 0 | } |
6584 | | |
6585 | | /************************************************************************/ |
6586 | | /* GDALAlgorithmArgGetMaxCount() */ |
6587 | | /************************************************************************/ |
6588 | | |
6589 | | /** Return the maximum number of values for the argument. |
6590 | | * |
6591 | | * Defaults to 1 for scalar types, and INT_MAX for list types. |
6592 | | * Only applies to list type of arguments. |
6593 | | * |
6594 | | * @param hArg Handle to an argument. Must NOT be null. |
6595 | | * @since 3.11 |
6596 | | */ |
6597 | | int GDALAlgorithmArgGetMaxCount(GDALAlgorithmArgH hArg) |
6598 | 0 | { |
6599 | 0 | VALIDATE_POINTER1(hArg, __func__, 0); |
6600 | 0 | return hArg->ptr->GetMaxCount(); |
6601 | 0 | } |
6602 | | |
6603 | | /************************************************************************/ |
6604 | | /* GDALAlgorithmArgGetPackedValuesAllowed() */ |
6605 | | /************************************************************************/ |
6606 | | |
6607 | | /** Return whether, for list type of arguments, several values, space |
6608 | | * separated, may be specified. That is "--foo=bar,baz". |
6609 | | * The default is true. |
6610 | | * |
6611 | | * @param hArg Handle to an argument. Must NOT be null. |
6612 | | * @since 3.11 |
6613 | | */ |
6614 | | bool GDALAlgorithmArgGetPackedValuesAllowed(GDALAlgorithmArgH hArg) |
6615 | 0 | { |
6616 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
6617 | 0 | return hArg->ptr->GetPackedValuesAllowed(); |
6618 | 0 | } |
6619 | | |
6620 | | /************************************************************************/ |
6621 | | /* GDALAlgorithmArgGetRepeatedArgAllowed() */ |
6622 | | /************************************************************************/ |
6623 | | |
6624 | | /** Return whether, for list type of arguments, the argument may be |
6625 | | * repeated. That is "--foo=bar --foo=baz". |
6626 | | * The default is true. |
6627 | | * |
6628 | | * @param hArg Handle to an argument. Must NOT be null. |
6629 | | * @since 3.11 |
6630 | | */ |
6631 | | bool GDALAlgorithmArgGetRepeatedArgAllowed(GDALAlgorithmArgH hArg) |
6632 | 0 | { |
6633 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
6634 | 0 | return hArg->ptr->GetRepeatedArgAllowed(); |
6635 | 0 | } |
6636 | | |
6637 | | /************************************************************************/ |
6638 | | /* GDALAlgorithmArgGetChoices() */ |
6639 | | /************************************************************************/ |
6640 | | |
6641 | | /** Return the allowed values (as strings) for the argument. |
6642 | | * |
6643 | | * Only honored for GAAT_STRING and GAAT_STRING_LIST types. |
6644 | | * |
6645 | | * @param hArg Handle to an argument. Must NOT be null. |
6646 | | * @return a NULL terminated list of names, which must be destroyed with |
6647 | | * CSLDestroy() |
6648 | | |
6649 | | * @since 3.11 |
6650 | | */ |
6651 | | char **GDALAlgorithmArgGetChoices(GDALAlgorithmArgH hArg) |
6652 | 0 | { |
6653 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
6654 | 0 | return CPLStringList(hArg->ptr->GetChoices()).StealList(); |
6655 | 0 | } |
6656 | | |
6657 | | /************************************************************************/ |
6658 | | /* GDALAlgorithmArgGetMetadataItem() */ |
6659 | | /************************************************************************/ |
6660 | | |
6661 | | /** Return the values of the metadata item of an argument. |
6662 | | * |
6663 | | * @param hArg Handle to an argument. Must NOT be null. |
6664 | | * @param pszItem Name of the item. Must NOT be null. |
6665 | | * @return a NULL terminated list of values, which must be destroyed with |
6666 | | * CSLDestroy() |
6667 | | |
6668 | | * @since 3.11 |
6669 | | */ |
6670 | | char **GDALAlgorithmArgGetMetadataItem(GDALAlgorithmArgH hArg, |
6671 | | const char *pszItem) |
6672 | 0 | { |
6673 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
6674 | 0 | VALIDATE_POINTER1(pszItem, __func__, nullptr); |
6675 | 0 | const auto pVecOfStrings = hArg->ptr->GetMetadataItem(pszItem); |
6676 | 0 | return pVecOfStrings ? CPLStringList(*pVecOfStrings).StealList() : nullptr; |
6677 | 0 | } |
6678 | | |
6679 | | /************************************************************************/ |
6680 | | /* GDALAlgorithmArgIsExplicitlySet() */ |
6681 | | /************************************************************************/ |
6682 | | |
6683 | | /** Return whether the argument value has been explicitly set with Set() |
6684 | | * |
6685 | | * @param hArg Handle to an argument. Must NOT be null. |
6686 | | * @since 3.11 |
6687 | | */ |
6688 | | bool GDALAlgorithmArgIsExplicitlySet(GDALAlgorithmArgH hArg) |
6689 | 0 | { |
6690 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
6691 | 0 | return hArg->ptr->IsExplicitlySet(); |
6692 | 0 | } |
6693 | | |
6694 | | /************************************************************************/ |
6695 | | /* GDALAlgorithmArgHasDefaultValue() */ |
6696 | | /************************************************************************/ |
6697 | | |
6698 | | /** Return if the argument has a declared default value. |
6699 | | * |
6700 | | * @param hArg Handle to an argument. Must NOT be null. |
6701 | | * @since 3.11 |
6702 | | */ |
6703 | | bool GDALAlgorithmArgHasDefaultValue(GDALAlgorithmArgH hArg) |
6704 | 0 | { |
6705 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
6706 | 0 | return hArg->ptr->HasDefaultValue(); |
6707 | 0 | } |
6708 | | |
6709 | | /************************************************************************/ |
6710 | | /* GDALAlgorithmArgIsHiddenForCLI() */ |
6711 | | /************************************************************************/ |
6712 | | |
6713 | | /** Return whether the argument must not be mentioned in CLI usage. |
6714 | | * |
6715 | | * For example, "output-value" for "gdal raster info", which is only |
6716 | | * meant when the algorithm is used from a non-CLI context. |
6717 | | * |
6718 | | * @param hArg Handle to an argument. Must NOT be null. |
6719 | | * @since 3.11 |
6720 | | */ |
6721 | | bool GDALAlgorithmArgIsHiddenForCLI(GDALAlgorithmArgH hArg) |
6722 | 0 | { |
6723 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
6724 | 0 | return hArg->ptr->IsHiddenForCLI(); |
6725 | 0 | } |
6726 | | |
6727 | | /************************************************************************/ |
6728 | | /* GDALAlgorithmArgIsOnlyForCLI() */ |
6729 | | /************************************************************************/ |
6730 | | |
6731 | | /** Return whether the argument is only for CLI usage. |
6732 | | * |
6733 | | * For example "--help" |
6734 | | * |
6735 | | * @param hArg Handle to an argument. Must NOT be null. |
6736 | | * @since 3.11 |
6737 | | */ |
6738 | | bool GDALAlgorithmArgIsOnlyForCLI(GDALAlgorithmArgH hArg) |
6739 | 0 | { |
6740 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
6741 | 0 | return hArg->ptr->IsOnlyForCLI(); |
6742 | 0 | } |
6743 | | |
6744 | | /************************************************************************/ |
6745 | | /* GDALAlgorithmArgIsInput() */ |
6746 | | /************************************************************************/ |
6747 | | |
6748 | | /** Indicate whether the value of the argument is read-only during the |
6749 | | * execution of the algorithm. |
6750 | | * |
6751 | | * Default is true. |
6752 | | * |
6753 | | * @param hArg Handle to an argument. Must NOT be null. |
6754 | | * @since 3.11 |
6755 | | */ |
6756 | | bool GDALAlgorithmArgIsInput(GDALAlgorithmArgH hArg) |
6757 | 0 | { |
6758 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
6759 | 0 | return hArg->ptr->IsInput(); |
6760 | 0 | } |
6761 | | |
6762 | | /************************************************************************/ |
6763 | | /* GDALAlgorithmArgIsOutput() */ |
6764 | | /************************************************************************/ |
6765 | | |
6766 | | /** Return whether (at least part of) the value of the argument is set |
6767 | | * during the execution of the algorithm. |
6768 | | * |
6769 | | * For example, "output-value" for "gdal raster info" |
6770 | | * Default is false. |
6771 | | * An argument may return both IsInput() and IsOutput() as true. |
6772 | | * For example the "gdal raster convert" algorithm consumes the dataset |
6773 | | * name of its "output" argument, and sets the dataset object during its |
6774 | | * execution. |
6775 | | * |
6776 | | * @param hArg Handle to an argument. Must NOT be null. |
6777 | | * @since 3.11 |
6778 | | */ |
6779 | | bool GDALAlgorithmArgIsOutput(GDALAlgorithmArgH hArg) |
6780 | 0 | { |
6781 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
6782 | 0 | return hArg->ptr->IsOutput(); |
6783 | 0 | } |
6784 | | |
6785 | | /************************************************************************/ |
6786 | | /* GDALAlgorithmArgGetDatasetType() */ |
6787 | | /************************************************************************/ |
6788 | | |
6789 | | /** Get which type of dataset is allowed / generated. |
6790 | | * |
6791 | | * Binary-or combination of GDAL_OF_RASTER, GDAL_OF_VECTOR and |
6792 | | * GDAL_OF_MULTIDIM_RASTER. |
6793 | | * Only applies to arguments of type GAAT_DATASET or GAAT_DATASET_LIST. |
6794 | | * |
6795 | | * @param hArg Handle to an argument. Must NOT be null. |
6796 | | * @since 3.11 |
6797 | | */ |
6798 | | GDALArgDatasetType GDALAlgorithmArgGetDatasetType(GDALAlgorithmArgH hArg) |
6799 | 0 | { |
6800 | 0 | VALIDATE_POINTER1(hArg, __func__, 0); |
6801 | 0 | return hArg->ptr->GetDatasetType(); |
6802 | 0 | } |
6803 | | |
6804 | | /************************************************************************/ |
6805 | | /* GDALAlgorithmArgGetDatasetInputFlags() */ |
6806 | | /************************************************************************/ |
6807 | | |
6808 | | /** Indicates which components among name and dataset are accepted as |
6809 | | * input, when this argument serves as an input. |
6810 | | * |
6811 | | * If the GADV_NAME bit is set, it indicates a dataset name is accepted as |
6812 | | * input. |
6813 | | * If the GADV_OBJECT bit is set, it indicates a dataset object is |
6814 | | * accepted as input. |
6815 | | * If both bits are set, the algorithm can accept either a name or a dataset |
6816 | | * object. |
6817 | | * Only applies to arguments of type GAAT_DATASET or GAAT_DATASET_LIST. |
6818 | | * |
6819 | | * @param hArg Handle to an argument. Must NOT be null. |
6820 | | * @return string whose lifetime is bound to hAlg and which must not |
6821 | | * be freed. |
6822 | | * @since 3.11 |
6823 | | */ |
6824 | | int GDALAlgorithmArgGetDatasetInputFlags(GDALAlgorithmArgH hArg) |
6825 | 0 | { |
6826 | 0 | VALIDATE_POINTER1(hArg, __func__, 0); |
6827 | 0 | return hArg->ptr->GetDatasetInputFlags(); |
6828 | 0 | } |
6829 | | |
6830 | | /************************************************************************/ |
6831 | | /* GDALAlgorithmArgGetDatasetOutputFlags() */ |
6832 | | /************************************************************************/ |
6833 | | |
6834 | | /** Indicates which components among name and dataset are modified, |
6835 | | * when this argument serves as an output. |
6836 | | * |
6837 | | * If the GADV_NAME bit is set, it indicates a dataset name is generated as |
6838 | | * output (that is the algorithm will generate the name. Rarely used). |
6839 | | * If the GADV_OBJECT bit is set, it indicates a dataset object is |
6840 | | * generated as output, and available for use after the algorithm has |
6841 | | * completed. |
6842 | | * Only applies to arguments of type GAAT_DATASET or GAAT_DATASET_LIST. |
6843 | | * |
6844 | | * @param hArg Handle to an argument. Must NOT be null. |
6845 | | * @return string whose lifetime is bound to hAlg and which must not |
6846 | | * be freed. |
6847 | | * @since 3.11 |
6848 | | */ |
6849 | | int GDALAlgorithmArgGetDatasetOutputFlags(GDALAlgorithmArgH hArg) |
6850 | 0 | { |
6851 | 0 | VALIDATE_POINTER1(hArg, __func__, 0); |
6852 | 0 | return hArg->ptr->GetDatasetOutputFlags(); |
6853 | 0 | } |
6854 | | |
6855 | | /************************************************************************/ |
6856 | | /* GDALAlgorithmArgGetMutualExclusionGroup() */ |
6857 | | /************************************************************************/ |
6858 | | |
6859 | | /** Return the name of the mutual exclusion group to which this argument |
6860 | | * belongs to. |
6861 | | * |
6862 | | * Or empty string if it does not belong to any exclusion group. |
6863 | | * |
6864 | | * @param hArg Handle to an argument. Must NOT be null. |
6865 | | * @return string whose lifetime is bound to hArg and which must not |
6866 | | * be freed. |
6867 | | * @since 3.11 |
6868 | | */ |
6869 | | const char *GDALAlgorithmArgGetMutualExclusionGroup(GDALAlgorithmArgH hArg) |
6870 | 0 | { |
6871 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
6872 | 0 | return hArg->ptr->GetMutualExclusionGroup().c_str(); |
6873 | 0 | } |
6874 | | |
6875 | | /************************************************************************/ |
6876 | | /* GDALAlgorithmArgGetAsBoolean() */ |
6877 | | /************************************************************************/ |
6878 | | |
6879 | | /** Return the argument value as a boolean. |
6880 | | * |
6881 | | * Must only be called on arguments whose type is GAAT_BOOLEAN. |
6882 | | * |
6883 | | * @param hArg Handle to an argument. Must NOT be null. |
6884 | | * @since 3.11 |
6885 | | */ |
6886 | | bool GDALAlgorithmArgGetAsBoolean(GDALAlgorithmArgH hArg) |
6887 | 0 | { |
6888 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
6889 | 0 | if (hArg->ptr->GetType() != GAAT_BOOLEAN) |
6890 | 0 | { |
6891 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6892 | 0 | "%s must only be called on arguments of type GAAT_BOOLEAN", |
6893 | 0 | __func__); |
6894 | 0 | return false; |
6895 | 0 | } |
6896 | 0 | return hArg->ptr->Get<bool>(); |
6897 | 0 | } |
6898 | | |
6899 | | /************************************************************************/ |
6900 | | /* GDALAlgorithmArgGetAsBoolean() */ |
6901 | | /************************************************************************/ |
6902 | | |
6903 | | /** Return the argument value as a string. |
6904 | | * |
6905 | | * Must only be called on arguments whose type is GAAT_STRING. |
6906 | | * |
6907 | | * @param hArg Handle to an argument. Must NOT be null. |
6908 | | * @return string whose lifetime is bound to hArg and which must not |
6909 | | * be freed. |
6910 | | * @since 3.11 |
6911 | | */ |
6912 | | const char *GDALAlgorithmArgGetAsString(GDALAlgorithmArgH hArg) |
6913 | 0 | { |
6914 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
6915 | 0 | if (hArg->ptr->GetType() != GAAT_STRING) |
6916 | 0 | { |
6917 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6918 | 0 | "%s must only be called on arguments of type GAAT_STRING", |
6919 | 0 | __func__); |
6920 | 0 | return nullptr; |
6921 | 0 | } |
6922 | 0 | return hArg->ptr->Get<std::string>().c_str(); |
6923 | 0 | } |
6924 | | |
6925 | | /************************************************************************/ |
6926 | | /* GDALAlgorithmArgGetAsDatasetValue() */ |
6927 | | /************************************************************************/ |
6928 | | |
6929 | | /** Return the argument value as a GDALArgDatasetValueH. |
6930 | | * |
6931 | | * Must only be called on arguments whose type is GAAT_DATASET |
6932 | | * |
6933 | | * @param hArg Handle to an argument. Must NOT be null. |
6934 | | * @return handle to a GDALArgDatasetValue that must be released with |
6935 | | * GDALArgDatasetValueRelease(). The lifetime of that handle does not exceed |
6936 | | * the one of hArg. |
6937 | | * @since 3.11 |
6938 | | */ |
6939 | | GDALArgDatasetValueH GDALAlgorithmArgGetAsDatasetValue(GDALAlgorithmArgH hArg) |
6940 | 0 | { |
6941 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
6942 | 0 | if (hArg->ptr->GetType() != GAAT_DATASET) |
6943 | 0 | { |
6944 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6945 | 0 | "%s must only be called on arguments of type GAAT_DATASET", |
6946 | 0 | __func__); |
6947 | 0 | return nullptr; |
6948 | 0 | } |
6949 | 0 | return std::make_unique<GDALArgDatasetValueHS>( |
6950 | 0 | &(hArg->ptr->Get<GDALArgDatasetValue>())) |
6951 | 0 | .release(); |
6952 | 0 | } |
6953 | | |
6954 | | /************************************************************************/ |
6955 | | /* GDALAlgorithmArgGetAsInteger() */ |
6956 | | /************************************************************************/ |
6957 | | |
6958 | | /** Return the argument value as a integer. |
6959 | | * |
6960 | | * Must only be called on arguments whose type is GAAT_INTEGER |
6961 | | * |
6962 | | * @param hArg Handle to an argument. Must NOT be null. |
6963 | | * @since 3.11 |
6964 | | */ |
6965 | | int GDALAlgorithmArgGetAsInteger(GDALAlgorithmArgH hArg) |
6966 | 0 | { |
6967 | 0 | VALIDATE_POINTER1(hArg, __func__, 0); |
6968 | 0 | if (hArg->ptr->GetType() != GAAT_INTEGER) |
6969 | 0 | { |
6970 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6971 | 0 | "%s must only be called on arguments of type GAAT_INTEGER", |
6972 | 0 | __func__); |
6973 | 0 | return 0; |
6974 | 0 | } |
6975 | 0 | return hArg->ptr->Get<int>(); |
6976 | 0 | } |
6977 | | |
6978 | | /************************************************************************/ |
6979 | | /* GDALAlgorithmArgGetAsDouble() */ |
6980 | | /************************************************************************/ |
6981 | | |
6982 | | /** Return the argument value as a double. |
6983 | | * |
6984 | | * Must only be called on arguments whose type is GAAT_REAL |
6985 | | * |
6986 | | * @param hArg Handle to an argument. Must NOT be null. |
6987 | | * @since 3.11 |
6988 | | */ |
6989 | | double GDALAlgorithmArgGetAsDouble(GDALAlgorithmArgH hArg) |
6990 | 0 | { |
6991 | 0 | VALIDATE_POINTER1(hArg, __func__, 0); |
6992 | 0 | if (hArg->ptr->GetType() != GAAT_REAL) |
6993 | 0 | { |
6994 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6995 | 0 | "%s must only be called on arguments of type GAAT_REAL", |
6996 | 0 | __func__); |
6997 | 0 | return 0; |
6998 | 0 | } |
6999 | 0 | return hArg->ptr->Get<double>(); |
7000 | 0 | } |
7001 | | |
7002 | | /************************************************************************/ |
7003 | | /* GDALAlgorithmArgGetAsStringList() */ |
7004 | | /************************************************************************/ |
7005 | | |
7006 | | /** Return the argument value as a double. |
7007 | | * |
7008 | | * Must only be called on arguments whose type is GAAT_STRING_LIST. |
7009 | | * |
7010 | | * @param hArg Handle to an argument. Must NOT be null. |
7011 | | * @return a NULL terminated list of names, which must be destroyed with |
7012 | | * CSLDestroy() |
7013 | | |
7014 | | * @since 3.11 |
7015 | | */ |
7016 | | char **GDALAlgorithmArgGetAsStringList(GDALAlgorithmArgH hArg) |
7017 | 0 | { |
7018 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
7019 | 0 | if (hArg->ptr->GetType() != GAAT_STRING_LIST) |
7020 | 0 | { |
7021 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
7022 | 0 | "%s must only be called on arguments of type GAAT_STRING_LIST", |
7023 | 0 | __func__); |
7024 | 0 | return nullptr; |
7025 | 0 | } |
7026 | 0 | return CPLStringList(hArg->ptr->Get<std::vector<std::string>>()) |
7027 | 0 | .StealList(); |
7028 | 0 | } |
7029 | | |
7030 | | /************************************************************************/ |
7031 | | /* GDALAlgorithmArgGetAsIntegerList() */ |
7032 | | /************************************************************************/ |
7033 | | |
7034 | | /** Return the argument value as a integer. |
7035 | | * |
7036 | | * Must only be called on arguments whose type is GAAT_INTEGER |
7037 | | * |
7038 | | * @param hArg Handle to an argument. Must NOT be null. |
7039 | | * @param[out] pnCount Pointer to the number of values in the list. Must NOT be null. |
7040 | | * @since 3.11 |
7041 | | */ |
7042 | | const int *GDALAlgorithmArgGetAsIntegerList(GDALAlgorithmArgH hArg, |
7043 | | size_t *pnCount) |
7044 | 0 | { |
7045 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
7046 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
7047 | 0 | if (hArg->ptr->GetType() != GAAT_INTEGER_LIST) |
7048 | 0 | { |
7049 | 0 | CPLError( |
7050 | 0 | CE_Failure, CPLE_AppDefined, |
7051 | 0 | "%s must only be called on arguments of type GAAT_INTEGER_LIST", |
7052 | 0 | __func__); |
7053 | 0 | *pnCount = 0; |
7054 | 0 | return nullptr; |
7055 | 0 | } |
7056 | 0 | const auto &val = hArg->ptr->Get<std::vector<int>>(); |
7057 | 0 | *pnCount = val.size(); |
7058 | 0 | return val.data(); |
7059 | 0 | } |
7060 | | |
7061 | | /************************************************************************/ |
7062 | | /* GDALAlgorithmArgGetAsDoubleList() */ |
7063 | | /************************************************************************/ |
7064 | | |
7065 | | /** Return the argument value as a integer. |
7066 | | * |
7067 | | * Must only be called on arguments whose type is GAAT_INTEGER |
7068 | | * |
7069 | | * @param hArg Handle to an argument. Must NOT be null. |
7070 | | * @param[out] pnCount Pointer to the number of values in the list. Must NOT be null. |
7071 | | * @since 3.11 |
7072 | | */ |
7073 | | const double *GDALAlgorithmArgGetAsDoubleList(GDALAlgorithmArgH hArg, |
7074 | | size_t *pnCount) |
7075 | 0 | { |
7076 | 0 | VALIDATE_POINTER1(hArg, __func__, nullptr); |
7077 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
7078 | 0 | if (hArg->ptr->GetType() != GAAT_REAL_LIST) |
7079 | 0 | { |
7080 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
7081 | 0 | "%s must only be called on arguments of type GAAT_REAL_LIST", |
7082 | 0 | __func__); |
7083 | 0 | *pnCount = 0; |
7084 | 0 | return nullptr; |
7085 | 0 | } |
7086 | 0 | const auto &val = hArg->ptr->Get<std::vector<double>>(); |
7087 | 0 | *pnCount = val.size(); |
7088 | 0 | return val.data(); |
7089 | 0 | } |
7090 | | |
7091 | | /************************************************************************/ |
7092 | | /* GDALAlgorithmArgSetAsBoolean() */ |
7093 | | /************************************************************************/ |
7094 | | |
7095 | | /** Set the value for a GAAT_BOOLEAN argument. |
7096 | | * |
7097 | | * It cannot be called several times for a given argument. |
7098 | | * Validation checks and other actions are run. |
7099 | | * |
7100 | | * @param hArg Handle to an argument. Must NOT be null. |
7101 | | * @param value value. |
7102 | | * @return true if success. |
7103 | | * @since 3.11 |
7104 | | */ |
7105 | | |
7106 | | bool GDALAlgorithmArgSetAsBoolean(GDALAlgorithmArgH hArg, bool value) |
7107 | 0 | { |
7108 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
7109 | 0 | return hArg->ptr->Set(value); |
7110 | 0 | } |
7111 | | |
7112 | | /************************************************************************/ |
7113 | | /* GDALAlgorithmArgSetAsString() */ |
7114 | | /************************************************************************/ |
7115 | | |
7116 | | /** Set the value for a GAAT_STRING argument. |
7117 | | * |
7118 | | * It cannot be called several times for a given argument. |
7119 | | * Validation checks and other actions are run. |
7120 | | * |
7121 | | * @param hArg Handle to an argument. Must NOT be null. |
7122 | | * @param value value (may be null) |
7123 | | * @return true if success. |
7124 | | * @since 3.11 |
7125 | | */ |
7126 | | |
7127 | | bool GDALAlgorithmArgSetAsString(GDALAlgorithmArgH hArg, const char *value) |
7128 | 0 | { |
7129 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
7130 | 0 | return hArg->ptr->Set(value ? value : ""); |
7131 | 0 | } |
7132 | | |
7133 | | /************************************************************************/ |
7134 | | /* GDALAlgorithmArgSetAsInteger() */ |
7135 | | /************************************************************************/ |
7136 | | |
7137 | | /** Set the value for a GAAT_INTEGER (or GAAT_REAL) argument. |
7138 | | * |
7139 | | * It cannot be called several times for a given argument. |
7140 | | * Validation checks and other actions are run. |
7141 | | * |
7142 | | * @param hArg Handle to an argument. Must NOT be null. |
7143 | | * @param value value. |
7144 | | * @return true if success. |
7145 | | * @since 3.11 |
7146 | | */ |
7147 | | |
7148 | | bool GDALAlgorithmArgSetAsInteger(GDALAlgorithmArgH hArg, int value) |
7149 | 0 | { |
7150 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
7151 | 0 | return hArg->ptr->Set(value); |
7152 | 0 | } |
7153 | | |
7154 | | /************************************************************************/ |
7155 | | /* GDALAlgorithmArgSetAsDouble() */ |
7156 | | /************************************************************************/ |
7157 | | |
7158 | | /** Set the value for a GAAT_REAL argument. |
7159 | | * |
7160 | | * It cannot be called several times for a given argument. |
7161 | | * Validation checks and other actions are run. |
7162 | | * |
7163 | | * @param hArg Handle to an argument. Must NOT be null. |
7164 | | * @param value value. |
7165 | | * @return true if success. |
7166 | | * @since 3.11 |
7167 | | */ |
7168 | | |
7169 | | bool GDALAlgorithmArgSetAsDouble(GDALAlgorithmArgH hArg, double value) |
7170 | 0 | { |
7171 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
7172 | 0 | return hArg->ptr->Set(value); |
7173 | 0 | } |
7174 | | |
7175 | | /************************************************************************/ |
7176 | | /* GDALAlgorithmArgSetAsDatasetValue() */ |
7177 | | /************************************************************************/ |
7178 | | |
7179 | | /** Set the value for a GAAT_DATASET argument. |
7180 | | * |
7181 | | * It cannot be called several times for a given argument. |
7182 | | * Validation checks and other actions are run. |
7183 | | * |
7184 | | * @param hArg Handle to an argument. Must NOT be null. |
7185 | | * @param value Handle to a GDALArgDatasetValue. Must NOT be null. |
7186 | | * @return true if success. |
7187 | | * @since 3.11 |
7188 | | */ |
7189 | | bool GDALAlgorithmArgSetAsDatasetValue(GDALAlgorithmArgH hArg, |
7190 | | GDALArgDatasetValueH value) |
7191 | 0 | { |
7192 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
7193 | 0 | VALIDATE_POINTER1(value, __func__, false); |
7194 | 0 | return hArg->ptr->SetFrom(*(value->ptr)); |
7195 | 0 | } |
7196 | | |
7197 | | /************************************************************************/ |
7198 | | /* GDALAlgorithmArgSetDataset() */ |
7199 | | /************************************************************************/ |
7200 | | |
7201 | | /** Set dataset object, increasing its reference counter. |
7202 | | * |
7203 | | * @param hArg Handle to an argument. Must NOT be null. |
7204 | | * @param hDS Dataset object. May be null. |
7205 | | * @return true if success. |
7206 | | * @since 3.11 |
7207 | | */ |
7208 | | |
7209 | | bool GDALAlgorithmArgSetDataset(GDALAlgorithmArgH hArg, GDALDatasetH hDS) |
7210 | 0 | { |
7211 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
7212 | 0 | return hArg->ptr->Set(GDALDataset::FromHandle(hDS)); |
7213 | 0 | } |
7214 | | |
7215 | | /************************************************************************/ |
7216 | | /* GDALAlgorithmArgSetAsStringList() */ |
7217 | | /************************************************************************/ |
7218 | | |
7219 | | /** Set the value for a GAAT_STRING_LIST argument. |
7220 | | * |
7221 | | * It cannot be called several times for a given argument. |
7222 | | * Validation checks and other actions are run. |
7223 | | * |
7224 | | * @param hArg Handle to an argument. Must NOT be null. |
7225 | | * @param value value as a NULL terminated list (may be null) |
7226 | | * @return true if success. |
7227 | | * @since 3.11 |
7228 | | */ |
7229 | | |
7230 | | bool GDALAlgorithmArgSetAsStringList(GDALAlgorithmArgH hArg, CSLConstList value) |
7231 | 0 | { |
7232 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
7233 | 0 | return hArg->ptr->Set( |
7234 | 0 | static_cast<std::vector<std::string>>(CPLStringList(value))); |
7235 | 0 | } |
7236 | | |
7237 | | /************************************************************************/ |
7238 | | /* GDALAlgorithmArgSetAsIntegerList() */ |
7239 | | /************************************************************************/ |
7240 | | |
7241 | | /** Set the value for a GAAT_INTEGER_LIST argument. |
7242 | | * |
7243 | | * It cannot be called several times for a given argument. |
7244 | | * Validation checks and other actions are run. |
7245 | | * |
7246 | | * @param hArg Handle to an argument. Must NOT be null. |
7247 | | * @param nCount Number of values in pnValues. |
7248 | | * @param pnValues Pointer to an array of integer values of size nCount. |
7249 | | * @return true if success. |
7250 | | * @since 3.11 |
7251 | | */ |
7252 | | bool GDALAlgorithmArgSetAsIntegerList(GDALAlgorithmArgH hArg, size_t nCount, |
7253 | | const int *pnValues) |
7254 | 0 | { |
7255 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
7256 | 0 | return hArg->ptr->Set(std::vector<int>(pnValues, pnValues + nCount)); |
7257 | 0 | } |
7258 | | |
7259 | | /************************************************************************/ |
7260 | | /* GDALAlgorithmArgSetAsDoubleList() */ |
7261 | | /************************************************************************/ |
7262 | | |
7263 | | /** Set the value for a GAAT_REAL_LIST argument. |
7264 | | * |
7265 | | * It cannot be called several times for a given argument. |
7266 | | * Validation checks and other actions are run. |
7267 | | * |
7268 | | * @param hArg Handle to an argument. Must NOT be null. |
7269 | | * @param nCount Number of values in pnValues. |
7270 | | * @param pnValues Pointer to an array of double values of size nCount. |
7271 | | * @return true if success. |
7272 | | * @since 3.11 |
7273 | | */ |
7274 | | bool GDALAlgorithmArgSetAsDoubleList(GDALAlgorithmArgH hArg, size_t nCount, |
7275 | | const double *pnValues) |
7276 | 0 | { |
7277 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
7278 | 0 | return hArg->ptr->Set(std::vector<double>(pnValues, pnValues + nCount)); |
7279 | 0 | } |
7280 | | |
7281 | | /************************************************************************/ |
7282 | | /* GDALAlgorithmArgSetDatasets() */ |
7283 | | /************************************************************************/ |
7284 | | |
7285 | | /** Set dataset objects to a GAAT_DATASET_LIST argument, increasing their reference counter. |
7286 | | * |
7287 | | * @param hArg Handle to an argument. Must NOT be null. |
7288 | | * @param nCount Number of values in pnValues. |
7289 | | * @param pahDS Pointer to an array of dataset of size nCount. |
7290 | | * @return true if success. |
7291 | | * @since 3.11 |
7292 | | */ |
7293 | | |
7294 | | bool GDALAlgorithmArgSetDatasets(GDALAlgorithmArgH hArg, size_t nCount, |
7295 | | GDALDatasetH *pahDS) |
7296 | 0 | { |
7297 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
7298 | 0 | std::vector<GDALArgDatasetValue> values; |
7299 | 0 | for (size_t i = 0; i < nCount; ++i) |
7300 | 0 | { |
7301 | 0 | values.emplace_back(GDALDataset::FromHandle(pahDS[i])); |
7302 | 0 | } |
7303 | 0 | return hArg->ptr->Set(std::move(values)); |
7304 | 0 | } |
7305 | | |
7306 | | /************************************************************************/ |
7307 | | /* GDALAlgorithmArgSetDatasetNames() */ |
7308 | | /************************************************************************/ |
7309 | | |
7310 | | /** Set dataset names to a GAAT_DATASET_LIST argument. |
7311 | | * |
7312 | | * @param hArg Handle to an argument. Must NOT be null. |
7313 | | * @param names Dataset names as a NULL terminated list (may be null) |
7314 | | * @return true if success. |
7315 | | * @since 3.11 |
7316 | | */ |
7317 | | |
7318 | | bool GDALAlgorithmArgSetDatasetNames(GDALAlgorithmArgH hArg, CSLConstList names) |
7319 | 0 | { |
7320 | 0 | VALIDATE_POINTER1(hArg, __func__, false); |
7321 | 0 | std::vector<GDALArgDatasetValue> values; |
7322 | 0 | for (size_t i = 0; names[i]; ++i) |
7323 | 0 | { |
7324 | 0 | values.emplace_back(names[i]); |
7325 | 0 | } |
7326 | 0 | return hArg->ptr->Set(std::move(values)); |
7327 | 0 | } |
7328 | | |
7329 | | /************************************************************************/ |
7330 | | /* GDALArgDatasetValueCreate() */ |
7331 | | /************************************************************************/ |
7332 | | |
7333 | | /** Instantiate an empty GDALArgDatasetValue |
7334 | | * |
7335 | | * @return new handle to free with GDALArgDatasetValueRelease() |
7336 | | * @since 3.11 |
7337 | | */ |
7338 | | GDALArgDatasetValueH GDALArgDatasetValueCreate() |
7339 | 0 | { |
7340 | 0 | return std::make_unique<GDALArgDatasetValueHS>().release(); |
7341 | 0 | } |
7342 | | |
7343 | | /************************************************************************/ |
7344 | | /* GDALArgDatasetValueRelease() */ |
7345 | | /************************************************************************/ |
7346 | | |
7347 | | /** Release a handle to a GDALArgDatasetValue |
7348 | | * |
7349 | | * @since 3.11 |
7350 | | */ |
7351 | | void GDALArgDatasetValueRelease(GDALArgDatasetValueH hValue) |
7352 | 0 | { |
7353 | 0 | delete hValue; |
7354 | 0 | } |
7355 | | |
7356 | | /************************************************************************/ |
7357 | | /* GDALArgDatasetValueGetName() */ |
7358 | | /************************************************************************/ |
7359 | | |
7360 | | /** Return the name component of the GDALArgDatasetValue |
7361 | | * |
7362 | | * @param hValue Handle to a GDALArgDatasetValue. Must NOT be null. |
7363 | | * @return string whose lifetime is bound to hAlg and which must not |
7364 | | * be freed. |
7365 | | * @since 3.11 |
7366 | | */ |
7367 | | const char *GDALArgDatasetValueGetName(GDALArgDatasetValueH hValue) |
7368 | 0 | { |
7369 | 0 | VALIDATE_POINTER1(hValue, __func__, nullptr); |
7370 | 0 | return hValue->ptr->GetName().c_str(); |
7371 | 0 | } |
7372 | | |
7373 | | /************************************************************************/ |
7374 | | /* GDALArgDatasetValueGetDatasetRef() */ |
7375 | | /************************************************************************/ |
7376 | | |
7377 | | /** Return the dataset component of the GDALArgDatasetValue. |
7378 | | * |
7379 | | * This does not modify the reference counter, hence the lifetime of the |
7380 | | * returned object is not guaranteed to exceed the one of hValue. |
7381 | | * |
7382 | | * @param hValue Handle to a GDALArgDatasetValue. Must NOT be null. |
7383 | | * @since 3.11 |
7384 | | */ |
7385 | | GDALDatasetH GDALArgDatasetValueGetDatasetRef(GDALArgDatasetValueH hValue) |
7386 | 0 | { |
7387 | 0 | VALIDATE_POINTER1(hValue, __func__, nullptr); |
7388 | 0 | return GDALDataset::ToHandle(hValue->ptr->GetDatasetRef()); |
7389 | 0 | } |
7390 | | |
7391 | | /************************************************************************/ |
7392 | | /* GDALArgDatasetValueGetDatasetIncreaseRefCount() */ |
7393 | | /************************************************************************/ |
7394 | | |
7395 | | /** Return the dataset component of the GDALArgDatasetValue, and increase its |
7396 | | * reference count if not null. Once done with the dataset, the caller should |
7397 | | * call GDALReleaseDataset(). |
7398 | | * |
7399 | | * @param hValue Handle to a GDALArgDatasetValue. Must NOT be null. |
7400 | | * @since 3.11 |
7401 | | */ |
7402 | | GDALDatasetH |
7403 | | GDALArgDatasetValueGetDatasetIncreaseRefCount(GDALArgDatasetValueH hValue) |
7404 | 0 | { |
7405 | 0 | VALIDATE_POINTER1(hValue, __func__, nullptr); |
7406 | 0 | return GDALDataset::ToHandle(hValue->ptr->GetDatasetIncreaseRefCount()); |
7407 | 0 | } |
7408 | | |
7409 | | /************************************************************************/ |
7410 | | /* GDALArgDatasetValueSetName() */ |
7411 | | /************************************************************************/ |
7412 | | |
7413 | | /** Set dataset name |
7414 | | * |
7415 | | * @param hValue Handle to a GDALArgDatasetValue. Must NOT be null. |
7416 | | * @param pszName Dataset name. May be null. |
7417 | | * @since 3.11 |
7418 | | */ |
7419 | | |
7420 | | void GDALArgDatasetValueSetName(GDALArgDatasetValueH hValue, |
7421 | | const char *pszName) |
7422 | 0 | { |
7423 | 0 | VALIDATE_POINTER0(hValue, __func__); |
7424 | 0 | hValue->ptr->Set(pszName ? pszName : ""); |
7425 | 0 | } |
7426 | | |
7427 | | /************************************************************************/ |
7428 | | /* GDALArgDatasetValueSetDataset() */ |
7429 | | /************************************************************************/ |
7430 | | |
7431 | | /** Set dataset object, increasing its reference counter. |
7432 | | * |
7433 | | * @param hValue Handle to a GDALArgDatasetValue. Must NOT be null. |
7434 | | * @param hDS Dataset object. May be null. |
7435 | | * @since 3.11 |
7436 | | */ |
7437 | | |
7438 | | void GDALArgDatasetValueSetDataset(GDALArgDatasetValueH hValue, |
7439 | | GDALDatasetH hDS) |
7440 | 0 | { |
7441 | 0 | VALIDATE_POINTER0(hValue, __func__); |
7442 | 0 | hValue->ptr->Set(GDALDataset::FromHandle(hDS)); |
7443 | 0 | } |