Line data Source code
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_enumerate.h"
16 : #include "cpl_error.h"
17 : #include "cpl_error_internal.h"
18 : #include "cpl_json.h"
19 : #include "cpl_levenshtein.h"
20 : #include "cpl_minixml.h"
21 : #include "cpl_multiproc.h"
22 :
23 : #include "gdalalgorithm.h"
24 : #include "gdalalg_abstract_pipeline.h"
25 : #include "gdal_priv.h"
26 : #include "gdal_thread_pool.h"
27 : #include "memdataset.h"
28 : #include "ogrsf_frmts.h"
29 : #include "ogr_p.h"
30 : #include "ogr_spatialref.h"
31 : #include "vrtdataset.h"
32 :
33 : #include <algorithm>
34 : #include <cassert>
35 : #include <cerrno>
36 : #include <cmath>
37 : #include <cstdlib>
38 : #include <limits>
39 : #include <map>
40 : #include <type_traits>
41 : #include <string_view>
42 : #include <regex>
43 :
44 : #ifndef _
45 : #define _(x) (x)
46 : #endif
47 :
48 : constexpr const char *GDAL_ARG_NAME_OUTPUT_DATA_TYPE = "output-data-type";
49 :
50 : constexpr const char *GDAL_ARG_NAME_OUTPUT_OPEN_OPTION = "output-open-option";
51 :
52 : constexpr const char *GDAL_ARG_NAME_BAND = "band";
53 :
54 : //! @cond Doxygen_Suppress
55 : struct GDALAlgorithmArgHS
56 : {
57 : GDALAlgorithmArg *ptr = nullptr;
58 :
59 352955 : explicit GDALAlgorithmArgHS(GDALAlgorithmArg *arg) : ptr(arg)
60 : {
61 352955 : }
62 : };
63 :
64 : //! @endcond
65 :
66 : //! @cond Doxygen_Suppress
67 : struct GDALArgDatasetValueHS
68 : {
69 : GDALArgDatasetValue val{};
70 : GDALArgDatasetValue *ptr = nullptr;
71 :
72 1 : GDALArgDatasetValueHS() : ptr(&val)
73 : {
74 1 : }
75 :
76 3240 : explicit GDALArgDatasetValueHS(GDALArgDatasetValue *arg) : ptr(arg)
77 : {
78 3240 : }
79 :
80 : GDALArgDatasetValueHS(const GDALArgDatasetValueHS &) = delete;
81 : GDALArgDatasetValueHS &operator=(const GDALArgDatasetValueHS &) = delete;
82 : };
83 :
84 : //! @endcond
85 :
86 : /************************************************************************/
87 : /* GDALAlgorithmArgTypeIsList() */
88 : /************************************************************************/
89 :
90 442263 : bool GDALAlgorithmArgTypeIsList(GDALAlgorithmArgType type)
91 : {
92 442263 : switch (type)
93 : {
94 290432 : case GAAT_BOOLEAN:
95 : case GAAT_STRING:
96 : case GAAT_INTEGER:
97 : case GAAT_REAL:
98 : case GAAT_DATASET:
99 290432 : break;
100 :
101 151831 : case GAAT_STRING_LIST:
102 : case GAAT_INTEGER_LIST:
103 : case GAAT_REAL_LIST:
104 : case GAAT_DATASET_LIST:
105 151831 : return true;
106 : }
107 :
108 290432 : return false;
109 : }
110 :
111 : /************************************************************************/
112 : /* GDALAlgorithmArgTypeName() */
113 : /************************************************************************/
114 :
115 5651 : const char *GDALAlgorithmArgTypeName(GDALAlgorithmArgType type)
116 : {
117 5651 : switch (type)
118 : {
119 1377 : case GAAT_BOOLEAN:
120 1377 : break;
121 1544 : case GAAT_STRING:
122 1544 : return "string";
123 392 : case GAAT_INTEGER:
124 392 : return "integer";
125 477 : case GAAT_REAL:
126 477 : return "real";
127 259 : case GAAT_DATASET:
128 259 : return "dataset";
129 1072 : case GAAT_STRING_LIST:
130 1072 : return "string_list";
131 93 : case GAAT_INTEGER_LIST:
132 93 : return "integer_list";
133 221 : case GAAT_REAL_LIST:
134 221 : return "real_list";
135 216 : case GAAT_DATASET_LIST:
136 216 : return "dataset_list";
137 : }
138 :
139 1377 : return "boolean";
140 : }
141 :
142 : /************************************************************************/
143 : /* GDALAlgorithmArgDatasetTypeName() */
144 : /************************************************************************/
145 :
146 28330 : std::string GDALAlgorithmArgDatasetTypeName(GDALArgDatasetType type)
147 : {
148 28330 : std::string ret;
149 28330 : if ((type & GDAL_OF_RASTER) != 0)
150 16915 : ret = "raster";
151 28330 : if ((type & GDAL_OF_VECTOR) != 0)
152 : {
153 12096 : if (!ret.empty())
154 : {
155 1773 : if ((type & GDAL_OF_MULTIDIM_RASTER) != 0)
156 286 : ret += ", ";
157 : else
158 1487 : ret += " or ";
159 : }
160 12096 : ret += "vector";
161 : }
162 28330 : if ((type & GDAL_OF_MULTIDIM_RASTER) != 0)
163 : {
164 1300 : if (!ret.empty())
165 : {
166 425 : ret += " or ";
167 : }
168 1300 : ret += "multidimensional raster";
169 : }
170 28330 : return ret;
171 : }
172 :
173 : /************************************************************************/
174 : /* GDALAlgorithmArgDecl() */
175 : /************************************************************************/
176 :
177 : // cppcheck-suppress uninitMemberVar
178 355416 : GDALAlgorithmArgDecl::GDALAlgorithmArgDecl(const std::string &longName,
179 : char chShortName,
180 : const std::string &description,
181 355416 : GDALAlgorithmArgType type)
182 : : m_longName(longName),
183 355416 : m_shortName(chShortName ? std::string(&chShortName, 1) : std::string()),
184 : m_description(description), m_type(type),
185 710832 : m_metaVar(CPLString(m_type == GAAT_BOOLEAN ? std::string() : longName)
186 355416 : .toupper()),
187 1066250 : m_maxCount(GDALAlgorithmArgTypeIsList(type) ? UNBOUNDED : 1)
188 : {
189 355416 : if (m_type == GAAT_BOOLEAN)
190 : {
191 148372 : m_defaultValue = false;
192 : }
193 355416 : }
194 :
195 : /************************************************************************/
196 : /* GDALAlgorithmArgDecl::SetMinCount() */
197 : /************************************************************************/
198 :
199 19628 : GDALAlgorithmArgDecl &GDALAlgorithmArgDecl::SetMinCount(int count)
200 : {
201 19628 : if (!GDALAlgorithmArgTypeIsList(m_type))
202 : {
203 1 : CPLError(CE_Failure, CPLE_NotSupported,
204 : "SetMinCount() illegal on scalar argument '%s'",
205 1 : GetName().c_str());
206 : }
207 : else
208 : {
209 19627 : m_minCount = count;
210 : }
211 19628 : return *this;
212 : }
213 :
214 : /************************************************************************/
215 : /* GDALAlgorithmArgDecl::SetMaxCount() */
216 : /************************************************************************/
217 :
218 18759 : GDALAlgorithmArgDecl &GDALAlgorithmArgDecl::SetMaxCount(int count)
219 : {
220 18759 : if (!GDALAlgorithmArgTypeIsList(m_type))
221 : {
222 1 : CPLError(CE_Failure, CPLE_NotSupported,
223 : "SetMaxCount() illegal on scalar argument '%s'",
224 1 : GetName().c_str());
225 : }
226 : else
227 : {
228 18758 : m_maxCount = count;
229 : }
230 18759 : return *this;
231 : }
232 :
233 : /************************************************************************/
234 : /* GDALAlgorithmArg::~GDALAlgorithmArg() */
235 : /************************************************************************/
236 :
237 : GDALAlgorithmArg::~GDALAlgorithmArg() = default;
238 :
239 : /************************************************************************/
240 : /* GDALAlgorithmArg::Set() */
241 : /************************************************************************/
242 :
243 1276 : bool GDALAlgorithmArg::Set(bool value)
244 : {
245 1276 : if (m_decl.GetType() != GAAT_BOOLEAN)
246 : {
247 14 : CPLError(
248 : CE_Failure, CPLE_AppDefined,
249 : "Calling Set(bool) on argument '%s' of type %s is not supported",
250 7 : GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()));
251 7 : return false;
252 : }
253 1269 : return SetInternal(value);
254 : }
255 :
256 4312 : bool GDALAlgorithmArg::ProcessString(std::string &value) const
257 : {
258 4363 : if (m_decl.IsReadFromFileAtSyntaxAllowed() && !value.empty() &&
259 51 : value.front() == '@')
260 : {
261 2 : GByte *pabyData = nullptr;
262 2 : if (VSIIngestFile(nullptr, value.c_str() + 1, &pabyData, nullptr,
263 2 : 10 * 1024 * 1024))
264 : {
265 : // Remove UTF-8 BOM
266 1 : size_t offset = 0;
267 1 : if (pabyData[0] == 0xEF && pabyData[1] == 0xBB &&
268 1 : pabyData[2] == 0xBF)
269 : {
270 1 : offset = 3;
271 : }
272 1 : value = reinterpret_cast<const char *>(pabyData + offset);
273 1 : VSIFree(pabyData);
274 : }
275 : else
276 : {
277 1 : return false;
278 : }
279 : }
280 :
281 4311 : if (m_decl.IsRemoveSQLCommentsEnabled())
282 50 : value = CPLRemoveSQLComments(value);
283 :
284 4311 : return true;
285 : }
286 :
287 4348 : bool GDALAlgorithmArg::Set(const std::string &value)
288 : {
289 4348 : switch (m_decl.GetType())
290 : {
291 9 : case GAAT_BOOLEAN:
292 17 : if (EQUAL(value.c_str(), "1") || EQUAL(value.c_str(), "TRUE") ||
293 17 : EQUAL(value.c_str(), "YES") || EQUAL(value.c_str(), "ON"))
294 : {
295 4 : return Set(true);
296 : }
297 5 : else if (EQUAL(value.c_str(), "0") ||
298 4 : EQUAL(value.c_str(), "FALSE") ||
299 9 : EQUAL(value.c_str(), "NO") || EQUAL(value.c_str(), "OFF"))
300 : {
301 4 : return Set(false);
302 : }
303 1 : break;
304 :
305 8 : case GAAT_INTEGER:
306 : case GAAT_INTEGER_LIST:
307 : {
308 8 : errno = 0;
309 8 : char *endptr = nullptr;
310 8 : const auto v = std::strtoll(value.c_str(), &endptr, 10);
311 13 : if (errno == 0 && v >= INT_MIN && v <= INT_MAX &&
312 5 : endptr == value.c_str() + value.size())
313 : {
314 3 : if (m_decl.GetType() == GAAT_INTEGER)
315 3 : return Set(static_cast<int>(v));
316 : else
317 1 : return Set(std::vector<int>{static_cast<int>(v)});
318 : }
319 5 : break;
320 : }
321 :
322 5 : case GAAT_REAL:
323 : case GAAT_REAL_LIST:
324 : {
325 5 : char *endptr = nullptr;
326 5 : const double v = CPLStrtod(value.c_str(), &endptr);
327 5 : if (endptr == value.c_str() + value.size())
328 : {
329 3 : if (m_decl.GetType() == GAAT_REAL)
330 3 : return Set(v);
331 : else
332 1 : return Set(std::vector<double>{v});
333 : }
334 2 : break;
335 : }
336 :
337 4290 : case GAAT_STRING:
338 4290 : break;
339 :
340 8 : case GAAT_STRING_LIST:
341 16 : return Set(std::vector<std::string>{value});
342 :
343 28 : case GAAT_DATASET:
344 28 : return SetDatasetName(value);
345 :
346 0 : case GAAT_DATASET_LIST:
347 : {
348 0 : std::vector<GDALArgDatasetValue> v;
349 0 : v.resize(1);
350 0 : v[0].Set(value);
351 0 : return Set(std::move(v));
352 : }
353 : }
354 :
355 4298 : if (m_decl.GetType() != GAAT_STRING)
356 : {
357 16 : CPLError(CE_Failure, CPLE_AppDefined,
358 : "Calling Set(std::string) on argument '%s' of type %s is not "
359 : "supported",
360 8 : GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()));
361 8 : return false;
362 : }
363 :
364 4290 : std::string newValue(value);
365 4290 : return ProcessString(newValue) && SetInternal(newValue);
366 : }
367 :
368 876 : bool GDALAlgorithmArg::Set(int value)
369 : {
370 876 : if (m_decl.GetType() == GAAT_BOOLEAN)
371 : {
372 3 : if (value == 1)
373 1 : return Set(true);
374 2 : else if (value == 0)
375 1 : return Set(false);
376 : }
377 873 : else if (m_decl.GetType() == GAAT_REAL)
378 : {
379 3 : return Set(static_cast<double>(value));
380 : }
381 870 : else if (m_decl.GetType() == GAAT_STRING)
382 : {
383 2 : return Set(std::to_string(value));
384 : }
385 868 : else if (m_decl.GetType() == GAAT_INTEGER_LIST)
386 : {
387 1 : return Set(std::vector<int>{value});
388 : }
389 867 : else if (m_decl.GetType() == GAAT_REAL_LIST)
390 : {
391 1 : return Set(std::vector<double>{static_cast<double>(value)});
392 : }
393 866 : else if (m_decl.GetType() == GAAT_STRING_LIST)
394 : {
395 2 : return Set(std::vector<std::string>{std::to_string(value)});
396 : }
397 :
398 866 : if (m_decl.GetType() != GAAT_INTEGER)
399 : {
400 2 : CPLError(
401 : CE_Failure, CPLE_AppDefined,
402 : "Calling Set(int) on argument '%s' of type %s is not supported",
403 1 : GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()));
404 1 : return false;
405 : }
406 865 : return SetInternal(value);
407 : }
408 :
409 315 : bool GDALAlgorithmArg::Set(double value)
410 : {
411 318 : if (m_decl.GetType() == GAAT_INTEGER && value >= INT_MIN &&
412 318 : value <= INT_MAX && static_cast<int>(value) == value)
413 : {
414 2 : return Set(static_cast<int>(value));
415 : }
416 313 : else if (m_decl.GetType() == GAAT_STRING)
417 : {
418 2 : return Set(std::to_string(value));
419 : }
420 313 : else if (m_decl.GetType() == GAAT_INTEGER_LIST && value >= INT_MIN &&
421 313 : value <= INT_MAX && static_cast<int>(value) == value)
422 : {
423 1 : return Set(std::vector<int>{static_cast<int>(value)});
424 : }
425 310 : else if (m_decl.GetType() == GAAT_REAL_LIST)
426 : {
427 0 : return Set(std::vector<double>{value});
428 : }
429 310 : else if (m_decl.GetType() == GAAT_STRING_LIST)
430 : {
431 2 : return Set(std::vector<std::string>{std::to_string(value)});
432 : }
433 309 : else if (m_decl.GetType() != GAAT_REAL)
434 : {
435 6 : CPLError(
436 : CE_Failure, CPLE_AppDefined,
437 : "Calling Set(double) on argument '%s' of type %s is not supported",
438 3 : GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()));
439 3 : return false;
440 : }
441 306 : return SetInternal(value);
442 : }
443 :
444 5832 : static bool CheckCanSetDatasetObject(const GDALAlgorithmArg *arg)
445 : {
446 5835 : if (arg->IsOutput() && arg->GetDatasetInputFlags() == GADV_NAME &&
447 3 : arg->GetDatasetOutputFlags() == GADV_OBJECT)
448 : {
449 3 : CPLError(
450 : CE_Failure, CPLE_AppDefined,
451 : "Dataset object '%s' is created by algorithm and cannot be set "
452 : "as an input.",
453 3 : arg->GetName().c_str());
454 3 : return false;
455 : }
456 5829 : else if ((arg->GetDatasetInputFlags() & GADV_OBJECT) == 0)
457 : {
458 8 : CPLError(CE_Failure, CPLE_AppDefined,
459 : "Dataset%s '%s' must be provided by name, not as object.",
460 8 : arg->GetMaxCount() > 1 ? "s" : "", arg->GetName().c_str());
461 4 : return false;
462 : }
463 :
464 5825 : return true;
465 : }
466 :
467 33 : bool GDALAlgorithmArg::Set(GDALDataset *ds)
468 : {
469 58 : if (m_decl.GetType() != GAAT_DATASET &&
470 25 : m_decl.GetType() != GAAT_DATASET_LIST)
471 : {
472 2 : CPLError(CE_Failure, CPLE_AppDefined,
473 : "Calling Set(GDALDataset*, bool) on argument '%s' of type %s "
474 : "is not supported",
475 1 : GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()));
476 1 : return false;
477 : }
478 32 : if (!CheckCanSetDatasetObject(this))
479 2 : return false;
480 30 : m_explicitlySet = true;
481 30 : if (m_decl.GetType() == GAAT_DATASET)
482 : {
483 6 : auto &val = *std::get<GDALArgDatasetValue *>(m_value);
484 6 : val.Set(ds);
485 : }
486 : else
487 : {
488 24 : CPLAssert(m_decl.GetType() == GAAT_DATASET_LIST);
489 24 : auto &val = *std::get<std::vector<GDALArgDatasetValue> *>(m_value);
490 24 : val.resize(1);
491 24 : val[0].Set(ds);
492 : }
493 30 : return RunAllActions();
494 : }
495 :
496 3 : bool GDALAlgorithmArg::Set(std::unique_ptr<GDALDataset> ds)
497 : {
498 3 : if (m_decl.GetType() != GAAT_DATASET)
499 : {
500 2 : CPLError(CE_Failure, CPLE_AppDefined,
501 : "Calling Set(GDALDataset*, bool) on argument '%s' of type %s "
502 : "is not supported",
503 1 : GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()));
504 1 : return false;
505 : }
506 2 : if (!CheckCanSetDatasetObject(this))
507 1 : return false;
508 1 : m_explicitlySet = true;
509 1 : auto &val = *std::get<GDALArgDatasetValue *>(m_value);
510 1 : val.Set(std::move(ds));
511 1 : return RunAllActions();
512 : }
513 :
514 627 : bool GDALAlgorithmArg::SetDatasetName(const std::string &name)
515 : {
516 627 : if (m_decl.GetType() != GAAT_DATASET)
517 : {
518 2 : CPLError(CE_Failure, CPLE_AppDefined,
519 : "Calling SetDatasetName() on argument '%s' of type %s is "
520 : "not supported",
521 1 : GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()));
522 1 : return false;
523 : }
524 626 : m_explicitlySet = true;
525 626 : std::get<GDALArgDatasetValue *>(m_value)->Set(name);
526 626 : return RunAllActions();
527 : }
528 :
529 1043 : bool GDALAlgorithmArg::SetFrom(const GDALArgDatasetValue &other)
530 : {
531 1043 : if (m_decl.GetType() != GAAT_DATASET)
532 : {
533 2 : CPLError(CE_Failure, CPLE_AppDefined,
534 : "Calling SetFrom() on argument '%s' of type %s is "
535 : "not supported",
536 1 : GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()));
537 1 : return false;
538 : }
539 1042 : if (other.GetDatasetRef() && !CheckCanSetDatasetObject(this))
540 1 : return false;
541 1041 : m_explicitlySet = true;
542 1041 : std::get<GDALArgDatasetValue *>(m_value)->SetFrom(other);
543 1041 : return RunAllActions();
544 : }
545 :
546 1134 : bool GDALAlgorithmArg::Set(const std::vector<std::string> &value)
547 : {
548 1134 : if (m_decl.GetType() == GAAT_INTEGER_LIST)
549 : {
550 3 : std::vector<int> v_i;
551 4 : for (const std::string &s : value)
552 : {
553 3 : errno = 0;
554 3 : char *endptr = nullptr;
555 3 : const auto v = std::strtoll(s.c_str(), &endptr, 10);
556 5 : if (errno == 0 && v >= INT_MIN && v <= INT_MAX &&
557 2 : endptr == s.c_str() + s.size())
558 : {
559 1 : v_i.push_back(static_cast<int>(v));
560 : }
561 : else
562 : {
563 2 : break;
564 : }
565 : }
566 3 : if (v_i.size() == value.size())
567 1 : return Set(v_i);
568 : }
569 1131 : else if (m_decl.GetType() == GAAT_REAL_LIST)
570 : {
571 2 : std::vector<double> v_d;
572 3 : for (const std::string &s : value)
573 : {
574 2 : char *endptr = nullptr;
575 2 : const double v = CPLStrtod(s.c_str(), &endptr);
576 2 : if (endptr == s.c_str() + s.size())
577 : {
578 1 : v_d.push_back(v);
579 : }
580 : else
581 : {
582 1 : break;
583 : }
584 : }
585 2 : if (v_d.size() == value.size())
586 1 : return Set(v_d);
587 : }
588 2256 : else if ((m_decl.GetType() == GAAT_INTEGER ||
589 2253 : m_decl.GetType() == GAAT_REAL ||
590 3384 : m_decl.GetType() == GAAT_STRING) &&
591 5 : value.size() == 1)
592 : {
593 4 : return Set(value[0]);
594 : }
595 1125 : else if (m_decl.GetType() == GAAT_DATASET_LIST)
596 : {
597 30 : std::vector<GDALArgDatasetValue> dsVector;
598 46 : for (const std::string &s : value)
599 31 : dsVector.emplace_back(s);
600 15 : return Set(std::move(dsVector));
601 : }
602 :
603 1113 : if (m_decl.GetType() != GAAT_STRING_LIST)
604 : {
605 10 : CPLError(CE_Failure, CPLE_AppDefined,
606 : "Calling Set(const std::vector<std::string> &) on argument "
607 : "'%s' of type %s is not supported",
608 5 : GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()));
609 5 : return false;
610 : }
611 :
612 2197 : if (m_decl.IsReadFromFileAtSyntaxAllowed() ||
613 1089 : m_decl.IsRemoveSQLCommentsEnabled())
614 : {
615 38 : std::vector<std::string> newValue(value);
616 41 : for (auto &s : newValue)
617 : {
618 22 : if (!ProcessString(s))
619 0 : return false;
620 : }
621 19 : return SetInternal(newValue);
622 : }
623 : else
624 : {
625 1089 : return SetInternal(value);
626 : }
627 : }
628 :
629 177 : bool GDALAlgorithmArg::Set(const std::vector<int> &value)
630 : {
631 177 : if (m_decl.GetType() == GAAT_REAL_LIST)
632 : {
633 2 : std::vector<double> v_d;
634 2 : for (int i : value)
635 1 : v_d.push_back(i);
636 1 : return Set(v_d);
637 : }
638 176 : else if (m_decl.GetType() == GAAT_STRING_LIST)
639 : {
640 2 : std::vector<std::string> v_s;
641 3 : for (int i : value)
642 2 : v_s.push_back(std::to_string(i));
643 1 : return Set(v_s);
644 : }
645 348 : else if ((m_decl.GetType() == GAAT_INTEGER ||
646 344 : m_decl.GetType() == GAAT_REAL ||
647 521 : m_decl.GetType() == GAAT_STRING) &&
648 5 : value.size() == 1)
649 : {
650 3 : return Set(value[0]);
651 : }
652 :
653 172 : if (m_decl.GetType() != GAAT_INTEGER_LIST)
654 : {
655 6 : CPLError(CE_Failure, CPLE_AppDefined,
656 : "Calling Set(const std::vector<int> &) on argument '%s' of "
657 : "type %s is not supported",
658 3 : GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()));
659 3 : return false;
660 : }
661 169 : return SetInternal(value);
662 : }
663 :
664 278 : bool GDALAlgorithmArg::Set(const std::vector<double> &value)
665 : {
666 278 : if (m_decl.GetType() == GAAT_INTEGER_LIST)
667 : {
668 2 : std::vector<int> v_i;
669 3 : for (double d : value)
670 : {
671 2 : if (d >= INT_MIN && d <= INT_MAX && static_cast<int>(d) == d)
672 : {
673 1 : v_i.push_back(static_cast<int>(d));
674 : }
675 : else
676 : {
677 : break;
678 : }
679 : }
680 2 : if (v_i.size() == value.size())
681 1 : return Set(v_i);
682 : }
683 276 : else if (m_decl.GetType() == GAAT_STRING_LIST)
684 : {
685 2 : std::vector<std::string> v_s;
686 3 : for (double d : value)
687 2 : v_s.push_back(std::to_string(d));
688 1 : return Set(v_s);
689 : }
690 549 : else if ((m_decl.GetType() == GAAT_INTEGER ||
691 547 : m_decl.GetType() == GAAT_REAL ||
692 823 : m_decl.GetType() == GAAT_STRING) &&
693 3 : value.size() == 1)
694 : {
695 3 : return Set(value[0]);
696 : }
697 :
698 273 : if (m_decl.GetType() != GAAT_REAL_LIST)
699 : {
700 4 : CPLError(CE_Failure, CPLE_AppDefined,
701 : "Calling Set(const std::vector<double> &) on argument '%s' of "
702 : "type %s is not supported",
703 2 : GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()));
704 2 : return false;
705 : }
706 271 : return SetInternal(value);
707 : }
708 :
709 3739 : bool GDALAlgorithmArg::Set(std::vector<GDALArgDatasetValue> &&value)
710 : {
711 3739 : if (m_decl.GetType() != GAAT_DATASET_LIST)
712 : {
713 2 : CPLError(CE_Failure, CPLE_AppDefined,
714 : "Calling Set(const std::vector<GDALArgDatasetValue> &&) on "
715 : "argument '%s' of type %s is not supported",
716 1 : GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()));
717 1 : return false;
718 : }
719 3738 : m_explicitlySet = true;
720 3738 : *std::get<std::vector<GDALArgDatasetValue> *>(m_value) = std::move(value);
721 3738 : return RunAllActions();
722 : }
723 :
724 : GDALAlgorithmArg &
725 0 : GDALAlgorithmArg::operator=(std::unique_ptr<GDALDataset> value)
726 : {
727 0 : Set(std::move(value));
728 0 : return *this;
729 : }
730 :
731 1 : bool GDALAlgorithmArg::Set(const OGRSpatialReference &value)
732 : {
733 1 : const char *const apszOptions[] = {"FORMAT=WKT2_2019", nullptr};
734 1 : return Set(value.exportToWkt(apszOptions));
735 : }
736 :
737 4511 : bool GDALAlgorithmArg::SetFrom(const GDALAlgorithmArg &other)
738 : {
739 4511 : if (m_decl.GetType() != other.GetType())
740 : {
741 2 : CPLError(CE_Failure, CPLE_AppDefined,
742 : "Calling SetFrom() on argument '%s' of type %s whereas "
743 : "other argument type is %s is not supported",
744 1 : GetName().c_str(), GDALAlgorithmArgTypeName(m_decl.GetType()),
745 : GDALAlgorithmArgTypeName(other.GetType()));
746 1 : return false;
747 : }
748 :
749 4510 : switch (m_decl.GetType())
750 : {
751 97 : case GAAT_BOOLEAN:
752 97 : *std::get<bool *>(m_value) = *std::get<bool *>(other.m_value);
753 97 : break;
754 835 : case GAAT_STRING:
755 1670 : *std::get<std::string *>(m_value) =
756 835 : *std::get<std::string *>(other.m_value);
757 835 : break;
758 7 : case GAAT_INTEGER:
759 7 : *std::get<int *>(m_value) = *std::get<int *>(other.m_value);
760 7 : break;
761 1 : case GAAT_REAL:
762 1 : *std::get<double *>(m_value) = *std::get<double *>(other.m_value);
763 1 : break;
764 1037 : case GAAT_DATASET:
765 1037 : return SetFrom(other.Get<GDALArgDatasetValue>());
766 60 : case GAAT_STRING_LIST:
767 120 : *std::get<std::vector<std::string> *>(m_value) =
768 60 : *std::get<std::vector<std::string> *>(other.m_value);
769 60 : break;
770 1 : case GAAT_INTEGER_LIST:
771 2 : *std::get<std::vector<int> *>(m_value) =
772 1 : *std::get<std::vector<int> *>(other.m_value);
773 1 : break;
774 1 : case GAAT_REAL_LIST:
775 2 : *std::get<std::vector<double> *>(m_value) =
776 1 : *std::get<std::vector<double> *>(other.m_value);
777 1 : break;
778 2471 : case GAAT_DATASET_LIST:
779 : {
780 2471 : std::get<std::vector<GDALArgDatasetValue> *>(m_value)->clear();
781 2477 : for (const auto &val :
782 7425 : *std::get<std::vector<GDALArgDatasetValue> *>(other.m_value))
783 : {
784 4954 : GDALArgDatasetValue v;
785 2477 : v.SetFrom(val);
786 2477 : std::get<std::vector<GDALArgDatasetValue> *>(m_value)
787 2477 : ->push_back(std::move(v));
788 : }
789 2471 : break;
790 : }
791 : }
792 3473 : m_explicitlySet = true;
793 3473 : return RunAllActions();
794 : }
795 :
796 : /************************************************************************/
797 : /* GDALAlgorithmArg::RunAllActions() */
798 : /************************************************************************/
799 :
800 17186 : bool GDALAlgorithmArg::RunAllActions()
801 : {
802 17186 : if (!RunValidationActions())
803 147 : return false;
804 17039 : RunActions();
805 17039 : return true;
806 : }
807 :
808 : /************************************************************************/
809 : /* GDALAlgorithmArg::RunActions() */
810 : /************************************************************************/
811 :
812 17040 : void GDALAlgorithmArg::RunActions()
813 : {
814 17356 : for (const auto &f : m_actions)
815 316 : f();
816 17040 : }
817 :
818 : /************************************************************************/
819 : /* GDALAlgorithmArg::ValidateChoice() */
820 : /************************************************************************/
821 :
822 : // Returns the canonical value if matching a valid choice, or empty string
823 : // otherwise.
824 2720 : std::string GDALAlgorithmArg::ValidateChoice(const std::string &value) const
825 : {
826 15628 : for (const std::string &choice : GetChoices())
827 : {
828 15510 : if (EQUAL(value.c_str(), choice.c_str()))
829 : {
830 2602 : return choice;
831 : }
832 : }
833 :
834 190 : for (const std::string &choice : GetHiddenChoices())
835 : {
836 172 : if (EQUAL(value.c_str(), choice.c_str()))
837 : {
838 100 : return choice;
839 : }
840 : }
841 :
842 36 : std::string expected;
843 222 : for (const auto &choice : GetChoices())
844 : {
845 204 : if (!expected.empty())
846 186 : expected += ", ";
847 204 : expected += '\'';
848 204 : expected += choice;
849 204 : expected += '\'';
850 : }
851 18 : if (m_owner && m_owner->IsCalledFromCommandLine() && value == "?")
852 : {
853 6 : return "?";
854 : }
855 24 : CPLError(CE_Failure, CPLE_IllegalArg,
856 : "Invalid value '%s' for string argument '%s'. Should be "
857 : "one among %s.",
858 12 : value.c_str(), GetName().c_str(), expected.c_str());
859 12 : return std::string();
860 : }
861 :
862 : /************************************************************************/
863 : /* GDALAlgorithmArg::ValidateIntRange() */
864 : /************************************************************************/
865 :
866 2761 : bool GDALAlgorithmArg::ValidateIntRange(int val) const
867 : {
868 2761 : bool ret = true;
869 :
870 2761 : const auto [minVal, minValIsIncluded] = GetMinValue();
871 2761 : if (!std::isnan(minVal))
872 : {
873 2081 : if (minValIsIncluded && val < minVal)
874 : {
875 3 : CPLError(CE_Failure, CPLE_IllegalArg,
876 : "Value of argument '%s' is %d, but should be >= %d",
877 3 : GetName().c_str(), val, static_cast<int>(minVal));
878 3 : ret = false;
879 : }
880 2078 : else if (!minValIsIncluded && val <= minVal)
881 : {
882 1 : CPLError(CE_Failure, CPLE_IllegalArg,
883 : "Value of argument '%s' is %d, but should be > %d",
884 1 : GetName().c_str(), val, static_cast<int>(minVal));
885 1 : ret = false;
886 : }
887 : }
888 :
889 2761 : const auto [maxVal, maxValIsIncluded] = GetMaxValue();
890 2761 : if (!std::isnan(maxVal))
891 : {
892 :
893 430 : if (maxValIsIncluded && val > maxVal)
894 : {
895 1 : CPLError(CE_Failure, CPLE_IllegalArg,
896 : "Value of argument '%s' is %d, but should be <= %d",
897 1 : GetName().c_str(), val, static_cast<int>(maxVal));
898 1 : ret = false;
899 : }
900 429 : else if (!maxValIsIncluded && val >= maxVal)
901 : {
902 1 : CPLError(CE_Failure, CPLE_IllegalArg,
903 : "Value of argument '%s' is %d, but should be < %d",
904 1 : GetName().c_str(), val, static_cast<int>(maxVal));
905 1 : ret = false;
906 : }
907 : }
908 :
909 2761 : return ret;
910 : }
911 :
912 : /************************************************************************/
913 : /* GDALAlgorithmArg::ValidateRealRange() */
914 : /************************************************************************/
915 :
916 2259 : bool GDALAlgorithmArg::ValidateRealRange(double val) const
917 : {
918 2259 : bool ret = true;
919 :
920 2259 : const auto [minVal, minValIsIncluded] = GetMinValue();
921 2259 : if (!std::isnan(minVal))
922 : {
923 216 : if (minValIsIncluded && !(val >= minVal))
924 : {
925 11 : CPLError(CE_Failure, CPLE_IllegalArg,
926 : "Value of argument '%s' is %g, but should be >= %g",
927 11 : GetName().c_str(), val, minVal);
928 11 : ret = false;
929 : }
930 205 : else if (!minValIsIncluded && !(val > minVal))
931 : {
932 4 : CPLError(CE_Failure, CPLE_IllegalArg,
933 : "Value of argument '%s' is %g, but should be > %g",
934 4 : GetName().c_str(), val, minVal);
935 4 : ret = false;
936 : }
937 : }
938 :
939 2259 : const auto [maxVal, maxValIsIncluded] = GetMaxValue();
940 2259 : if (!std::isnan(maxVal))
941 : {
942 :
943 58 : if (maxValIsIncluded && !(val <= maxVal))
944 : {
945 2 : CPLError(CE_Failure, CPLE_IllegalArg,
946 : "Value of argument '%s' is %g, but should be <= %g",
947 2 : GetName().c_str(), val, maxVal);
948 2 : ret = false;
949 : }
950 56 : else if (!maxValIsIncluded && !(val < maxVal))
951 : {
952 1 : CPLError(CE_Failure, CPLE_IllegalArg,
953 : "Value of argument '%s' is %g, but should be < %g",
954 1 : GetName().c_str(), val, maxVal);
955 1 : ret = false;
956 : }
957 : }
958 :
959 2259 : return ret;
960 : }
961 :
962 : /************************************************************************/
963 : /* CheckDuplicateValues() */
964 : /************************************************************************/
965 :
966 : template <class T>
967 95 : static bool CheckDuplicateValues(const GDALAlgorithmArg *arg,
968 : const std::vector<T> &values)
969 : {
970 190 : auto tmpValues = values;
971 95 : bool bHasDupValues = false;
972 : if constexpr (std::is_floating_point_v<T>)
973 : {
974 : // Avoid undefined behavior with NaN values
975 4 : std::sort(tmpValues.begin(), tmpValues.end(),
976 21 : [](T a, T b)
977 : {
978 21 : if (std::isnan(a) && !std::isnan(b))
979 3 : return true;
980 18 : if (std::isnan(b))
981 10 : return false;
982 8 : return a < b;
983 : });
984 :
985 : bHasDupValues =
986 4 : std::adjacent_find(tmpValues.begin(), tmpValues.end(),
987 6 : [](T a, T b)
988 : {
989 6 : if (std::isnan(a) && std::isnan(b))
990 1 : return true;
991 5 : return a == b;
992 8 : }) != tmpValues.end();
993 : }
994 : else
995 : {
996 91 : std::sort(tmpValues.begin(), tmpValues.end());
997 91 : bHasDupValues = std::adjacent_find(tmpValues.begin(),
998 182 : tmpValues.end()) != tmpValues.end();
999 : }
1000 95 : if (bHasDupValues)
1001 : {
1002 10 : CPLError(CE_Failure, CPLE_AppDefined,
1003 : "'%s' must be a list of unique values.",
1004 10 : arg->GetName().c_str());
1005 10 : return false;
1006 : }
1007 85 : return true;
1008 : }
1009 :
1010 : /************************************************************************/
1011 : /* GDALAlgorithmArg::RunValidationActions() */
1012 : /************************************************************************/
1013 :
1014 38148 : bool GDALAlgorithmArg::RunValidationActions()
1015 : {
1016 38148 : bool ret = true;
1017 :
1018 38148 : if (GetType() == GAAT_STRING && !GetChoices().empty())
1019 : {
1020 1783 : auto &val = Get<std::string>();
1021 3566 : std::string validVal = ValidateChoice(val);
1022 1783 : if (validVal.empty())
1023 7 : ret = false;
1024 : else
1025 1776 : val = std::move(validVal);
1026 : }
1027 36365 : else if (GetType() == GAAT_STRING_LIST && !GetChoices().empty())
1028 : {
1029 677 : auto &values = Get<std::vector<std::string>>();
1030 1614 : for (std::string &val : values)
1031 : {
1032 1874 : std::string validVal = ValidateChoice(val);
1033 937 : if (validVal.empty())
1034 5 : ret = false;
1035 : else
1036 932 : val = std::move(validVal);
1037 : }
1038 : }
1039 :
1040 : const auto CheckMinCharCount =
1041 1143 : [this, &ret](const std::string &val, int nMinCharCount)
1042 : {
1043 1131 : if (val.size() < static_cast<size_t>(nMinCharCount))
1044 : {
1045 12 : CPLError(CE_Failure, CPLE_IllegalArg,
1046 : "Value of argument '%s' is '%s', but should have at least "
1047 : "%d character%s",
1048 6 : GetName().c_str(), val.c_str(), nMinCharCount,
1049 : nMinCharCount > 1 ? "s" : "");
1050 6 : ret = false;
1051 : }
1052 39279 : };
1053 :
1054 : const auto CheckMaxCharCount =
1055 13644 : [this, &ret](const std::string &val, int nMaxCharCount)
1056 : {
1057 13642 : if (val.size() > static_cast<size_t>(nMaxCharCount))
1058 : {
1059 2 : CPLError(
1060 : CE_Failure, CPLE_IllegalArg,
1061 : "Value of argument '%s' is '%s', but should have no more than "
1062 : "%d character%s",
1063 1 : GetName().c_str(), val.c_str(), nMaxCharCount,
1064 : nMaxCharCount > 1 ? "s" : "");
1065 1 : ret = false;
1066 : }
1067 51790 : };
1068 :
1069 38148 : switch (GetType())
1070 : {
1071 2868 : case GAAT_BOOLEAN:
1072 2868 : break;
1073 :
1074 10683 : case GAAT_STRING:
1075 : {
1076 10683 : const auto &val = Get<std::string>();
1077 10683 : const int nMinCharCount = GetMinCharCount();
1078 10683 : if (nMinCharCount > 0)
1079 : {
1080 1049 : CheckMinCharCount(val, nMinCharCount);
1081 : }
1082 :
1083 10683 : const int nMaxCharCount = GetMaxCharCount();
1084 10683 : CheckMaxCharCount(val, nMaxCharCount);
1085 10683 : break;
1086 : }
1087 :
1088 2448 : case GAAT_STRING_LIST:
1089 : {
1090 2448 : const int nMinCharCount = GetMinCharCount();
1091 2448 : const int nMaxCharCount = GetMaxCharCount();
1092 2448 : const auto &values = Get<std::vector<std::string>>();
1093 5407 : for (const auto &val : values)
1094 : {
1095 2959 : if (nMinCharCount > 0)
1096 82 : CheckMinCharCount(val, nMinCharCount);
1097 2959 : CheckMaxCharCount(val, nMaxCharCount);
1098 : }
1099 :
1100 2525 : if (!GetDuplicateValuesAllowed() &&
1101 77 : !CheckDuplicateValues(this, values))
1102 2 : ret = false;
1103 2448 : break;
1104 : }
1105 :
1106 2041 : case GAAT_INTEGER:
1107 : {
1108 2041 : ret = ValidateIntRange(Get<int>()) && ret;
1109 2041 : break;
1110 : }
1111 :
1112 356 : case GAAT_INTEGER_LIST:
1113 : {
1114 356 : const auto &values = Get<std::vector<int>>();
1115 1076 : for (int v : values)
1116 720 : ret = ValidateIntRange(v) && ret;
1117 :
1118 359 : if (!GetDuplicateValuesAllowed() &&
1119 3 : !CheckDuplicateValues(this, values))
1120 1 : ret = false;
1121 356 : break;
1122 : }
1123 :
1124 601 : case GAAT_REAL:
1125 : {
1126 601 : ret = ValidateRealRange(Get<double>()) && ret;
1127 601 : break;
1128 : }
1129 :
1130 603 : case GAAT_REAL_LIST:
1131 : {
1132 603 : const auto &values = Get<std::vector<double>>();
1133 2261 : for (double v : values)
1134 1658 : ret = ValidateRealRange(v) && ret;
1135 :
1136 607 : if (!GetDuplicateValuesAllowed() &&
1137 4 : !CheckDuplicateValues(this, values))
1138 2 : ret = false;
1139 603 : break;
1140 : }
1141 :
1142 6145 : case GAAT_DATASET:
1143 6145 : break;
1144 :
1145 12403 : case GAAT_DATASET_LIST:
1146 : {
1147 12403 : if (!GetDuplicateValuesAllowed())
1148 : {
1149 11 : const auto &values = Get<std::vector<GDALArgDatasetValue>>();
1150 22 : std::vector<std::string> aosValues;
1151 34 : for (const auto &v : values)
1152 : {
1153 23 : const GDALDataset *poDS = v.GetDatasetRef();
1154 23 : if (poDS)
1155 : {
1156 16 : auto poDriver = poDS->GetDriver();
1157 : // The dataset name for a MEM driver is not relevant,
1158 : // so use the pointer address
1159 32 : if ((poDriver &&
1160 24 : EQUAL(poDriver->GetDescription(), "MEM")) ||
1161 8 : poDS->GetDescription()[0] == 0)
1162 : {
1163 8 : aosValues.push_back(CPLSPrintf("%p", poDS));
1164 : }
1165 : else
1166 : {
1167 8 : aosValues.push_back(poDS->GetDescription());
1168 : }
1169 : }
1170 : else
1171 : {
1172 7 : aosValues.push_back(v.GetName());
1173 : }
1174 : }
1175 11 : if (!CheckDuplicateValues(this, aosValues))
1176 5 : ret = false;
1177 : }
1178 12403 : break;
1179 : }
1180 : }
1181 :
1182 38148 : if (GDALAlgorithmArgTypeIsList(GetType()))
1183 : {
1184 15810 : int valueCount = 0;
1185 15810 : if (GetType() == GAAT_STRING_LIST)
1186 : {
1187 2448 : valueCount =
1188 2448 : static_cast<int>(Get<std::vector<std::string>>().size());
1189 : }
1190 13362 : else if (GetType() == GAAT_INTEGER_LIST)
1191 : {
1192 356 : valueCount = static_cast<int>(Get<std::vector<int>>().size());
1193 : }
1194 13006 : else if (GetType() == GAAT_REAL_LIST)
1195 : {
1196 603 : valueCount = static_cast<int>(Get<std::vector<double>>().size());
1197 : }
1198 12403 : else if (GetType() == GAAT_DATASET_LIST)
1199 : {
1200 12403 : valueCount = static_cast<int>(
1201 12403 : Get<std::vector<GDALArgDatasetValue>>().size());
1202 : }
1203 :
1204 15810 : if (valueCount != GetMinCount() && GetMinCount() == GetMaxCount())
1205 : {
1206 14 : ReportError(CE_Failure, CPLE_AppDefined,
1207 : "%d value%s been specified for argument '%s', "
1208 : "whereas exactly %d %s expected.",
1209 : valueCount, valueCount > 1 ? "s have" : " has",
1210 7 : GetName().c_str(), GetMinCount(),
1211 7 : GetMinCount() > 1 ? "were" : "was");
1212 7 : ret = false;
1213 : }
1214 15803 : else if (valueCount < GetMinCount())
1215 : {
1216 6 : ReportError(CE_Failure, CPLE_AppDefined,
1217 : "Only %d value%s been specified for argument '%s', "
1218 : "whereas at least %d %s expected.",
1219 : valueCount, valueCount > 1 ? "s have" : " has",
1220 3 : GetName().c_str(), GetMinCount(),
1221 3 : GetMinCount() > 1 ? "were" : "was");
1222 3 : ret = false;
1223 : }
1224 15800 : else if (valueCount > GetMaxCount())
1225 : {
1226 2 : ReportError(CE_Failure, CPLE_AppDefined,
1227 : "%d value%s been specified for argument '%s', "
1228 : "whereas at most %d %s expected.",
1229 : valueCount, valueCount > 1 ? "s have" : " has",
1230 1 : GetName().c_str(), GetMaxCount(),
1231 1 : GetMaxCount() > 1 ? "were" : "was");
1232 1 : ret = false;
1233 : }
1234 : }
1235 :
1236 38148 : if (ret)
1237 : {
1238 45278 : for (const auto &f : m_validationActions)
1239 : {
1240 7194 : if (!f())
1241 92 : ret = false;
1242 : }
1243 : }
1244 :
1245 38148 : return ret;
1246 : }
1247 :
1248 : /************************************************************************/
1249 : /* GDALAlgorithmArg::ReportError() */
1250 : /************************************************************************/
1251 :
1252 11 : void GDALAlgorithmArg::ReportError(CPLErr eErrClass, CPLErrorNum err_no,
1253 : const char *fmt, ...) const
1254 : {
1255 : va_list args;
1256 11 : va_start(args, fmt);
1257 11 : if (m_owner)
1258 : {
1259 11 : m_owner->ReportError(eErrClass, err_no, "%s",
1260 22 : CPLString().vPrintf(fmt, args).c_str());
1261 : }
1262 : else
1263 : {
1264 0 : CPLError(eErrClass, err_no, "%s",
1265 0 : CPLString().vPrintf(fmt, args).c_str());
1266 : }
1267 11 : va_end(args);
1268 11 : }
1269 :
1270 : /************************************************************************/
1271 : /* GDALAlgorithmArg::GetEscapedString() */
1272 : /************************************************************************/
1273 :
1274 : /* static */
1275 197 : std::string GDALAlgorithmArg::GetEscapedString(const std::string &s)
1276 : {
1277 215 : if (s.find_first_of("\" \\,") != std::string::npos &&
1278 9 : !(s.size() > 4 &&
1279 9 : s[0] == GDALAbstractPipelineAlgorithm::OPEN_NESTED_PIPELINE[0] &&
1280 2 : s[1] == ' ' && s[s.size() - 2] == ' ' &&
1281 2 : s.back() == GDALAbstractPipelineAlgorithm::CLOSE_NESTED_PIPELINE[0]))
1282 : {
1283 14 : return std::string("\"")
1284 : .append(
1285 14 : CPLString(s).replaceAll('\\', "\\\\").replaceAll('"', "\\\""))
1286 7 : .append("\"");
1287 : }
1288 : else
1289 : {
1290 190 : return s;
1291 : }
1292 : }
1293 :
1294 : /************************************************************************/
1295 : /* GDALAlgorithmArg::Serialize() */
1296 : /************************************************************************/
1297 :
1298 43 : bool GDALAlgorithmArg::Serialize(std::string &serializedArg,
1299 : bool absolutePath) const
1300 : {
1301 43 : serializedArg.clear();
1302 :
1303 43 : if (!IsExplicitlySet())
1304 : {
1305 0 : return false;
1306 : }
1307 :
1308 86 : std::string ret = "--";
1309 43 : ret += GetName();
1310 43 : if (GetType() == GAAT_BOOLEAN)
1311 : {
1312 0 : serializedArg = std::move(ret);
1313 0 : return true;
1314 : }
1315 :
1316 7 : const auto AddListValueSeparator = [this, &ret]()
1317 : {
1318 2 : if (GetPackedValuesAllowed())
1319 : {
1320 1 : ret += ',';
1321 : }
1322 : else
1323 : {
1324 1 : ret += " --";
1325 1 : ret += GetName();
1326 1 : ret += ' ';
1327 : }
1328 45 : };
1329 :
1330 0 : const auto MakeAbsolutePath = [](const std::string &filename)
1331 : {
1332 : VSIStatBufL sStat;
1333 0 : if (VSIStatL(filename.c_str(), &sStat) != 0 ||
1334 0 : !CPLIsFilenameRelative(filename.c_str()))
1335 0 : return filename;
1336 0 : char *pszCWD = CPLGetCurrentDir();
1337 0 : if (!pszCWD)
1338 0 : return filename;
1339 : const auto absPath =
1340 0 : CPLFormFilenameSafe(pszCWD, filename.c_str(), nullptr);
1341 0 : CPLFree(pszCWD);
1342 0 : return absPath;
1343 : };
1344 :
1345 43 : ret += ' ';
1346 43 : switch (GetType())
1347 : {
1348 0 : case GAAT_BOOLEAN:
1349 0 : break;
1350 8 : case GAAT_STRING:
1351 : {
1352 8 : const auto &val = Get<std::string>();
1353 8 : ret += GetEscapedString(val);
1354 8 : break;
1355 : }
1356 1 : case GAAT_INTEGER:
1357 : {
1358 1 : ret += CPLSPrintf("%d", Get<int>());
1359 1 : break;
1360 : }
1361 0 : case GAAT_REAL:
1362 : {
1363 0 : ret += CPLSPrintf("%.17g", Get<double>());
1364 0 : break;
1365 : }
1366 2 : case GAAT_DATASET:
1367 : {
1368 2 : const auto &val = Get<GDALArgDatasetValue>();
1369 2 : const auto &str = val.GetName();
1370 2 : if (str.empty())
1371 : {
1372 0 : return false;
1373 : }
1374 2 : ret += GetEscapedString(absolutePath ? MakeAbsolutePath(str) : str);
1375 2 : break;
1376 : }
1377 5 : case GAAT_STRING_LIST:
1378 : {
1379 5 : const auto &vals = Get<std::vector<std::string>>();
1380 11 : for (size_t i = 0; i < vals.size(); ++i)
1381 : {
1382 6 : if (i > 0)
1383 2 : AddListValueSeparator();
1384 6 : ret += GetEscapedString(vals[i]);
1385 : }
1386 5 : break;
1387 : }
1388 0 : case GAAT_INTEGER_LIST:
1389 : {
1390 0 : const auto &vals = Get<std::vector<int>>();
1391 0 : for (size_t i = 0; i < vals.size(); ++i)
1392 : {
1393 0 : if (i > 0)
1394 0 : AddListValueSeparator();
1395 0 : ret += CPLSPrintf("%d", vals[i]);
1396 : }
1397 0 : break;
1398 : }
1399 0 : case GAAT_REAL_LIST:
1400 : {
1401 0 : const auto &vals = Get<std::vector<double>>();
1402 0 : for (size_t i = 0; i < vals.size(); ++i)
1403 : {
1404 0 : if (i > 0)
1405 0 : AddListValueSeparator();
1406 0 : ret += CPLSPrintf("%.17g", vals[i]);
1407 : }
1408 0 : break;
1409 : }
1410 27 : case GAAT_DATASET_LIST:
1411 : {
1412 27 : const auto &vals = Get<std::vector<GDALArgDatasetValue>>();
1413 53 : for (size_t i = 0; i < vals.size(); ++i)
1414 : {
1415 27 : if (i > 0)
1416 0 : AddListValueSeparator();
1417 27 : const auto &val = vals[i];
1418 27 : const auto &str = val.GetName();
1419 27 : if (str.empty())
1420 : {
1421 1 : return false;
1422 : }
1423 52 : ret += GetEscapedString(absolutePath ? MakeAbsolutePath(str)
1424 26 : : str);
1425 : }
1426 26 : break;
1427 : }
1428 : }
1429 :
1430 42 : serializedArg = std::move(ret);
1431 42 : return true;
1432 : }
1433 :
1434 : /************************************************************************/
1435 : /* ~GDALInConstructionAlgorithmArg() */
1436 : /************************************************************************/
1437 :
1438 : GDALInConstructionAlgorithmArg::~GDALInConstructionAlgorithmArg() = default;
1439 :
1440 : /************************************************************************/
1441 : /* GDALInConstructionAlgorithmArg::AddAlias() */
1442 : /************************************************************************/
1443 :
1444 : GDALInConstructionAlgorithmArg &
1445 66603 : GDALInConstructionAlgorithmArg::AddAlias(const std::string &alias)
1446 : {
1447 66603 : m_decl.AddAlias(alias);
1448 66603 : if (m_owner)
1449 66603 : m_owner->AddAliasFor(this, alias);
1450 66603 : return *this;
1451 : }
1452 :
1453 : /************************************************************************/
1454 : /* GDALInConstructionAlgorithmArg::AddHiddenAlias() */
1455 : /************************************************************************/
1456 :
1457 : GDALInConstructionAlgorithmArg &
1458 17863 : GDALInConstructionAlgorithmArg::AddHiddenAlias(const std::string &alias)
1459 : {
1460 17863 : m_decl.AddHiddenAlias(alias);
1461 17863 : if (m_owner)
1462 17863 : m_owner->AddAliasFor(this, alias);
1463 17863 : return *this;
1464 : }
1465 :
1466 : /************************************************************************/
1467 : /* GDALInConstructionAlgorithmArg::AddShortNameAlias() */
1468 : /************************************************************************/
1469 :
1470 : GDALInConstructionAlgorithmArg &
1471 49 : GDALInConstructionAlgorithmArg::AddShortNameAlias(char shortNameAlias)
1472 : {
1473 49 : m_decl.AddShortNameAlias(shortNameAlias);
1474 49 : if (m_owner)
1475 49 : m_owner->AddShortNameAliasFor(this, shortNameAlias);
1476 49 : return *this;
1477 : }
1478 :
1479 : /************************************************************************/
1480 : /* GDALInConstructionAlgorithmArg::SetPositional() */
1481 : /************************************************************************/
1482 :
1483 23183 : GDALInConstructionAlgorithmArg &GDALInConstructionAlgorithmArg::SetPositional()
1484 : {
1485 23183 : m_decl.SetPositional();
1486 23183 : if (m_owner)
1487 23183 : m_owner->SetPositional(this);
1488 23183 : return *this;
1489 : }
1490 :
1491 : /************************************************************************/
1492 : /* GDALArgDatasetValue::GDALArgDatasetValue() */
1493 : /************************************************************************/
1494 :
1495 1374 : GDALArgDatasetValue::GDALArgDatasetValue(GDALDataset *poDS)
1496 2748 : : m_poDS(poDS), m_name(m_poDS ? m_poDS->GetDescription() : std::string()),
1497 1374 : m_nameSet(true)
1498 : {
1499 1374 : if (m_poDS)
1500 1374 : m_poDS->Reference();
1501 1374 : }
1502 :
1503 : /************************************************************************/
1504 : /* GDALArgDatasetValue::Set() */
1505 : /************************************************************************/
1506 :
1507 2373 : void GDALArgDatasetValue::Set(const std::string &name)
1508 : {
1509 2373 : Close();
1510 2373 : m_name = name;
1511 2373 : m_nameSet = true;
1512 2373 : if (m_ownerArg)
1513 2367 : m_ownerArg->NotifyValueSet();
1514 2373 : }
1515 :
1516 : /************************************************************************/
1517 : /* GDALArgDatasetValue::Set() */
1518 : /************************************************************************/
1519 :
1520 2191 : void GDALArgDatasetValue::Set(std::unique_ptr<GDALDataset> poDS)
1521 : {
1522 2191 : Close();
1523 2191 : m_poDS = poDS.release();
1524 2191 : m_name = m_poDS ? m_poDS->GetDescription() : std::string();
1525 2191 : m_nameSet = true;
1526 2191 : if (m_ownerArg)
1527 1989 : m_ownerArg->NotifyValueSet();
1528 2191 : }
1529 :
1530 : /************************************************************************/
1531 : /* GDALArgDatasetValue::Set() */
1532 : /************************************************************************/
1533 :
1534 8695 : void GDALArgDatasetValue::Set(GDALDataset *poDS)
1535 : {
1536 8695 : Close();
1537 8695 : m_poDS = poDS;
1538 8695 : if (m_poDS)
1539 7781 : m_poDS->Reference();
1540 8695 : m_name = m_poDS ? m_poDS->GetDescription() : std::string();
1541 8695 : m_nameSet = true;
1542 8695 : if (m_ownerArg)
1543 3483 : m_ownerArg->NotifyValueSet();
1544 8695 : }
1545 :
1546 : /************************************************************************/
1547 : /* GDALArgDatasetValue::SetFrom() */
1548 : /************************************************************************/
1549 :
1550 3518 : void GDALArgDatasetValue::SetFrom(const GDALArgDatasetValue &other)
1551 : {
1552 3518 : Close();
1553 3518 : m_name = other.m_name;
1554 3518 : m_nameSet = other.m_nameSet;
1555 3518 : m_poDS = other.m_poDS;
1556 3518 : if (m_poDS)
1557 2457 : m_poDS->Reference();
1558 3518 : }
1559 :
1560 : /************************************************************************/
1561 : /* GDALArgDatasetValue::~GDALArgDatasetValue() */
1562 : /************************************************************************/
1563 :
1564 33785 : GDALArgDatasetValue::~GDALArgDatasetValue()
1565 : {
1566 33785 : Close();
1567 33785 : }
1568 :
1569 : /************************************************************************/
1570 : /* GDALArgDatasetValue::Close() */
1571 : /************************************************************************/
1572 :
1573 56545 : bool GDALArgDatasetValue::Close()
1574 : {
1575 56545 : bool ret = true;
1576 56545 : if (m_poDS && m_poDS->Dereference() == 0)
1577 : {
1578 3623 : ret = m_poDS->Close() == CE_None;
1579 3623 : delete m_poDS;
1580 : }
1581 56545 : m_poDS = nullptr;
1582 56545 : return ret;
1583 : }
1584 :
1585 : /************************************************************************/
1586 : /* GDALArgDatasetValue::operator=() */
1587 : /************************************************************************/
1588 :
1589 2 : GDALArgDatasetValue &GDALArgDatasetValue::operator=(GDALArgDatasetValue &&other)
1590 : {
1591 2 : Close();
1592 2 : m_poDS = other.m_poDS;
1593 2 : m_name = other.m_name;
1594 2 : m_nameSet = other.m_nameSet;
1595 2 : other.m_poDS = nullptr;
1596 2 : other.m_name.clear();
1597 2 : other.m_nameSet = false;
1598 2 : return *this;
1599 : }
1600 :
1601 : /************************************************************************/
1602 : /* GDALArgDatasetValue::GetDataset() */
1603 : /************************************************************************/
1604 :
1605 1101 : GDALDataset *GDALArgDatasetValue::GetDatasetIncreaseRefCount()
1606 : {
1607 1101 : if (m_poDS)
1608 1100 : m_poDS->Reference();
1609 1101 : return m_poDS;
1610 : }
1611 :
1612 : /************************************************************************/
1613 : /* GDALArgDatasetValue(GDALArgDatasetValue &&other) */
1614 : /************************************************************************/
1615 :
1616 3401 : GDALArgDatasetValue::GDALArgDatasetValue(GDALArgDatasetValue &&other)
1617 3401 : : m_poDS(other.m_poDS), m_name(other.m_name), m_nameSet(other.m_nameSet)
1618 : {
1619 3401 : other.m_poDS = nullptr;
1620 3401 : other.m_name.clear();
1621 3401 : }
1622 :
1623 : /************************************************************************/
1624 : /* GDALInConstructionAlgorithmArg::SetIsCRSArg() */
1625 : /************************************************************************/
1626 :
1627 3454 : GDALInConstructionAlgorithmArg &GDALInConstructionAlgorithmArg::SetIsCRSArg(
1628 : bool noneAllowed, const std::vector<std::string> &specialValues)
1629 : {
1630 3454 : if (GetType() != GAAT_STRING)
1631 : {
1632 1 : CPLError(CE_Failure, CPLE_AppDefined,
1633 : "SetIsCRSArg() can only be called on a String argument");
1634 1 : return *this;
1635 : }
1636 : AddValidationAction(
1637 771 : [this, noneAllowed, specialValues]()
1638 : {
1639 : const std::string &osVal =
1640 : static_cast<const GDALInConstructionAlgorithmArg *>(this)
1641 382 : ->Get<std::string>();
1642 382 : if (osVal == "?" && m_owner && m_owner->IsCalledFromCommandLine())
1643 0 : return true;
1644 :
1645 751 : if ((!noneAllowed || (osVal != "none" && osVal != "null")) &&
1646 369 : std::find(specialValues.begin(), specialValues.end(), osVal) ==
1647 751 : specialValues.end())
1648 : {
1649 361 : OGRSpatialReference oSRS;
1650 361 : if (oSRS.SetFromUserInput(osVal.c_str()) != OGRERR_NONE)
1651 : {
1652 7 : m_owner->ReportError(CE_Failure, CPLE_AppDefined,
1653 : "Invalid value for '%s' argument",
1654 7 : GetName().c_str());
1655 7 : return false;
1656 : }
1657 : }
1658 375 : return true;
1659 3453 : });
1660 :
1661 : SetAutoCompleteFunction(
1662 44 : [this, noneAllowed, specialValues](const std::string ¤tValue)
1663 : {
1664 11 : bool bIsRaster = false;
1665 11 : OGREnvelope sDatasetLongLatEnv;
1666 22 : std::string osCelestialBodyName;
1667 11 : if (GetName() == GDAL_ARG_NAME_OUTPUT_CRS)
1668 : {
1669 11 : auto inputArg = m_owner->GetArg(GDAL_ARG_NAME_INPUT);
1670 11 : if (inputArg && inputArg->GetType() == GAAT_DATASET_LIST)
1671 : {
1672 : auto &val =
1673 11 : inputArg->Get<std::vector<GDALArgDatasetValue>>();
1674 11 : if (val.size() == 1)
1675 : {
1676 4 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
1677 : auto poDS = std::unique_ptr<GDALDataset>(
1678 4 : GDALDataset::Open(val[0].GetName().c_str()));
1679 2 : if (poDS)
1680 : {
1681 2 : bIsRaster = poDS->GetRasterCount() != 0;
1682 2 : if (auto poCRS = poDS->GetSpatialRef())
1683 : {
1684 : const char *pszCelestialBodyName =
1685 2 : poCRS->GetCelestialBodyName();
1686 2 : if (pszCelestialBodyName)
1687 2 : osCelestialBodyName = pszCelestialBodyName;
1688 :
1689 2 : if (!pszCelestialBodyName ||
1690 2 : !EQUAL(pszCelestialBodyName, "Earth"))
1691 : {
1692 0 : OGRSpatialReference oLongLat;
1693 0 : oLongLat.CopyGeogCSFrom(poCRS);
1694 0 : oLongLat.SetAxisMappingStrategy(
1695 : OAMS_TRADITIONAL_GIS_ORDER);
1696 0 : poDS->GetExtent(&sDatasetLongLatEnv,
1697 0 : &oLongLat);
1698 : }
1699 : else
1700 : {
1701 2 : poDS->GetExtentWGS84LongLat(
1702 2 : &sDatasetLongLatEnv);
1703 : }
1704 : }
1705 : }
1706 : }
1707 : }
1708 : }
1709 :
1710 : const auto IsCRSCompatible =
1711 42959 : [bIsRaster, &sDatasetLongLatEnv,
1712 73695 : &osCelestialBodyName](const OSRCRSInfo *crsInfo)
1713 : {
1714 42959 : if (!sDatasetLongLatEnv.IsInit())
1715 30685 : return true;
1716 24108 : return crsInfo->eType != OSR_CRS_TYPE_VERTICAL &&
1717 11834 : !(bIsRaster &&
1718 5917 : crsInfo->eType == OSR_CRS_TYPE_GEOCENTRIC) &&
1719 11652 : crsInfo->dfWestLongitudeDeg <
1720 11652 : crsInfo->dfEastLongitudeDeg &&
1721 11517 : sDatasetLongLatEnv.MinX < crsInfo->dfEastLongitudeDeg &&
1722 5618 : sDatasetLongLatEnv.MaxX > crsInfo->dfWestLongitudeDeg &&
1723 615 : sDatasetLongLatEnv.MinY < crsInfo->dfNorthLatitudeDeg &&
1724 24437 : sDatasetLongLatEnv.MaxY > crsInfo->dfSouthLatitudeDeg &&
1725 329 : ((!osCelestialBodyName.empty() &&
1726 658 : crsInfo->pszCelestialBodyName &&
1727 329 : osCelestialBodyName ==
1728 329 : crsInfo->pszCelestialBodyName) ||
1729 0 : (osCelestialBodyName.empty() &&
1730 12274 : !crsInfo->pszCelestialBodyName));
1731 11 : };
1732 :
1733 11 : std::vector<std::string> oRet;
1734 11 : if (noneAllowed)
1735 0 : oRet.push_back("none");
1736 11 : oRet.insert(oRet.end(), specialValues.begin(), specialValues.end());
1737 11 : if (!currentValue.empty())
1738 : {
1739 : const CPLStringList aosTokens(
1740 14 : CSLTokenizeString2(currentValue.c_str(), ":", 0));
1741 7 : int nCount = 0;
1742 : std::unique_ptr<OSRCRSInfo *, decltype(&OSRDestroyCRSInfoList)>
1743 : pCRSList(OSRGetCRSInfoListFromDatabase(aosTokens[0],
1744 : nullptr, &nCount),
1745 14 : OSRDestroyCRSInfoList);
1746 14 : std::string osCode;
1747 :
1748 14 : std::vector<const OSRCRSInfo *> candidates;
1749 46270 : for (int i = 0; i < nCount; ++i)
1750 : {
1751 46263 : const auto *entry = (pCRSList.get())[i];
1752 46263 : if (!entry->bDeprecated && IsCRSCompatible(entry))
1753 : {
1754 49425 : if (aosTokens.size() == 1 ||
1755 18411 : STARTS_WITH(entry->pszCode, aosTokens[1]))
1756 : {
1757 12666 : if (candidates.empty())
1758 7 : osCode = entry->pszCode;
1759 12666 : candidates.push_back(entry);
1760 : }
1761 : }
1762 : }
1763 7 : if (candidates.size() == 1)
1764 : {
1765 1 : oRet.push_back(std::move(osCode));
1766 : }
1767 : else
1768 : {
1769 6 : if (sDatasetLongLatEnv.IsInit())
1770 : {
1771 2 : std::sort(
1772 : candidates.begin(), candidates.end(),
1773 2999 : [](const OSRCRSInfo *a, const OSRCRSInfo *b)
1774 : {
1775 2999 : const double dfXa =
1776 2999 : a->dfWestLongitudeDeg >
1777 2999 : a->dfEastLongitudeDeg
1778 2999 : ? a->dfWestLongitudeDeg -
1779 0 : a->dfEastLongitudeDeg
1780 2999 : : (180 - a->dfWestLongitudeDeg) +
1781 2999 : (a->dfEastLongitudeDeg - -180);
1782 2999 : const double dfYa = a->dfNorthLatitudeDeg -
1783 2999 : a->dfSouthLatitudeDeg;
1784 2999 : const double dfXb =
1785 2999 : b->dfWestLongitudeDeg >
1786 2999 : b->dfEastLongitudeDeg
1787 2999 : ? b->dfWestLongitudeDeg -
1788 0 : b->dfEastLongitudeDeg
1789 2999 : : (180 - b->dfWestLongitudeDeg) +
1790 2999 : (b->dfEastLongitudeDeg - -180);
1791 2999 : const double dfYb = b->dfNorthLatitudeDeg -
1792 2999 : b->dfSouthLatitudeDeg;
1793 2999 : const double diffArea =
1794 2999 : dfXa * dfYa - dfXb * dfYb;
1795 2999 : if (diffArea < 0)
1796 279 : return true;
1797 2720 : if (diffArea == 0)
1798 : {
1799 2506 : if (std::string_view(a->pszName) ==
1800 2506 : b->pszName)
1801 : {
1802 57 : if (a->eType ==
1803 13 : OSR_CRS_TYPE_GEOGRAPHIC_2D &&
1804 13 : b->eType !=
1805 : OSR_CRS_TYPE_GEOGRAPHIC_2D)
1806 13 : return true;
1807 44 : if (a->eType ==
1808 32 : OSR_CRS_TYPE_GEOGRAPHIC_3D &&
1809 32 : b->eType == OSR_CRS_TYPE_GEOCENTRIC)
1810 9 : return true;
1811 35 : return false;
1812 : }
1813 4898 : return std::string_view(a->pszCode) <
1814 4898 : b->pszCode;
1815 : }
1816 214 : return false;
1817 : });
1818 : }
1819 :
1820 12671 : for (const auto *entry : candidates)
1821 : {
1822 25330 : std::string val = std::string(entry->pszCode)
1823 12665 : .append(" -- ")
1824 25330 : .append(entry->pszName);
1825 12665 : if (entry->eType == OSR_CRS_TYPE_GEOGRAPHIC_2D)
1826 1294 : val.append(" (geographic 2D)");
1827 11371 : else if (entry->eType == OSR_CRS_TYPE_GEOGRAPHIC_3D)
1828 446 : val.append(" (geographic 3D)");
1829 10925 : else if (entry->eType == OSR_CRS_TYPE_GEOCENTRIC)
1830 397 : val.append(" (geocentric)");
1831 12665 : oRet.push_back(std::move(val));
1832 : }
1833 : }
1834 : }
1835 11 : if (currentValue.empty() || oRet.empty())
1836 : {
1837 : const CPLStringList aosAuthorities(
1838 8 : OSRGetAuthorityListFromDatabase());
1839 24 : for (const char *pszAuth : cpl::Iterate(aosAuthorities))
1840 : {
1841 20 : int nCount = 0;
1842 20 : OSRDestroyCRSInfoList(OSRGetCRSInfoListFromDatabase(
1843 : pszAuth, nullptr, &nCount));
1844 20 : if (nCount)
1845 16 : oRet.push_back(std::string(pszAuth).append(":"));
1846 : }
1847 : }
1848 22 : return oRet;
1849 3453 : });
1850 :
1851 3453 : return *this;
1852 : }
1853 :
1854 : /************************************************************************/
1855 : /* GDALAlgorithm::GDALAlgorithm() */
1856 : /************************************************************************/
1857 :
1858 23836 : GDALAlgorithm::GDALAlgorithm(const std::string &name,
1859 : const std::string &description,
1860 23836 : const std::string &helpURL)
1861 : : m_name(name), m_description(description), m_helpURL(helpURL),
1862 47110 : m_helpFullURL(!m_helpURL.empty() && m_helpURL[0] == '/'
1863 23836 : ? "https://gdal.org" + m_helpURL
1864 70691 : : m_helpURL)
1865 : {
1866 : auto &helpArg =
1867 : AddArg("help", 'h', _("Display help message and exit"),
1868 47672 : &m_helpRequested)
1869 23836 : .SetHiddenForAPI()
1870 47672 : .SetCategory(GAAC_COMMON)
1871 14 : .AddAction([this]()
1872 23836 : { m_specialActionRequested = m_calledFromCommandLine; });
1873 : auto &helpDocArg =
1874 : AddArg("help-doc", 0,
1875 : _("Display help message for use by documentation"),
1876 47672 : &m_helpDocRequested)
1877 23836 : .SetHidden()
1878 16 : .AddAction([this]()
1879 23836 : { m_specialActionRequested = m_calledFromCommandLine; });
1880 : auto &jsonUsageArg =
1881 : AddArg("json-usage", 0, _("Display usage as JSON document and exit"),
1882 47672 : &m_JSONUsageRequested)
1883 23836 : .SetHiddenForAPI()
1884 47672 : .SetCategory(GAAC_COMMON)
1885 4 : .AddAction([this]()
1886 23836 : { m_specialActionRequested = m_calledFromCommandLine; });
1887 47672 : AddArg("config", 0, _("Configuration option"), &m_dummyConfigOptions)
1888 47672 : .SetMetaVar("<KEY>=<VALUE>")
1889 23836 : .SetHiddenForAPI()
1890 47672 : .SetCategory(GAAC_COMMON)
1891 : .AddAction(
1892 2 : [this]()
1893 : {
1894 2 : ReportError(
1895 : CE_Warning, CPLE_AppDefined,
1896 : "Configuration options passed with the 'config' argument "
1897 : "are ignored");
1898 23836 : });
1899 :
1900 23836 : AddValidationAction(
1901 14745 : [this, &helpArg, &helpDocArg, &jsonUsageArg]()
1902 : {
1903 7596 : if (!m_calledFromCommandLine && m_specialActionRequested)
1904 : {
1905 0 : for (auto &arg : {&helpArg, &helpDocArg, &jsonUsageArg})
1906 : {
1907 0 : if (arg->IsExplicitlySet())
1908 : {
1909 0 : ReportError(CE_Failure, CPLE_AppDefined,
1910 : "'%s' argument only available when called "
1911 : "from command line",
1912 0 : arg->GetName().c_str());
1913 0 : return false;
1914 : }
1915 : }
1916 : }
1917 7596 : return true;
1918 : });
1919 23836 : }
1920 :
1921 : /************************************************************************/
1922 : /* GDALAlgorithm::~GDALAlgorithm() */
1923 : /************************************************************************/
1924 :
1925 : GDALAlgorithm::~GDALAlgorithm() = default;
1926 :
1927 : /************************************************************************/
1928 : /* GDALAlgorithm::ParseArgument() */
1929 : /************************************************************************/
1930 :
1931 3395 : bool GDALAlgorithm::ParseArgument(
1932 : GDALAlgorithmArg *arg, const std::string &name, const std::string &value,
1933 : std::map<
1934 : GDALAlgorithmArg *,
1935 : std::variant<std::vector<std::string>, std::vector<int>,
1936 : std::vector<double>, std::vector<GDALArgDatasetValue>>>
1937 : &inConstructionValues)
1938 : {
1939 : const bool isListArg =
1940 3395 : GDALAlgorithmArgTypeIsList(arg->GetType()) && arg->GetMaxCount() > 1;
1941 3395 : if (arg->IsExplicitlySet() && !isListArg)
1942 : {
1943 : // Hack for "gdal info" to be able to pass an opened raster dataset
1944 : // by "gdal raster info" to the "gdal vector info" algorithm.
1945 4 : if (arg->SkipIfAlreadySet())
1946 : {
1947 1 : arg->SetSkipIfAlreadySet(false);
1948 1 : return true;
1949 : }
1950 :
1951 3 : ReportError(CE_Failure, CPLE_IllegalArg,
1952 : "Argument '%s' has already been specified.", name.c_str());
1953 3 : return false;
1954 : }
1955 :
1956 3463 : if (!arg->GetRepeatedArgAllowed() &&
1957 72 : cpl::contains(inConstructionValues, arg))
1958 : {
1959 1 : ReportError(CE_Failure, CPLE_IllegalArg,
1960 : "Argument '%s' has already been specified.", name.c_str());
1961 1 : return false;
1962 : }
1963 :
1964 3390 : switch (arg->GetType())
1965 : {
1966 329 : case GAAT_BOOLEAN:
1967 : {
1968 329 : if (value.empty() || value == "true")
1969 327 : return arg->Set(true);
1970 2 : else if (value == "false")
1971 1 : return arg->Set(false);
1972 : else
1973 : {
1974 1 : ReportError(
1975 : CE_Failure, CPLE_IllegalArg,
1976 : "Invalid value '%s' for boolean argument '%s'. Should be "
1977 : "'true' or 'false'.",
1978 : value.c_str(), name.c_str());
1979 1 : return false;
1980 : }
1981 : }
1982 :
1983 850 : case GAAT_STRING:
1984 : {
1985 850 : return arg->Set(value);
1986 : }
1987 :
1988 359 : case GAAT_INTEGER:
1989 : {
1990 359 : errno = 0;
1991 359 : char *endptr = nullptr;
1992 359 : const auto val = std::strtol(value.c_str(), &endptr, 10);
1993 358 : if (errno == 0 && endptr &&
1994 717 : endptr == value.c_str() + value.size() && val >= INT_MIN &&
1995 : val <= INT_MAX)
1996 : {
1997 356 : return arg->Set(static_cast<int>(val));
1998 : }
1999 : else
2000 : {
2001 3 : ReportError(CE_Failure, CPLE_IllegalArg,
2002 : "Expected integer value for argument '%s', "
2003 : "but got '%s'.",
2004 : name.c_str(), value.c_str());
2005 3 : return false;
2006 : }
2007 : }
2008 :
2009 36 : case GAAT_REAL:
2010 : {
2011 36 : char *endptr = nullptr;
2012 36 : double dfValue = CPLStrtod(value.c_str(), &endptr);
2013 36 : if (endptr != value.c_str() + value.size())
2014 : {
2015 1 : ReportError(
2016 : CE_Failure, CPLE_IllegalArg,
2017 : "Expected real value for argument '%s', but got '%s'.",
2018 : name.c_str(), value.c_str());
2019 1 : return false;
2020 : }
2021 35 : return arg->Set(dfValue);
2022 : }
2023 :
2024 597 : case GAAT_DATASET:
2025 : {
2026 597 : return arg->SetDatasetName(value);
2027 : }
2028 :
2029 272 : case GAAT_STRING_LIST:
2030 : {
2031 : const CPLStringList aosTokens(
2032 272 : arg->GetPackedValuesAllowed()
2033 185 : ? CSLTokenizeString2(value.c_str(), ",",
2034 : CSLT_HONOURSTRINGS |
2035 : CSLT_PRESERVEQUOTES)
2036 457 : : CSLAddString(nullptr, value.c_str()));
2037 272 : if (!cpl::contains(inConstructionValues, arg))
2038 : {
2039 248 : inConstructionValues[arg] = std::vector<std::string>();
2040 : }
2041 : auto &valueVector =
2042 272 : std::get<std::vector<std::string>>(inConstructionValues[arg]);
2043 580 : for (const char *v : aosTokens)
2044 : {
2045 308 : valueVector.push_back(v);
2046 : }
2047 272 : if (arg->GetMaxCount() == 1)
2048 : {
2049 4 : bool ret = arg->Set(std::move(valueVector));
2050 4 : inConstructionValues.erase(inConstructionValues.find(arg));
2051 4 : return ret;
2052 : }
2053 :
2054 268 : break;
2055 : }
2056 :
2057 65 : case GAAT_INTEGER_LIST:
2058 : {
2059 : const CPLStringList aosTokens(
2060 65 : arg->GetPackedValuesAllowed()
2061 65 : ? CSLTokenizeString2(
2062 : value.c_str(), ",",
2063 : CSLT_HONOURSTRINGS | CSLT_STRIPLEADSPACES |
2064 : CSLT_STRIPENDSPACES | CSLT_ALLOWEMPTYTOKENS)
2065 130 : : CSLAddString(nullptr, value.c_str()));
2066 65 : if (!cpl::contains(inConstructionValues, arg))
2067 : {
2068 61 : inConstructionValues[arg] = std::vector<int>();
2069 : }
2070 : auto &valueVector =
2071 65 : std::get<std::vector<int>>(inConstructionValues[arg]);
2072 197 : for (const char *v : aosTokens)
2073 : {
2074 138 : errno = 0;
2075 138 : char *endptr = nullptr;
2076 138 : const auto val = std::strtol(v, &endptr, 10);
2077 138 : if (errno == 0 && endptr && endptr == v + strlen(v) &&
2078 134 : val >= INT_MIN && val <= INT_MAX && strlen(v) > 0)
2079 : {
2080 132 : valueVector.push_back(static_cast<int>(val));
2081 : }
2082 : else
2083 : {
2084 6 : ReportError(
2085 : CE_Failure, CPLE_IllegalArg,
2086 : "Expected list of integer value for argument '%s', "
2087 : "but got '%s'.",
2088 : name.c_str(), value.c_str());
2089 6 : return false;
2090 : }
2091 : }
2092 59 : if (arg->GetMaxCount() == 1)
2093 : {
2094 2 : bool ret = arg->Set(std::move(valueVector));
2095 2 : inConstructionValues.erase(inConstructionValues.find(arg));
2096 2 : return ret;
2097 : }
2098 :
2099 57 : break;
2100 : }
2101 :
2102 107 : case GAAT_REAL_LIST:
2103 : {
2104 : const CPLStringList aosTokens(
2105 107 : arg->GetPackedValuesAllowed()
2106 107 : ? CSLTokenizeString2(
2107 : value.c_str(), ",",
2108 : CSLT_HONOURSTRINGS | CSLT_STRIPLEADSPACES |
2109 : CSLT_STRIPENDSPACES | CSLT_ALLOWEMPTYTOKENS)
2110 214 : : CSLAddString(nullptr, value.c_str()));
2111 107 : if (!cpl::contains(inConstructionValues, arg))
2112 : {
2113 105 : inConstructionValues[arg] = std::vector<double>();
2114 : }
2115 : auto &valueVector =
2116 107 : std::get<std::vector<double>>(inConstructionValues[arg]);
2117 432 : for (const char *v : aosTokens)
2118 : {
2119 329 : char *endptr = nullptr;
2120 329 : double dfValue = CPLStrtod(v, &endptr);
2121 329 : if (strlen(v) == 0 || endptr != v + strlen(v))
2122 : {
2123 4 : ReportError(
2124 : CE_Failure, CPLE_IllegalArg,
2125 : "Expected list of real value for argument '%s', "
2126 : "but got '%s'.",
2127 : name.c_str(), value.c_str());
2128 4 : return false;
2129 : }
2130 325 : valueVector.push_back(dfValue);
2131 : }
2132 103 : if (arg->GetMaxCount() == 1)
2133 : {
2134 2 : bool ret = arg->Set(std::move(valueVector));
2135 2 : inConstructionValues.erase(inConstructionValues.find(arg));
2136 2 : return ret;
2137 : }
2138 :
2139 101 : break;
2140 : }
2141 :
2142 775 : case GAAT_DATASET_LIST:
2143 : {
2144 775 : if (!cpl::contains(inConstructionValues, arg))
2145 : {
2146 767 : inConstructionValues[arg] = std::vector<GDALArgDatasetValue>();
2147 : }
2148 : auto &valueVector = std::get<std::vector<GDALArgDatasetValue>>(
2149 775 : inConstructionValues[arg]);
2150 775 : if (!value.empty() && value[0] == '{' && value.back() == '}')
2151 : {
2152 12 : valueVector.push_back(GDALArgDatasetValue(value));
2153 : }
2154 : else
2155 : {
2156 : const CPLStringList aosTokens(
2157 763 : arg->GetPackedValuesAllowed()
2158 6 : ? CSLTokenizeString2(value.c_str(), ",",
2159 : CSLT_HONOURSTRINGS |
2160 : CSLT_STRIPLEADSPACES)
2161 1532 : : CSLAddString(nullptr, value.c_str()));
2162 1529 : for (const char *v : aosTokens)
2163 : {
2164 766 : valueVector.push_back(GDALArgDatasetValue(v));
2165 : }
2166 : }
2167 775 : if (arg->GetMaxCount() == 1)
2168 : {
2169 668 : bool ret = arg->Set(std::move(valueVector));
2170 668 : inConstructionValues.erase(inConstructionValues.find(arg));
2171 668 : return ret;
2172 : }
2173 :
2174 107 : break;
2175 : }
2176 : }
2177 :
2178 533 : return true;
2179 : }
2180 :
2181 : /************************************************************************/
2182 : /* FormatSuggestionsAsString() */
2183 : /************************************************************************/
2184 :
2185 : static std::string
2186 6 : FormatSuggestionsAsString(const std::vector<std::string> &suggestions,
2187 : bool addDashDashPrefix)
2188 : {
2189 6 : std::string ret;
2190 14 : for (auto [i, suggestion] : cpl::enumerate(suggestions))
2191 : {
2192 8 : if (i > 0)
2193 : {
2194 2 : ret += (i + 1 < suggestions.size()) ? ", " : " or ";
2195 : }
2196 8 : ret += '\'';
2197 8 : if (addDashDashPrefix)
2198 6 : ret += "--";
2199 8 : ret += suggestion;
2200 8 : ret += '\'';
2201 : }
2202 6 : return ret;
2203 : }
2204 :
2205 : /************************************************************************/
2206 : /* GDALAlgorithm::ParseCommandLineArguments() */
2207 : /************************************************************************/
2208 :
2209 2330 : bool GDALAlgorithm::ParseCommandLineArguments(
2210 : const std::vector<std::string> &args)
2211 : {
2212 2330 : if (m_parsedSubStringAlreadyCalled)
2213 : {
2214 6 : ReportError(CE_Failure, CPLE_AppDefined,
2215 : "ParseCommandLineArguments() can only be called once per "
2216 : "instance.");
2217 6 : return false;
2218 : }
2219 2324 : m_parsedSubStringAlreadyCalled = true;
2220 :
2221 : // AWS like syntax supported too (not advertized)
2222 2324 : if (args.size() == 1 && args[0] == "help")
2223 : {
2224 1 : auto arg = GetArg("help");
2225 1 : assert(arg);
2226 1 : arg->Set(true);
2227 1 : arg->RunActions();
2228 1 : return true;
2229 : }
2230 :
2231 2323 : if (HasSubAlgorithms())
2232 : {
2233 495 : if (args.empty())
2234 : {
2235 2 : ReportError(CE_Failure, CPLE_AppDefined, "Missing %s name.",
2236 2 : m_callPath.size() == 1 ? "command" : "subcommand");
2237 2 : return false;
2238 : }
2239 493 : if (!args[0].empty() && args[0][0] == '-')
2240 : {
2241 : // go on argument parsing
2242 : }
2243 : else
2244 : {
2245 490 : const auto nCounter = CPLGetErrorCounter();
2246 490 : m_selectedSubAlgHolder = InstantiateSubAlgorithm(args[0]);
2247 490 : if (m_selectedSubAlgHolder)
2248 : {
2249 487 : m_selectedSubAlg = m_selectedSubAlgHolder.get();
2250 487 : m_selectedSubAlg->SetReferencePathForRelativePaths(
2251 487 : m_referencePath);
2252 487 : m_selectedSubAlg->m_executionForStreamOutput =
2253 487 : m_executionForStreamOutput;
2254 487 : m_selectedSubAlg->m_calledFromCommandLine =
2255 487 : m_calledFromCommandLine;
2256 487 : m_selectedSubAlg->m_skipValidationInParseCommandLine =
2257 487 : m_skipValidationInParseCommandLine;
2258 487 : bool bRet = m_selectedSubAlg->ParseCommandLineArguments(
2259 974 : std::vector<std::string>(args.begin() + 1, args.end()));
2260 487 : m_selectedSubAlg->PropagateSpecialActionTo(this);
2261 487 : return bRet;
2262 : }
2263 : else
2264 : {
2265 4 : if (!(CPLGetErrorCounter() == nCounter + 1 &&
2266 1 : strstr(CPLGetLastErrorMsg(), "Do you mean")))
2267 : {
2268 2 : ReportError(CE_Failure, CPLE_AppDefined,
2269 2 : "Unknown command: '%s'", args[0].c_str());
2270 : }
2271 3 : return false;
2272 : }
2273 : }
2274 : }
2275 :
2276 : std::map<
2277 : GDALAlgorithmArg *,
2278 : std::variant<std::vector<std::string>, std::vector<int>,
2279 : std::vector<double>, std::vector<GDALArgDatasetValue>>>
2280 3662 : inConstructionValues;
2281 :
2282 3662 : std::vector<std::string> lArgs(args);
2283 1831 : bool helpValueRequested = false;
2284 5230 : for (size_t i = 0; i < lArgs.size(); /* incremented in loop */)
2285 : {
2286 3512 : const auto &strArg = lArgs[i];
2287 3512 : GDALAlgorithmArg *arg = nullptr;
2288 3512 : std::string name;
2289 3512 : std::string value;
2290 3512 : bool hasValue = false;
2291 3512 : if (m_calledFromCommandLine && cpl::ends_with(strArg, "=?"))
2292 5 : helpValueRequested = true;
2293 3512 : if (strArg.size() >= 2 && strArg[0] == '-' && strArg[1] == '-')
2294 : {
2295 2179 : const auto equalPos = strArg.find('=');
2296 4358 : name = (equalPos != std::string::npos) ? strArg.substr(0, equalPos)
2297 2179 : : strArg;
2298 2179 : const std::string nameWithoutDash = name.substr(2);
2299 2179 : auto iterArg = m_mapLongNameToArg.find(nameWithoutDash);
2300 2235 : if (m_arbitraryLongNameArgsAllowed &&
2301 2235 : iterArg == m_mapLongNameToArg.end())
2302 : {
2303 17 : GetArg(nameWithoutDash);
2304 17 : iterArg = m_mapLongNameToArg.find(nameWithoutDash);
2305 : }
2306 2179 : if (iterArg == m_mapLongNameToArg.end())
2307 : {
2308 : const auto suggestions =
2309 28 : GetSuggestionsForArgumentName(nameWithoutDash);
2310 28 : if (!suggestions.empty())
2311 : {
2312 3 : ReportError(CE_Failure, CPLE_IllegalArg,
2313 : "Option '%s' is unknown. Do you mean %s?",
2314 : name.c_str(),
2315 6 : FormatSuggestionsAsString(
2316 : suggestions, /* addDashDashPrefix = */ true)
2317 : .c_str());
2318 : }
2319 : else
2320 : {
2321 25 : ReportError(CE_Failure, CPLE_IllegalArg,
2322 : "Option '%s' is unknown.", name.c_str());
2323 : }
2324 28 : return false;
2325 : }
2326 2151 : arg = iterArg->second;
2327 2151 : if (equalPos != std::string::npos)
2328 : {
2329 472 : hasValue = true;
2330 472 : value = strArg.substr(equalPos + 1);
2331 : }
2332 : }
2333 1410 : else if (strArg.size() >= 2 && strArg[0] == '-' &&
2334 77 : CPLGetValueType(strArg.c_str()) == CPL_VALUE_STRING)
2335 : {
2336 149 : for (size_t j = 1; j < strArg.size(); ++j)
2337 : {
2338 77 : name.clear();
2339 77 : name += strArg[j];
2340 77 : const auto iterArg = m_mapShortNameToArg.find(name);
2341 77 : if (iterArg == m_mapShortNameToArg.end())
2342 : {
2343 5 : const std::string nameWithoutDash = strArg.substr(1);
2344 5 : if (m_mapLongNameToArg.find(nameWithoutDash) !=
2345 10 : m_mapLongNameToArg.end())
2346 : {
2347 1 : ReportError(CE_Failure, CPLE_IllegalArg,
2348 : "Short name option '%s' is unknown. Do you "
2349 : "mean '--%s' (with leading double dash) ?",
2350 : name.c_str(), nameWithoutDash.c_str());
2351 : }
2352 : else
2353 : {
2354 : const auto suggestions =
2355 8 : GetSuggestionsForArgumentName(nameWithoutDash);
2356 4 : if (!suggestions.empty())
2357 : {
2358 1 : ReportError(
2359 : CE_Failure, CPLE_IllegalArg,
2360 : "Short name option '%s' is unknown. Do you "
2361 : "mean %s (with leading double dash) ?",
2362 : name.c_str(),
2363 2 : FormatSuggestionsAsString(
2364 : suggestions, /* addDashDashPrefix = */ true)
2365 : .c_str());
2366 : }
2367 : else
2368 : {
2369 3 : ReportError(CE_Failure, CPLE_IllegalArg,
2370 : "Short name option '%s' is unknown.",
2371 : name.c_str());
2372 : }
2373 : }
2374 5 : return false;
2375 : }
2376 72 : arg = iterArg->second;
2377 72 : if (strArg.size() > 2)
2378 : {
2379 0 : if (arg->GetType() != GAAT_BOOLEAN)
2380 : {
2381 0 : ReportError(CE_Failure, CPLE_IllegalArg,
2382 : "Invalid argument '%s'. Option '%s' is not "
2383 : "a boolean option.",
2384 : strArg.c_str(), name.c_str());
2385 0 : return false;
2386 : }
2387 :
2388 0 : if (!ParseArgument(arg, name, "true", inConstructionValues))
2389 0 : return false;
2390 : }
2391 : }
2392 72 : if (strArg.size() > 2)
2393 : {
2394 0 : lArgs.erase(lArgs.begin() + i);
2395 0 : continue;
2396 : }
2397 : }
2398 : else
2399 : {
2400 1256 : ++i;
2401 1256 : continue;
2402 : }
2403 2223 : CPLAssert(arg);
2404 :
2405 2223 : if (arg && arg->GetType() == GAAT_BOOLEAN)
2406 : {
2407 330 : if (!hasValue)
2408 : {
2409 327 : hasValue = true;
2410 327 : value = "true";
2411 : }
2412 : }
2413 :
2414 2223 : if (!hasValue)
2415 : {
2416 1424 : if (i + 1 == lArgs.size())
2417 : {
2418 41 : if (m_parseForAutoCompletion)
2419 : {
2420 35 : lArgs.erase(lArgs.begin() + i);
2421 35 : break;
2422 : }
2423 6 : ReportError(
2424 : CE_Failure, CPLE_IllegalArg,
2425 : "Expected value for argument '%s', but ran short of tokens",
2426 : name.c_str());
2427 6 : return false;
2428 : }
2429 1383 : value = lArgs[i + 1];
2430 1383 : lArgs.erase(lArgs.begin() + i + 1);
2431 : }
2432 :
2433 2182 : if (arg && !ParseArgument(arg, name, value, inConstructionValues))
2434 39 : return false;
2435 :
2436 2143 : lArgs.erase(lArgs.begin() + i);
2437 : }
2438 :
2439 1753 : if (m_specialActionRequested)
2440 : {
2441 26 : return true;
2442 : }
2443 :
2444 2195 : const auto ProcessInConstructionValues = [&inConstructionValues]()
2445 : {
2446 2161 : for (auto &[arg, value] : inConstructionValues)
2447 : {
2448 492 : if (arg->GetType() == GAAT_STRING_LIST)
2449 : {
2450 240 : if (!arg->Set(std::get<std::vector<std::string>>(
2451 240 : inConstructionValues[arg])))
2452 : {
2453 34 : return false;
2454 : }
2455 : }
2456 252 : else if (arg->GetType() == GAAT_INTEGER_LIST)
2457 : {
2458 54 : if (!arg->Set(
2459 54 : std::get<std::vector<int>>(inConstructionValues[arg])))
2460 : {
2461 4 : return false;
2462 : }
2463 : }
2464 198 : else if (arg->GetType() == GAAT_REAL_LIST)
2465 : {
2466 99 : if (!arg->Set(std::get<std::vector<double>>(
2467 99 : inConstructionValues[arg])))
2468 : {
2469 10 : return false;
2470 : }
2471 : }
2472 99 : else if (arg->GetType() == GAAT_DATASET_LIST)
2473 : {
2474 99 : if (!arg->Set(
2475 : std::move(std::get<std::vector<GDALArgDatasetValue>>(
2476 99 : inConstructionValues[arg]))))
2477 : {
2478 2 : return false;
2479 : }
2480 : }
2481 : }
2482 1669 : return true;
2483 1727 : };
2484 :
2485 : // Process positional arguments that have not been set through their
2486 : // option name.
2487 1727 : size_t i = 0;
2488 1727 : size_t iCurPosArg = 0;
2489 :
2490 : // Special case for <INPUT> <AUXILIARY>... <OUTPUT>
2491 1750 : if (m_positionalArgs.size() == 3 &&
2492 24 : (m_positionalArgs[0]->IsRequired() ||
2493 23 : m_positionalArgs[0]->GetMinCount() == 1) &&
2494 44 : m_positionalArgs[0]->GetMaxCount() == 1 &&
2495 29 : (m_positionalArgs[1]->IsRequired() ||
2496 29 : m_positionalArgs[1]->GetMinCount() == 1) &&
2497 : /* Second argument may have several occurrences */
2498 44 : m_positionalArgs[1]->GetMaxCount() >= 1 &&
2499 31 : (m_positionalArgs[2]->IsRequired() ||
2500 22 : m_positionalArgs[2]->GetMinCount() == 1) &&
2501 13 : m_positionalArgs[2]->GetMaxCount() == 1 &&
2502 9 : !m_positionalArgs[0]->IsExplicitlySet() &&
2503 1759 : !m_positionalArgs[1]->IsExplicitlySet() &&
2504 9 : !m_positionalArgs[2]->IsExplicitlySet())
2505 : {
2506 7 : if (lArgs.size() - i < 3)
2507 : {
2508 1 : ReportError(CE_Failure, CPLE_AppDefined,
2509 : "Not enough positional values.");
2510 1 : return false;
2511 : }
2512 12 : bool ok = ParseArgument(m_positionalArgs[0],
2513 6 : m_positionalArgs[0]->GetName().c_str(),
2514 6 : lArgs[i], inConstructionValues);
2515 6 : if (ok)
2516 : {
2517 5 : ++i;
2518 11 : for (; i + 1 < lArgs.size() && ok; ++i)
2519 : {
2520 12 : ok = ParseArgument(m_positionalArgs[1],
2521 6 : m_positionalArgs[1]->GetName().c_str(),
2522 6 : lArgs[i], inConstructionValues);
2523 : }
2524 : }
2525 6 : if (ok)
2526 : {
2527 10 : ok = ParseArgument(m_positionalArgs[2],
2528 10 : m_positionalArgs[2]->GetName().c_str(), lArgs[i],
2529 : inConstructionValues);
2530 5 : ++i;
2531 : }
2532 6 : if (!ok)
2533 : {
2534 3 : ProcessInConstructionValues();
2535 3 : return false;
2536 : }
2537 : }
2538 :
2539 575 : if (m_inputDatasetCanBeOmitted && m_positionalArgs.size() >= 1 &&
2540 636 : !m_positionalArgs[0]->IsExplicitlySet() &&
2541 2617 : m_positionalArgs[0]->GetName() == GDAL_ARG_NAME_INPUT &&
2542 72 : (m_positionalArgs[0]->GetType() == GAAT_DATASET ||
2543 36 : m_positionalArgs[0]->GetType() == GAAT_DATASET_LIST))
2544 : {
2545 36 : ++iCurPosArg;
2546 : }
2547 :
2548 2899 : while (i < lArgs.size() && iCurPosArg < m_positionalArgs.size())
2549 : {
2550 1183 : GDALAlgorithmArg *arg = m_positionalArgs[iCurPosArg];
2551 1194 : while (arg->IsExplicitlySet())
2552 : {
2553 12 : ++iCurPosArg;
2554 12 : if (iCurPosArg == m_positionalArgs.size())
2555 1 : break;
2556 11 : arg = m_positionalArgs[iCurPosArg];
2557 : }
2558 1183 : if (iCurPosArg == m_positionalArgs.size())
2559 : {
2560 1 : break;
2561 : }
2562 1849 : if (GDALAlgorithmArgTypeIsList(arg->GetType()) &&
2563 667 : arg->GetMinCount() != arg->GetMaxCount())
2564 : {
2565 102 : if (iCurPosArg == 0)
2566 : {
2567 80 : size_t nCountAtEnd = 0;
2568 109 : for (size_t j = 1; j < m_positionalArgs.size(); j++)
2569 : {
2570 31 : const auto *otherArg = m_positionalArgs[j];
2571 31 : if (GDALAlgorithmArgTypeIsList(otherArg->GetType()))
2572 : {
2573 4 : if (otherArg->GetMinCount() != otherArg->GetMaxCount())
2574 : {
2575 2 : ReportError(
2576 : CE_Failure, CPLE_AppDefined,
2577 : "Ambiguity in definition of positional "
2578 : "argument "
2579 : "'%s' given it has a varying number of values, "
2580 : "but follows argument '%s' which also has a "
2581 : "varying number of values",
2582 1 : otherArg->GetName().c_str(),
2583 1 : arg->GetName().c_str());
2584 1 : ProcessInConstructionValues();
2585 1 : return false;
2586 : }
2587 3 : nCountAtEnd += otherArg->GetMinCount();
2588 : }
2589 : else
2590 : {
2591 27 : if (!otherArg->IsRequired())
2592 : {
2593 2 : ReportError(
2594 : CE_Failure, CPLE_AppDefined,
2595 : "Ambiguity in definition of positional "
2596 : "argument "
2597 : "'%s', given it is not required but follows "
2598 : "argument '%s' which has a varying number of "
2599 : "values",
2600 1 : otherArg->GetName().c_str(),
2601 1 : arg->GetName().c_str());
2602 1 : ProcessInConstructionValues();
2603 1 : return false;
2604 : }
2605 26 : nCountAtEnd++;
2606 : }
2607 : }
2608 78 : if (lArgs.size() < nCountAtEnd)
2609 : {
2610 1 : ReportError(CE_Failure, CPLE_AppDefined,
2611 : "Not enough positional values.");
2612 1 : ProcessInConstructionValues();
2613 1 : return false;
2614 : }
2615 162 : for (; i < lArgs.size() - nCountAtEnd; ++i)
2616 : {
2617 85 : if (!ParseArgument(arg, arg->GetName().c_str(), lArgs[i],
2618 : inConstructionValues))
2619 : {
2620 0 : ProcessInConstructionValues();
2621 0 : return false;
2622 : }
2623 : }
2624 : }
2625 22 : else if (iCurPosArg == m_positionalArgs.size() - 1)
2626 : {
2627 49 : for (; i < lArgs.size(); ++i)
2628 : {
2629 28 : if (!ParseArgument(arg, arg->GetName().c_str(), lArgs[i],
2630 : inConstructionValues))
2631 : {
2632 0 : ProcessInConstructionValues();
2633 0 : return false;
2634 : }
2635 : }
2636 : }
2637 : else
2638 : {
2639 1 : ReportError(CE_Failure, CPLE_AppDefined,
2640 : "Ambiguity in definition of positional arguments: "
2641 : "arguments with varying number of values must be "
2642 : "first or last one.");
2643 1 : return false;
2644 : }
2645 : }
2646 : else
2647 : {
2648 1080 : if (lArgs.size() - i < static_cast<size_t>(arg->GetMaxCount()))
2649 : {
2650 1 : ReportError(CE_Failure, CPLE_AppDefined,
2651 : "Not enough positional values.");
2652 1 : return false;
2653 : }
2654 1079 : const size_t iMax = i + arg->GetMaxCount();
2655 2161 : for (; i < iMax; ++i)
2656 : {
2657 1083 : if (!ParseArgument(arg, arg->GetName().c_str(), lArgs[i],
2658 : inConstructionValues))
2659 : {
2660 1 : ProcessInConstructionValues();
2661 1 : return false;
2662 : }
2663 : }
2664 : }
2665 1176 : ++iCurPosArg;
2666 : }
2667 :
2668 1717 : if (i < lArgs.size())
2669 : {
2670 21 : ReportError(CE_Failure, CPLE_AppDefined,
2671 : "Positional values starting at '%s' are not expected.",
2672 21 : lArgs[i].c_str());
2673 21 : return false;
2674 : }
2675 :
2676 1696 : if (!ProcessInConstructionValues())
2677 : {
2678 33 : return false;
2679 : }
2680 :
2681 : // Skip to first unset positional argument.
2682 2740 : while (iCurPosArg < m_positionalArgs.size() &&
2683 588 : m_positionalArgs[iCurPosArg]->IsExplicitlySet())
2684 : {
2685 489 : ++iCurPosArg;
2686 : }
2687 : // Check if this positional argument is required.
2688 1761 : if (iCurPosArg < m_positionalArgs.size() && !helpValueRequested &&
2689 98 : (GDALAlgorithmArgTypeIsList(m_positionalArgs[iCurPosArg]->GetType())
2690 50 : ? m_positionalArgs[iCurPosArg]->GetMinCount() > 0
2691 48 : : m_positionalArgs[iCurPosArg]->IsRequired()))
2692 : {
2693 87 : ReportError(CE_Failure, CPLE_AppDefined,
2694 : "Positional arguments starting at '%s' have not been "
2695 : "specified.",
2696 87 : m_positionalArgs[iCurPosArg]->GetMetaVar().c_str());
2697 87 : return false;
2698 : }
2699 :
2700 1576 : if (m_calledFromCommandLine)
2701 : {
2702 5460 : for (auto &arg : m_args)
2703 : {
2704 7076 : if (arg->IsExplicitlySet() &&
2705 1105 : ((arg->GetType() == GAAT_STRING &&
2706 1102 : arg->Get<std::string>() == "?") ||
2707 999 : (arg->GetType() == GAAT_STRING_LIST &&
2708 157 : arg->Get<std::vector<std::string>>().size() == 1 &&
2709 78 : arg->Get<std::vector<std::string>>()[0] == "?")))
2710 : {
2711 : {
2712 10 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
2713 5 : ValidateArguments();
2714 : }
2715 :
2716 5 : auto choices = arg->GetChoices();
2717 5 : if (choices.empty())
2718 2 : choices = arg->GetAutoCompleteChoices(std::string());
2719 5 : if (!choices.empty())
2720 : {
2721 5 : if (choices.size() == 1)
2722 : {
2723 4 : ReportError(
2724 : CE_Failure, CPLE_AppDefined,
2725 : "Single potential value for argument '%s' is '%s'",
2726 4 : arg->GetName().c_str(), choices.front().c_str());
2727 : }
2728 : else
2729 : {
2730 6 : std::string msg("Potential values for argument '");
2731 3 : msg += arg->GetName();
2732 3 : msg += "' are:";
2733 45 : for (const auto &v : choices)
2734 : {
2735 42 : msg += "\n- ";
2736 42 : msg += v;
2737 : }
2738 3 : ReportError(CE_Failure, CPLE_AppDefined, "%s",
2739 : msg.c_str());
2740 : }
2741 5 : return false;
2742 : }
2743 : }
2744 : }
2745 : }
2746 :
2747 1571 : return m_skipValidationInParseCommandLine || ValidateArguments();
2748 : }
2749 :
2750 : /************************************************************************/
2751 : /* GDALAlgorithm::ReportError() */
2752 : /************************************************************************/
2753 :
2754 : //! @cond Doxygen_Suppress
2755 959 : void GDALAlgorithm::ReportError(CPLErr eErrClass, CPLErrorNum err_no,
2756 : const char *fmt, ...) const
2757 : {
2758 : va_list args;
2759 959 : va_start(args, fmt);
2760 959 : CPLError(eErrClass, err_no, "%s",
2761 959 : std::string(m_name)
2762 959 : .append(": ")
2763 1918 : .append(CPLString().vPrintf(fmt, args))
2764 : .c_str());
2765 959 : va_end(args);
2766 959 : }
2767 :
2768 : //! @endcond
2769 :
2770 : /************************************************************************/
2771 : /* GDALAlgorithm::ProcessDatasetArg() */
2772 : /************************************************************************/
2773 :
2774 10864 : bool GDALAlgorithm::ProcessDatasetArg(GDALAlgorithmArg *arg,
2775 : GDALAlgorithm *algForOutput)
2776 : {
2777 10864 : bool ret = true;
2778 :
2779 10864 : const auto updateArg = algForOutput->GetArg(GDAL_ARG_NAME_UPDATE);
2780 10864 : const bool hasUpdateArg = updateArg && updateArg->GetType() == GAAT_BOOLEAN;
2781 10864 : const bool update = hasUpdateArg && updateArg->Get<bool>();
2782 :
2783 10864 : const auto appendArg = algForOutput->GetArg(GDAL_ARG_NAME_APPEND);
2784 10864 : const bool hasAppendArg = appendArg && appendArg->GetType() == GAAT_BOOLEAN;
2785 10864 : const bool append = hasAppendArg && appendArg->Get<bool>();
2786 :
2787 10864 : const auto overwriteArg = algForOutput->GetArg(GDAL_ARG_NAME_OVERWRITE);
2788 : const bool overwrite =
2789 17783 : (arg->IsOutput() && overwriteArg &&
2790 17783 : overwriteArg->GetType() == GAAT_BOOLEAN && overwriteArg->Get<bool>());
2791 :
2792 10864 : auto outputArg = algForOutput->GetArg(GDAL_ARG_NAME_OUTPUT);
2793 21728 : auto &val = [arg]() -> GDALArgDatasetValue &
2794 : {
2795 10864 : if (arg->GetType() == GAAT_DATASET_LIST)
2796 6393 : return arg->Get<std::vector<GDALArgDatasetValue>>()[0];
2797 : else
2798 4471 : return arg->Get<GDALArgDatasetValue>();
2799 10864 : }();
2800 : const bool onlyInputSpecifiedInUpdateAndOutputNotRequired =
2801 17277 : arg->GetName() == GDAL_ARG_NAME_INPUT && outputArg &&
2802 17285 : !outputArg->IsExplicitlySet() && !outputArg->IsRequired() && update &&
2803 8 : !overwrite;
2804 :
2805 : // Used for nested pipelines
2806 : const auto oIterDatasetNameToDataset =
2807 21725 : val.IsNameSet() ? m_oMapDatasetNameToDataset.find(val.GetName())
2808 10864 : : m_oMapDatasetNameToDataset.end();
2809 :
2810 10864 : if (!val.GetDatasetRef() && !val.IsNameSet())
2811 : {
2812 3 : ReportError(CE_Failure, CPLE_AppDefined,
2813 : "Argument '%s' has no dataset object or dataset name.",
2814 3 : arg->GetName().c_str());
2815 3 : ret = false;
2816 : }
2817 10861 : else if (val.GetDatasetRef() && !CheckCanSetDatasetObject(arg))
2818 : {
2819 3 : return false;
2820 : }
2821 309 : else if (m_inputDatasetCanBeOmitted &&
2822 11167 : val.GetName() == GDAL_DATASET_PIPELINE_PLACEHOLDER_VALUE &&
2823 17 : !arg->IsOutput())
2824 : {
2825 17 : return true;
2826 : }
2827 16021 : else if (!val.GetDatasetRef() &&
2828 5500 : (arg->AutoOpenDataset() ||
2829 16341 : oIterDatasetNameToDataset != m_oMapDatasetNameToDataset.end()) &&
2830 4861 : (!arg->IsOutput() || (arg == outputArg && update && !overwrite) ||
2831 : onlyInputSpecifiedInUpdateAndOutputNotRequired))
2832 : {
2833 1525 : int flags = arg->GetDatasetType();
2834 1525 : bool assignToOutputArg = false;
2835 :
2836 : // Check if input and output parameters point to the same
2837 : // filename (for vector datasets)
2838 2828 : if (arg->GetName() == GDAL_ARG_NAME_INPUT && update && !overwrite &&
2839 2828 : outputArg && outputArg->GetType() == GAAT_DATASET)
2840 : {
2841 62 : auto &outputVal = outputArg->Get<GDALArgDatasetValue>();
2842 121 : if (!outputVal.GetDatasetRef() &&
2843 121 : outputVal.GetName() == val.GetName() &&
2844 2 : (outputArg->GetDatasetInputFlags() & GADV_OBJECT) != 0)
2845 : {
2846 2 : assignToOutputArg = true;
2847 2 : flags |= GDAL_OF_UPDATE | GDAL_OF_VERBOSE_ERROR;
2848 : }
2849 60 : else if (onlyInputSpecifiedInUpdateAndOutputNotRequired)
2850 : {
2851 2 : flags |= GDAL_OF_UPDATE | GDAL_OF_VERBOSE_ERROR;
2852 : }
2853 : }
2854 :
2855 1525 : if (!arg->IsOutput() || arg->GetDatasetInputFlags() == GADV_NAME)
2856 1442 : flags |= GDAL_OF_VERBOSE_ERROR;
2857 1525 : if ((arg == outputArg || !outputArg) && update)
2858 : {
2859 85 : flags |= GDAL_OF_UPDATE;
2860 85 : if (!append)
2861 64 : flags |= GDAL_OF_VERBOSE_ERROR;
2862 : }
2863 :
2864 1525 : const auto readOnlyArg = GetArg(GDAL_ARG_NAME_READ_ONLY);
2865 : const bool readOnly =
2866 1569 : (readOnlyArg && readOnlyArg->GetType() == GAAT_BOOLEAN &&
2867 44 : readOnlyArg->Get<bool>());
2868 1525 : if (readOnly)
2869 12 : flags &= ~GDAL_OF_UPDATE;
2870 :
2871 3050 : CPLStringList aosOpenOptions;
2872 3050 : CPLStringList aosAllowedDrivers;
2873 1525 : if (arg->IsInput())
2874 : {
2875 1525 : if (arg == outputArg)
2876 : {
2877 83 : if (update && !overwrite)
2878 : {
2879 83 : const auto ooArg = GetArg(GDAL_ARG_NAME_OUTPUT_OPEN_OPTION);
2880 83 : if (ooArg && ooArg->GetType() == GAAT_STRING_LIST)
2881 46 : aosOpenOptions = CPLStringList(
2882 46 : ooArg->Get<std::vector<std::string>>());
2883 : }
2884 : }
2885 : else
2886 : {
2887 1442 : const auto ooArg = GetArg(GDAL_ARG_NAME_OPEN_OPTION);
2888 1442 : if (ooArg && ooArg->GetType() == GAAT_STRING_LIST)
2889 : aosOpenOptions =
2890 1364 : CPLStringList(ooArg->Get<std::vector<std::string>>());
2891 :
2892 1442 : const auto ifArg = GetArg(GDAL_ARG_NAME_INPUT_FORMAT);
2893 1442 : if (ifArg && ifArg->GetType() == GAAT_STRING_LIST)
2894 : aosAllowedDrivers =
2895 1320 : CPLStringList(ifArg->Get<std::vector<std::string>>());
2896 : }
2897 : }
2898 :
2899 3050 : std::string osDatasetName = val.GetName();
2900 1525 : if (!m_referencePath.empty())
2901 : {
2902 46 : osDatasetName = GDALDataset::BuildFilename(
2903 23 : osDatasetName.c_str(), m_referencePath.c_str(), true);
2904 : }
2905 1525 : if (osDatasetName == "-" && (flags & GDAL_OF_UPDATE) == 0)
2906 0 : osDatasetName = "/vsistdin/";
2907 :
2908 : // Handle special case of overview delete in GTiff which would fail
2909 : // if it is COG without IGNORE_COG_LAYOUT_BREAK=YES open option.
2910 145 : if ((flags & GDAL_OF_UPDATE) != 0 && m_callPath.size() == 4 &&
2911 1672 : m_callPath[2] == "overview" && m_callPath[3] == "delete" &&
2912 2 : aosOpenOptions.FetchNameValue("IGNORE_COG_LAYOUT_BREAK") == nullptr)
2913 : {
2914 4 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
2915 : GDALDriverH hDrv =
2916 2 : GDALIdentifyDriver(osDatasetName.c_str(), nullptr);
2917 2 : if (hDrv && EQUAL(GDALGetDescription(hDrv), "GTiff"))
2918 : {
2919 : // Cleaning does not break COG layout
2920 2 : aosOpenOptions.SetNameValue("IGNORE_COG_LAYOUT_BREAK", "YES");
2921 : }
2922 : }
2923 :
2924 : GDALDataset *poDS;
2925 3050 : CPLErrorAccumulator oAccumulator;
2926 : {
2927 3050 : auto oContext = oAccumulator.InstallForCurrentScope();
2928 :
2929 1525 : poDS = oIterDatasetNameToDataset != m_oMapDatasetNameToDataset.end()
2930 1525 : ? oIterDatasetNameToDataset->second
2931 1509 : : GDALDataset::Open(osDatasetName.c_str(), flags,
2932 1509 : aosAllowedDrivers.List(),
2933 1509 : aosOpenOptions.List());
2934 :
2935 70 : if (!poDS && aosAllowedDrivers.empty() && aosOpenOptions.empty() &&
2936 1595 : !arg->IsOutput() && arg->GetDatasetType() & GDAL_OF_VECTOR)
2937 : {
2938 40 : auto [poWktGeom, eErr] = OGRGeometryFactory::createFromWkt(
2939 80 : osDatasetName.c_str(), nullptr);
2940 40 : if (eErr == OGRERR_NONE)
2941 : {
2942 12 : auto poMemDS = std::make_unique<MEMDataset>();
2943 12 : auto *poLayer = poMemDS->CreateLayer(
2944 : "layer", poWktGeom->getSpatialReference(),
2945 6 : poWktGeom->getGeometryType());
2946 :
2947 6 : auto poFeatureDefn = poLayer->GetLayerDefn();
2948 12 : OGRFeature oFeature(poFeatureDefn);
2949 :
2950 6 : oFeature.SetGeometry(std::move(poWktGeom));
2951 6 : if (poLayer->CreateFeature(&oFeature) == OGRERR_NONE)
2952 : {
2953 6 : poDS = poMemDS.release();
2954 6 : oAccumulator.ClearErrors();
2955 : }
2956 : }
2957 : }
2958 :
2959 : // Retry with PostGIS vector driver
2960 64 : if (!poDS && (flags & (GDAL_OF_RASTER | GDAL_OF_VECTOR)) != 0 &&
2961 62 : cpl::starts_with(osDatasetName, "PG:") &&
2962 0 : GetGDALDriverManager()->GetDriverByName("PostGISRaster") &&
2963 1589 : aosAllowedDrivers.empty() && aosOpenOptions.empty())
2964 : {
2965 0 : oAccumulator.ClearErrors();
2966 0 : poDS = GDALDataset::Open(
2967 0 : osDatasetName.c_str(), flags & ~GDAL_OF_RASTER,
2968 0 : aosAllowedDrivers.List(), aosOpenOptions.List());
2969 : }
2970 : }
2971 1525 : oAccumulator.ReplayErrors();
2972 :
2973 1525 : if (poDS)
2974 : {
2975 1461 : if (oIterDatasetNameToDataset != m_oMapDatasetNameToDataset.end())
2976 : {
2977 16 : if (arg->GetType() == GAAT_DATASET)
2978 8 : arg->Get<GDALArgDatasetValue>().Set(poDS->GetDescription());
2979 16 : poDS->Reference();
2980 16 : m_oMapDatasetNameToDataset.erase(oIterDatasetNameToDataset);
2981 : }
2982 :
2983 : // A bit of a hack for situations like 'gdal raster clip --like "PG:..."'
2984 : // where the PG: dataset will be first opened with the PostGISRaster
2985 : // driver whereas the PostgreSQL (vector) one is actually wanted.
2986 2034 : if (poDS->GetRasterCount() == 0 && (flags & GDAL_OF_RASTER) != 0 &&
2987 2148 : (flags & GDAL_OF_VECTOR) != 0 && aosAllowedDrivers.empty() &&
2988 114 : aosOpenOptions.empty())
2989 : {
2990 110 : auto poDrv = poDS->GetDriver();
2991 110 : if (poDrv && EQUAL(poDrv->GetDescription(), "PostGISRaster"))
2992 : {
2993 : // Retry with PostgreSQL (vector) driver
2994 : std::unique_ptr<GDALDataset> poTmpDS(GDALDataset::Open(
2995 0 : osDatasetName.c_str(), flags & ~GDAL_OF_RASTER));
2996 0 : if (poTmpDS)
2997 : {
2998 0 : poDS->ReleaseRef();
2999 0 : poDS = poTmpDS.release();
3000 : }
3001 : }
3002 : }
3003 :
3004 1461 : if (assignToOutputArg)
3005 : {
3006 : // Avoid opening twice the same datasource if it is both
3007 : // the input and output.
3008 : // Known to cause problems with at least FGdb, SQLite
3009 : // and GPKG drivers. See #4270
3010 : // Restrict to those 3 drivers. For example it is known
3011 : // to break with the PG driver due to the way it
3012 : // manages transactions.
3013 2 : auto poDriver = poDS->GetDriver();
3014 4 : if (poDriver && (EQUAL(poDriver->GetDescription(), "FileGDB") ||
3015 2 : EQUAL(poDriver->GetDescription(), "SQLite") ||
3016 2 : EQUAL(poDriver->GetDescription(), "GPKG")))
3017 : {
3018 2 : outputArg->Get<GDALArgDatasetValue>().Set(poDS);
3019 : }
3020 : }
3021 1461 : val.SetDatasetOpenedByAlgorithm();
3022 1461 : val.Set(poDS);
3023 1461 : poDS->ReleaseRef();
3024 : }
3025 64 : else if (!append)
3026 : {
3027 62 : ret = false;
3028 : }
3029 : }
3030 :
3031 : // Deal with overwriting the output dataset
3032 10844 : if (ret && arg == outputArg && val.GetDatasetRef() == nullptr)
3033 : {
3034 3338 : if (!append)
3035 : {
3036 : // If outputting to MEM, do not try to erase a real file of the same name!
3037 : const auto outputFormatArg =
3038 3326 : algForOutput->GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT);
3039 9940 : if (!(outputFormatArg &&
3040 3307 : outputFormatArg->GetType() == GAAT_STRING &&
3041 3307 : (EQUAL(outputFormatArg->Get<std::string>().c_str(), "MEM") ||
3042 2175 : EQUAL(outputFormatArg->Get<std::string>().c_str(),
3043 1220 : "stream") ||
3044 1220 : EQUAL(outputFormatArg->Get<std::string>().c_str(),
3045 : "Memory"))))
3046 : {
3047 1239 : const char *pszType = "";
3048 1239 : GDALDriver *poDriver = nullptr;
3049 2432 : if (!val.GetName().empty() &&
3050 1193 : GDALDoesFileOrDatasetExist(val.GetName().c_str(), &pszType,
3051 : &poDriver))
3052 : {
3053 79 : if (!overwrite)
3054 : {
3055 68 : std::string options;
3056 34 : if (algForOutput->GetArg(GDAL_ARG_NAME_OVERWRITE_LAYER))
3057 : {
3058 11 : options += "--";
3059 11 : options += GDAL_ARG_NAME_OVERWRITE_LAYER;
3060 : }
3061 34 : if (hasAppendArg)
3062 : {
3063 22 : if (!options.empty())
3064 8 : options += '/';
3065 22 : options += "--";
3066 22 : options += GDAL_ARG_NAME_APPEND;
3067 : }
3068 34 : if (hasUpdateArg)
3069 : {
3070 15 : if (!options.empty())
3071 12 : options += '/';
3072 15 : options += "--";
3073 15 : options += GDAL_ARG_NAME_UPDATE;
3074 : }
3075 :
3076 34 : if (poDriver)
3077 : {
3078 68 : const char *pszPrefix = poDriver->GetMetadataItem(
3079 34 : GDAL_DMD_CONNECTION_PREFIX);
3080 34 : if (pszPrefix &&
3081 0 : STARTS_WITH_CI(val.GetName().c_str(),
3082 : pszPrefix))
3083 : {
3084 0 : bool bExists = false;
3085 : {
3086 : CPLErrorStateBackuper oBackuper(
3087 0 : CPLQuietErrorHandler);
3088 0 : bExists = std::unique_ptr<GDALDataset>(
3089 : GDALDataset::Open(
3090 0 : val.GetName().c_str())) !=
3091 : nullptr;
3092 : }
3093 0 : if (bExists)
3094 : {
3095 0 : if (!options.empty())
3096 0 : options = " You may specify the " +
3097 0 : options + " option.";
3098 0 : ReportError(CE_Failure, CPLE_AppDefined,
3099 : "%s '%s' already exists.%s",
3100 0 : pszType, val.GetName().c_str(),
3101 : options.c_str());
3102 0 : return false;
3103 : }
3104 :
3105 0 : return true;
3106 : }
3107 : }
3108 :
3109 34 : if (!options.empty())
3110 28 : options = '/' + options;
3111 68 : ReportError(
3112 : CE_Failure, CPLE_AppDefined,
3113 : "%s '%s' already exists. You may specify the "
3114 : "--overwrite%s option.",
3115 34 : pszType, val.GetName().c_str(), options.c_str());
3116 34 : return false;
3117 : }
3118 45 : else if (EQUAL(pszType, "File"))
3119 : {
3120 1 : if (VSIUnlink(val.GetName().c_str()) != 0)
3121 : {
3122 0 : ReportError(CE_Failure, CPLE_AppDefined,
3123 : "Deleting %s failed: %s",
3124 0 : val.GetName().c_str(),
3125 0 : VSIStrerror(errno));
3126 0 : return false;
3127 : }
3128 : }
3129 44 : else if (EQUAL(pszType, "Directory"))
3130 : {
3131 : // We don't want the user to accidentally erase a non-GDAL dataset
3132 1 : ReportError(CE_Failure, CPLE_AppDefined,
3133 : "Directory '%s' already exists, but is not "
3134 : "recognized as a valid GDAL dataset. "
3135 : "Please manually delete it before retrying",
3136 1 : val.GetName().c_str());
3137 1 : return false;
3138 : }
3139 43 : else if (poDriver)
3140 : {
3141 : bool bDeleteOK;
3142 : {
3143 : CPLErrorStateBackuper oBackuper(
3144 43 : CPLQuietErrorHandler);
3145 43 : bDeleteOK = (poDriver->Delete(
3146 43 : val.GetName().c_str()) == CE_None);
3147 : }
3148 : VSIStatBufL sStat;
3149 46 : if (!bDeleteOK &&
3150 3 : VSIStatL(val.GetName().c_str(), &sStat) == 0)
3151 : {
3152 3 : if (VSI_ISDIR(sStat.st_mode))
3153 : {
3154 : // We don't want the user to accidentally erase a non-GDAL dataset
3155 0 : ReportError(
3156 : CE_Failure, CPLE_AppDefined,
3157 : "Directory '%s' already exists, but is not "
3158 : "recognized as a valid GDAL dataset. "
3159 : "Please manually delete it before retrying",
3160 0 : val.GetName().c_str());
3161 2 : return false;
3162 : }
3163 3 : else if (VSIUnlink(val.GetName().c_str()) != 0)
3164 : {
3165 2 : ReportError(CE_Failure, CPLE_AppDefined,
3166 : "Deleting %s failed: %s",
3167 2 : val.GetName().c_str(),
3168 2 : VSIStrerror(errno));
3169 2 : return false;
3170 : }
3171 : }
3172 : }
3173 : }
3174 : }
3175 : }
3176 : }
3177 :
3178 : // If outputting to stdout, automatically turn off progress bar
3179 10807 : if (arg == outputArg && val.GetName() == "/vsistdout/")
3180 : {
3181 8 : auto quietArg = GetArg(GDAL_ARG_NAME_QUIET);
3182 8 : if (quietArg && quietArg->GetType() == GAAT_BOOLEAN)
3183 5 : quietArg->Set(true);
3184 : }
3185 :
3186 10807 : return ret;
3187 : }
3188 :
3189 : /************************************************************************/
3190 : /* GDALAlgorithm::ValidateArguments() */
3191 : /************************************************************************/
3192 :
3193 7600 : bool GDALAlgorithm::ValidateArguments()
3194 : {
3195 7600 : if (m_selectedSubAlg)
3196 3 : return m_selectedSubAlg->ValidateArguments();
3197 :
3198 7597 : if (m_specialActionRequested)
3199 1 : return true;
3200 :
3201 7596 : m_arbitraryLongNameArgsAllowed = false;
3202 :
3203 : // If only --output=format=MEM/stream is specified and not --output,
3204 : // then set empty name for --output.
3205 7596 : auto outputArg = GetArg(GDAL_ARG_NAME_OUTPUT);
3206 7596 : auto outputFormatArg = GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT);
3207 4432 : if (outputArg && outputFormatArg && outputFormatArg->IsExplicitlySet() &&
3208 2812 : !outputArg->IsExplicitlySet() &&
3209 380 : outputFormatArg->GetType() == GAAT_STRING &&
3210 380 : (EQUAL(outputFormatArg->Get<std::string>().c_str(), "MEM") ||
3211 613 : EQUAL(outputFormatArg->Get<std::string>().c_str(), "stream")) &&
3212 12379 : outputArg->GetType() == GAAT_DATASET &&
3213 351 : (outputArg->GetDatasetInputFlags() & GADV_NAME))
3214 : {
3215 351 : outputArg->Get<GDALArgDatasetValue>().Set("");
3216 : }
3217 :
3218 : // The method may emit several errors if several constraints are not met.
3219 7596 : bool ret = true;
3220 15192 : std::map<std::string, std::string> mutualExclusionGroupUsed;
3221 15192 : std::map<std::string, std::vector<std::string>> mutualDependencyGroupUsed;
3222 140461 : for (auto &arg : m_args)
3223 : {
3224 : // Check mutually exclusive/dependent arguments
3225 132865 : if (arg->IsExplicitlySet())
3226 : {
3227 :
3228 20962 : const auto &mutualExclusionGroup = arg->GetMutualExclusionGroup();
3229 20962 : if (!mutualExclusionGroup.empty())
3230 : {
3231 : auto oIter =
3232 823 : mutualExclusionGroupUsed.find(mutualExclusionGroup);
3233 823 : if (oIter != mutualExclusionGroupUsed.end())
3234 : {
3235 13 : ret = false;
3236 26 : ReportError(
3237 : CE_Failure, CPLE_AppDefined,
3238 : "Argument '%s' is mutually exclusive with '%s'.",
3239 26 : arg->GetName().c_str(), oIter->second.c_str());
3240 : }
3241 : else
3242 : {
3243 810 : mutualExclusionGroupUsed[mutualExclusionGroup] =
3244 1620 : arg->GetName();
3245 : }
3246 : }
3247 :
3248 20962 : const auto &mutualDependencyGroup = arg->GetMutualDependencyGroup();
3249 20962 : if (!mutualDependencyGroup.empty())
3250 : {
3251 78 : if (mutualDependencyGroupUsed.find(mutualDependencyGroup) ==
3252 156 : mutualDependencyGroupUsed.end())
3253 : {
3254 129 : mutualDependencyGroupUsed[mutualDependencyGroup] = {
3255 129 : arg->GetName()};
3256 : }
3257 : else
3258 : {
3259 70 : mutualDependencyGroupUsed[mutualDependencyGroup].push_back(
3260 35 : arg->GetName());
3261 : }
3262 : }
3263 :
3264 : // Check direct dependencies
3265 20974 : for (const auto &dependency : arg->GetDirectDependencies())
3266 : {
3267 12 : auto depArg = GetArg(dependency);
3268 12 : if (!depArg)
3269 : {
3270 0 : ret = false;
3271 0 : ReportError(CE_Failure, CPLE_AppDefined,
3272 : "Argument '%s' depends on argument '%s' that "
3273 : "is not defined.",
3274 0 : arg->GetName().c_str(), dependency.c_str());
3275 : }
3276 12 : else if (!depArg->IsExplicitlySet())
3277 : {
3278 6 : ret = false;
3279 12 : ReportError(CE_Failure, CPLE_AppDefined,
3280 : "Argument '%s' depends on argument '%s' that "
3281 : "has not been specified.",
3282 6 : arg->GetName().c_str(),
3283 6 : depArg->GetName().c_str());
3284 : }
3285 : }
3286 : }
3287 :
3288 133041 : if (arg->IsRequired() && !arg->IsExplicitlySet() &&
3289 176 : !arg->HasDefaultValue())
3290 : {
3291 176 : bool emitError = true;
3292 176 : const auto &mutualExclusionGroup = arg->GetMutualExclusionGroup();
3293 176 : if (!mutualExclusionGroup.empty())
3294 : {
3295 1885 : for (const auto &otherArg : m_args)
3296 : {
3297 1859 : if (otherArg->GetMutualExclusionGroup() ==
3298 1988 : mutualExclusionGroup &&
3299 129 : otherArg->IsExplicitlySet())
3300 : {
3301 74 : emitError = false;
3302 74 : break;
3303 : }
3304 : }
3305 : }
3306 266 : if (emitError && !(m_inputDatasetCanBeOmitted &&
3307 57 : arg->GetName() == GDAL_ARG_NAME_INPUT &&
3308 66 : (arg->GetType() == GAAT_DATASET ||
3309 33 : arg->GetType() == GAAT_DATASET_LIST)))
3310 : {
3311 69 : ReportError(CE_Failure, CPLE_AppDefined,
3312 : "Required argument '%s' has not been specified.",
3313 69 : arg->GetName().c_str());
3314 69 : ret = false;
3315 : }
3316 : }
3317 132689 : else if (arg->IsExplicitlySet() && arg->GetType() == GAAT_DATASET)
3318 : {
3319 4471 : if (!ProcessDatasetArg(arg.get(), this))
3320 49 : ret = false;
3321 : }
3322 :
3323 132865 : if (arg->IsExplicitlySet() && arg->GetType() == GAAT_DATASET_LIST)
3324 : {
3325 6170 : auto &listVal = arg->Get<std::vector<GDALArgDatasetValue>>();
3326 6170 : if (listVal.size() == 1)
3327 : {
3328 6016 : if (!ProcessDatasetArg(arg.get(), this))
3329 42 : ret = false;
3330 : }
3331 : else
3332 : {
3333 473 : for (auto &val : listVal)
3334 : {
3335 319 : if (val.GetDatasetRef())
3336 : {
3337 120 : if (!CheckCanSetDatasetObject(arg.get()))
3338 : {
3339 0 : ret = false;
3340 : }
3341 315 : continue;
3342 : }
3343 :
3344 199 : if (val.GetName().empty())
3345 : {
3346 0 : ReportError(CE_Failure, CPLE_AppDefined,
3347 : "Argument '%s' has no dataset object or "
3348 : "dataset name.",
3349 0 : arg->GetName().c_str());
3350 0 : ret = false;
3351 0 : continue;
3352 : }
3353 :
3354 199 : auto oIter = m_oMapDatasetNameToDataset.find(val.GetName());
3355 199 : if (oIter != m_oMapDatasetNameToDataset.end())
3356 : {
3357 2 : auto poDS = oIter->second;
3358 2 : val.SetDatasetOpenedByAlgorithm();
3359 2 : val.Set(poDS);
3360 2 : m_oMapDatasetNameToDataset.erase(oIter);
3361 2 : continue;
3362 : }
3363 :
3364 197 : if (!arg->AutoOpenDataset())
3365 193 : continue;
3366 :
3367 4 : int flags = arg->GetDatasetType() | GDAL_OF_VERBOSE_ERROR;
3368 :
3369 8 : CPLStringList aosOpenOptions;
3370 8 : CPLStringList aosAllowedDrivers;
3371 4 : if (arg->GetName() == GDAL_ARG_NAME_INPUT)
3372 : {
3373 4 : const auto ooArg = GetArg(GDAL_ARG_NAME_OPEN_OPTION);
3374 4 : if (ooArg && ooArg->GetType() == GAAT_STRING_LIST)
3375 : {
3376 4 : aosOpenOptions = CPLStringList(
3377 4 : ooArg->Get<std::vector<std::string>>());
3378 : }
3379 :
3380 4 : const auto ifArg = GetArg(GDAL_ARG_NAME_INPUT_FORMAT);
3381 4 : if (ifArg && ifArg->GetType() == GAAT_STRING_LIST)
3382 : {
3383 4 : aosAllowedDrivers = CPLStringList(
3384 4 : ifArg->Get<std::vector<std::string>>());
3385 : }
3386 :
3387 4 : const auto updateArg = GetArg(GDAL_ARG_NAME_UPDATE);
3388 4 : if (updateArg && updateArg->GetType() == GAAT_BOOLEAN &&
3389 0 : updateArg->Get<bool>())
3390 : {
3391 0 : flags |= GDAL_OF_UPDATE;
3392 : }
3393 : }
3394 :
3395 : auto poDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
3396 4 : val.GetName().c_str(), flags, aosAllowedDrivers.List(),
3397 12 : aosOpenOptions.List()));
3398 4 : if (poDS)
3399 : {
3400 3 : val.Set(std::move(poDS));
3401 : }
3402 : else
3403 : {
3404 1 : ret = false;
3405 : }
3406 : }
3407 : }
3408 : }
3409 :
3410 132865 : if (arg->IsExplicitlySet() && !arg->RunValidationActions())
3411 : {
3412 8 : ret = false;
3413 : }
3414 : }
3415 :
3416 : // Check mutual dependency groups
3417 7596 : std::vector<std::string> processedGroups;
3418 : // Loop through group map and check there are not required args in the group that are not set
3419 7639 : for (const auto &[groupName, argNames] : mutualDependencyGroupUsed)
3420 : {
3421 43 : if (std::find(processedGroups.begin(), processedGroups.end(),
3422 43 : groupName) != processedGroups.end())
3423 0 : continue;
3424 86 : std::vector<std::string> missingArgs;
3425 848 : for (auto &arg : m_args)
3426 : {
3427 805 : const auto &mutualDependencyGroup = arg->GetMutualDependencyGroup();
3428 898 : if (mutualDependencyGroup == groupName &&
3429 93 : std::find(argNames.begin(), argNames.end(), arg->GetName()) ==
3430 898 : argNames.end())
3431 : {
3432 15 : missingArgs.push_back(arg->GetName());
3433 : }
3434 : }
3435 43 : if (!missingArgs.empty())
3436 : {
3437 12 : ret = false;
3438 24 : std::string missingArgsStr;
3439 27 : for (const auto &missingArg : missingArgs)
3440 : {
3441 15 : if (!missingArgsStr.empty())
3442 3 : missingArgsStr += ", ";
3443 15 : missingArgsStr += missingArg;
3444 : }
3445 24 : std::string givenArgsStr;
3446 27 : for (const auto &givenArg : argNames)
3447 : {
3448 15 : if (!givenArgsStr.empty())
3449 3 : givenArgsStr += ", ";
3450 15 : givenArgsStr += givenArg;
3451 : }
3452 12 : ReportError(CE_Failure, CPLE_AppDefined,
3453 : "Argument(s) '%s' require(s) that the following "
3454 : "argument(s) are also specified: %s.",
3455 : givenArgsStr.c_str(), missingArgsStr.c_str());
3456 : }
3457 43 : processedGroups.push_back(groupName);
3458 : }
3459 :
3460 32226 : for (const auto &f : m_validationActions)
3461 : {
3462 24630 : if (!f())
3463 81 : ret = false;
3464 : }
3465 :
3466 7596 : return ret;
3467 : }
3468 :
3469 : /************************************************************************/
3470 : /* GDALAlgorithm::InstantiateSubAlgorithm */
3471 : /************************************************************************/
3472 :
3473 : std::unique_ptr<GDALAlgorithm>
3474 10659 : GDALAlgorithm::InstantiateSubAlgorithm(const std::string &name,
3475 : bool suggestionAllowed) const
3476 : {
3477 10659 : auto ret = m_subAlgRegistry.Instantiate(name);
3478 21318 : auto childCallPath = m_callPath;
3479 10659 : childCallPath.push_back(name);
3480 10659 : if (!ret)
3481 : {
3482 1191 : ret = GDALGlobalAlgorithmRegistry::GetSingleton()
3483 1191 : .InstantiateDeclaredSubAlgorithm(childCallPath);
3484 : }
3485 10659 : if (ret)
3486 : {
3487 10479 : ret->SetCallPath(childCallPath);
3488 : }
3489 180 : else if (suggestionAllowed)
3490 : {
3491 70 : std::string bestCandidate;
3492 35 : size_t bestDistance = std::numeric_limits<size_t>::max();
3493 515 : for (const std::string &candidate : GetSubAlgorithmNames())
3494 : {
3495 : const size_t distance =
3496 480 : CPLLevenshteinDistance(name.c_str(), candidate.c_str(),
3497 : /* transpositionAllowed = */ true);
3498 480 : if (distance < bestDistance)
3499 : {
3500 81 : bestCandidate = candidate;
3501 81 : bestDistance = distance;
3502 : }
3503 399 : else if (distance == bestDistance)
3504 : {
3505 49 : bestCandidate.clear();
3506 : }
3507 : }
3508 35 : if (!bestCandidate.empty() && bestDistance <= 2)
3509 : {
3510 4 : CPLError(CE_Failure, CPLE_AppDefined,
3511 : "Algorithm '%s' is unknown. Do you mean '%s'?",
3512 : name.c_str(), bestCandidate.c_str());
3513 : }
3514 : }
3515 21318 : return ret;
3516 : }
3517 :
3518 : /************************************************************************/
3519 : /* GDALAlgorithm::GetSuggestionForArgumentName() */
3520 : /************************************************************************/
3521 :
3522 : std::string
3523 39 : GDALAlgorithm::GetSuggestionForArgumentName(const std::string &osName) const
3524 : {
3525 39 : if (osName.size() >= 3)
3526 : {
3527 34 : std::string bestCandidate;
3528 34 : size_t bestDistance = std::numeric_limits<size_t>::max();
3529 752 : for (const auto &[key, value] : m_mapLongNameToArg)
3530 : {
3531 718 : CPL_IGNORE_RET_VAL(value);
3532 718 : const size_t distance = CPLLevenshteinDistance(
3533 : osName.c_str(), key.c_str(), /* transpositionAllowed = */ true);
3534 718 : if (distance < bestDistance)
3535 : {
3536 89 : bestCandidate = key;
3537 89 : bestDistance = distance;
3538 : }
3539 629 : else if (distance == bestDistance)
3540 : {
3541 77 : bestCandidate.clear();
3542 : }
3543 : }
3544 48 : if (!bestCandidate.empty() &&
3545 14 : bestDistance <= (bestCandidate.size() >= 4U ? 2U : 1U))
3546 : {
3547 5 : return bestCandidate;
3548 : }
3549 : }
3550 34 : return std::string();
3551 : }
3552 :
3553 : /************************************************************************/
3554 : /* GDALAlgorithm::GetSuggestionsForArgumentName() */
3555 : /************************************************************************/
3556 :
3557 : std::vector<std::string>
3558 39 : GDALAlgorithm::GetSuggestionsForArgumentName(const std::string &osName) const
3559 : {
3560 39 : std::vector<std::string> ret;
3561 78 : std::string suggestion = GetSuggestionForArgumentName(osName);
3562 39 : if (!suggestion.empty())
3563 : {
3564 5 : ret.push_back(std::move(suggestion));
3565 : }
3566 34 : else if (osName.size() >= 3)
3567 : {
3568 : // e.g "crs" for reproject will match "input-crs" and "target-crs"
3569 87 : const std::string dashName = std::string("-").append(osName);
3570 532 : for (const auto &arg : m_args)
3571 : {
3572 503 : if (cpl::ends_with(arg->GetName(), dashName))
3573 : {
3574 3 : ret.push_back(arg->GetName());
3575 : }
3576 : }
3577 : }
3578 78 : return ret;
3579 : }
3580 :
3581 : /************************************************************************/
3582 : /* GDALAlgorithm::IsKnownOutputRelatedBooleanArgName() */
3583 : /************************************************************************/
3584 :
3585 : /* static */
3586 23 : bool GDALAlgorithm::IsKnownOutputRelatedBooleanArgName(std::string_view osName)
3587 : {
3588 69 : return osName == GDAL_ARG_NAME_APPEND || osName == GDAL_ARG_NAME_UPDATE ||
3589 69 : osName == GDAL_ARG_NAME_OVERWRITE ||
3590 46 : osName == GDAL_ARG_NAME_OVERWRITE_LAYER;
3591 : }
3592 :
3593 : /************************************************************************/
3594 : /* GDALAlgorithm::HasOutputString() */
3595 : /************************************************************************/
3596 :
3597 74 : bool GDALAlgorithm::HasOutputString() const
3598 : {
3599 74 : auto outputStringArg = GetArg(GDAL_ARG_NAME_OUTPUT_STRING);
3600 74 : return outputStringArg && outputStringArg->IsOutput();
3601 : }
3602 :
3603 : /************************************************************************/
3604 : /* GDALAlgorithm::GetArg() */
3605 : /************************************************************************/
3606 :
3607 489727 : GDALAlgorithmArg *GDALAlgorithm::GetArg(const std::string &osName,
3608 : bool suggestionAllowed, bool isConst)
3609 : {
3610 489727 : const auto nPos = osName.find_first_not_of('-');
3611 489727 : if (nPos == std::string::npos)
3612 27 : return nullptr;
3613 979400 : std::string osKey = osName.substr(nPos);
3614 : {
3615 489700 : const auto oIter = m_mapLongNameToArg.find(osKey);
3616 489700 : if (oIter != m_mapLongNameToArg.end())
3617 453530 : return oIter->second;
3618 : }
3619 : {
3620 36170 : const auto oIter = m_mapShortNameToArg.find(osKey);
3621 36170 : if (oIter != m_mapShortNameToArg.end())
3622 8 : return oIter->second;
3623 : }
3624 :
3625 36162 : if (!isConst && m_arbitraryLongNameArgsAllowed)
3626 : {
3627 23 : const auto nDotPos = osKey.find('.');
3628 : const std::string osKeyEnd =
3629 23 : nDotPos == std::string::npos ? osKey : osKey.substr(nDotPos + 1);
3630 23 : if (IsKnownOutputRelatedBooleanArgName(osKeyEnd))
3631 : {
3632 : m_arbitraryLongNameArgsValuesBool.emplace_back(
3633 0 : std::make_unique<bool>());
3634 0 : AddArg(osKey, 0, std::string("User-provided argument ") + osKey,
3635 0 : m_arbitraryLongNameArgsValuesBool.back().get())
3636 0 : .SetUserProvided();
3637 : }
3638 : else
3639 : {
3640 46 : const std::string osKeyInit = osKey;
3641 23 : if (osKey == "oo")
3642 0 : osKey = GDAL_ARG_NAME_OPEN_OPTION;
3643 23 : else if (osKey == "co")
3644 0 : osKey = GDAL_ARG_NAME_CREATION_OPTION;
3645 23 : else if (osKey == "of")
3646 0 : osKey = GDAL_ARG_NAME_OUTPUT_FORMAT;
3647 23 : else if (osKey == "if")
3648 0 : osKey = GDAL_ARG_NAME_INPUT_FORMAT;
3649 : m_arbitraryLongNameArgsValuesStr.emplace_back(
3650 23 : std::make_unique<std::string>());
3651 : auto &arg =
3652 46 : AddArg(osKey, 0, std::string("User-provided argument ") + osKey,
3653 46 : m_arbitraryLongNameArgsValuesStr.back().get())
3654 23 : .SetUserProvided();
3655 23 : if (osKey != osKeyInit)
3656 0 : arg.AddAlias(osKeyInit);
3657 : }
3658 23 : const auto oIter = m_mapLongNameToArg.find(osKey);
3659 23 : CPLAssert(oIter != m_mapLongNameToArg.end());
3660 23 : return oIter->second;
3661 : }
3662 :
3663 36139 : if (suggestionAllowed)
3664 : {
3665 14 : const auto suggestions = GetSuggestionsForArgumentName(osName);
3666 7 : if (!suggestions.empty())
3667 : {
3668 2 : CPLError(CE_Failure, CPLE_AppDefined,
3669 : "Argument '%s' is unknown. Do you mean %s?",
3670 : osName.c_str(),
3671 4 : FormatSuggestionsAsString(suggestions,
3672 : /* addDashDashPrefix = */ false)
3673 : .c_str());
3674 : }
3675 : }
3676 :
3677 36139 : return nullptr;
3678 : }
3679 :
3680 : /************************************************************************/
3681 : /* GDALAlgorithm::AddAliasFor() */
3682 : /************************************************************************/
3683 :
3684 : //! @cond Doxygen_Suppress
3685 84466 : void GDALAlgorithm::AddAliasFor(GDALInConstructionAlgorithmArg *arg,
3686 : const std::string &alias)
3687 : {
3688 84466 : if (cpl::contains(m_mapLongNameToArg, alias))
3689 : {
3690 1 : ReportError(CE_Failure, CPLE_AppDefined, "Name '%s' already declared.",
3691 : alias.c_str());
3692 : }
3693 : else
3694 : {
3695 84465 : m_mapLongNameToArg[alias] = arg;
3696 : }
3697 84466 : }
3698 :
3699 : //! @endcond
3700 :
3701 : /************************************************************************/
3702 : /* GDALAlgorithm::AddShortNameAliasFor() */
3703 : /************************************************************************/
3704 :
3705 : //! @cond Doxygen_Suppress
3706 49 : void GDALAlgorithm::AddShortNameAliasFor(GDALInConstructionAlgorithmArg *arg,
3707 : char shortNameAlias)
3708 : {
3709 98 : std::string alias;
3710 49 : alias += shortNameAlias;
3711 49 : if (cpl::contains(m_mapShortNameToArg, alias))
3712 : {
3713 0 : ReportError(CE_Failure, CPLE_AppDefined,
3714 : "Short name '%s' already declared.", alias.c_str());
3715 : }
3716 : else
3717 : {
3718 49 : m_mapShortNameToArg[alias] = arg;
3719 : }
3720 49 : }
3721 :
3722 : //! @endcond
3723 :
3724 : /************************************************************************/
3725 : /* GDALAlgorithm::SetPositional() */
3726 : /************************************************************************/
3727 :
3728 : //! @cond Doxygen_Suppress
3729 23183 : void GDALAlgorithm::SetPositional(GDALInConstructionAlgorithmArg *arg)
3730 : {
3731 23183 : CPLAssert(std::find(m_positionalArgs.begin(), m_positionalArgs.end(),
3732 : arg) == m_positionalArgs.end());
3733 23183 : m_positionalArgs.push_back(arg);
3734 23183 : }
3735 :
3736 : //! @endcond
3737 :
3738 : /************************************************************************/
3739 : /* GDALAlgorithm::HasSubAlgorithms() */
3740 : /************************************************************************/
3741 :
3742 13818 : bool GDALAlgorithm::HasSubAlgorithms() const
3743 : {
3744 13818 : if (!m_subAlgRegistry.empty())
3745 3455 : return true;
3746 10363 : return !GDALGlobalAlgorithmRegistry::GetSingleton()
3747 20726 : .GetDeclaredSubAlgorithmNames(m_callPath)
3748 10363 : .empty();
3749 : }
3750 :
3751 : /************************************************************************/
3752 : /* GDALAlgorithm::GetSubAlgorithmNames() */
3753 : /************************************************************************/
3754 :
3755 1449 : std::vector<std::string> GDALAlgorithm::GetSubAlgorithmNames() const
3756 : {
3757 1449 : std::vector<std::string> ret = m_subAlgRegistry.GetNames();
3758 1449 : const auto other = GDALGlobalAlgorithmRegistry::GetSingleton()
3759 2898 : .GetDeclaredSubAlgorithmNames(m_callPath);
3760 1449 : ret.insert(ret.end(), other.begin(), other.end());
3761 1449 : if (!other.empty())
3762 507 : std::sort(ret.begin(), ret.end());
3763 2898 : return ret;
3764 : }
3765 :
3766 : /************************************************************************/
3767 : /* GDALAlgorithm::AddArg() */
3768 : /************************************************************************/
3769 :
3770 : GDALInConstructionAlgorithmArg &
3771 331550 : GDALAlgorithm::AddArg(std::unique_ptr<GDALInConstructionAlgorithmArg> arg)
3772 : {
3773 331550 : auto argRaw = arg.get();
3774 331550 : const auto &longName = argRaw->GetName();
3775 331550 : if (!longName.empty())
3776 : {
3777 331537 : if (longName[0] == '-')
3778 : {
3779 1 : ReportError(CE_Failure, CPLE_AppDefined,
3780 : "Long name '%s' should not start with '-'",
3781 : longName.c_str());
3782 : }
3783 331537 : if (longName.find('=') != std::string::npos)
3784 : {
3785 1 : ReportError(CE_Failure, CPLE_AppDefined,
3786 : "Long name '%s' should not contain a '=' character",
3787 : longName.c_str());
3788 : }
3789 331537 : if (cpl::contains(m_mapLongNameToArg, longName))
3790 : {
3791 1 : ReportError(CE_Failure, CPLE_AppDefined,
3792 : "Long name '%s' already declared", longName.c_str());
3793 : }
3794 331537 : m_mapLongNameToArg[longName] = argRaw;
3795 : }
3796 331550 : const auto &shortName = argRaw->GetShortName();
3797 331550 : if (!shortName.empty())
3798 : {
3799 160946 : if (shortName.size() != 1 ||
3800 80473 : !((shortName[0] >= 'a' && shortName[0] <= 'z') ||
3801 65 : (shortName[0] >= 'A' && shortName[0] <= 'Z') ||
3802 2 : (shortName[0] >= '0' && shortName[0] <= '9')))
3803 : {
3804 1 : ReportError(CE_Failure, CPLE_AppDefined,
3805 : "Short name '%s' should be a single letter or digit",
3806 : shortName.c_str());
3807 : }
3808 80473 : if (cpl::contains(m_mapShortNameToArg, shortName))
3809 : {
3810 1 : ReportError(CE_Failure, CPLE_AppDefined,
3811 : "Short name '%s' already declared", shortName.c_str());
3812 : }
3813 80473 : m_mapShortNameToArg[shortName] = argRaw;
3814 : }
3815 331550 : m_args.emplace_back(std::move(arg));
3816 : return *(
3817 331550 : cpl::down_cast<GDALInConstructionAlgorithmArg *>(m_args.back().get()));
3818 : }
3819 :
3820 : GDALInConstructionAlgorithmArg &
3821 148368 : GDALAlgorithm::AddArg(const std::string &longName, char chShortName,
3822 : const std::string &helpMessage, bool *pValue)
3823 : {
3824 148368 : return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>(
3825 : this,
3826 296736 : GDALAlgorithmArgDecl(longName, chShortName, helpMessage, GAAT_BOOLEAN),
3827 296736 : pValue));
3828 : }
3829 :
3830 : GDALInConstructionAlgorithmArg &
3831 53570 : GDALAlgorithm::AddArg(const std::string &longName, char chShortName,
3832 : const std::string &helpMessage, std::string *pValue)
3833 : {
3834 53570 : return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>(
3835 : this,
3836 107140 : GDALAlgorithmArgDecl(longName, chShortName, helpMessage, GAAT_STRING),
3837 107140 : pValue));
3838 : }
3839 :
3840 : GDALInConstructionAlgorithmArg &
3841 12709 : GDALAlgorithm::AddArg(const std::string &longName, char chShortName,
3842 : const std::string &helpMessage, int *pValue)
3843 : {
3844 12709 : return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>(
3845 : this,
3846 25418 : GDALAlgorithmArgDecl(longName, chShortName, helpMessage, GAAT_INTEGER),
3847 25418 : pValue));
3848 : }
3849 :
3850 : GDALInConstructionAlgorithmArg &
3851 10272 : GDALAlgorithm::AddArg(const std::string &longName, char chShortName,
3852 : const std::string &helpMessage, double *pValue)
3853 : {
3854 10272 : return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>(
3855 : this,
3856 20544 : GDALAlgorithmArgDecl(longName, chShortName, helpMessage, GAAT_REAL),
3857 20544 : pValue));
3858 : }
3859 :
3860 : GDALInConstructionAlgorithmArg &
3861 12533 : GDALAlgorithm::AddArg(const std::string &longName, char chShortName,
3862 : const std::string &helpMessage,
3863 : GDALArgDatasetValue *pValue, GDALArgDatasetType type)
3864 : {
3865 25066 : auto &arg = AddArg(std::make_unique<GDALInConstructionAlgorithmArg>(
3866 : this,
3867 25066 : GDALAlgorithmArgDecl(longName, chShortName,
3868 : helpMessage, GAAT_DATASET),
3869 12533 : pValue))
3870 12533 : .SetDatasetType(type);
3871 12533 : pValue->SetOwnerArgument(&arg);
3872 12533 : return arg;
3873 : }
3874 :
3875 : GDALInConstructionAlgorithmArg &
3876 71462 : GDALAlgorithm::AddArg(const std::string &longName, char chShortName,
3877 : const std::string &helpMessage,
3878 : std::vector<std::string> *pValue)
3879 : {
3880 71462 : return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>(
3881 : this,
3882 142924 : GDALAlgorithmArgDecl(longName, chShortName, helpMessage,
3883 : GAAT_STRING_LIST),
3884 142924 : pValue));
3885 : }
3886 :
3887 : GDALInConstructionAlgorithmArg &
3888 2260 : GDALAlgorithm::AddArg(const std::string &longName, char chShortName,
3889 : const std::string &helpMessage, std::vector<int> *pValue)
3890 : {
3891 2260 : return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>(
3892 : this,
3893 4520 : GDALAlgorithmArgDecl(longName, chShortName, helpMessage,
3894 : GAAT_INTEGER_LIST),
3895 4520 : pValue));
3896 : }
3897 :
3898 : GDALInConstructionAlgorithmArg &
3899 5296 : GDALAlgorithm::AddArg(const std::string &longName, char chShortName,
3900 : const std::string &helpMessage,
3901 : std::vector<double> *pValue)
3902 : {
3903 5296 : return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>(
3904 : this,
3905 10592 : GDALAlgorithmArgDecl(longName, chShortName, helpMessage,
3906 : GAAT_REAL_LIST),
3907 10592 : pValue));
3908 : }
3909 :
3910 : GDALInConstructionAlgorithmArg &
3911 15080 : GDALAlgorithm::AddArg(const std::string &longName, char chShortName,
3912 : const std::string &helpMessage,
3913 : std::vector<GDALArgDatasetValue> *pValue,
3914 : GDALArgDatasetType type)
3915 : {
3916 30160 : return AddArg(std::make_unique<GDALInConstructionAlgorithmArg>(
3917 : this,
3918 30160 : GDALAlgorithmArgDecl(longName, chShortName, helpMessage,
3919 : GAAT_DATASET_LIST),
3920 15080 : pValue))
3921 30160 : .SetDatasetType(type);
3922 : }
3923 :
3924 : /************************************************************************/
3925 : /* MsgOrDefault() */
3926 : /************************************************************************/
3927 :
3928 110655 : inline const char *MsgOrDefault(const char *helpMessage,
3929 : const char *defaultMessage)
3930 : {
3931 110655 : return helpMessage && helpMessage[0] ? helpMessage : defaultMessage;
3932 : }
3933 :
3934 : /************************************************************************/
3935 : /* GDALAlgorithm::SetAutoCompleteFunctionForFilename() */
3936 : /************************************************************************/
3937 :
3938 : /* static */
3939 18445 : void GDALAlgorithm::SetAutoCompleteFunctionForFilename(
3940 : GDALInConstructionAlgorithmArg &arg, GDALArgDatasetType type)
3941 : {
3942 : arg.SetAutoCompleteFunction(
3943 7 : [&arg,
3944 2483 : type](const std::string ¤tValue) -> std::vector<std::string>
3945 : {
3946 14 : std::vector<std::string> oRet;
3947 :
3948 7 : if (arg.IsHidden())
3949 0 : return oRet;
3950 :
3951 : {
3952 7 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
3953 : VSIStatBufL sStat;
3954 10 : if (!currentValue.empty() && currentValue.back() != '/' &&
3955 3 : VSIStatL(currentValue.c_str(), &sStat) == 0)
3956 : {
3957 0 : return oRet;
3958 : }
3959 : }
3960 :
3961 7 : auto poDM = GetGDALDriverManager();
3962 14 : std::set<std::string> oExtensions;
3963 7 : if (type)
3964 : {
3965 1386 : for (int i = 0; i < poDM->GetDriverCount(); ++i)
3966 : {
3967 1380 : auto poDriver = poDM->GetDriver(i);
3968 3910 : if (((type & GDAL_OF_RASTER) != 0 &&
3969 1150 : poDriver->GetMetadataItem(GDAL_DCAP_RASTER)) ||
3970 590 : ((type & GDAL_OF_VECTOR) != 0 &&
3971 2899 : poDriver->GetMetadataItem(GDAL_DCAP_VECTOR)) ||
3972 499 : ((type & GDAL_OF_MULTIDIM_RASTER) != 0 &&
3973 0 : poDriver->GetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER)))
3974 : {
3975 : const char *pszExtensions =
3976 881 : poDriver->GetMetadataItem(GDAL_DMD_EXTENSIONS);
3977 881 : if (pszExtensions)
3978 : {
3979 : const CPLStringList aosExts(
3980 1164 : CSLTokenizeString2(pszExtensions, " ", 0));
3981 1313 : for (const char *pszExt : cpl::Iterate(aosExts))
3982 731 : oExtensions.insert(CPLString(pszExt).tolower());
3983 : }
3984 : }
3985 : }
3986 : }
3987 :
3988 14 : std::string osDir;
3989 14 : const CPLStringList aosVSIPrefixes(VSIGetFileSystemsPrefixes());
3990 14 : std::string osPrefix;
3991 7 : if (STARTS_WITH(currentValue.c_str(), "/vsi"))
3992 : {
3993 82 : for (const char *pszPrefix : cpl::Iterate(aosVSIPrefixes))
3994 : {
3995 81 : if (STARTS_WITH(currentValue.c_str(), pszPrefix))
3996 : {
3997 2 : osPrefix = pszPrefix;
3998 2 : break;
3999 : }
4000 : }
4001 3 : if (osPrefix.empty())
4002 1 : return aosVSIPrefixes;
4003 2 : if (currentValue == osPrefix)
4004 1 : osDir = osPrefix;
4005 : }
4006 6 : if (osDir.empty())
4007 : {
4008 5 : osDir = CPLGetDirnameSafe(currentValue.c_str());
4009 5 : if (!osPrefix.empty() && osDir.size() < osPrefix.size())
4010 0 : osDir = std::move(osPrefix);
4011 : }
4012 :
4013 6 : auto psDir = VSIOpenDir(osDir.c_str(), 0, nullptr);
4014 12 : const std::string osSep = VSIGetDirectorySeparator(osDir.c_str());
4015 6 : if (currentValue.empty())
4016 1 : osDir.clear();
4017 : const std::string currentFilename =
4018 12 : CPLGetFilename(currentValue.c_str());
4019 6 : if (psDir)
4020 : {
4021 456 : while (const VSIDIREntry *psEntry = VSIGetNextDirEntry(psDir))
4022 : {
4023 451 : if ((currentFilename.empty() ||
4024 225 : STARTS_WITH(psEntry->pszName,
4025 227 : currentFilename.c_str())) &&
4026 227 : strcmp(psEntry->pszName, ".") != 0 &&
4027 1355 : strcmp(psEntry->pszName, "..") != 0 &&
4028 227 : (oExtensions.empty() ||
4029 226 : !strstr(psEntry->pszName, ".aux.xml")))
4030 : {
4031 898 : if (oExtensions.empty() ||
4032 224 : cpl::contains(
4033 : oExtensions,
4034 449 : CPLString(CPLGetExtensionSafe(psEntry->pszName))
4035 673 : .tolower()) ||
4036 192 : VSI_ISDIR(psEntry->nMode))
4037 : {
4038 74 : std::string osVal;
4039 37 : if (osDir.empty() || osDir == ".")
4040 4 : osVal = psEntry->pszName;
4041 : else
4042 66 : osVal = CPLFormFilenameSafe(
4043 66 : osDir.c_str(), psEntry->pszName, nullptr);
4044 37 : if (VSI_ISDIR(psEntry->nMode))
4045 4 : osVal += osSep;
4046 37 : oRet.push_back(std::move(osVal));
4047 : }
4048 : }
4049 451 : }
4050 5 : VSICloseDir(psDir);
4051 : }
4052 6 : return oRet;
4053 18445 : });
4054 18445 : }
4055 :
4056 : /************************************************************************/
4057 : /* GDALAlgorithm::AddInputDatasetArg() */
4058 : /************************************************************************/
4059 :
4060 887 : GDALInConstructionAlgorithmArg &GDALAlgorithm::AddInputDatasetArg(
4061 : GDALArgDatasetValue *pValue, GDALArgDatasetType type,
4062 : bool positionalAndRequired, const char *helpMessage)
4063 : {
4064 : auto &arg = AddArg(
4065 : GDAL_ARG_NAME_INPUT, 'i',
4066 : MsgOrDefault(helpMessage,
4067 : CPLSPrintf("Input %s dataset",
4068 887 : GDALAlgorithmArgDatasetTypeName(type).c_str())),
4069 1774 : pValue, type);
4070 887 : if (positionalAndRequired)
4071 880 : arg.SetPositional().SetRequired();
4072 :
4073 887 : SetAutoCompleteFunctionForFilename(arg, type);
4074 :
4075 887 : AddValidationAction(
4076 80 : [pValue]()
4077 : {
4078 79 : if (pValue->GetName() == "-")
4079 1 : pValue->Set("/vsistdin/");
4080 79 : return true;
4081 : });
4082 :
4083 887 : return arg;
4084 : }
4085 :
4086 : /************************************************************************/
4087 : /* GDALAlgorithm::AddInputDatasetArg() */
4088 : /************************************************************************/
4089 :
4090 14616 : GDALInConstructionAlgorithmArg &GDALAlgorithm::AddInputDatasetArg(
4091 : std::vector<GDALArgDatasetValue> *pValue, GDALArgDatasetType type,
4092 : bool positionalAndRequired, const char *helpMessage)
4093 : {
4094 : auto &arg =
4095 : AddArg(GDAL_ARG_NAME_INPUT, 'i',
4096 : MsgOrDefault(
4097 : helpMessage,
4098 : CPLSPrintf("Input %s datasets",
4099 14616 : GDALAlgorithmArgDatasetTypeName(type).c_str())),
4100 43848 : pValue, type)
4101 14616 : .SetPackedValuesAllowed(false);
4102 14616 : if (positionalAndRequired)
4103 1668 : arg.SetPositional().SetRequired();
4104 :
4105 14616 : SetAutoCompleteFunctionForFilename(arg, type);
4106 :
4107 14616 : AddValidationAction(
4108 7104 : [pValue]()
4109 : {
4110 13363 : for (auto &val : *pValue)
4111 : {
4112 6259 : if (val.GetName() == "-")
4113 1 : val.Set("/vsistdin/");
4114 : }
4115 7104 : return true;
4116 : });
4117 14616 : return arg;
4118 : }
4119 :
4120 : /************************************************************************/
4121 : /* GDALAlgorithm::AddOutputDatasetArg() */
4122 : /************************************************************************/
4123 :
4124 8795 : GDALInConstructionAlgorithmArg &GDALAlgorithm::AddOutputDatasetArg(
4125 : GDALArgDatasetValue *pValue, GDALArgDatasetType type,
4126 : bool positionalAndRequired, const char *helpMessage)
4127 : {
4128 : auto &arg =
4129 : AddArg(GDAL_ARG_NAME_OUTPUT, 'o',
4130 : MsgOrDefault(
4131 : helpMessage,
4132 : CPLSPrintf("Output %s dataset",
4133 8795 : GDALAlgorithmArgDatasetTypeName(type).c_str())),
4134 26385 : pValue, type)
4135 8795 : .SetIsInput(true)
4136 8795 : .SetIsOutput(true)
4137 8795 : .SetDatasetInputFlags(GADV_NAME)
4138 8795 : .SetDatasetOutputFlags(GADV_OBJECT);
4139 8795 : if (positionalAndRequired)
4140 4375 : arg.SetPositional().SetRequired();
4141 :
4142 8795 : AddValidationAction(
4143 13248 : [this, &arg, pValue]()
4144 : {
4145 4014 : if (pValue->GetName() == "-")
4146 4 : pValue->Set("/vsistdout/");
4147 :
4148 4014 : auto outputFormatArg = GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT);
4149 3962 : if (outputFormatArg && outputFormatArg->GetType() == GAAT_STRING &&
4150 6262 : (!outputFormatArg->IsExplicitlySet() ||
4151 10276 : outputFormatArg->Get<std::string>().empty()) &&
4152 1662 : arg.IsExplicitlySet())
4153 : {
4154 : const auto vrtCompatible =
4155 1172 : outputFormatArg->GetMetadataItem(GAAMDI_VRT_COMPATIBLE);
4156 192 : if (vrtCompatible && !vrtCompatible->empty() &&
4157 1364 : vrtCompatible->front() == "false" &&
4158 1268 : EQUAL(
4159 : CPLGetExtensionSafe(pValue->GetName().c_str()).c_str(),
4160 : "VRT"))
4161 : {
4162 6 : ReportError(
4163 : CE_Failure, CPLE_NotSupported,
4164 : "VRT output is not supported.%s",
4165 6 : outputFormatArg->GetDescription().find("GDALG") !=
4166 : std::string::npos
4167 : ? " Consider using the GDALG driver instead (files "
4168 : "with .gdalg.json extension)"
4169 : : "");
4170 6 : return false;
4171 : }
4172 1166 : else if (pValue->GetName().size() > strlen(".gdalg.json") &&
4173 2309 : EQUAL(pValue->GetName()
4174 : .substr(pValue->GetName().size() -
4175 : strlen(".gdalg.json"))
4176 : .c_str(),
4177 3475 : ".gdalg.json") &&
4178 28 : outputFormatArg->GetDescription().find("GDALG") ==
4179 : std::string::npos)
4180 : {
4181 0 : ReportError(CE_Failure, CPLE_NotSupported,
4182 : "GDALG output is not supported");
4183 0 : return false;
4184 : }
4185 : }
4186 4008 : return true;
4187 : });
4188 :
4189 8795 : return arg;
4190 : }
4191 :
4192 : /************************************************************************/
4193 : /* GDALAlgorithm::AddOverwriteArg() */
4194 : /************************************************************************/
4195 :
4196 : GDALInConstructionAlgorithmArg &
4197 8661 : GDALAlgorithm::AddOverwriteArg(bool *pValue, const char *helpMessage)
4198 : {
4199 : return AddArg(
4200 : GDAL_ARG_NAME_OVERWRITE, 0,
4201 : MsgOrDefault(
4202 : helpMessage,
4203 : _("Whether overwriting existing output dataset is allowed")),
4204 17322 : pValue)
4205 17322 : .SetDefault(false);
4206 : }
4207 :
4208 : /************************************************************************/
4209 : /* GDALAlgorithm::AddOverwriteLayerArg() */
4210 : /************************************************************************/
4211 :
4212 : GDALInConstructionAlgorithmArg &
4213 3612 : GDALAlgorithm::AddOverwriteLayerArg(bool *pValue, const char *helpMessage)
4214 : {
4215 3612 : AddValidationAction(
4216 1691 : [this]
4217 : {
4218 1690 : auto updateArg = GetArg(GDAL_ARG_NAME_UPDATE);
4219 1690 : if (!(updateArg && updateArg->GetType() == GAAT_BOOLEAN))
4220 : {
4221 1 : ReportError(CE_Failure, CPLE_AppDefined,
4222 : "--update argument must exist for "
4223 : "--overwrite-layer, even if hidden");
4224 1 : return false;
4225 : }
4226 1689 : return true;
4227 : });
4228 : return AddArg(
4229 : GDAL_ARG_NAME_OVERWRITE_LAYER, 0,
4230 : MsgOrDefault(
4231 : helpMessage,
4232 : _("Whether overwriting existing output layer is allowed")),
4233 7224 : pValue)
4234 3612 : .SetDefault(false)
4235 : .AddAction(
4236 19 : [this]
4237 : {
4238 19 : auto updateArg = GetArg(GDAL_ARG_NAME_UPDATE);
4239 19 : if (updateArg && updateArg->GetType() == GAAT_BOOLEAN)
4240 : {
4241 19 : updateArg->Set(true);
4242 : }
4243 7243 : });
4244 : }
4245 :
4246 : /************************************************************************/
4247 : /* GDALAlgorithm::AddUpdateArg() */
4248 : /************************************************************************/
4249 :
4250 : GDALInConstructionAlgorithmArg &
4251 4175 : GDALAlgorithm::AddUpdateArg(bool *pValue, const char *helpMessage)
4252 : {
4253 : return AddArg(GDAL_ARG_NAME_UPDATE, 0,
4254 : MsgOrDefault(
4255 : helpMessage,
4256 : _("Whether to open existing dataset in update mode")),
4257 8350 : pValue)
4258 8350 : .SetDefault(false);
4259 : }
4260 :
4261 : /************************************************************************/
4262 : /* GDALAlgorithm::AddAppendLayerArg() */
4263 : /************************************************************************/
4264 :
4265 : GDALInConstructionAlgorithmArg &
4266 3387 : GDALAlgorithm::AddAppendLayerArg(bool *pValue, const char *helpMessage)
4267 : {
4268 3387 : AddValidationAction(
4269 1646 : [this]
4270 : {
4271 1645 : auto updateArg = GetArg(GDAL_ARG_NAME_UPDATE);
4272 1645 : if (!(updateArg && updateArg->GetType() == GAAT_BOOLEAN))
4273 : {
4274 1 : ReportError(CE_Failure, CPLE_AppDefined,
4275 : "--update argument must exist for --append, even "
4276 : "if hidden");
4277 1 : return false;
4278 : }
4279 1644 : return true;
4280 : });
4281 : return AddArg(GDAL_ARG_NAME_APPEND, 0,
4282 : MsgOrDefault(
4283 : helpMessage,
4284 : _("Whether appending to existing layer is allowed")),
4285 6774 : pValue)
4286 3387 : .SetDefault(false)
4287 : .AddAction(
4288 25 : [this]
4289 : {
4290 25 : auto updateArg = GetArg(GDAL_ARG_NAME_UPDATE);
4291 25 : if (updateArg && updateArg->GetType() == GAAT_BOOLEAN)
4292 : {
4293 25 : updateArg->Set(true);
4294 : }
4295 6799 : });
4296 : }
4297 :
4298 : /************************************************************************/
4299 : /* GDALAlgorithm::AddOptionsSuggestions() */
4300 : /************************************************************************/
4301 :
4302 : /* static */
4303 30 : bool GDALAlgorithm::AddOptionsSuggestions(const char *pszXML, int datasetType,
4304 : const std::string ¤tValue,
4305 : std::vector<std::string> &oRet)
4306 : {
4307 30 : if (!pszXML)
4308 0 : return false;
4309 60 : CPLXMLTreeCloser poTree(CPLParseXMLString(pszXML));
4310 30 : if (!poTree)
4311 0 : return false;
4312 :
4313 60 : std::string typedOptionName = currentValue;
4314 30 : const auto posEqual = typedOptionName.find('=');
4315 60 : std::string typedValue;
4316 30 : if (posEqual != 0 && posEqual != std::string::npos)
4317 : {
4318 2 : typedValue = currentValue.substr(posEqual + 1);
4319 2 : typedOptionName.resize(posEqual);
4320 : }
4321 :
4322 453 : for (const CPLXMLNode *psChild = poTree.get()->psChild; psChild;
4323 423 : psChild = psChild->psNext)
4324 : {
4325 436 : const char *pszName = CPLGetXMLValue(psChild, "name", nullptr);
4326 449 : if (pszName && typedOptionName == pszName &&
4327 13 : (strcmp(psChild->pszValue, "Option") == 0 ||
4328 2 : strcmp(psChild->pszValue, "Argument") == 0))
4329 : {
4330 13 : const char *pszType = CPLGetXMLValue(psChild, "type", "");
4331 13 : const char *pszMin = CPLGetXMLValue(psChild, "min", nullptr);
4332 13 : const char *pszMax = CPLGetXMLValue(psChild, "max", nullptr);
4333 13 : if (EQUAL(pszType, "string-select"))
4334 : {
4335 90 : for (const CPLXMLNode *psChild2 = psChild->psChild; psChild2;
4336 85 : psChild2 = psChild2->psNext)
4337 : {
4338 85 : if (EQUAL(psChild2->pszValue, "Value"))
4339 : {
4340 75 : oRet.push_back(CPLGetXMLValue(psChild2, "", ""));
4341 : }
4342 : }
4343 : }
4344 8 : else if (EQUAL(pszType, "boolean"))
4345 : {
4346 3 : if (typedValue == "YES" || typedValue == "NO")
4347 : {
4348 1 : oRet.push_back(currentValue);
4349 1 : return true;
4350 : }
4351 2 : oRet.push_back("NO");
4352 2 : oRet.push_back("YES");
4353 : }
4354 5 : else if (EQUAL(pszType, "int"))
4355 : {
4356 5 : if (pszMin && pszMax && atoi(pszMax) - atoi(pszMin) > 0 &&
4357 2 : atoi(pszMax) - atoi(pszMin) < 25)
4358 : {
4359 1 : const int nMax = atoi(pszMax);
4360 13 : for (int i = atoi(pszMin); i <= nMax; ++i)
4361 12 : oRet.push_back(std::to_string(i));
4362 : }
4363 : }
4364 :
4365 12 : if (oRet.empty())
4366 : {
4367 4 : if (pszMin && pszMax)
4368 : {
4369 1 : oRet.push_back(std::string("##"));
4370 2 : oRet.push_back(std::string("validity range: [")
4371 1 : .append(pszMin)
4372 1 : .append(",")
4373 1 : .append(pszMax)
4374 1 : .append("]"));
4375 : }
4376 3 : else if (pszMin)
4377 : {
4378 1 : oRet.push_back(std::string("##"));
4379 1 : oRet.push_back(
4380 1 : std::string("validity range: >= ").append(pszMin));
4381 : }
4382 2 : else if (pszMax)
4383 : {
4384 1 : oRet.push_back(std::string("##"));
4385 1 : oRet.push_back(
4386 1 : std::string("validity range: <= ").append(pszMax));
4387 : }
4388 1 : else if (const char *pszDescription =
4389 1 : CPLGetXMLValue(psChild, "description", nullptr))
4390 : {
4391 1 : oRet.push_back(std::string("##"));
4392 2 : oRet.push_back(std::string("type: ")
4393 1 : .append(pszType)
4394 1 : .append(", description: ")
4395 1 : .append(pszDescription));
4396 : }
4397 : }
4398 :
4399 12 : return true;
4400 : }
4401 : }
4402 :
4403 367 : for (const CPLXMLNode *psChild = poTree.get()->psChild; psChild;
4404 350 : psChild = psChild->psNext)
4405 : {
4406 350 : const char *pszName = CPLGetXMLValue(psChild, "name", nullptr);
4407 350 : if (pszName && (strcmp(psChild->pszValue, "Option") == 0 ||
4408 5 : strcmp(psChild->pszValue, "Argument") == 0))
4409 : {
4410 347 : const char *pszScope = CPLGetXMLValue(psChild, "scope", nullptr);
4411 347 : if (!pszScope ||
4412 40 : (EQUAL(pszScope, "raster") &&
4413 40 : (datasetType & GDAL_OF_RASTER) != 0) ||
4414 20 : (EQUAL(pszScope, "vector") &&
4415 0 : (datasetType & GDAL_OF_VECTOR) != 0))
4416 : {
4417 327 : oRet.push_back(std::string(pszName).append("="));
4418 : }
4419 : }
4420 : }
4421 :
4422 17 : return false;
4423 : }
4424 :
4425 : /************************************************************************/
4426 : /* GDALAlgorithm::OpenOptionCompleteFunction() */
4427 : /************************************************************************/
4428 :
4429 : //! @cond Doxygen_Suppress
4430 : std::vector<std::string>
4431 2 : GDALAlgorithm::OpenOptionCompleteFunction(const std::string ¤tValue) const
4432 : {
4433 2 : std::vector<std::string> oRet;
4434 :
4435 2 : int datasetType = GDAL_OF_RASTER | GDAL_OF_VECTOR | GDAL_OF_MULTIDIM_RASTER;
4436 2 : auto inputArg = GetArg(GDAL_ARG_NAME_INPUT);
4437 4 : if (inputArg && (inputArg->GetType() == GAAT_DATASET ||
4438 2 : inputArg->GetType() == GAAT_DATASET_LIST))
4439 : {
4440 2 : datasetType = inputArg->GetDatasetType();
4441 : }
4442 :
4443 2 : auto inputFormat = GetArg(GDAL_ARG_NAME_INPUT_FORMAT);
4444 4 : if (inputFormat && inputFormat->GetType() == GAAT_STRING_LIST &&
4445 2 : inputFormat->IsExplicitlySet())
4446 : {
4447 : const auto &aosAllowedDrivers =
4448 1 : inputFormat->Get<std::vector<std::string>>();
4449 1 : if (aosAllowedDrivers.size() == 1)
4450 : {
4451 2 : auto poDriver = GetGDALDriverManager()->GetDriverByName(
4452 1 : aosAllowedDrivers[0].c_str());
4453 1 : if (poDriver)
4454 : {
4455 1 : AddOptionsSuggestions(
4456 1 : poDriver->GetMetadataItem(GDAL_DMD_OPENOPTIONLIST),
4457 : datasetType, currentValue, oRet);
4458 : }
4459 1 : return oRet;
4460 : }
4461 : }
4462 :
4463 1 : const auto AddSuggestions = [datasetType, ¤tValue,
4464 375 : &oRet](const GDALArgDatasetValue &datasetValue)
4465 : {
4466 1 : auto poDM = GetGDALDriverManager();
4467 :
4468 1 : const auto &osDSName = datasetValue.GetName();
4469 1 : const std::string osExt = CPLGetExtensionSafe(osDSName.c_str());
4470 1 : if (!osExt.empty())
4471 : {
4472 1 : std::set<std::string> oVisitedExtensions;
4473 231 : for (int i = 0; i < poDM->GetDriverCount(); ++i)
4474 : {
4475 230 : auto poDriver = poDM->GetDriver(i);
4476 690 : if (((datasetType & GDAL_OF_RASTER) != 0 &&
4477 230 : poDriver->GetMetadataItem(GDAL_DCAP_RASTER)) ||
4478 72 : ((datasetType & GDAL_OF_VECTOR) != 0 &&
4479 460 : poDriver->GetMetadataItem(GDAL_DCAP_VECTOR)) ||
4480 72 : ((datasetType & GDAL_OF_MULTIDIM_RASTER) != 0 &&
4481 0 : poDriver->GetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER)))
4482 : {
4483 : const char *pszExtensions =
4484 158 : poDriver->GetMetadataItem(GDAL_DMD_EXTENSIONS);
4485 158 : if (pszExtensions)
4486 : {
4487 : const CPLStringList aosExts(
4488 104 : CSLTokenizeString2(pszExtensions, " ", 0));
4489 229 : for (const char *pszExt : cpl::Iterate(aosExts))
4490 : {
4491 129 : if (EQUAL(pszExt, osExt.c_str()) &&
4492 3 : !cpl::contains(oVisitedExtensions, pszExt))
4493 : {
4494 1 : oVisitedExtensions.insert(pszExt);
4495 1 : if (AddOptionsSuggestions(
4496 : poDriver->GetMetadataItem(
4497 1 : GDAL_DMD_OPENOPTIONLIST),
4498 : datasetType, currentValue, oRet))
4499 : {
4500 0 : return;
4501 : }
4502 1 : break;
4503 : }
4504 : }
4505 : }
4506 : }
4507 : }
4508 : }
4509 1 : };
4510 :
4511 1 : if (inputArg && inputArg->GetType() == GAAT_DATASET)
4512 : {
4513 0 : auto &datasetValue = inputArg->Get<GDALArgDatasetValue>();
4514 0 : AddSuggestions(datasetValue);
4515 : }
4516 1 : else if (inputArg && inputArg->GetType() == GAAT_DATASET_LIST)
4517 : {
4518 1 : auto &datasetValues = inputArg->Get<std::vector<GDALArgDatasetValue>>();
4519 1 : if (datasetValues.size() == 1)
4520 1 : AddSuggestions(datasetValues[0]);
4521 : }
4522 :
4523 1 : return oRet;
4524 : }
4525 :
4526 : //! @endcond
4527 :
4528 : /************************************************************************/
4529 : /* GDALAlgorithm::AddOpenOptionsArg() */
4530 : /************************************************************************/
4531 :
4532 : GDALInConstructionAlgorithmArg &
4533 9671 : GDALAlgorithm::AddOpenOptionsArg(std::vector<std::string> *pValue,
4534 : const char *helpMessage)
4535 : {
4536 : auto &arg = AddArg(GDAL_ARG_NAME_OPEN_OPTION, 0,
4537 19342 : MsgOrDefault(helpMessage, _("Open options")), pValue)
4538 19342 : .AddAlias("oo")
4539 19342 : .SetMetaVar("<KEY>=<VALUE>")
4540 9671 : .SetPackedValuesAllowed(false)
4541 9671 : .SetCategory(GAAC_ADVANCED);
4542 :
4543 31 : arg.AddValidationAction([this, &arg]()
4544 9702 : { return ParseAndValidateKeyValue(arg); });
4545 :
4546 : arg.SetAutoCompleteFunction(
4547 2 : [this](const std::string ¤tValue)
4548 9673 : { return OpenOptionCompleteFunction(currentValue); });
4549 :
4550 9671 : return arg;
4551 : }
4552 :
4553 : /************************************************************************/
4554 : /* GDALAlgorithm::AddOutputOpenOptionsArg() */
4555 : /************************************************************************/
4556 :
4557 : GDALInConstructionAlgorithmArg &
4558 3463 : GDALAlgorithm::AddOutputOpenOptionsArg(std::vector<std::string> *pValue,
4559 : const char *helpMessage)
4560 : {
4561 : auto &arg =
4562 : AddArg(GDAL_ARG_NAME_OUTPUT_OPEN_OPTION, 0,
4563 6926 : MsgOrDefault(helpMessage, _("Output open options")), pValue)
4564 6926 : .AddAlias("output-oo")
4565 6926 : .SetMetaVar("<KEY>=<VALUE>")
4566 3463 : .SetPackedValuesAllowed(false)
4567 3463 : .SetCategory(GAAC_ADVANCED);
4568 :
4569 0 : arg.AddValidationAction([this, &arg]()
4570 3463 : { return ParseAndValidateKeyValue(arg); });
4571 :
4572 : arg.SetAutoCompleteFunction(
4573 0 : [this](const std::string ¤tValue)
4574 3463 : { return OpenOptionCompleteFunction(currentValue); });
4575 :
4576 3463 : return arg;
4577 : }
4578 :
4579 : /************************************************************************/
4580 : /* ValidateFormat() */
4581 : /************************************************************************/
4582 :
4583 4903 : bool GDALAlgorithm::ValidateFormat(const GDALAlgorithmArg &arg,
4584 : bool bStreamAllowed,
4585 : bool bGDALGAllowed) const
4586 : {
4587 4903 : if (arg.GetChoices().empty())
4588 : {
4589 : const auto Validate =
4590 21179 : [this, &arg, bStreamAllowed, bGDALGAllowed](const std::string &val)
4591 : {
4592 4782 : if (const auto extraFormats =
4593 4782 : arg.GetMetadataItem(GAAMDI_EXTRA_FORMATS))
4594 : {
4595 60 : for (const auto &extraFormat : *extraFormats)
4596 : {
4597 48 : if (EQUAL(val.c_str(), extraFormat.c_str()))
4598 14 : return true;
4599 : }
4600 : }
4601 :
4602 4768 : if (bStreamAllowed && EQUAL(val.c_str(), "stream"))
4603 1853 : return true;
4604 :
4605 2923 : if (EQUAL(val.c_str(), "GDALG") &&
4606 8 : arg.GetName() == GDAL_ARG_NAME_OUTPUT_FORMAT)
4607 : {
4608 4 : if (bGDALGAllowed)
4609 : {
4610 4 : return true;
4611 : }
4612 : else
4613 : {
4614 0 : ReportError(CE_Failure, CPLE_NotSupported,
4615 : "GDALG output is not supported.");
4616 0 : return false;
4617 : }
4618 : }
4619 :
4620 : const auto vrtCompatible =
4621 2911 : arg.GetMetadataItem(GAAMDI_VRT_COMPATIBLE);
4622 540 : if (vrtCompatible && !vrtCompatible->empty() &&
4623 3451 : vrtCompatible->front() == "false" && EQUAL(val.c_str(), "VRT"))
4624 : {
4625 7 : ReportError(CE_Failure, CPLE_NotSupported,
4626 : "VRT output is not supported.%s",
4627 : bGDALGAllowed
4628 : ? " Consider using the GDALG driver instead "
4629 : "(files with .gdalg.json extension)."
4630 : : "");
4631 7 : return false;
4632 : }
4633 :
4634 : const auto allowedFormats =
4635 2904 : arg.GetMetadataItem(GAAMDI_ALLOWED_FORMATS);
4636 2957 : if (allowedFormats && !allowedFormats->empty() &&
4637 0 : std::find(allowedFormats->begin(), allowedFormats->end(),
4638 2957 : val) != allowedFormats->end())
4639 : {
4640 12 : return true;
4641 : }
4642 :
4643 : const auto excludedFormats =
4644 2892 : arg.GetMetadataItem(GAAMDI_EXCLUDED_FORMATS);
4645 2939 : if (excludedFormats && !excludedFormats->empty() &&
4646 0 : std::find(excludedFormats->begin(), excludedFormats->end(),
4647 2939 : val) != excludedFormats->end())
4648 : {
4649 0 : ReportError(CE_Failure, CPLE_NotSupported,
4650 : "%s output is not supported.", val.c_str());
4651 0 : return false;
4652 : }
4653 :
4654 2892 : auto hDriver = GDALGetDriverByName(val.c_str());
4655 2892 : if (!hDriver)
4656 : {
4657 : auto poMissingDriver =
4658 4 : GetGDALDriverManager()->GetHiddenDriverByName(val.c_str());
4659 4 : if (poMissingDriver)
4660 : {
4661 : const std::string msg =
4662 0 : GDALGetMessageAboutMissingPluginDriver(poMissingDriver);
4663 0 : ReportError(CE_Failure, CPLE_AppDefined,
4664 : "Invalid value for argument '%s'. Driver '%s' "
4665 : "not found but is known. However plugin %s",
4666 0 : arg.GetName().c_str(), val.c_str(),
4667 : msg.c_str());
4668 : }
4669 : else
4670 : {
4671 8 : ReportError(CE_Failure, CPLE_AppDefined,
4672 : "Invalid value for argument '%s'. Driver '%s' "
4673 : "does not exist.",
4674 4 : arg.GetName().c_str(), val.c_str());
4675 : }
4676 4 : return false;
4677 : }
4678 :
4679 2888 : const auto caps = arg.GetMetadataItem(GAAMDI_REQUIRED_CAPABILITIES);
4680 2888 : if (caps)
4681 : {
4682 8758 : for (const std::string &cap : *caps)
4683 : {
4684 : const char *pszVal =
4685 5901 : GDALGetMetadataItem(hDriver, cap.c_str(), nullptr);
4686 5901 : if (!(pszVal && pszVal[0]))
4687 : {
4688 1587 : if (cap == GDAL_DCAP_CREATECOPY &&
4689 0 : std::find(caps->begin(), caps->end(),
4690 792 : GDAL_DCAP_RASTER) != caps->end() &&
4691 792 : GDALGetMetadataItem(hDriver, GDAL_DCAP_RASTER,
4692 1587 : nullptr) &&
4693 792 : GDALGetMetadataItem(hDriver, GDAL_DCAP_CREATE,
4694 : nullptr))
4695 : {
4696 : // if it supports Create, it supports CreateCopy
4697 : }
4698 3 : else if (cap == GDAL_DMD_EXTENSIONS)
4699 : {
4700 2 : ReportError(
4701 : CE_Failure, CPLE_AppDefined,
4702 : "Invalid value for argument '%s'. Driver '%s' "
4703 : "does "
4704 : "not advertise any file format extension.",
4705 1 : arg.GetName().c_str(), val.c_str());
4706 3 : return false;
4707 : }
4708 : else
4709 : {
4710 2 : if (cap == GDAL_DCAP_CREATE)
4711 : {
4712 1 : auto updateArg = GetArg(GDAL_ARG_NAME_UPDATE);
4713 1 : if (updateArg &&
4714 2 : updateArg->GetType() == GAAT_BOOLEAN &&
4715 1 : updateArg->IsExplicitlySet())
4716 : {
4717 0 : continue;
4718 : }
4719 :
4720 2 : ReportError(
4721 : CE_Failure, CPLE_AppDefined,
4722 : "Invalid value for argument '%s'. "
4723 : "Driver '%s' does not have write support.",
4724 1 : arg.GetName().c_str(), val.c_str());
4725 1 : return false;
4726 : }
4727 : else
4728 : {
4729 2 : ReportError(
4730 : CE_Failure, CPLE_AppDefined,
4731 : "Invalid value for argument '%s'. Driver "
4732 : "'%s' "
4733 : "does "
4734 : "not expose the required '%s' capability.",
4735 1 : arg.GetName().c_str(), val.c_str(),
4736 : cap.c_str());
4737 1 : return false;
4738 : }
4739 : }
4740 : }
4741 : }
4742 : }
4743 2885 : return true;
4744 4785 : };
4745 :
4746 4785 : if (arg.GetType() == GAAT_STRING)
4747 : {
4748 4772 : return Validate(arg.Get<std::string>());
4749 : }
4750 15 : else if (arg.GetType() == GAAT_STRING_LIST)
4751 : {
4752 25 : for (const auto &val : arg.Get<std::vector<std::string>>())
4753 : {
4754 12 : if (!Validate(val))
4755 2 : return false;
4756 : }
4757 : }
4758 : }
4759 :
4760 131 : return true;
4761 : }
4762 :
4763 : /************************************************************************/
4764 : /* FormatAutoCompleteFunction() */
4765 : /************************************************************************/
4766 :
4767 : /* static */
4768 7 : std::vector<std::string> GDALAlgorithm::FormatAutoCompleteFunction(
4769 : const GDALAlgorithmArg &arg, bool /* bStreamAllowed */, bool bGDALGAllowed)
4770 : {
4771 7 : std::vector<std::string> res;
4772 7 : auto poDM = GetGDALDriverManager();
4773 7 : const auto vrtCompatible = arg.GetMetadataItem(GAAMDI_VRT_COMPATIBLE);
4774 7 : const auto allowedFormats = arg.GetMetadataItem(GAAMDI_ALLOWED_FORMATS);
4775 7 : const auto excludedFormats = arg.GetMetadataItem(GAAMDI_EXCLUDED_FORMATS);
4776 7 : const auto caps = arg.GetMetadataItem(GAAMDI_REQUIRED_CAPABILITIES);
4777 7 : if (auto extraFormats = arg.GetMetadataItem(GAAMDI_EXTRA_FORMATS))
4778 0 : res = std::move(*extraFormats);
4779 1616 : for (int i = 0; i < poDM->GetDriverCount(); ++i)
4780 : {
4781 1609 : auto poDriver = poDM->GetDriver(i);
4782 :
4783 0 : if (vrtCompatible && !vrtCompatible->empty() &&
4784 1609 : vrtCompatible->front() == "false" &&
4785 0 : EQUAL(poDriver->GetDescription(), "VRT"))
4786 : {
4787 : // do nothing
4788 : }
4789 1609 : else if (allowedFormats && !allowedFormats->empty() &&
4790 0 : std::find(allowedFormats->begin(), allowedFormats->end(),
4791 1609 : poDriver->GetDescription()) != allowedFormats->end())
4792 : {
4793 0 : res.push_back(poDriver->GetDescription());
4794 : }
4795 1609 : else if (excludedFormats && !excludedFormats->empty() &&
4796 0 : std::find(excludedFormats->begin(), excludedFormats->end(),
4797 0 : poDriver->GetDescription()) !=
4798 1609 : excludedFormats->end())
4799 : {
4800 0 : continue;
4801 : }
4802 1609 : else if (caps)
4803 : {
4804 1609 : bool ok = true;
4805 3183 : for (const std::string &cap : *caps)
4806 : {
4807 2398 : if (cap == GDAL_ALG_DCAP_RASTER_OR_MULTIDIM_RASTER)
4808 : {
4809 0 : if (!poDriver->GetMetadataItem(GDAL_DCAP_RASTER) &&
4810 0 : !poDriver->GetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER))
4811 : {
4812 0 : ok = false;
4813 0 : break;
4814 : }
4815 : }
4816 2398 : else if (const char *pszVal =
4817 2398 : poDriver->GetMetadataItem(cap.c_str());
4818 1502 : pszVal && pszVal[0])
4819 : {
4820 : }
4821 1292 : else if (cap == GDAL_DCAP_CREATECOPY &&
4822 0 : (std::find(caps->begin(), caps->end(),
4823 396 : GDAL_DCAP_RASTER) != caps->end() &&
4824 1688 : poDriver->GetMetadataItem(GDAL_DCAP_RASTER)) &&
4825 396 : poDriver->GetMetadataItem(GDAL_DCAP_CREATE))
4826 : {
4827 : // if it supports Create, it supports CreateCopy
4828 : }
4829 : else
4830 : {
4831 824 : ok = false;
4832 824 : break;
4833 : }
4834 : }
4835 1609 : if (ok)
4836 : {
4837 785 : res.push_back(poDriver->GetDescription());
4838 : }
4839 : }
4840 : }
4841 7 : if (bGDALGAllowed)
4842 4 : res.push_back("GDALG");
4843 7 : return res;
4844 : }
4845 :
4846 : /************************************************************************/
4847 : /* GDALAlgorithm::AddInputFormatsArg() */
4848 : /************************************************************************/
4849 :
4850 : GDALInConstructionAlgorithmArg &
4851 9456 : GDALAlgorithm::AddInputFormatsArg(std::vector<std::string> *pValue,
4852 : const char *helpMessage)
4853 : {
4854 : auto &arg = AddArg(GDAL_ARG_NAME_INPUT_FORMAT, 0,
4855 18912 : MsgOrDefault(helpMessage, _("Input formats")), pValue)
4856 18912 : .AddAlias("if")
4857 9456 : .SetCategory(GAAC_ADVANCED);
4858 15 : arg.AddValidationAction([this, &arg]()
4859 9471 : { return ValidateFormat(arg, false, false); });
4860 : arg.SetAutoCompleteFunction(
4861 1 : [&arg](const std::string &)
4862 9457 : { return FormatAutoCompleteFunction(arg, false, false); });
4863 9456 : return arg;
4864 : }
4865 :
4866 : /************************************************************************/
4867 : /* GDALAlgorithm::AddOutputFormatArg() */
4868 : /************************************************************************/
4869 :
4870 : GDALInConstructionAlgorithmArg &
4871 9832 : GDALAlgorithm::AddOutputFormatArg(std::string *pValue, bool bStreamAllowed,
4872 : bool bGDALGAllowed, const char *helpMessage)
4873 : {
4874 : auto &arg = AddArg(GDAL_ARG_NAME_OUTPUT_FORMAT, 'f',
4875 : MsgOrDefault(helpMessage,
4876 : bGDALGAllowed
4877 : ? _("Output format (\"GDALG\" allowed)")
4878 : : _("Output format")),
4879 19664 : pValue)
4880 19664 : .AddAlias("of")
4881 9832 : .AddAlias("format");
4882 : arg.AddValidationAction(
4883 4884 : [this, &arg, bStreamAllowed, bGDALGAllowed]()
4884 14716 : { return ValidateFormat(arg, bStreamAllowed, bGDALGAllowed); });
4885 : arg.SetAutoCompleteFunction(
4886 4 : [&arg, bStreamAllowed, bGDALGAllowed](const std::string &)
4887 : {
4888 : return FormatAutoCompleteFunction(arg, bStreamAllowed,
4889 4 : bGDALGAllowed);
4890 9832 : });
4891 9832 : return arg;
4892 : }
4893 :
4894 : /************************************************************************/
4895 : /* GDALAlgorithm::AddOutputDataTypeArg() */
4896 : /************************************************************************/
4897 : GDALInConstructionAlgorithmArg &
4898 1762 : GDALAlgorithm::AddOutputDataTypeArg(std::string *pValue,
4899 : const char *helpMessage)
4900 : {
4901 : auto &arg =
4902 : AddArg(GDAL_ARG_NAME_OUTPUT_DATA_TYPE, 0,
4903 3524 : MsgOrDefault(helpMessage, _("Output data type")), pValue)
4904 3524 : .AddAlias("ot")
4905 3524 : .AddAlias("datatype")
4906 5286 : .AddMetadataItem("type", {"GDALDataType"})
4907 : .SetChoices("UInt8", "Int8", "UInt16", "Int16", "UInt32", "Int32",
4908 : "UInt64", "Int64", "CInt16", "CInt32", "Float16",
4909 1762 : "Float32", "Float64", "CFloat32", "CFloat64")
4910 1762 : .SetHiddenChoices("Byte");
4911 1762 : return arg;
4912 : }
4913 :
4914 : /************************************************************************/
4915 : /* GDALAlgorithm::AddNodataArg() */
4916 : /************************************************************************/
4917 :
4918 : GDALInConstructionAlgorithmArg &
4919 655 : GDALAlgorithm::AddNodataArg(std::string *pValue, bool noneAllowed,
4920 : const std::string &optionName,
4921 : const char *helpMessage)
4922 : {
4923 : auto &arg = AddArg(
4924 : optionName, 0,
4925 : MsgOrDefault(helpMessage,
4926 : noneAllowed
4927 : ? _("Assign a specified nodata value to output bands "
4928 : "('none', numeric value, 'nan', 'inf', '-inf')")
4929 : : _("Assign a specified nodata value to output bands "
4930 : "(numeric value, 'nan', 'inf', '-inf')")),
4931 655 : pValue);
4932 : arg.AddValidationAction(
4933 336 : [this, pValue, noneAllowed, optionName]()
4934 : {
4935 73 : if (!(noneAllowed && EQUAL(pValue->c_str(), "none")))
4936 : {
4937 63 : char *endptr = nullptr;
4938 63 : CPLStrtod(pValue->c_str(), &endptr);
4939 63 : if (endptr != pValue->c_str() + pValue->size())
4940 : {
4941 1 : ReportError(CE_Failure, CPLE_IllegalArg,
4942 : "Value of '%s' should be %sa "
4943 : "numeric value, 'nan', 'inf' or '-inf'",
4944 : optionName.c_str(),
4945 : noneAllowed ? "'none', " : "");
4946 1 : return false;
4947 : }
4948 : }
4949 72 : return true;
4950 655 : });
4951 655 : return arg;
4952 : }
4953 :
4954 : /************************************************************************/
4955 : /* GDALAlgorithm::AddOutputStringArg() */
4956 : /************************************************************************/
4957 :
4958 : GDALInConstructionAlgorithmArg &
4959 6671 : GDALAlgorithm::AddOutputStringArg(std::string *pValue, const char *helpMessage)
4960 : {
4961 : return AddArg(
4962 : GDAL_ARG_NAME_OUTPUT_STRING, 0,
4963 : MsgOrDefault(helpMessage,
4964 : _("Output string, in which the result is placed")),
4965 13342 : pValue)
4966 6671 : .SetHiddenForCLI()
4967 6671 : .SetIsInput(false)
4968 13342 : .SetIsOutput(true);
4969 : }
4970 :
4971 : /************************************************************************/
4972 : /* GDALAlgorithm::AddStdoutArg() */
4973 : /************************************************************************/
4974 :
4975 : GDALInConstructionAlgorithmArg &
4976 1657 : GDALAlgorithm::AddStdoutArg(bool *pValue, const char *helpMessage)
4977 : {
4978 : return AddArg(GDAL_ARG_NAME_STDOUT, 0,
4979 : MsgOrDefault(helpMessage,
4980 : _("Directly output on stdout. If enabled, "
4981 : "output-string will be empty")),
4982 3314 : pValue)
4983 3314 : .SetHidden();
4984 : }
4985 :
4986 : /************************************************************************/
4987 : /* GDALAlgorithm::AddLayerNameArg() */
4988 : /************************************************************************/
4989 :
4990 : GDALInConstructionAlgorithmArg &
4991 218 : GDALAlgorithm::AddLayerNameArg(std::string *pValue, const char *helpMessage)
4992 : {
4993 : return AddArg(GDAL_ARG_NAME_INPUT_LAYER, 'l',
4994 218 : MsgOrDefault(helpMessage, _("Input layer name")), pValue);
4995 : }
4996 :
4997 : /************************************************************************/
4998 : /* GDALAlgorithm::AddArrayNameArg() */
4999 : /************************************************************************/
5000 :
5001 : GDALInConstructionAlgorithmArg &
5002 58 : GDALAlgorithm::AddArrayNameArg(std::string *pValue, const char *helpMessage)
5003 : {
5004 : return AddArg("array", 0, MsgOrDefault(helpMessage, _("Array name")),
5005 116 : pValue)
5006 2 : .SetAutoCompleteFunction([this](const std::string &)
5007 118 : { return AutoCompleteArrayName(); });
5008 : }
5009 :
5010 : /************************************************************************/
5011 : /* GDALAlgorithm::AddArrayNameArg() */
5012 : /************************************************************************/
5013 :
5014 : GDALInConstructionAlgorithmArg &
5015 133 : GDALAlgorithm::AddArrayNameArg(std::vector<std::string> *pValue,
5016 : const char *helpMessage)
5017 : {
5018 : return AddArg("array", 0, MsgOrDefault(helpMessage, _("Array name(s)")),
5019 266 : pValue)
5020 0 : .SetAutoCompleteFunction([this](const std::string &)
5021 266 : { return AutoCompleteArrayName(); });
5022 : }
5023 :
5024 : /************************************************************************/
5025 : /* GDALAlgorithm::AutoCompleteArrayName() */
5026 : /************************************************************************/
5027 :
5028 2 : std::vector<std::string> GDALAlgorithm::AutoCompleteArrayName() const
5029 : {
5030 2 : std::vector<std::string> ret;
5031 4 : std::string osDSName;
5032 2 : auto inputArg = GetArg(GDAL_ARG_NAME_INPUT);
5033 2 : if (inputArg && inputArg->GetType() == GAAT_DATASET_LIST)
5034 : {
5035 2 : auto &inputDatasets = inputArg->Get<std::vector<GDALArgDatasetValue>>();
5036 2 : if (!inputDatasets.empty())
5037 : {
5038 2 : osDSName = inputDatasets[0].GetName();
5039 : }
5040 : }
5041 0 : else if (inputArg && inputArg->GetType() == GAAT_DATASET)
5042 : {
5043 0 : auto &inputDataset = inputArg->Get<GDALArgDatasetValue>();
5044 0 : osDSName = inputDataset.GetName();
5045 : }
5046 :
5047 2 : if (!osDSName.empty())
5048 : {
5049 4 : CPLStringList aosAllowedDrivers;
5050 2 : const auto ifArg = GetArg(GDAL_ARG_NAME_INPUT_FORMAT);
5051 2 : if (ifArg && ifArg->GetType() == GAAT_STRING_LIST)
5052 : aosAllowedDrivers =
5053 2 : CPLStringList(ifArg->Get<std::vector<std::string>>());
5054 :
5055 4 : CPLStringList aosOpenOptions;
5056 2 : const auto ooArg = GetArg(GDAL_ARG_NAME_OPEN_OPTION);
5057 2 : if (ooArg && ooArg->GetType() == GAAT_STRING_LIST)
5058 : aosOpenOptions =
5059 2 : CPLStringList(ooArg->Get<std::vector<std::string>>());
5060 :
5061 2 : if (auto poDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
5062 : osDSName.c_str(), GDAL_OF_MULTIDIM_RASTER,
5063 4 : aosAllowedDrivers.List(), aosOpenOptions.List(), nullptr)))
5064 : {
5065 2 : if (auto poRG = poDS->GetRootGroup())
5066 : {
5067 1 : ret = poRG->GetMDArrayFullNamesRecursive();
5068 : }
5069 : }
5070 : }
5071 :
5072 4 : return ret;
5073 : }
5074 :
5075 : /************************************************************************/
5076 : /* GDALAlgorithm::AddMemorySizeArg() */
5077 : /************************************************************************/
5078 :
5079 : GDALInConstructionAlgorithmArg &
5080 226 : GDALAlgorithm::AddMemorySizeArg(size_t *pValue, std::string *pStrValue,
5081 : const std::string &optionName,
5082 : const char *helpMessage)
5083 : {
5084 452 : return AddArg(optionName, 0, helpMessage, pStrValue)
5085 226 : .SetDefault(*pStrValue)
5086 : .AddValidationAction(
5087 139 : [this, pValue, pStrValue]()
5088 : {
5089 47 : CPLDebug("GDAL", "StrValue `%s`", pStrValue->c_str());
5090 : GIntBig nBytes;
5091 : bool bUnitSpecified;
5092 47 : if (CPLParseMemorySize(pStrValue->c_str(), &nBytes,
5093 47 : &bUnitSpecified) != CE_None)
5094 : {
5095 2 : return false;
5096 : }
5097 45 : if (!bUnitSpecified)
5098 : {
5099 1 : ReportError(CE_Failure, CPLE_AppDefined,
5100 : "Memory size must have a unit or be a "
5101 : "percentage of usable RAM (2GB, 5%%, etc.)");
5102 1 : return false;
5103 : }
5104 : if constexpr (sizeof(std::uint64_t) > sizeof(size_t))
5105 : {
5106 : // -1 to please CoverityScan
5107 : if (static_cast<std::uint64_t>(nBytes) >
5108 : std::numeric_limits<size_t>::max() - 1U)
5109 : {
5110 : ReportError(CE_Failure, CPLE_AppDefined,
5111 : "Memory size %s is too large.",
5112 : pStrValue->c_str());
5113 : return false;
5114 : }
5115 : }
5116 :
5117 44 : *pValue = static_cast<size_t>(nBytes);
5118 44 : return true;
5119 452 : });
5120 : }
5121 :
5122 : /************************************************************************/
5123 : /* GDALAlgorithm::AddOutputLayerNameArg() */
5124 : /************************************************************************/
5125 :
5126 : GDALInConstructionAlgorithmArg &
5127 398 : GDALAlgorithm::AddOutputLayerNameArg(std::string *pValue,
5128 : const char *helpMessage)
5129 : {
5130 : return AddArg(GDAL_ARG_NAME_OUTPUT_LAYER, 0,
5131 398 : MsgOrDefault(helpMessage, _("Output layer name")), pValue);
5132 : }
5133 :
5134 : /************************************************************************/
5135 : /* GDALAlgorithm::AddLayerNameArg() */
5136 : /************************************************************************/
5137 :
5138 : GDALInConstructionAlgorithmArg &
5139 904 : GDALAlgorithm::AddLayerNameArg(std::vector<std::string> *pValue,
5140 : const char *helpMessage)
5141 : {
5142 : return AddArg(GDAL_ARG_NAME_INPUT_LAYER, 'l',
5143 904 : MsgOrDefault(helpMessage, _("Input layer name")), pValue);
5144 : }
5145 :
5146 : /************************************************************************/
5147 : /* GDALAlgorithm::AddGeometryTypeArg() */
5148 : /************************************************************************/
5149 :
5150 : GDALInConstructionAlgorithmArg &
5151 472 : GDALAlgorithm::AddGeometryTypeArg(std::string *pValue, const char *helpMessage)
5152 : {
5153 : return AddArg("geometry-type", 0,
5154 944 : MsgOrDefault(helpMessage, _("Geometry type")), pValue)
5155 : .SetAutoCompleteFunction(
5156 3 : [](const std::string ¤tValue)
5157 : {
5158 3 : std::vector<std::string> oRet;
5159 51 : for (const char *type :
5160 : {"GEOMETRY", "POINT", "LINESTRING", "POLYGON",
5161 : "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON",
5162 : "GEOMETRYCOLLECTION", "CURVE", "CIRCULARSTRING",
5163 : "COMPOUNDCURVE", "SURFACE", "CURVEPOLYGON", "MULTICURVE",
5164 54 : "MULTISURFACE", "POLYHEDRALSURFACE", "TIN"})
5165 : {
5166 68 : if (currentValue.empty() ||
5167 17 : STARTS_WITH(type, currentValue.c_str()))
5168 : {
5169 35 : oRet.push_back(type);
5170 35 : oRet.push_back(std::string(type).append("Z"));
5171 35 : oRet.push_back(std::string(type).append("M"));
5172 35 : oRet.push_back(std::string(type).append("ZM"));
5173 : }
5174 : }
5175 3 : return oRet;
5176 944 : })
5177 : .AddValidationAction(
5178 121 : [this, pValue]()
5179 : {
5180 110 : if (wkbFlatten(OGRFromOGCGeomType(pValue->c_str())) ==
5181 118 : wkbUnknown &&
5182 8 : !STARTS_WITH_CI(pValue->c_str(), "GEOMETRY"))
5183 : {
5184 3 : ReportError(CE_Failure, CPLE_AppDefined,
5185 : "Invalid geometry type '%s'", pValue->c_str());
5186 3 : return false;
5187 : }
5188 107 : return true;
5189 944 : });
5190 : }
5191 :
5192 : /************************************************************************/
5193 : /* GDALAlgorithm::SetAutoCompleteFunctionForLayerName() */
5194 : /************************************************************************/
5195 :
5196 : /* static */
5197 3142 : void GDALAlgorithm::SetAutoCompleteFunctionForLayerName(
5198 : GDALInConstructionAlgorithmArg &layerArg, GDALAlgorithmArg &datasetArg)
5199 : {
5200 3142 : CPLAssert(datasetArg.GetType() == GAAT_DATASET ||
5201 : datasetArg.GetType() == GAAT_DATASET_LIST);
5202 :
5203 : layerArg.SetAutoCompleteFunction(
5204 18 : [&datasetArg](const std::string ¤tValue)
5205 : {
5206 6 : std::vector<std::string> ret;
5207 12 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
5208 6 : GDALArgDatasetValue *dsVal = nullptr;
5209 6 : if (datasetArg.GetType() == GAAT_DATASET)
5210 : {
5211 0 : dsVal = &(datasetArg.Get<GDALArgDatasetValue>());
5212 : }
5213 : else
5214 : {
5215 6 : auto &val = datasetArg.Get<std::vector<GDALArgDatasetValue>>();
5216 6 : if (val.size() == 1)
5217 : {
5218 6 : dsVal = &val[0];
5219 : }
5220 : }
5221 6 : if (dsVal && !dsVal->GetName().empty())
5222 : {
5223 : auto poDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
5224 12 : dsVal->GetName().c_str(), GDAL_OF_VECTOR));
5225 6 : if (poDS)
5226 : {
5227 12 : for (auto &&poLayer : poDS->GetLayers())
5228 : {
5229 6 : if (currentValue == poLayer->GetDescription())
5230 : {
5231 1 : ret.clear();
5232 1 : ret.push_back(poLayer->GetDescription());
5233 1 : break;
5234 : }
5235 5 : ret.push_back(poLayer->GetDescription());
5236 : }
5237 : }
5238 : }
5239 12 : return ret;
5240 3142 : });
5241 3142 : }
5242 :
5243 : /************************************************************************/
5244 : /* GDALAlgorithm::SetAutoCompleteFunctionForFieldName() */
5245 : /************************************************************************/
5246 :
5247 580 : void GDALAlgorithm::SetAutoCompleteFunctionForFieldName(
5248 : GDALInConstructionAlgorithmArg &fieldArg,
5249 : const GDALAlgorithmArg *layerNameArg, bool attributeFields,
5250 : bool geometryFields, std::vector<GDALArgDatasetValue> &datasetArg,
5251 : const std::vector<std::string> &extraValues,
5252 : std::function<bool(const OGRFieldDefn *)> filterFn)
5253 : {
5254 :
5255 : fieldArg.SetAutoCompleteFunction(
5256 11 : [&datasetArg, layerNameArg, attributeFields, geometryFields,
5257 46 : extraValues, filterFn](const std::string ¤tValue)
5258 : {
5259 22 : std::set<std::string> ret{};
5260 11 : if (!datasetArg.empty())
5261 : {
5262 18 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
5263 :
5264 : const auto getLayerFields =
5265 7 : [&ret, ¤tValue, attributeFields, geometryFields,
5266 87 : &extraValues, &filterFn](const OGRLayer *poLayer)
5267 : {
5268 7 : const auto poDefn = poLayer->GetLayerDefn();
5269 7 : if (attributeFields)
5270 : {
5271 27 : for (const auto poFieldDefn : poDefn->GetFields())
5272 : {
5273 20 : if (filterFn && !filterFn(poFieldDefn))
5274 : {
5275 1 : continue;
5276 : }
5277 :
5278 19 : const char *fieldName = poFieldDefn->GetNameRef();
5279 :
5280 19 : if (currentValue == fieldName)
5281 : {
5282 0 : ret.clear();
5283 0 : ret.insert(fieldName);
5284 0 : break;
5285 : }
5286 19 : ret.insert(fieldName);
5287 : }
5288 : }
5289 7 : if (geometryFields)
5290 : {
5291 2 : for (const auto poFieldDefn : poDefn->GetGeomFields())
5292 : {
5293 1 : const char *fieldName = poFieldDefn->GetNameRef();
5294 1 : if (fieldName[0] == 0)
5295 1 : fieldName = OGR_GEOMETRY_DEFAULT_NON_EMPTY_NAME;
5296 1 : if (currentValue == fieldName)
5297 : {
5298 0 : ret.clear();
5299 0 : ret.insert(fieldName);
5300 0 : break;
5301 : }
5302 1 : ret.insert(fieldName);
5303 : }
5304 : }
5305 8 : for (const auto &value : extraValues)
5306 : {
5307 1 : if (currentValue == value)
5308 : {
5309 0 : ret.clear();
5310 0 : ret.insert(value);
5311 0 : break;
5312 : }
5313 1 : ret.insert(value);
5314 : }
5315 7 : };
5316 :
5317 9 : const GDALArgDatasetValue &dsVal = datasetArg[0];
5318 :
5319 9 : if (!dsVal.GetName().empty())
5320 : {
5321 : auto poDS = std::unique_ptr<GDALDataset>(
5322 9 : GDALDataset::Open(dsVal.GetName().c_str(),
5323 18 : GDAL_OF_VECTOR | GDAL_OF_READONLY));
5324 9 : if (poDS)
5325 : {
5326 18 : std::vector<std::string> layerNames;
5327 9 : if (layerNameArg && layerNameArg->IsExplicitlySet())
5328 : {
5329 4 : if (layerNameArg->GetType() == GAAT_STRING_LIST)
5330 : {
5331 : layerNames =
5332 : layerNameArg
5333 2 : ->Get<std::vector<std::string>>();
5334 : }
5335 2 : else if (layerNameArg->GetType() == GAAT_STRING)
5336 : {
5337 2 : layerNames.push_back(
5338 2 : layerNameArg->Get<std::string>());
5339 : }
5340 : }
5341 9 : if (layerNames.empty())
5342 : {
5343 : // Loop through all layers
5344 10 : for (const auto *poLayer : poDS->GetLayers())
5345 : {
5346 5 : getLayerFields(poLayer);
5347 : }
5348 : }
5349 : else
5350 : {
5351 8 : for (const std::string &layerName : layerNames)
5352 : {
5353 : const auto poLayer =
5354 4 : poDS->GetLayerByName(layerName.c_str());
5355 4 : if (poLayer)
5356 : {
5357 2 : getLayerFields(poLayer);
5358 : }
5359 : }
5360 : }
5361 : }
5362 : }
5363 : }
5364 11 : std::vector<std::string> retVector(ret.begin(), ret.end());
5365 22 : return retVector;
5366 580 : });
5367 580 : }
5368 :
5369 : /************************************************************************/
5370 : /* GDALAlgorithm::AddFieldNameArg() */
5371 : /************************************************************************/
5372 :
5373 : GDALInConstructionAlgorithmArg &
5374 137 : GDALAlgorithm::AddFieldNameArg(std::string *pValue, const char *helpMessage)
5375 : {
5376 : return AddArg("field-name", 0, MsgOrDefault(helpMessage, _("Field name")),
5377 137 : pValue);
5378 : }
5379 :
5380 : /************************************************************************/
5381 : /* GDALAlgorithm::ParseFieldDefinition() */
5382 : /************************************************************************/
5383 67 : bool GDALAlgorithm::ParseFieldDefinition(const std::string &posStrDef,
5384 : OGRFieldDefn *poFieldDefn,
5385 : std::string *posError)
5386 : {
5387 : static const std::regex re(
5388 67 : R"(^([^:]+):([^(\s]+)(?:\((\d+)(?:,(\d+))?\))?$)");
5389 134 : std::smatch match;
5390 67 : if (std::regex_match(posStrDef, match, re))
5391 : {
5392 132 : const std::string name = match[1];
5393 132 : const std::string type = match[2];
5394 66 : const int width = match[3].matched ? std::stoi(match[3]) : 0;
5395 66 : const int precision = match[4].matched ? std::stoi(match[4]) : 0;
5396 66 : poFieldDefn->SetName(name.c_str());
5397 :
5398 66 : const auto typeEnum{OGRFieldDefn::GetFieldTypeByName(type.c_str())};
5399 66 : if (typeEnum == OFTString && !EQUAL(type.c_str(), "String"))
5400 : {
5401 1 : if (posError)
5402 1 : *posError = "Unsupported field type: " + type;
5403 :
5404 1 : return false;
5405 : }
5406 65 : poFieldDefn->SetType(typeEnum);
5407 65 : poFieldDefn->SetWidth(width);
5408 65 : poFieldDefn->SetPrecision(precision);
5409 65 : return true;
5410 : }
5411 :
5412 1 : if (posError)
5413 : *posError = "Invalid field definition format. Expected "
5414 1 : "<NAME>:<TYPE>[(<WIDTH>[,<PRECISION>])]";
5415 :
5416 1 : return false;
5417 : }
5418 :
5419 : /************************************************************************/
5420 : /* GDALAlgorithm::AddFieldDefinitionArg() */
5421 : /************************************************************************/
5422 :
5423 : GDALInConstructionAlgorithmArg &
5424 131 : GDALAlgorithm::AddFieldDefinitionArg(std::vector<std::string> *pValues,
5425 : std::vector<OGRFieldDefn> *pFieldDefns,
5426 : const char *helpMessage)
5427 : {
5428 : auto &arg =
5429 : AddArg("field", 0, MsgOrDefault(helpMessage, _("Field definition")),
5430 262 : pValues)
5431 262 : .SetMetaVar("<NAME>:<TYPE>[(<WIDTH>[,<PRECISION>])]")
5432 131 : .SetPackedValuesAllowed(true)
5433 131 : .SetRepeatedArgAllowed(true);
5434 :
5435 132 : auto validationFunction = [this, pFieldDefns, pValues]()
5436 : {
5437 65 : pFieldDefns->clear();
5438 130 : for (const auto &strValue : *pValues)
5439 : {
5440 67 : OGRFieldDefn fieldDefn("", OFTString);
5441 67 : std::string error;
5442 67 : if (!GDALAlgorithm::ParseFieldDefinition(strValue, &fieldDefn,
5443 : &error))
5444 : {
5445 2 : ReportError(CE_Failure, CPLE_AppDefined, "%s", error.c_str());
5446 2 : return false;
5447 : }
5448 : // Check uniqueness of field names
5449 67 : for (const auto &existingFieldDefn : *pFieldDefns)
5450 : {
5451 2 : if (EQUAL(existingFieldDefn.GetNameRef(),
5452 : fieldDefn.GetNameRef()))
5453 : {
5454 0 : ReportError(CE_Failure, CPLE_AppDefined,
5455 : "Duplicate field name: '%s'",
5456 : fieldDefn.GetNameRef());
5457 0 : return false;
5458 : }
5459 : }
5460 65 : pFieldDefns->push_back(fieldDefn);
5461 : }
5462 63 : return true;
5463 131 : };
5464 :
5465 131 : arg.AddValidationAction(std::move(validationFunction));
5466 :
5467 131 : return arg;
5468 : }
5469 :
5470 : /************************************************************************/
5471 : /* GDALAlgorithm::AddFieldTypeSubtypeArg() */
5472 : /************************************************************************/
5473 :
5474 274 : GDALInConstructionAlgorithmArg &GDALAlgorithm::AddFieldTypeSubtypeArg(
5475 : OGRFieldType *pTypeValue, OGRFieldSubType *pSubtypeValue,
5476 : std::string *pStrValue, const std::string &argName, const char *helpMessage)
5477 : {
5478 : auto &arg =
5479 548 : AddArg(argName.empty() ? std::string("field-type") : argName, 0,
5480 822 : MsgOrDefault(helpMessage, _("Field type or subtype")), pStrValue)
5481 : .SetAutoCompleteFunction(
5482 1 : [](const std::string ¤tValue)
5483 : {
5484 1 : std::vector<std::string> oRet;
5485 6 : for (int i = 1; i <= OGRFieldSubType::OFSTMaxSubType; i++)
5486 : {
5487 : const char *pszSubType =
5488 5 : OGRFieldDefn::GetFieldSubTypeName(
5489 : static_cast<OGRFieldSubType>(i));
5490 5 : if (pszSubType != nullptr)
5491 : {
5492 5 : if (currentValue.empty() ||
5493 0 : STARTS_WITH(pszSubType, currentValue.c_str()))
5494 : {
5495 5 : oRet.push_back(pszSubType);
5496 : }
5497 : }
5498 : }
5499 :
5500 15 : for (int i = 0; i <= OGRFieldType::OFTMaxType; i++)
5501 : {
5502 : // Skip deprecated
5503 14 : if (static_cast<OGRFieldType>(i) ==
5504 13 : OGRFieldType::OFTWideString ||
5505 : static_cast<OGRFieldType>(i) ==
5506 : OGRFieldType::OFTWideStringList)
5507 2 : continue;
5508 12 : const char *pszType = OGRFieldDefn::GetFieldTypeName(
5509 : static_cast<OGRFieldType>(i));
5510 12 : if (pszType != nullptr)
5511 : {
5512 12 : if (currentValue.empty() ||
5513 0 : STARTS_WITH(pszType, currentValue.c_str()))
5514 : {
5515 12 : oRet.push_back(pszType);
5516 : }
5517 : }
5518 : }
5519 1 : return oRet;
5520 274 : });
5521 :
5522 : auto validationFunction =
5523 845 : [this, &arg, pTypeValue, pSubtypeValue, pStrValue]()
5524 : {
5525 120 : bool isValid{true};
5526 120 : *pTypeValue = OGRFieldDefn::GetFieldTypeByName(pStrValue->c_str());
5527 :
5528 : // String is returned for unknown types
5529 120 : if (!EQUAL(pStrValue->c_str(), "String") && *pTypeValue == OFTString)
5530 : {
5531 16 : isValid = false;
5532 : }
5533 :
5534 120 : *pSubtypeValue =
5535 120 : OGRFieldDefn::GetFieldSubTypeByName(pStrValue->c_str());
5536 :
5537 120 : if (*pSubtypeValue != OFSTNone)
5538 : {
5539 15 : isValid = true;
5540 15 : switch (*pSubtypeValue)
5541 : {
5542 6 : case OFSTBoolean:
5543 : case OFSTInt16:
5544 : {
5545 6 : *pTypeValue = OFTInteger;
5546 6 : break;
5547 : }
5548 3 : case OFSTFloat32:
5549 : {
5550 3 : *pTypeValue = OFTReal;
5551 3 : break;
5552 : }
5553 6 : default:
5554 : {
5555 6 : *pTypeValue = OFTString;
5556 6 : break;
5557 : }
5558 : }
5559 : }
5560 :
5561 120 : if (!isValid)
5562 : {
5563 2 : ReportError(CE_Failure, CPLE_AppDefined,
5564 : "Invalid value for argument '%s': '%s'",
5565 1 : arg.GetName().c_str(), pStrValue->c_str());
5566 : }
5567 :
5568 120 : return isValid;
5569 274 : };
5570 :
5571 274 : if (!pStrValue->empty())
5572 : {
5573 0 : arg.SetDefault(*pStrValue);
5574 0 : validationFunction();
5575 : }
5576 :
5577 274 : arg.AddValidationAction(std::move(validationFunction));
5578 :
5579 274 : return arg;
5580 : }
5581 :
5582 : /************************************************************************/
5583 : /* GDALAlgorithm::ValidateBandArg() */
5584 : /************************************************************************/
5585 :
5586 4502 : bool GDALAlgorithm::ValidateBandArg() const
5587 : {
5588 4502 : bool ret = true;
5589 4502 : const auto bandArg = GetArg(GDAL_ARG_NAME_BAND);
5590 4502 : const auto inputDatasetArg = GetArg(GDAL_ARG_NAME_INPUT);
5591 1689 : if (bandArg && bandArg->IsExplicitlySet() && inputDatasetArg &&
5592 292 : (inputDatasetArg->GetType() == GAAT_DATASET ||
5593 6185 : inputDatasetArg->GetType() == GAAT_DATASET_LIST) &&
5594 149 : (inputDatasetArg->GetDatasetType() & GDAL_OF_RASTER) != 0)
5595 : {
5596 104 : const auto CheckBand = [this](const GDALDataset *poDS, int nBand)
5597 : {
5598 99 : if (nBand > poDS->GetRasterCount())
5599 : {
5600 5 : ReportError(CE_Failure, CPLE_AppDefined,
5601 : "Value of 'band' should be greater or equal than "
5602 : "1 and less or equal than %d.",
5603 : poDS->GetRasterCount());
5604 5 : return false;
5605 : }
5606 94 : return true;
5607 92 : };
5608 :
5609 : const auto ValidateForOneDataset =
5610 304 : [&bandArg, &CheckBand](const GDALDataset *poDS)
5611 : {
5612 87 : bool l_ret = true;
5613 87 : if (bandArg->GetType() == GAAT_INTEGER)
5614 : {
5615 24 : l_ret = CheckBand(poDS, bandArg->Get<int>());
5616 : }
5617 63 : else if (bandArg->GetType() == GAAT_INTEGER_LIST)
5618 : {
5619 130 : for (int nBand : bandArg->Get<std::vector<int>>())
5620 : {
5621 75 : l_ret = l_ret && CheckBand(poDS, nBand);
5622 : }
5623 : }
5624 87 : return l_ret;
5625 92 : };
5626 :
5627 92 : if (inputDatasetArg->GetType() == GAAT_DATASET)
5628 : {
5629 : auto poDS =
5630 6 : inputDatasetArg->Get<GDALArgDatasetValue>().GetDatasetRef();
5631 6 : if (poDS && !ValidateForOneDataset(poDS))
5632 2 : ret = false;
5633 : }
5634 : else
5635 : {
5636 86 : CPLAssert(inputDatasetArg->GetType() == GAAT_DATASET_LIST);
5637 85 : for (auto &datasetValue :
5638 256 : inputDatasetArg->Get<std::vector<GDALArgDatasetValue>>())
5639 : {
5640 85 : auto poDS = datasetValue.GetDatasetRef();
5641 85 : if (poDS && !ValidateForOneDataset(poDS))
5642 3 : ret = false;
5643 : }
5644 : }
5645 : }
5646 4502 : return ret;
5647 : }
5648 :
5649 : /************************************************************************/
5650 : /* GDALAlgorithm::RunPreStepPipelineValidations() */
5651 : /************************************************************************/
5652 :
5653 3552 : bool GDALAlgorithm::RunPreStepPipelineValidations() const
5654 : {
5655 3552 : return ValidateBandArg();
5656 : }
5657 :
5658 : /************************************************************************/
5659 : /* GDALAlgorithm::AddBandArg() */
5660 : /************************************************************************/
5661 :
5662 : GDALInConstructionAlgorithmArg &
5663 1695 : GDALAlgorithm::AddBandArg(int *pValue, const char *helpMessage)
5664 : {
5665 2157 : AddValidationAction([this]() { return ValidateBandArg(); });
5666 :
5667 : return AddArg(GDAL_ARG_NAME_BAND, 'b',
5668 : MsgOrDefault(helpMessage, _("Input band (1-based index)")),
5669 3390 : pValue)
5670 : .AddValidationAction(
5671 34 : [pValue]()
5672 : {
5673 34 : if (*pValue <= 0)
5674 : {
5675 1 : CPLError(CE_Failure, CPLE_AppDefined,
5676 : "Value of 'band' should greater or equal to 1.");
5677 1 : return false;
5678 : }
5679 33 : return true;
5680 3390 : });
5681 : }
5682 :
5683 : /************************************************************************/
5684 : /* GDALAlgorithm::AddBandArg() */
5685 : /************************************************************************/
5686 :
5687 : GDALInConstructionAlgorithmArg &
5688 869 : GDALAlgorithm::AddBandArg(std::vector<int> *pValue, const char *helpMessage)
5689 : {
5690 1357 : AddValidationAction([this]() { return ValidateBandArg(); });
5691 :
5692 : return AddArg(GDAL_ARG_NAME_BAND, 'b',
5693 : MsgOrDefault(helpMessage, _("Input band(s) (1-based index)")),
5694 1738 : pValue)
5695 : .AddValidationAction(
5696 126 : [pValue]()
5697 : {
5698 397 : for (int val : *pValue)
5699 : {
5700 272 : if (val <= 0)
5701 : {
5702 1 : CPLError(CE_Failure, CPLE_AppDefined,
5703 : "Value of 'band' should greater or equal "
5704 : "to 1.");
5705 1 : return false;
5706 : }
5707 : }
5708 125 : return true;
5709 1738 : });
5710 : }
5711 :
5712 : /************************************************************************/
5713 : /* ParseAndValidateKeyValue() */
5714 : /************************************************************************/
5715 :
5716 557 : bool GDALAlgorithm::ParseAndValidateKeyValue(GDALAlgorithmArg &arg)
5717 : {
5718 521 : const auto Validate = [this, &arg](const std::string &val)
5719 : {
5720 516 : if (val.find('=') == std::string::npos)
5721 : {
5722 5 : ReportError(
5723 : CE_Failure, CPLE_AppDefined,
5724 : "Invalid value for argument '%s'. <KEY>=<VALUE> expected",
5725 5 : arg.GetName().c_str());
5726 5 : return false;
5727 : }
5728 :
5729 511 : return true;
5730 557 : };
5731 :
5732 557 : if (arg.GetType() == GAAT_STRING)
5733 : {
5734 0 : return Validate(arg.Get<std::string>());
5735 : }
5736 557 : else if (arg.GetType() == GAAT_STRING_LIST)
5737 : {
5738 557 : std::vector<std::string> &vals = arg.Get<std::vector<std::string>>();
5739 557 : if (vals.size() == 1)
5740 : {
5741 : // Try to split A=B,C=D into A=B and C=D if there is no ambiguity
5742 900 : std::vector<std::string> newVals;
5743 900 : std::string curToken;
5744 450 : bool canSplitOnComma = true;
5745 450 : char lastSep = 0;
5746 450 : bool inString = false;
5747 450 : bool equalFoundInLastToken = false;
5748 6948 : for (char c : vals[0])
5749 : {
5750 6502 : if (!inString && c == ',')
5751 : {
5752 10 : if (lastSep != '=' || !equalFoundInLastToken)
5753 : {
5754 2 : canSplitOnComma = false;
5755 2 : break;
5756 : }
5757 8 : lastSep = c;
5758 8 : newVals.push_back(curToken);
5759 8 : curToken.clear();
5760 8 : equalFoundInLastToken = false;
5761 : }
5762 6492 : else if (!inString && c == '=')
5763 : {
5764 449 : if (lastSep == '=')
5765 : {
5766 2 : canSplitOnComma = false;
5767 2 : break;
5768 : }
5769 447 : equalFoundInLastToken = true;
5770 447 : lastSep = c;
5771 447 : curToken += c;
5772 : }
5773 6043 : else if (c == '"')
5774 : {
5775 4 : inString = !inString;
5776 4 : curToken += c;
5777 : }
5778 : else
5779 : {
5780 6039 : curToken += c;
5781 : }
5782 : }
5783 450 : if (canSplitOnComma && !inString && equalFoundInLastToken)
5784 : {
5785 437 : if (!curToken.empty())
5786 437 : newVals.emplace_back(std::move(curToken));
5787 437 : vals = std::move(newVals);
5788 : }
5789 : }
5790 :
5791 1068 : for (const auto &val : vals)
5792 : {
5793 516 : if (!Validate(val))
5794 5 : return false;
5795 : }
5796 : }
5797 :
5798 552 : return true;
5799 : }
5800 :
5801 : /************************************************************************/
5802 : /* IsGDALGOutput() */
5803 : /************************************************************************/
5804 :
5805 2427 : bool GDALAlgorithm::IsGDALGOutput() const
5806 : {
5807 2427 : bool isGDALGOutput = false;
5808 2427 : const auto outputFormatArg = GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT);
5809 2427 : const auto outputArg = GetArg(GDAL_ARG_NAME_OUTPUT);
5810 4205 : if (outputArg && outputArg->GetType() == GAAT_DATASET &&
5811 1778 : outputArg->IsExplicitlySet())
5812 : {
5813 3479 : if (outputFormatArg && outputFormatArg->GetType() == GAAT_STRING &&
5814 1727 : outputFormatArg->IsExplicitlySet())
5815 : {
5816 : const auto &val =
5817 1069 : outputFormatArg->GDALAlgorithmArg::Get<std::string>();
5818 1069 : isGDALGOutput = EQUAL(val.c_str(), "GDALG");
5819 : }
5820 : else
5821 : {
5822 : const auto &filename =
5823 683 : outputArg->GDALAlgorithmArg::Get<GDALArgDatasetValue>();
5824 683 : isGDALGOutput =
5825 1337 : filename.GetName().size() > strlen(".gdalg.json") &&
5826 654 : EQUAL(filename.GetName().c_str() + filename.GetName().size() -
5827 : strlen(".gdalg.json"),
5828 : ".gdalg.json");
5829 : }
5830 : }
5831 2427 : return isGDALGOutput;
5832 : }
5833 :
5834 : /************************************************************************/
5835 : /* ProcessGDALGOutput() */
5836 : /************************************************************************/
5837 :
5838 2566 : GDALAlgorithm::ProcessGDALGOutputRet GDALAlgorithm::ProcessGDALGOutput()
5839 : {
5840 2566 : if (!SupportsStreamedOutput())
5841 730 : return ProcessGDALGOutputRet::NOT_GDALG;
5842 :
5843 1836 : if (IsGDALGOutput())
5844 : {
5845 12 : const auto outputArg = GetArg(GDAL_ARG_NAME_OUTPUT);
5846 : const auto &filename =
5847 12 : outputArg->GDALAlgorithmArg::Get<GDALArgDatasetValue>().GetName();
5848 : VSIStatBufL sStat;
5849 12 : if (VSIStatL(filename.c_str(), &sStat) == 0)
5850 : {
5851 0 : const auto overwriteArg = GetArg(GDAL_ARG_NAME_OVERWRITE);
5852 0 : if (overwriteArg && overwriteArg->GetType() == GAAT_BOOLEAN)
5853 : {
5854 0 : if (!overwriteArg->GDALAlgorithmArg::Get<bool>())
5855 : {
5856 0 : CPLError(CE_Failure, CPLE_AppDefined,
5857 : "File '%s' already exists. Specify the "
5858 : "--overwrite option to overwrite it.",
5859 : filename.c_str());
5860 0 : return ProcessGDALGOutputRet::GDALG_ERROR;
5861 : }
5862 : }
5863 : }
5864 :
5865 24 : std::string osCommandLine;
5866 :
5867 48 : for (const auto &path : GDALAlgorithm::m_callPath)
5868 : {
5869 36 : if (!osCommandLine.empty())
5870 24 : osCommandLine += ' ';
5871 36 : osCommandLine += path;
5872 : }
5873 :
5874 278 : for (const auto &arg : GetArgs())
5875 : {
5876 296 : if (arg->IsExplicitlySet() &&
5877 48 : arg->GetName() != GDAL_ARG_NAME_OUTPUT &&
5878 35 : arg->GetName() != GDAL_ARG_NAME_OUTPUT_FORMAT &&
5879 313 : arg->GetName() != GDAL_ARG_NAME_UPDATE &&
5880 17 : arg->GetName() != GDAL_ARG_NAME_OVERWRITE)
5881 : {
5882 16 : osCommandLine += ' ';
5883 16 : std::string strArg;
5884 16 : if (!arg->Serialize(strArg))
5885 : {
5886 0 : CPLError(CE_Failure, CPLE_AppDefined,
5887 : "Cannot serialize argument %s",
5888 0 : arg->GetName().c_str());
5889 0 : return ProcessGDALGOutputRet::GDALG_ERROR;
5890 : }
5891 16 : osCommandLine += strArg;
5892 : }
5893 : }
5894 :
5895 12 : osCommandLine += " --output-format stream --output streamed_dataset";
5896 :
5897 12 : std::string outStringUnused;
5898 12 : return SaveGDALG(filename, outStringUnused, osCommandLine)
5899 12 : ? ProcessGDALGOutputRet::GDALG_OK
5900 12 : : ProcessGDALGOutputRet::GDALG_ERROR;
5901 : }
5902 :
5903 1824 : return ProcessGDALGOutputRet::NOT_GDALG;
5904 : }
5905 :
5906 : /************************************************************************/
5907 : /* GDALAlgorithm::SaveGDALG() */
5908 : /************************************************************************/
5909 :
5910 24 : /* static */ bool GDALAlgorithm::SaveGDALG(const std::string &filename,
5911 : std::string &outString,
5912 : const std::string &commandLine)
5913 : {
5914 48 : CPLJSONDocument oDoc;
5915 24 : oDoc.GetRoot().Add("type", "gdal_streamed_alg");
5916 24 : oDoc.GetRoot().Add("command_line", commandLine);
5917 24 : oDoc.GetRoot().Add("gdal_version", GDALVersionInfo("VERSION_NUM"));
5918 :
5919 24 : if (!filename.empty())
5920 23 : return oDoc.Save(filename);
5921 :
5922 1 : outString = oDoc.GetRoot().Format(CPLJSONObject::PrettyFormat::Pretty);
5923 1 : return true;
5924 : }
5925 :
5926 : /************************************************************************/
5927 : /* GDALAlgorithm::AddCreationOptionsArg() */
5928 : /************************************************************************/
5929 :
5930 : GDALInConstructionAlgorithmArg &
5931 8531 : GDALAlgorithm::AddCreationOptionsArg(std::vector<std::string> *pValue,
5932 : const char *helpMessage)
5933 : {
5934 : auto &arg = AddArg(GDAL_ARG_NAME_CREATION_OPTION, 0,
5935 17062 : MsgOrDefault(helpMessage, _("Creation option")), pValue)
5936 17062 : .AddAlias("co")
5937 17062 : .SetMetaVar("<KEY>=<VALUE>")
5938 8531 : .SetPackedValuesAllowed(false);
5939 293 : arg.AddValidationAction([this, &arg]()
5940 8824 : { return ParseAndValidateKeyValue(arg); });
5941 :
5942 : arg.SetAutoCompleteFunction(
5943 51 : [this](const std::string ¤tValue)
5944 : {
5945 17 : std::vector<std::string> oRet;
5946 :
5947 17 : int datasetType =
5948 : GDAL_OF_RASTER | GDAL_OF_VECTOR | GDAL_OF_MULTIDIM_RASTER;
5949 17 : auto outputArg = GetArg(GDAL_ARG_NAME_OUTPUT);
5950 17 : if (outputArg && (outputArg->GetType() == GAAT_DATASET ||
5951 0 : outputArg->GetType() == GAAT_DATASET_LIST))
5952 : {
5953 17 : datasetType = outputArg->GetDatasetType();
5954 : }
5955 :
5956 17 : const char *pszMDCreationOptionList =
5957 : (datasetType == GDAL_OF_MULTIDIM_RASTER)
5958 17 : ? GDAL_DMD_MULTIDIM_DATASET_CREATIONOPTIONLIST
5959 : : GDAL_DMD_CREATIONOPTIONLIST;
5960 :
5961 17 : auto outputFormat = GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT);
5962 34 : if (outputFormat && outputFormat->GetType() == GAAT_STRING &&
5963 17 : outputFormat->IsExplicitlySet())
5964 : {
5965 14 : auto poDriver = GetGDALDriverManager()->GetDriverByName(
5966 7 : outputFormat->Get<std::string>().c_str());
5967 7 : if (poDriver)
5968 : {
5969 7 : AddOptionsSuggestions(
5970 7 : poDriver->GetMetadataItem(pszMDCreationOptionList),
5971 : datasetType, currentValue, oRet);
5972 : }
5973 7 : return oRet;
5974 : }
5975 :
5976 10 : if (outputArg && outputArg->GetType() == GAAT_DATASET)
5977 : {
5978 10 : auto poDM = GetGDALDriverManager();
5979 10 : auto &datasetValue = outputArg->Get<GDALArgDatasetValue>();
5980 10 : const auto &osDSName = datasetValue.GetName();
5981 10 : const std::string osExt = CPLGetExtensionSafe(osDSName.c_str());
5982 10 : if (!osExt.empty())
5983 : {
5984 10 : std::set<std::string> oVisitedExtensions;
5985 721 : for (int i = 0; i < poDM->GetDriverCount(); ++i)
5986 : {
5987 718 : auto poDriver = poDM->GetDriver(i);
5988 2154 : if (((datasetType & GDAL_OF_RASTER) != 0 &&
5989 718 : poDriver->GetMetadataItem(GDAL_DCAP_RASTER)) ||
5990 216 : ((datasetType & GDAL_OF_VECTOR) != 0 &&
5991 1436 : poDriver->GetMetadataItem(GDAL_DCAP_VECTOR)) ||
5992 216 : ((datasetType & GDAL_OF_MULTIDIM_RASTER) != 0 &&
5993 0 : poDriver->GetMetadataItem(
5994 0 : GDAL_DCAP_MULTIDIM_RASTER)))
5995 : {
5996 : const char *pszExtensions =
5997 502 : poDriver->GetMetadataItem(GDAL_DMD_EXTENSIONS);
5998 502 : if (pszExtensions)
5999 : {
6000 : const CPLStringList aosExts(
6001 326 : CSLTokenizeString2(pszExtensions, " ", 0));
6002 722 : for (const char *pszExt : cpl::Iterate(aosExts))
6003 : {
6004 422 : if (EQUAL(pszExt, osExt.c_str()) &&
6005 16 : !cpl::contains(oVisitedExtensions,
6006 : pszExt))
6007 : {
6008 10 : oVisitedExtensions.insert(pszExt);
6009 10 : if (AddOptionsSuggestions(
6010 : poDriver->GetMetadataItem(
6011 10 : pszMDCreationOptionList),
6012 : datasetType, currentValue,
6013 : oRet))
6014 : {
6015 7 : return oRet;
6016 : }
6017 3 : break;
6018 : }
6019 : }
6020 : }
6021 : }
6022 : }
6023 : }
6024 : }
6025 :
6026 3 : return oRet;
6027 8531 : });
6028 :
6029 8531 : return arg;
6030 : }
6031 :
6032 : /************************************************************************/
6033 : /* GDALAlgorithm::AddLayerCreationOptionsArg() */
6034 : /************************************************************************/
6035 :
6036 : GDALInConstructionAlgorithmArg &
6037 4096 : GDALAlgorithm::AddLayerCreationOptionsArg(std::vector<std::string> *pValue,
6038 : const char *helpMessage)
6039 : {
6040 : auto &arg =
6041 : AddArg(GDAL_ARG_NAME_LAYER_CREATION_OPTION, 0,
6042 8192 : MsgOrDefault(helpMessage, _("Layer creation option")), pValue)
6043 8192 : .AddAlias("lco")
6044 8192 : .SetMetaVar("<KEY>=<VALUE>")
6045 4096 : .SetPackedValuesAllowed(false);
6046 76 : arg.AddValidationAction([this, &arg]()
6047 4172 : { return ParseAndValidateKeyValue(arg); });
6048 :
6049 : arg.SetAutoCompleteFunction(
6050 5 : [this](const std::string ¤tValue)
6051 : {
6052 2 : std::vector<std::string> oRet;
6053 :
6054 2 : auto outputFormat = GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT);
6055 4 : if (outputFormat && outputFormat->GetType() == GAAT_STRING &&
6056 2 : outputFormat->IsExplicitlySet())
6057 : {
6058 2 : auto poDriver = GetGDALDriverManager()->GetDriverByName(
6059 1 : outputFormat->Get<std::string>().c_str());
6060 1 : if (poDriver)
6061 : {
6062 1 : AddOptionsSuggestions(poDriver->GetMetadataItem(
6063 1 : GDAL_DS_LAYER_CREATIONOPTIONLIST),
6064 : GDAL_OF_VECTOR, currentValue, oRet);
6065 : }
6066 1 : return oRet;
6067 : }
6068 :
6069 1 : auto outputArg = GetArg(GDAL_ARG_NAME_OUTPUT);
6070 1 : if (outputArg && outputArg->GetType() == GAAT_DATASET)
6071 : {
6072 1 : auto poDM = GetGDALDriverManager();
6073 1 : auto &datasetValue = outputArg->Get<GDALArgDatasetValue>();
6074 1 : const auto &osDSName = datasetValue.GetName();
6075 1 : const std::string osExt = CPLGetExtensionSafe(osDSName.c_str());
6076 1 : if (!osExt.empty())
6077 : {
6078 1 : std::set<std::string> oVisitedExtensions;
6079 231 : for (int i = 0; i < poDM->GetDriverCount(); ++i)
6080 : {
6081 230 : auto poDriver = poDM->GetDriver(i);
6082 230 : if (poDriver->GetMetadataItem(GDAL_DCAP_VECTOR))
6083 : {
6084 : const char *pszExtensions =
6085 91 : poDriver->GetMetadataItem(GDAL_DMD_EXTENSIONS);
6086 91 : if (pszExtensions)
6087 : {
6088 : const CPLStringList aosExts(
6089 62 : CSLTokenizeString2(pszExtensions, " ", 0));
6090 156 : for (const char *pszExt : cpl::Iterate(aosExts))
6091 : {
6092 96 : if (EQUAL(pszExt, osExt.c_str()) &&
6093 1 : !cpl::contains(oVisitedExtensions,
6094 : pszExt))
6095 : {
6096 1 : oVisitedExtensions.insert(pszExt);
6097 1 : if (AddOptionsSuggestions(
6098 : poDriver->GetMetadataItem(
6099 1 : GDAL_DS_LAYER_CREATIONOPTIONLIST),
6100 : GDAL_OF_VECTOR, currentValue,
6101 : oRet))
6102 : {
6103 0 : return oRet;
6104 : }
6105 1 : break;
6106 : }
6107 : }
6108 : }
6109 : }
6110 : }
6111 : }
6112 : }
6113 :
6114 1 : return oRet;
6115 4096 : });
6116 :
6117 4096 : return arg;
6118 : }
6119 :
6120 : /************************************************************************/
6121 : /* GDALAlgorithm::AddBBOXArg() */
6122 : /************************************************************************/
6123 :
6124 : /** Add bbox=xmin,ymin,xmax,ymax argument. */
6125 : GDALInConstructionAlgorithmArg &
6126 1908 : GDALAlgorithm::AddBBOXArg(std::vector<double> *pValue, const char *helpMessage)
6127 : {
6128 : auto &arg = AddArg("bbox", 0,
6129 : MsgOrDefault(helpMessage,
6130 : _("Bounding box as xmin,ymin,xmax,ymax")),
6131 3816 : pValue)
6132 1908 : .SetRepeatedArgAllowed(false)
6133 1908 : .SetMinCount(4)
6134 1908 : .SetMaxCount(4)
6135 1908 : .SetDisplayHintAboutRepetition(false);
6136 : arg.AddValidationAction(
6137 181 : [&arg]()
6138 : {
6139 181 : const auto &val = arg.Get<std::vector<double>>();
6140 181 : CPLAssert(val.size() == 4);
6141 181 : if (!(val[0] <= val[2]) || !(val[1] <= val[3]))
6142 : {
6143 5 : CPLError(CE_Failure, CPLE_AppDefined,
6144 : "Value of 'bbox' should be xmin,ymin,xmax,ymax with "
6145 : "xmin <= xmax and ymin <= ymax");
6146 5 : return false;
6147 : }
6148 176 : return true;
6149 1908 : });
6150 1908 : return arg;
6151 : }
6152 :
6153 : /************************************************************************/
6154 : /* GDALAlgorithm::AddActiveLayerArg() */
6155 : /************************************************************************/
6156 :
6157 : GDALInConstructionAlgorithmArg &
6158 1909 : GDALAlgorithm::AddActiveLayerArg(std::string *pValue, const char *helpMessage)
6159 : {
6160 : return AddArg("active-layer", 0,
6161 : MsgOrDefault(helpMessage,
6162 : _("Set active layer (if not specified, all)")),
6163 1909 : pValue);
6164 : }
6165 :
6166 : /************************************************************************/
6167 : /* GDALAlgorithm::AddNumThreadsArg() */
6168 : /************************************************************************/
6169 :
6170 : GDALInConstructionAlgorithmArg &
6171 723 : GDALAlgorithm::AddNumThreadsArg(int *pValue, std::string *pStrValue,
6172 : const char *helpMessage)
6173 : {
6174 : auto &arg =
6175 : AddArg(GDAL_ARG_NAME_NUM_THREADS, 'j',
6176 : MsgOrDefault(helpMessage, _("Number of jobs (or ALL_CPUS)")),
6177 723 : pStrValue);
6178 :
6179 : AddArg(GDAL_ARG_NAME_NUM_THREADS_INT_HIDDEN, 0,
6180 1446 : _("Number of jobs (read-only, hidden argument)"), pValue)
6181 723 : .SetHidden();
6182 :
6183 2724 : auto lambda = [this, &arg, pValue, pStrValue]
6184 : {
6185 908 : bool bOK = false;
6186 908 : const char *pszVal = CPLGetConfigOption("GDAL_NUM_THREADS", nullptr);
6187 : const int nLimit = std::clamp(
6188 908 : pszVal && !EQUAL(pszVal, "ALL_CPUS") ? atoi(pszVal) : INT_MAX, 1,
6189 1816 : CPLGetNumCPUs());
6190 : const int nNumThreads =
6191 908 : GDALGetNumThreads(pStrValue->c_str(), nLimit,
6192 : /* bDefaultToAllCPUs = */ false, nullptr, &bOK);
6193 908 : if (bOK)
6194 : {
6195 908 : *pValue = nNumThreads;
6196 : }
6197 : else
6198 : {
6199 0 : ReportError(CE_Failure, CPLE_IllegalArg,
6200 : "Invalid value for '%s' argument",
6201 0 : arg.GetName().c_str());
6202 : }
6203 908 : return bOK;
6204 723 : };
6205 723 : if (!pStrValue->empty())
6206 : {
6207 677 : arg.SetDefault(*pStrValue);
6208 677 : lambda();
6209 : }
6210 723 : arg.AddValidationAction(std::move(lambda));
6211 723 : return arg;
6212 : }
6213 :
6214 : /************************************************************************/
6215 : /* GDALAlgorithm::AddAbsolutePathArg() */
6216 : /************************************************************************/
6217 :
6218 : GDALInConstructionAlgorithmArg &
6219 623 : GDALAlgorithm::AddAbsolutePathArg(bool *pValue, const char *helpMessage)
6220 : {
6221 : return AddArg(
6222 : "absolute-path", 0,
6223 : MsgOrDefault(helpMessage, _("Whether the path to the input dataset "
6224 : "should be stored as an absolute path")),
6225 623 : pValue);
6226 : }
6227 :
6228 : /************************************************************************/
6229 : /* GDALAlgorithm::AddPixelFunctionNameArg() */
6230 : /************************************************************************/
6231 :
6232 : GDALInConstructionAlgorithmArg &
6233 138 : GDALAlgorithm::AddPixelFunctionNameArg(std::string *pValue,
6234 : const char *helpMessage)
6235 : {
6236 :
6237 : const auto pixelFunctionNames =
6238 138 : VRTDerivedRasterBand::GetPixelFunctionNames();
6239 : return AddArg(
6240 : "pixel-function", 0,
6241 : MsgOrDefault(
6242 : helpMessage,
6243 : _("Specify a pixel function to calculate output value from "
6244 : "overlapping inputs")),
6245 276 : pValue)
6246 276 : .SetChoices(pixelFunctionNames);
6247 : }
6248 :
6249 : /************************************************************************/
6250 : /* GDALAlgorithm::AddPixelFunctionArgsArg() */
6251 : /************************************************************************/
6252 :
6253 : GDALInConstructionAlgorithmArg &
6254 138 : GDALAlgorithm::AddPixelFunctionArgsArg(std::vector<std::string> *pValue,
6255 : const char *helpMessage)
6256 : {
6257 : auto &pixelFunctionArgArg =
6258 : AddArg("pixel-function-arg", 0,
6259 : MsgOrDefault(
6260 : helpMessage,
6261 : _("Specify argument(s) to pass to the pixel function")),
6262 276 : pValue)
6263 276 : .SetMetaVar("<NAME>=<VALUE>")
6264 138 : .SetRepeatedArgAllowed(true);
6265 : pixelFunctionArgArg.AddValidationAction(
6266 7 : [this, &pixelFunctionArgArg]()
6267 145 : { return ParseAndValidateKeyValue(pixelFunctionArgArg); });
6268 :
6269 : pixelFunctionArgArg.SetAutoCompleteFunction(
6270 12 : [this](const std::string ¤tValue)
6271 : {
6272 12 : std::string pixelFunction;
6273 6 : const auto pixelFunctionArg = GetArg("pixel-function");
6274 6 : if (pixelFunctionArg && pixelFunctionArg->GetType() == GAAT_STRING)
6275 : {
6276 6 : pixelFunction = pixelFunctionArg->Get<std::string>();
6277 : }
6278 :
6279 6 : std::vector<std::string> ret;
6280 :
6281 6 : if (!pixelFunction.empty())
6282 : {
6283 5 : const auto *pair = VRTDerivedRasterBand::GetPixelFunction(
6284 : pixelFunction.c_str());
6285 5 : if (!pair)
6286 : {
6287 1 : ret.push_back("**");
6288 : // Non printable UTF-8 space, to avoid autocompletion to pickup on 'd'
6289 1 : ret.push_back(std::string("\xC2\xA0"
6290 : "Invalid pixel function name"));
6291 : }
6292 4 : else if (pair->second.find("Argument name=") ==
6293 : std::string::npos)
6294 : {
6295 1 : ret.push_back("**");
6296 : // Non printable UTF-8 space, to avoid autocompletion to pickup on 'd'
6297 1 : ret.push_back(
6298 2 : std::string(
6299 : "\xC2\xA0"
6300 : "No pixel function arguments for pixel function '")
6301 1 : .append(pixelFunction)
6302 1 : .append("'"));
6303 : }
6304 : else
6305 : {
6306 3 : AddOptionsSuggestions(pair->second.c_str(), 0, currentValue,
6307 : ret);
6308 : }
6309 : }
6310 :
6311 12 : return ret;
6312 138 : });
6313 :
6314 138 : return pixelFunctionArgArg;
6315 : }
6316 :
6317 : /************************************************************************/
6318 : /* GDALAlgorithm::AddProgressArg() */
6319 : /************************************************************************/
6320 :
6321 8323 : void GDALAlgorithm::AddProgressArg()
6322 : {
6323 : AddArg(GDAL_ARG_NAME_QUIET, 'q',
6324 16646 : _("Quiet mode (no progress bar or warning message)"), &m_quiet)
6325 8323 : .SetAvailableInPipelineStep(false)
6326 16646 : .SetCategory(GAAC_COMMON)
6327 8323 : .AddAction([this]() { m_progressBarRequested = false; });
6328 :
6329 16646 : AddArg("progress", 0, _("Display progress bar"), &m_progressBarRequested)
6330 8323 : .SetAvailableInPipelineStep(false)
6331 8323 : .SetHidden();
6332 8323 : }
6333 :
6334 : /************************************************************************/
6335 : /* GDALAlgorithm::Run() */
6336 : /************************************************************************/
6337 :
6338 4977 : bool GDALAlgorithm::Run(GDALProgressFunc pfnProgress, void *pProgressData)
6339 : {
6340 4977 : WarnIfDeprecated();
6341 :
6342 4977 : if (m_selectedSubAlg)
6343 : {
6344 463 : if (m_calledFromCommandLine)
6345 275 : m_selectedSubAlg->m_calledFromCommandLine = true;
6346 463 : return m_selectedSubAlg->Run(pfnProgress, pProgressData);
6347 : }
6348 :
6349 4514 : if (m_helpRequested || m_helpDocRequested)
6350 : {
6351 19 : if (m_calledFromCommandLine)
6352 19 : printf("%s", GetUsageForCLI(false).c_str()); /*ok*/
6353 19 : return true;
6354 : }
6355 :
6356 4495 : if (m_JSONUsageRequested)
6357 : {
6358 3 : if (m_calledFromCommandLine)
6359 3 : printf("%s", GetUsageAsJSON().c_str()); /*ok*/
6360 3 : return true;
6361 : }
6362 :
6363 4492 : if (!ValidateArguments())
6364 125 : return false;
6365 :
6366 4367 : if (m_alreadyRun)
6367 : {
6368 3 : ReportError(CE_Failure, CPLE_AppDefined,
6369 : "Run() can be called only once per algorithm instance");
6370 3 : return false;
6371 : }
6372 4364 : m_alreadyRun = true;
6373 :
6374 4364 : switch (ProcessGDALGOutput())
6375 : {
6376 0 : case ProcessGDALGOutputRet::GDALG_ERROR:
6377 0 : return false;
6378 :
6379 12 : case ProcessGDALGOutputRet::GDALG_OK:
6380 12 : return true;
6381 :
6382 4352 : case ProcessGDALGOutputRet::NOT_GDALG:
6383 4352 : break;
6384 : }
6385 :
6386 4352 : if (m_executionForStreamOutput)
6387 : {
6388 98 : if (!CheckSafeForStreamOutput())
6389 : {
6390 4 : return false;
6391 : }
6392 : }
6393 :
6394 4348 : return RunImpl(pfnProgress, pProgressData);
6395 : }
6396 :
6397 : /************************************************************************/
6398 : /* GDALAlgorithm::CheckSafeForStreamOutput() */
6399 : /************************************************************************/
6400 :
6401 50 : bool GDALAlgorithm::CheckSafeForStreamOutput()
6402 : {
6403 50 : const auto outputFormatArg = GetArg(GDAL_ARG_NAME_OUTPUT_FORMAT);
6404 50 : if (outputFormatArg && outputFormatArg->GetType() == GAAT_STRING)
6405 : {
6406 50 : const auto &val = outputFormatArg->GDALAlgorithmArg::Get<std::string>();
6407 50 : if (!EQUAL(val.c_str(), "stream"))
6408 : {
6409 : // For security reasons, to avoid that reading a .gdalg.json file
6410 : // writes a file on the file system.
6411 4 : ReportError(
6412 : CE_Failure, CPLE_NotSupported,
6413 : "in streamed execution, --format stream should be used");
6414 4 : return false;
6415 : }
6416 : }
6417 46 : return true;
6418 : }
6419 :
6420 : /************************************************************************/
6421 : /* GDALAlgorithm::Finalize() */
6422 : /************************************************************************/
6423 :
6424 1962 : bool GDALAlgorithm::Finalize()
6425 : {
6426 1962 : bool ret = true;
6427 1962 : if (m_selectedSubAlg)
6428 281 : ret = m_selectedSubAlg->Finalize();
6429 :
6430 35241 : for (auto &arg : m_args)
6431 : {
6432 33279 : if (arg->GetType() == GAAT_DATASET)
6433 : {
6434 1494 : ret = arg->Get<GDALArgDatasetValue>().Close() && ret;
6435 : }
6436 31785 : else if (arg->GetType() == GAAT_DATASET_LIST)
6437 : {
6438 3055 : for (auto &ds : arg->Get<std::vector<GDALArgDatasetValue>>())
6439 : {
6440 1432 : ret = ds.Close() && ret;
6441 : }
6442 : }
6443 : }
6444 1962 : return ret;
6445 : }
6446 :
6447 : /************************************************************************/
6448 : /* GDALAlgorithm::GetArgNamesForCLI() */
6449 : /************************************************************************/
6450 :
6451 : std::pair<std::vector<std::pair<GDALAlgorithmArg *, std::string>>, size_t>
6452 719 : GDALAlgorithm::GetArgNamesForCLI() const
6453 : {
6454 1438 : std::vector<std::pair<GDALAlgorithmArg *, std::string>> options;
6455 :
6456 719 : size_t maxOptLen = 0;
6457 9051 : for (const auto &arg : m_args)
6458 : {
6459 8332 : if (arg->IsHidden() || arg->IsHiddenForCLI())
6460 1735 : continue;
6461 6597 : std::string opt;
6462 6597 : bool addComma = false;
6463 6597 : if (!arg->GetShortName().empty())
6464 : {
6465 1427 : opt += '-';
6466 1427 : opt += arg->GetShortName();
6467 1427 : addComma = true;
6468 : }
6469 6597 : for (char alias : arg->GetShortNameAliases())
6470 : {
6471 0 : if (addComma)
6472 0 : opt += ", ";
6473 0 : opt += "-";
6474 0 : opt += alias;
6475 0 : addComma = true;
6476 : }
6477 7351 : for (const std::string &alias : arg->GetAliases())
6478 : {
6479 754 : if (addComma)
6480 326 : opt += ", ";
6481 754 : opt += "--";
6482 754 : opt += alias;
6483 754 : addComma = true;
6484 : }
6485 6597 : if (!arg->GetName().empty())
6486 : {
6487 6597 : if (addComma)
6488 1855 : opt += ", ";
6489 6597 : opt += "--";
6490 6597 : opt += arg->GetName();
6491 : }
6492 6597 : const auto &metaVar = arg->GetMetaVar();
6493 6597 : if (!metaVar.empty())
6494 : {
6495 4146 : opt += ' ';
6496 4146 : if (metaVar.front() != '<')
6497 3000 : opt += '<';
6498 4146 : opt += metaVar;
6499 4146 : if (metaVar.back() != '>')
6500 2994 : opt += '>';
6501 : }
6502 6597 : maxOptLen = std::max(maxOptLen, opt.size());
6503 6597 : options.emplace_back(arg.get(), opt);
6504 : }
6505 :
6506 1438 : return std::make_pair(std::move(options), maxOptLen);
6507 : }
6508 :
6509 : /************************************************************************/
6510 : /* GDALAlgorithm::GetUsageForCLI() */
6511 : /************************************************************************/
6512 :
6513 : std::string
6514 428 : GDALAlgorithm::GetUsageForCLI(bool shortUsage,
6515 : const UsageOptions &usageOptions) const
6516 : {
6517 428 : if (m_selectedSubAlg)
6518 7 : return m_selectedSubAlg->GetUsageForCLI(shortUsage, usageOptions);
6519 :
6520 842 : std::string osRet(usageOptions.isPipelineStep ? "*" : "Usage:");
6521 842 : std::string osPath;
6522 849 : for (const std::string &s : m_callPath)
6523 : {
6524 428 : if (!osPath.empty())
6525 53 : osPath += ' ';
6526 428 : osPath += s;
6527 : }
6528 421 : osRet += ' ';
6529 421 : osRet += osPath;
6530 :
6531 421 : bool hasNonPositionals = false;
6532 5259 : for (const auto &arg : m_args)
6533 : {
6534 4838 : if (!arg->IsHidden() && !arg->IsHiddenForCLI() && !arg->IsPositional())
6535 3506 : hasNonPositionals = true;
6536 : }
6537 :
6538 421 : if (HasSubAlgorithms())
6539 : {
6540 9 : if (m_callPath.size() == 1)
6541 : {
6542 8 : osRet += " <COMMAND>";
6543 8 : if (hasNonPositionals)
6544 8 : osRet += " [OPTIONS]";
6545 8 : if (usageOptions.isPipelineStep)
6546 : {
6547 5 : const size_t nLenFirstLine = osRet.size();
6548 5 : osRet += '\n';
6549 5 : osRet.append(nLenFirstLine, '-');
6550 5 : osRet += '\n';
6551 : }
6552 8 : osRet += "\nwhere <COMMAND> is one of:\n";
6553 : }
6554 : else
6555 : {
6556 1 : osRet += " <SUBCOMMAND>";
6557 1 : if (hasNonPositionals)
6558 1 : osRet += " [OPTIONS]";
6559 1 : if (usageOptions.isPipelineStep)
6560 : {
6561 0 : const size_t nLenFirstLine = osRet.size();
6562 0 : osRet += '\n';
6563 0 : osRet.append(nLenFirstLine, '-');
6564 0 : osRet += '\n';
6565 : }
6566 1 : osRet += "\nwhere <SUBCOMMAND> is one of:\n";
6567 : }
6568 9 : size_t maxNameLen = 0;
6569 52 : for (const auto &subAlgName : GetSubAlgorithmNames())
6570 : {
6571 43 : maxNameLen = std::max(maxNameLen, subAlgName.size());
6572 : }
6573 52 : for (const auto &subAlgName : GetSubAlgorithmNames())
6574 : {
6575 86 : auto subAlg = InstantiateSubAlgorithm(subAlgName);
6576 43 : if (subAlg && !subAlg->IsHidden())
6577 : {
6578 43 : const std::string &name(subAlg->GetName());
6579 43 : osRet += " - ";
6580 43 : osRet += name;
6581 43 : osRet += ": ";
6582 43 : osRet.append(maxNameLen - name.size(), ' ');
6583 43 : osRet += subAlg->GetDescription();
6584 43 : if (!subAlg->m_aliases.empty())
6585 : {
6586 6 : bool first = true;
6587 6 : for (const auto &alias : subAlg->GetAliases())
6588 : {
6589 6 : if (alias ==
6590 : GDALAlgorithmRegistry::HIDDEN_ALIAS_SEPARATOR)
6591 6 : break;
6592 0 : if (first)
6593 0 : osRet += " (alias: ";
6594 : else
6595 0 : osRet += ", ";
6596 0 : osRet += alias;
6597 0 : first = false;
6598 : }
6599 6 : if (!first)
6600 : {
6601 0 : osRet += ')';
6602 : }
6603 : }
6604 43 : osRet += '\n';
6605 : }
6606 : }
6607 :
6608 9 : if (shortUsage && hasNonPositionals)
6609 : {
6610 2 : osRet += "\nTry '";
6611 2 : osRet += osPath;
6612 2 : osRet += " --help' for help.\n";
6613 : }
6614 : }
6615 : else
6616 : {
6617 412 : if (!m_args.empty())
6618 : {
6619 412 : if (hasNonPositionals)
6620 412 : osRet += " [OPTIONS]";
6621 603 : for (const auto *arg : m_positionalArgs)
6622 : {
6623 268 : if ((!arg->IsHidden() && !arg->IsHiddenForCLI()) ||
6624 77 : (GetName() == "pipeline" && arg->GetName() == "pipeline"))
6625 : {
6626 : const bool optional =
6627 204 : (!arg->IsRequired() && !(GetName() == "pipeline" &&
6628 30 : arg->GetName() == "pipeline"));
6629 174 : osRet += ' ';
6630 174 : if (optional)
6631 30 : osRet += '[';
6632 174 : const std::string &metavar = arg->GetMetaVar();
6633 174 : if (!metavar.empty() && metavar[0] == '<')
6634 : {
6635 4 : osRet += metavar;
6636 : }
6637 : else
6638 : {
6639 170 : osRet += '<';
6640 170 : osRet += metavar;
6641 170 : osRet += '>';
6642 : }
6643 216 : if (arg->GetType() == GAAT_DATASET_LIST &&
6644 42 : arg->GetMaxCount() > 1)
6645 : {
6646 28 : osRet += "...";
6647 : }
6648 174 : if (optional)
6649 30 : osRet += ']';
6650 : }
6651 : }
6652 : }
6653 :
6654 412 : const size_t nLenFirstLine = osRet.size();
6655 412 : osRet += '\n';
6656 412 : if (usageOptions.isPipelineStep)
6657 : {
6658 322 : osRet.append(nLenFirstLine, '-');
6659 322 : osRet += '\n';
6660 : }
6661 :
6662 412 : if (shortUsage)
6663 : {
6664 23 : osRet += "Try '";
6665 23 : osRet += osPath;
6666 23 : osRet += " --help' for help.\n";
6667 23 : return osRet;
6668 : }
6669 :
6670 389 : osRet += '\n';
6671 389 : osRet += m_description;
6672 389 : osRet += '\n';
6673 : }
6674 :
6675 398 : if (!m_args.empty() && !shortUsage)
6676 : {
6677 792 : std::vector<std::pair<GDALAlgorithmArg *, std::string>> options;
6678 : size_t maxOptLen;
6679 396 : std::tie(options, maxOptLen) = GetArgNamesForCLI();
6680 396 : if (usageOptions.maxOptLen)
6681 323 : maxOptLen = usageOptions.maxOptLen;
6682 :
6683 792 : const std::string userProvidedOpt = "--<user-provided-option>=<value>";
6684 396 : if (m_arbitraryLongNameArgsAllowed)
6685 2 : maxOptLen = std::max(maxOptLen, userProvidedOpt.size());
6686 :
6687 : const auto OutputArg =
6688 2514 : [this, maxOptLen, &osRet,
6689 25146 : &usageOptions](const GDALAlgorithmArg *arg, const std::string &opt)
6690 : {
6691 2514 : osRet += " ";
6692 2514 : osRet += opt;
6693 2514 : osRet += " ";
6694 2514 : osRet.append(maxOptLen - opt.size(), ' ');
6695 2514 : osRet += arg->GetDescription();
6696 :
6697 2514 : const auto &choices = arg->GetChoices();
6698 2514 : if (!choices.empty())
6699 : {
6700 237 : osRet += ". ";
6701 237 : osRet += arg->GetMetaVar();
6702 237 : osRet += '=';
6703 237 : bool firstChoice = true;
6704 1800 : for (const auto &choice : choices)
6705 : {
6706 1563 : if (!firstChoice)
6707 1326 : osRet += '|';
6708 1563 : osRet += choice;
6709 1563 : firstChoice = false;
6710 : }
6711 : }
6712 :
6713 4958 : if (arg->GetType() == GAAT_DATASET ||
6714 2444 : arg->GetType() == GAAT_DATASET_LIST)
6715 : {
6716 148 : if (arg->IsOutput() &&
6717 148 : arg->GetDatasetInputFlags() == GADV_NAME &&
6718 9 : arg->GetDatasetOutputFlags() == GADV_OBJECT)
6719 : {
6720 9 : osRet += " (created by algorithm)";
6721 : }
6722 : }
6723 :
6724 2514 : if (arg->GetType() == GAAT_STRING && arg->HasDefaultValue())
6725 : {
6726 198 : osRet += " (default: ";
6727 198 : osRet += arg->GetDefault<std::string>();
6728 198 : osRet += ')';
6729 : }
6730 2316 : else if (arg->GetType() == GAAT_BOOLEAN && arg->HasDefaultValue())
6731 : {
6732 70 : if (arg->GetDefault<bool>())
6733 0 : osRet += " (default: true)";
6734 : }
6735 2246 : else if (arg->GetType() == GAAT_INTEGER && arg->HasDefaultValue())
6736 : {
6737 84 : osRet += " (default: ";
6738 84 : osRet += CPLSPrintf("%d", arg->GetDefault<int>());
6739 84 : osRet += ')';
6740 : }
6741 2162 : else if (arg->GetType() == GAAT_REAL && arg->HasDefaultValue())
6742 : {
6743 49 : osRet += " (default: ";
6744 49 : osRet += CPLSPrintf("%g", arg->GetDefault<double>());
6745 49 : osRet += ')';
6746 : }
6747 2551 : else if (arg->GetType() == GAAT_STRING_LIST &&
6748 438 : arg->HasDefaultValue())
6749 : {
6750 : const auto &defaultVal =
6751 17 : arg->GetDefault<std::vector<std::string>>();
6752 17 : if (defaultVal.size() == 1)
6753 : {
6754 17 : osRet += " (default: ";
6755 17 : osRet += defaultVal[0];
6756 17 : osRet += ')';
6757 : }
6758 : }
6759 2123 : else if (arg->GetType() == GAAT_INTEGER_LIST &&
6760 27 : arg->HasDefaultValue())
6761 : {
6762 0 : const auto &defaultVal = arg->GetDefault<std::vector<int>>();
6763 0 : if (defaultVal.size() == 1)
6764 : {
6765 0 : osRet += " (default: ";
6766 0 : osRet += CPLSPrintf("%d", defaultVal[0]);
6767 0 : osRet += ')';
6768 : }
6769 : }
6770 2096 : else if (arg->GetType() == GAAT_REAL_LIST && arg->HasDefaultValue())
6771 : {
6772 0 : const auto &defaultVal = arg->GetDefault<std::vector<double>>();
6773 0 : if (defaultVal.size() == 1)
6774 : {
6775 0 : osRet += " (default: ";
6776 0 : osRet += CPLSPrintf("%g", defaultVal[0]);
6777 0 : osRet += ')';
6778 : }
6779 : }
6780 :
6781 2514 : if (arg->GetDisplayHintAboutRepetition())
6782 : {
6783 2547 : if (arg->GetMinCount() > 0 &&
6784 92 : arg->GetMinCount() == arg->GetMaxCount())
6785 : {
6786 18 : if (arg->GetMinCount() != 1)
6787 5 : osRet += CPLSPrintf(" [%d values]", arg->GetMaxCount());
6788 : }
6789 2511 : else if (arg->GetMinCount() > 0 &&
6790 74 : arg->GetMaxCount() < GDALAlgorithmArgDecl::UNBOUNDED)
6791 : {
6792 : osRet += CPLSPrintf(" [%d..%d values]", arg->GetMinCount(),
6793 8 : arg->GetMaxCount());
6794 : }
6795 2429 : else if (arg->GetMinCount() > 0)
6796 : {
6797 66 : osRet += CPLSPrintf(" [%d.. values]", arg->GetMinCount());
6798 : }
6799 2363 : else if (arg->GetMaxCount() > 1)
6800 : {
6801 427 : osRet += " [may be repeated]";
6802 : }
6803 : }
6804 :
6805 2514 : if (arg->IsRequired())
6806 : {
6807 172 : osRet += " [required]";
6808 : }
6809 :
6810 2763 : if (!arg->IsAvailableInPipelineStep() &&
6811 249 : !usageOptions.isPipelineStep)
6812 : {
6813 29 : osRet += " [not available in pipelines]";
6814 : }
6815 :
6816 2514 : osRet += '\n';
6817 :
6818 2514 : const auto &mutualExclusionGroup = arg->GetMutualExclusionGroup();
6819 2514 : if (!mutualExclusionGroup.empty())
6820 : {
6821 502 : std::string otherArgs;
6822 4739 : for (const auto &otherArg : m_args)
6823 : {
6824 8249 : if (otherArg->IsHidden() || otherArg->IsHiddenForCLI() ||
6825 3761 : otherArg.get() == arg)
6826 978 : continue;
6827 3510 : if (otherArg->GetMutualExclusionGroup() ==
6828 : mutualExclusionGroup)
6829 : {
6830 348 : if (!otherArgs.empty())
6831 101 : otherArgs += ", ";
6832 348 : otherArgs += "--";
6833 348 : otherArgs += otherArg->GetName();
6834 : }
6835 : }
6836 251 : if (!otherArgs.empty())
6837 : {
6838 247 : osRet += " ";
6839 247 : osRet += " ";
6840 247 : osRet.append(maxOptLen, ' ');
6841 247 : osRet += "Mutually exclusive with ";
6842 247 : osRet += otherArgs;
6843 247 : osRet += '\n';
6844 : }
6845 : }
6846 :
6847 : // Check dependency
6848 5028 : std::string dependencyArgs;
6849 :
6850 32 : for (const auto &dependencyArgumentName :
6851 2578 : GetArgDependencies(arg->GetName()))
6852 : {
6853 32 : const auto otherArg{GetArg(dependencyArgumentName)};
6854 32 : if (otherArg != nullptr)
6855 : {
6856 32 : if (otherArg->IsHidden() || otherArg->IsHiddenForCLI() ||
6857 : otherArg == arg)
6858 : {
6859 0 : continue;
6860 : }
6861 :
6862 32 : if (!dependencyArgs.empty())
6863 : {
6864 3 : dependencyArgs += ", ";
6865 : }
6866 :
6867 32 : dependencyArgs += "--";
6868 32 : dependencyArgs += otherArg->GetName();
6869 : }
6870 : else
6871 : {
6872 0 : CPLError(CE_Warning, CPLE_AppDefined,
6873 : "Argument '%s' depends on unknown argument '%s'",
6874 0 : arg->GetName().c_str(),
6875 : dependencyArgumentName.c_str());
6876 : }
6877 : }
6878 :
6879 2514 : if (!dependencyArgs.empty())
6880 : {
6881 29 : osRet += " ";
6882 29 : osRet += " ";
6883 29 : osRet.append(maxOptLen, ' ');
6884 29 : osRet += "Depends on ";
6885 29 : osRet += dependencyArgs;
6886 29 : osRet += '\n';
6887 : }
6888 2514 : };
6889 :
6890 396 : if (!m_positionalArgs.empty())
6891 : {
6892 151 : osRet += "\nPositional arguments:\n";
6893 1614 : for (const auto &[arg, opt] : options)
6894 : {
6895 1463 : if (arg->IsPositional())
6896 141 : OutputArg(arg, opt);
6897 : }
6898 : }
6899 :
6900 396 : if (hasNonPositionals)
6901 : {
6902 396 : bool hasCommon = false;
6903 396 : bool hasBase = false;
6904 396 : bool hasAdvanced = false;
6905 396 : bool hasEsoteric = false;
6906 792 : std::vector<std::string> categories;
6907 3895 : for (const auto &iter : options)
6908 : {
6909 3499 : const auto &arg = iter.first;
6910 3499 : if (!arg->IsPositional())
6911 : {
6912 3358 : const auto &category = arg->GetCategory();
6913 3358 : if (category == GAAC_COMMON)
6914 : {
6915 1207 : hasCommon = true;
6916 : }
6917 2151 : else if (category == GAAC_BASE)
6918 : {
6919 1879 : hasBase = true;
6920 : }
6921 272 : else if (category == GAAC_ADVANCED)
6922 : {
6923 210 : hasAdvanced = true;
6924 : }
6925 62 : else if (category == GAAC_ESOTERIC)
6926 : {
6927 29 : hasEsoteric = true;
6928 : }
6929 33 : else if (std::find(categories.begin(), categories.end(),
6930 33 : category) == categories.end())
6931 : {
6932 9 : categories.push_back(category);
6933 : }
6934 : }
6935 : }
6936 396 : if (hasAdvanced || m_arbitraryLongNameArgsAllowed)
6937 71 : categories.insert(categories.begin(), GAAC_ADVANCED);
6938 396 : if (hasBase)
6939 349 : categories.insert(categories.begin(), GAAC_BASE);
6940 396 : if (hasCommon && !usageOptions.isPipelineStep)
6941 69 : categories.insert(categories.begin(), GAAC_COMMON);
6942 396 : if (hasEsoteric)
6943 11 : categories.push_back(GAAC_ESOTERIC);
6944 :
6945 905 : for (const auto &category : categories)
6946 : {
6947 509 : osRet += "\n";
6948 509 : if (category != GAAC_BASE)
6949 : {
6950 160 : osRet += category;
6951 160 : osRet += ' ';
6952 : }
6953 509 : osRet += "Options:\n";
6954 5574 : for (const auto &[arg, opt] : options)
6955 : {
6956 5065 : if (!arg->IsPositional() && arg->GetCategory() == category)
6957 2373 : OutputArg(arg, opt);
6958 : }
6959 509 : if (m_arbitraryLongNameArgsAllowed && category == GAAC_ADVANCED)
6960 : {
6961 2 : osRet += " ";
6962 2 : osRet += userProvidedOpt;
6963 2 : osRet += " ";
6964 2 : if (userProvidedOpt.size() < maxOptLen)
6965 0 : osRet.append(maxOptLen - userProvidedOpt.size(), ' ');
6966 2 : osRet += "Argument provided by user";
6967 2 : osRet += '\n';
6968 : }
6969 : }
6970 : }
6971 : }
6972 :
6973 398 : if (!m_longDescription.empty())
6974 : {
6975 6 : osRet += '\n';
6976 6 : osRet += m_longDescription;
6977 6 : osRet += '\n';
6978 : }
6979 :
6980 398 : if (!m_helpDocRequested && !usageOptions.isPipelineMain)
6981 : {
6982 383 : if (!m_helpURL.empty())
6983 : {
6984 383 : osRet += "\nFor more details, consult ";
6985 383 : osRet += GetHelpFullURL();
6986 383 : osRet += '\n';
6987 : }
6988 383 : osRet += GetUsageForCLIEnd();
6989 : }
6990 :
6991 398 : return osRet;
6992 : }
6993 :
6994 : /************************************************************************/
6995 : /* GDALAlgorithm::GetUsageForCLIEnd() */
6996 : /************************************************************************/
6997 :
6998 : //! @cond Doxygen_Suppress
6999 390 : std::string GDALAlgorithm::GetUsageForCLIEnd() const
7000 : {
7001 390 : std::string osRet;
7002 :
7003 390 : if (!m_callPath.empty() && m_callPath[0] == "gdal")
7004 : {
7005 : osRet += "\nWARNING: the gdal command is provisionally provided as an "
7006 : "alternative interface to GDAL and OGR command line "
7007 : "utilities.\nThe project reserves the right to modify, "
7008 : "rename, reorganize, and change the behavior of the utility\n"
7009 : "until it is officially frozen in a future feature release of "
7010 13 : "GDAL.\n";
7011 : }
7012 390 : return osRet;
7013 : }
7014 :
7015 : //! @endcond
7016 :
7017 : /************************************************************************/
7018 : /* GDALAlgorithm::GetUsageAsJSON() */
7019 : /************************************************************************/
7020 :
7021 591 : std::string GDALAlgorithm::GetUsageAsJSON() const
7022 : {
7023 1182 : CPLJSONDocument oDoc;
7024 1182 : auto oRoot = oDoc.GetRoot();
7025 :
7026 591 : if (m_displayInJSONUsage)
7027 : {
7028 589 : oRoot.Add("name", m_name);
7029 589 : CPLJSONArray jFullPath;
7030 1226 : for (const std::string &s : m_callPath)
7031 : {
7032 637 : jFullPath.Add(s);
7033 : }
7034 589 : oRoot.Add("full_path", jFullPath);
7035 : }
7036 :
7037 591 : oRoot.Add("description", m_description);
7038 591 : if (!m_helpURL.empty())
7039 : {
7040 588 : oRoot.Add("short_url", m_helpURL);
7041 588 : oRoot.Add("url", GetHelpFullURL());
7042 : }
7043 :
7044 1182 : CPLJSONArray jSubAlgorithms;
7045 800 : for (const auto &subAlgName : GetSubAlgorithmNames())
7046 : {
7047 418 : auto subAlg = InstantiateSubAlgorithm(subAlgName);
7048 209 : if (subAlg && subAlg->m_displayInJSONUsage && !subAlg->IsHidden())
7049 : {
7050 207 : CPLJSONDocument oSubDoc;
7051 207 : CPL_IGNORE_RET_VAL(oSubDoc.LoadMemory(subAlg->GetUsageAsJSON()));
7052 207 : jSubAlgorithms.Add(oSubDoc.GetRoot());
7053 : }
7054 : }
7055 591 : oRoot.Add("sub_algorithms", jSubAlgorithms);
7056 :
7057 591 : if (m_arbitraryLongNameArgsAllowed)
7058 : {
7059 1 : oRoot.Add("user_provided_arguments_allowed", true);
7060 : }
7061 :
7062 11212 : const auto ProcessArg = [this](const GDALAlgorithmArg *arg)
7063 : {
7064 5606 : CPLJSONObject jArg;
7065 5606 : jArg.Add("name", arg->GetName());
7066 5606 : jArg.Add("type", GDALAlgorithmArgTypeName(arg->GetType()));
7067 5606 : jArg.Add("description", arg->GetDescription());
7068 :
7069 5606 : const auto &metaVar = arg->GetMetaVar();
7070 5606 : if (!metaVar.empty() && metaVar != CPLString(arg->GetName()).toupper())
7071 : {
7072 1699 : if (metaVar.front() == '<' && metaVar.back() == '>' &&
7073 1699 : metaVar.substr(1, metaVar.size() - 2).find('>') ==
7074 : std::string::npos)
7075 32 : jArg.Add("metavar", metaVar.substr(1, metaVar.size() - 2));
7076 : else
7077 902 : jArg.Add("metavar", metaVar);
7078 : }
7079 :
7080 5606 : if (!arg->IsAvailableInPipelineStep())
7081 : {
7082 1659 : jArg.Add("available_in_pipeline_step", false);
7083 : }
7084 :
7085 5606 : const auto &choices = arg->GetChoices();
7086 5606 : if (!choices.empty())
7087 : {
7088 431 : CPLJSONArray jChoices;
7089 3647 : for (const auto &choice : choices)
7090 3216 : jChoices.Add(choice);
7091 431 : jArg.Add("choices", jChoices);
7092 : }
7093 5606 : if (arg->HasDefaultValue())
7094 : {
7095 1236 : switch (arg->GetType())
7096 : {
7097 436 : case GAAT_BOOLEAN:
7098 436 : jArg.Add("default", arg->GetDefault<bool>());
7099 436 : break;
7100 378 : case GAAT_STRING:
7101 378 : jArg.Add("default", arg->GetDefault<std::string>());
7102 378 : break;
7103 210 : case GAAT_INTEGER:
7104 210 : jArg.Add("default", arg->GetDefault<int>());
7105 210 : break;
7106 178 : case GAAT_REAL:
7107 178 : jArg.Add("default", arg->GetDefault<double>());
7108 178 : break;
7109 32 : case GAAT_STRING_LIST:
7110 : {
7111 : const auto &val =
7112 32 : arg->GetDefault<std::vector<std::string>>();
7113 32 : if (val.size() == 1)
7114 : {
7115 31 : jArg.Add("default", val[0]);
7116 : }
7117 : else
7118 : {
7119 1 : CPLJSONArray jArr;
7120 3 : for (const auto &s : val)
7121 : {
7122 2 : jArr.Add(s);
7123 : }
7124 1 : jArg.Add("default", jArr);
7125 : }
7126 32 : break;
7127 : }
7128 1 : case GAAT_INTEGER_LIST:
7129 : {
7130 1 : const auto &val = arg->GetDefault<std::vector<int>>();
7131 1 : if (val.size() == 1)
7132 : {
7133 0 : jArg.Add("default", val[0]);
7134 : }
7135 : else
7136 : {
7137 1 : CPLJSONArray jArr;
7138 3 : for (int i : val)
7139 : {
7140 2 : jArr.Add(i);
7141 : }
7142 1 : jArg.Add("default", jArr);
7143 : }
7144 1 : break;
7145 : }
7146 1 : case GAAT_REAL_LIST:
7147 : {
7148 1 : const auto &val = arg->GetDefault<std::vector<double>>();
7149 1 : if (val.size() == 1)
7150 : {
7151 0 : jArg.Add("default", val[0]);
7152 : }
7153 : else
7154 : {
7155 1 : CPLJSONArray jArr;
7156 3 : for (double d : val)
7157 : {
7158 2 : jArr.Add(d);
7159 : }
7160 1 : jArg.Add("default", jArr);
7161 : }
7162 1 : break;
7163 : }
7164 0 : case GAAT_DATASET:
7165 : case GAAT_DATASET_LIST:
7166 0 : CPLError(CE_Warning, CPLE_AppDefined,
7167 : "Unhandled default value for arg %s",
7168 0 : arg->GetName().c_str());
7169 0 : break;
7170 : }
7171 : }
7172 :
7173 5606 : const auto [minVal, minValIsIncluded] = arg->GetMinValue();
7174 5606 : if (!std::isnan(minVal))
7175 : {
7176 681 : if (arg->GetType() == GAAT_INTEGER ||
7177 261 : arg->GetType() == GAAT_INTEGER_LIST)
7178 183 : jArg.Add("min_value", static_cast<int>(minVal));
7179 : else
7180 237 : jArg.Add("min_value", minVal);
7181 420 : jArg.Add("min_value_is_included", minValIsIncluded);
7182 : }
7183 :
7184 5606 : const auto [maxVal, maxValIsIncluded] = arg->GetMaxValue();
7185 5606 : if (!std::isnan(maxVal))
7186 : {
7187 199 : if (arg->GetType() == GAAT_INTEGER ||
7188 82 : arg->GetType() == GAAT_INTEGER_LIST)
7189 35 : jArg.Add("max_value", static_cast<int>(maxVal));
7190 : else
7191 82 : jArg.Add("max_value", maxVal);
7192 117 : jArg.Add("max_value_is_included", maxValIsIncluded);
7193 : }
7194 :
7195 5606 : jArg.Add("required", arg->IsRequired());
7196 5606 : if (GDALAlgorithmArgTypeIsList(arg->GetType()))
7197 : {
7198 1586 : jArg.Add("packed_values_allowed", arg->GetPackedValuesAllowed());
7199 1586 : jArg.Add("repeated_arg_allowed", arg->GetRepeatedArgAllowed());
7200 1586 : jArg.Add("min_count", arg->GetMinCount());
7201 1586 : jArg.Add("max_count", arg->GetMaxCount());
7202 : }
7203 :
7204 : // Process dependencies
7205 5606 : const auto &mutualDependencyGroup = arg->GetMutualDependencyGroup();
7206 5606 : if (!mutualDependencyGroup.empty())
7207 : {
7208 32 : jArg.Add("mutual_dependency_group", mutualDependencyGroup);
7209 : }
7210 :
7211 11212 : CPLJSONArray jDependencies;
7212 50 : for (const auto &dependencyArgumentName :
7213 5706 : GetArgDependencies(arg->GetName()))
7214 : {
7215 50 : jDependencies.Add(dependencyArgumentName);
7216 : }
7217 :
7218 5606 : if (jDependencies.Size() > 0)
7219 : {
7220 50 : jArg.Add("depends_on", jDependencies);
7221 : }
7222 :
7223 5606 : jArg.Add("category", arg->GetCategory());
7224 :
7225 10954 : if (arg->GetType() == GAAT_DATASET ||
7226 5348 : arg->GetType() == GAAT_DATASET_LIST)
7227 : {
7228 : {
7229 472 : CPLJSONArray jAr;
7230 472 : if (arg->GetDatasetType() & GDAL_OF_RASTER)
7231 313 : jAr.Add("raster");
7232 472 : if (arg->GetDatasetType() & GDAL_OF_VECTOR)
7233 185 : jAr.Add("vector");
7234 472 : if (arg->GetDatasetType() & GDAL_OF_MULTIDIM_RASTER)
7235 41 : jAr.Add("multidim_raster");
7236 472 : jArg.Add("dataset_type", jAr);
7237 : }
7238 :
7239 643 : const auto GetFlags = [](int flags)
7240 : {
7241 643 : CPLJSONArray jAr;
7242 643 : if (flags & GADV_NAME)
7243 472 : jAr.Add("name");
7244 643 : if (flags & GADV_OBJECT)
7245 595 : jAr.Add("dataset");
7246 643 : return jAr;
7247 : };
7248 :
7249 472 : if (arg->IsInput())
7250 : {
7251 472 : jArg.Add("input_flags", GetFlags(arg->GetDatasetInputFlags()));
7252 : }
7253 472 : if (arg->IsOutput())
7254 : {
7255 171 : jArg.Add("output_flags",
7256 342 : GetFlags(arg->GetDatasetOutputFlags()));
7257 : }
7258 : }
7259 :
7260 5606 : const auto &mutualExclusionGroup = arg->GetMutualExclusionGroup();
7261 5606 : if (!mutualExclusionGroup.empty())
7262 : {
7263 712 : jArg.Add("mutual_exclusion_group", mutualExclusionGroup);
7264 : }
7265 :
7266 11212 : const auto &metadata = arg->GetMetadata();
7267 5606 : if (!metadata.empty())
7268 : {
7269 460 : CPLJSONObject jMetadata;
7270 959 : for (const auto &[key, values] : metadata)
7271 : {
7272 998 : CPLJSONArray jValue;
7273 1204 : for (const auto &value : values)
7274 705 : jValue.Add(value);
7275 499 : jMetadata.Add(key, jValue);
7276 : }
7277 460 : jArg.Add("metadata", jMetadata);
7278 : }
7279 :
7280 11212 : return jArg;
7281 591 : };
7282 :
7283 : {
7284 591 : CPLJSONArray jArgs;
7285 9235 : for (const auto &arg : m_args)
7286 : {
7287 8644 : if (!arg->IsHiddenForAPI() && arg->IsInput() && !arg->IsOutput())
7288 5370 : jArgs.Add(ProcessArg(arg.get()));
7289 : }
7290 591 : oRoot.Add("input_arguments", jArgs);
7291 : }
7292 :
7293 : {
7294 591 : CPLJSONArray jArgs;
7295 9235 : for (const auto &arg : m_args)
7296 : {
7297 8644 : if (!arg->IsHiddenForAPI() && !arg->IsInput() && arg->IsOutput())
7298 65 : jArgs.Add(ProcessArg(arg.get()));
7299 : }
7300 591 : oRoot.Add("output_arguments", jArgs);
7301 : }
7302 :
7303 : {
7304 591 : CPLJSONArray jArgs;
7305 9235 : for (const auto &arg : m_args)
7306 : {
7307 8644 : if (!arg->IsHiddenForAPI() && arg->IsInput() && arg->IsOutput())
7308 171 : jArgs.Add(ProcessArg(arg.get()));
7309 : }
7310 591 : oRoot.Add("input_output_arguments", jArgs);
7311 : }
7312 :
7313 591 : if (m_supportsStreamedOutput)
7314 : {
7315 127 : oRoot.Add("supports_streamed_output", true);
7316 : }
7317 :
7318 1182 : return oDoc.SaveAsString();
7319 : }
7320 :
7321 : /************************************************************************/
7322 : /* GDALAlgorithm::GetAutoComplete() */
7323 : /************************************************************************/
7324 :
7325 : std::vector<std::string>
7326 295 : GDALAlgorithm::GetAutoComplete(std::vector<std::string> &args,
7327 : bool lastWordIsComplete, bool showAllOptions)
7328 : {
7329 590 : std::vector<std::string> ret;
7330 :
7331 : // Get inner-most algorithm
7332 295 : std::unique_ptr<GDALAlgorithm> curAlgHolder;
7333 295 : GDALAlgorithm *curAlg = this;
7334 580 : while (!args.empty() && !args.front().empty() && args.front()[0] != '-')
7335 : {
7336 : auto subAlg = curAlg->InstantiateSubAlgorithm(
7337 431 : args.front(), /* suggestionAllowed = */ false);
7338 431 : if (!subAlg)
7339 145 : break;
7340 286 : if (args.size() == 1 && !lastWordIsComplete)
7341 : {
7342 5 : int nCount = 0;
7343 116 : for (const auto &subAlgName : curAlg->GetSubAlgorithmNames())
7344 : {
7345 111 : if (STARTS_WITH(subAlgName.c_str(), args.front().c_str()))
7346 6 : nCount++;
7347 : }
7348 5 : if (nCount >= 2)
7349 : {
7350 11 : for (const std::string &subAlgName :
7351 23 : curAlg->GetSubAlgorithmNames())
7352 : {
7353 11 : subAlg = curAlg->InstantiateSubAlgorithm(subAlgName);
7354 11 : if (subAlg && !subAlg->IsHidden())
7355 11 : ret.push_back(subAlg->GetName());
7356 : }
7357 1 : return ret;
7358 : }
7359 : }
7360 285 : showAllOptions = false;
7361 285 : args.erase(args.begin());
7362 285 : curAlgHolder = std::move(subAlg);
7363 285 : curAlg = curAlgHolder.get();
7364 : }
7365 294 : if (curAlg != this)
7366 : {
7367 155 : curAlg->m_calledFromCommandLine = m_calledFromCommandLine;
7368 : return curAlg->GetAutoComplete(args, lastWordIsComplete,
7369 155 : /* showAllOptions = */ false);
7370 : }
7371 :
7372 278 : std::string option;
7373 278 : std::string value;
7374 139 : ExtractLastOptionAndValue(args, option, value);
7375 :
7376 170 : if (option.empty() && !args.empty() && !args.back().empty() &&
7377 31 : args.back()[0] == '-')
7378 : {
7379 28 : const auto &lastArg = args.back();
7380 : // List available options
7381 405 : for (const auto &arg : GetArgs())
7382 : {
7383 701 : if (arg->IsHidden() || arg->IsHiddenForCLI() ||
7384 643 : (!showAllOptions &&
7385 876 : (arg->GetName() == "help" || arg->GetName() == "config" ||
7386 530 : arg->GetName() == "version" ||
7387 265 : arg->GetName() == "json-usage")))
7388 : {
7389 134 : continue;
7390 : }
7391 243 : if (!arg->GetShortName().empty())
7392 : {
7393 153 : std::string str = std::string("-").append(arg->GetShortName());
7394 51 : if (lastArg == str)
7395 0 : ret.push_back(std::move(str));
7396 : }
7397 243 : if (lastArg != "-" && lastArg != "--")
7398 : {
7399 54 : for (const std::string &alias : arg->GetAliases())
7400 : {
7401 48 : std::string str = std::string("--").append(alias);
7402 16 : if (cpl::starts_with(str, lastArg))
7403 3 : ret.push_back(std::move(str));
7404 : }
7405 : }
7406 243 : if (!arg->GetName().empty())
7407 : {
7408 729 : std::string str = std::string("--").append(arg->GetName());
7409 243 : if (cpl::starts_with(str, lastArg))
7410 207 : ret.push_back(std::move(str));
7411 : }
7412 : }
7413 28 : std::sort(ret.begin(), ret.end());
7414 : }
7415 111 : else if (!option.empty())
7416 : {
7417 : // List possible choices for current option
7418 104 : auto arg = GetArg(option);
7419 104 : if (arg && arg->GetType() != GAAT_BOOLEAN)
7420 : {
7421 104 : ret = arg->GetChoices();
7422 104 : if (ret.empty())
7423 : {
7424 : {
7425 99 : CPLErrorStateBackuper oErrorQuieter(CPLQuietErrorHandler);
7426 99 : SetParseForAutoCompletion();
7427 99 : CPL_IGNORE_RET_VAL(ParseCommandLineArguments(args));
7428 : }
7429 99 : ret = arg->GetAutoCompleteChoices(value);
7430 : }
7431 : else
7432 : {
7433 5 : std::sort(ret.begin(), ret.end());
7434 : }
7435 104 : if (!ret.empty() && ret.back() == value)
7436 : {
7437 2 : ret.clear();
7438 : }
7439 102 : else if (ret.empty())
7440 : {
7441 13 : ret.push_back("**");
7442 : // Non printable UTF-8 space, to avoid autocompletion to pickup on 'd'
7443 26 : ret.push_back(std::string("\xC2\xA0"
7444 : "description: ")
7445 13 : .append(arg->GetDescription()));
7446 : }
7447 : }
7448 : }
7449 : else
7450 : {
7451 : // List possible sub-algorithms
7452 69 : for (const std::string &subAlgName : GetSubAlgorithmNames())
7453 : {
7454 124 : auto subAlg = InstantiateSubAlgorithm(subAlgName);
7455 62 : if (subAlg && !subAlg->IsHidden())
7456 62 : ret.push_back(subAlg->GetName());
7457 : }
7458 7 : if (!ret.empty())
7459 : {
7460 3 : std::sort(ret.begin(), ret.end());
7461 : }
7462 :
7463 : // Try filenames
7464 7 : if (ret.empty() && !args.empty())
7465 : {
7466 : {
7467 3 : CPLErrorStateBackuper oErrorQuieter(CPLQuietErrorHandler);
7468 3 : SetParseForAutoCompletion();
7469 3 : CPL_IGNORE_RET_VAL(ParseCommandLineArguments(args));
7470 : }
7471 :
7472 3 : const std::string &lastArg = args.back();
7473 3 : GDALAlgorithmArg *arg = nullptr;
7474 18 : for (const char *name : {GDAL_ARG_NAME_INPUT, "dataset", "filename",
7475 21 : "like", "source", "destination"})
7476 : {
7477 18 : if (!arg)
7478 : {
7479 3 : auto newArg = GetArg(name);
7480 3 : if (newArg)
7481 : {
7482 3 : if (!newArg->IsExplicitlySet())
7483 : {
7484 0 : arg = newArg;
7485 : }
7486 6 : else if (newArg->GetType() == GAAT_STRING ||
7487 5 : newArg->GetType() == GAAT_STRING_LIST ||
7488 8 : newArg->GetType() == GAAT_DATASET ||
7489 2 : newArg->GetType() == GAAT_DATASET_LIST)
7490 : {
7491 : VSIStatBufL sStat;
7492 5 : if ((!lastArg.empty() && lastArg.back() == '/') ||
7493 2 : VSIStatL(lastArg.c_str(), &sStat) != 0)
7494 : {
7495 3 : arg = newArg;
7496 : }
7497 : }
7498 : }
7499 : }
7500 : }
7501 3 : if (arg)
7502 : {
7503 3 : ret = arg->GetAutoCompleteChoices(lastArg);
7504 : }
7505 : }
7506 : }
7507 :
7508 139 : return ret;
7509 : }
7510 :
7511 : /************************************************************************/
7512 : /* GDALAlgorithm::GetFieldIndices() */
7513 : /************************************************************************/
7514 :
7515 44 : bool GDALAlgorithm::GetFieldIndices(const std::vector<std::string> &names,
7516 : OGRLayerH hLayer, std::vector<int> &indices)
7517 : {
7518 44 : VALIDATE_POINTER1(hLayer, __func__, false);
7519 :
7520 44 : const OGRLayer &layer = *OGRLayer::FromHandle(hLayer);
7521 :
7522 44 : if (names.size() == 1 && names[0] == "ALL")
7523 : {
7524 12 : const int nSrcFieldCount = layer.GetLayerDefn()->GetFieldCount();
7525 28 : for (int i = 0; i < nSrcFieldCount; ++i)
7526 : {
7527 16 : indices.push_back(i);
7528 : }
7529 : }
7530 32 : else if (!names.empty() && !(names.size() == 1 && names[0] == "NONE"))
7531 : {
7532 6 : std::set<int> fieldsAdded;
7533 14 : for (const std::string &osFieldName : names)
7534 : {
7535 :
7536 : const int nIdx =
7537 10 : layer.GetLayerDefn()->GetFieldIndex(osFieldName.c_str());
7538 :
7539 10 : if (nIdx < 0)
7540 : {
7541 2 : CPLError(CE_Failure, CPLE_AppDefined,
7542 : "Field '%s' does not exist in layer '%s'",
7543 2 : osFieldName.c_str(), layer.GetName());
7544 2 : return false;
7545 : }
7546 :
7547 8 : if (fieldsAdded.insert(nIdx).second)
7548 : {
7549 7 : indices.push_back(nIdx);
7550 : }
7551 : }
7552 : }
7553 :
7554 42 : return true;
7555 : }
7556 :
7557 : /************************************************************************/
7558 : /* GDALAlgorithm::ExtractLastOptionAndValue() */
7559 : /************************************************************************/
7560 :
7561 139 : void GDALAlgorithm::ExtractLastOptionAndValue(std::vector<std::string> &args,
7562 : std::string &option,
7563 : std::string &value) const
7564 : {
7565 139 : if (!args.empty() && !args.back().empty() && args.back()[0] == '-')
7566 : {
7567 97 : const auto nPosEqual = args.back().find('=');
7568 97 : if (nPosEqual == std::string::npos)
7569 : {
7570 : // Deal with "gdal ... --option"
7571 78 : if (GetArg(args.back()))
7572 : {
7573 50 : option = args.back();
7574 50 : args.pop_back();
7575 : }
7576 : }
7577 : else
7578 : {
7579 : // Deal with "gdal ... --option=<value>"
7580 19 : if (GetArg(args.back().substr(0, nPosEqual)))
7581 : {
7582 19 : option = args.back().substr(0, nPosEqual);
7583 19 : value = args.back().substr(nPosEqual + 1);
7584 19 : args.pop_back();
7585 : }
7586 : }
7587 : }
7588 78 : else if (args.size() >= 2 && !args[args.size() - 2].empty() &&
7589 36 : args[args.size() - 2][0] == '-')
7590 : {
7591 : // Deal with "gdal ... --option <value>"
7592 35 : auto arg = GetArg(args[args.size() - 2]);
7593 35 : if (arg && arg->GetType() != GAAT_BOOLEAN)
7594 : {
7595 35 : option = args[args.size() - 2];
7596 35 : value = args.back();
7597 35 : args.pop_back();
7598 : }
7599 : }
7600 :
7601 139 : const auto IsKeyValueOption = [](const std::string &osStr)
7602 : {
7603 382 : return osStr == "--co" || osStr == "--creation-option" ||
7604 357 : osStr == "--lco" || osStr == "--layer-creation-option" ||
7605 380 : osStr == "--oo" || osStr == "--open-option";
7606 : };
7607 :
7608 139 : if (IsKeyValueOption(option))
7609 : {
7610 23 : const auto nPosEqual = value.find('=');
7611 23 : if (nPosEqual != std::string::npos)
7612 : {
7613 11 : value.resize(nPosEqual);
7614 : }
7615 : }
7616 139 : }
7617 :
7618 : /************************************************************************/
7619 : /* GDALAlgorithm::GetArgDependencies() */
7620 : /************************************************************************/
7621 :
7622 : std::vector<std::string>
7623 8129 : GDALAlgorithm::GetArgDependencies(const std::string &osName) const
7624 : {
7625 8129 : const auto arg = GetArg(osName, false);
7626 8129 : if (!arg)
7627 : {
7628 0 : ReportError(CE_Failure, CPLE_AppDefined, "Argument '%s' does not exist",
7629 : osName.c_str());
7630 0 : return {};
7631 : }
7632 16258 : std::vector<std::string> dependencies = arg->GetDirectDependencies();
7633 8129 : if (const auto &mutualDependencyGroup = arg->GetMutualDependencyGroup();
7634 8129 : !mutualDependencyGroup.empty())
7635 : {
7636 896 : for (const auto &otherArg : m_args)
7637 : {
7638 1627 : if (otherArg.get() == arg ||
7639 786 : mutualDependencyGroup.compare(
7640 786 : otherArg->GetMutualDependencyGroup()) != 0)
7641 783 : continue;
7642 58 : dependencies.push_back(otherArg->GetName());
7643 : }
7644 : }
7645 8129 : return dependencies;
7646 : }
7647 :
7648 : //! @cond Doxygen_Suppress
7649 :
7650 : /************************************************************************/
7651 : /* GDALContainerAlgorithm::RunImpl() */
7652 : /************************************************************************/
7653 :
7654 0 : bool GDALContainerAlgorithm::RunImpl(GDALProgressFunc, void *)
7655 : {
7656 0 : return false;
7657 : }
7658 :
7659 : //! @endcond
7660 :
7661 : /************************************************************************/
7662 : /* GDALAlgorithmRelease() */
7663 : /************************************************************************/
7664 :
7665 : /** Release a handle to an algorithm.
7666 : *
7667 : * @since 3.11
7668 : */
7669 13100 : void GDALAlgorithmRelease(GDALAlgorithmH hAlg)
7670 : {
7671 13100 : delete hAlg;
7672 13100 : }
7673 :
7674 : /************************************************************************/
7675 : /* GDALAlgorithmGetName() */
7676 : /************************************************************************/
7677 :
7678 : /** Return the algorithm name.
7679 : *
7680 : * @param hAlg Handle to an algorithm. Must NOT be null.
7681 : * @return algorithm name whose lifetime is bound to hAlg and which must not
7682 : * be freed.
7683 : * @since 3.11
7684 : */
7685 6073 : const char *GDALAlgorithmGetName(GDALAlgorithmH hAlg)
7686 : {
7687 6073 : VALIDATE_POINTER1(hAlg, __func__, nullptr);
7688 6073 : return hAlg->ptr->GetName().c_str();
7689 : }
7690 :
7691 : /************************************************************************/
7692 : /* GDALAlgorithmGetDescription() */
7693 : /************************************************************************/
7694 :
7695 : /** Return the algorithm (short) description.
7696 : *
7697 : * @param hAlg Handle to an algorithm. Must NOT be null.
7698 : * @return algorithm description whose lifetime is bound to hAlg and which must
7699 : * not be freed.
7700 : * @since 3.11
7701 : */
7702 5988 : const char *GDALAlgorithmGetDescription(GDALAlgorithmH hAlg)
7703 : {
7704 5988 : VALIDATE_POINTER1(hAlg, __func__, nullptr);
7705 5988 : return hAlg->ptr->GetDescription().c_str();
7706 : }
7707 :
7708 : /************************************************************************/
7709 : /* GDALAlgorithmGetLongDescription() */
7710 : /************************************************************************/
7711 :
7712 : /** Return the algorithm (longer) description.
7713 : *
7714 : * @param hAlg Handle to an algorithm. Must NOT be null.
7715 : * @return algorithm description whose lifetime is bound to hAlg and which must
7716 : * not be freed.
7717 : * @since 3.11
7718 : */
7719 2 : const char *GDALAlgorithmGetLongDescription(GDALAlgorithmH hAlg)
7720 : {
7721 2 : VALIDATE_POINTER1(hAlg, __func__, nullptr);
7722 2 : return hAlg->ptr->GetLongDescription().c_str();
7723 : }
7724 :
7725 : /************************************************************************/
7726 : /* GDALAlgorithmGetHelpFullURL() */
7727 : /************************************************************************/
7728 :
7729 : /** Return the algorithm full URL.
7730 : *
7731 : * @param hAlg Handle to an algorithm. Must NOT be null.
7732 : * @return algorithm URL whose lifetime is bound to hAlg and which must
7733 : * not be freed.
7734 : * @since 3.11
7735 : */
7736 5250 : const char *GDALAlgorithmGetHelpFullURL(GDALAlgorithmH hAlg)
7737 : {
7738 5250 : VALIDATE_POINTER1(hAlg, __func__, nullptr);
7739 5250 : return hAlg->ptr->GetHelpFullURL().c_str();
7740 : }
7741 :
7742 : /************************************************************************/
7743 : /* GDALAlgorithmHasSubAlgorithms() */
7744 : /************************************************************************/
7745 :
7746 : /** Return whether the algorithm has sub-algorithms.
7747 : *
7748 : * @param hAlg Handle to an algorithm. Must NOT be null.
7749 : * @since 3.11
7750 : */
7751 9666 : bool GDALAlgorithmHasSubAlgorithms(GDALAlgorithmH hAlg)
7752 : {
7753 9666 : VALIDATE_POINTER1(hAlg, __func__, false);
7754 9666 : return hAlg->ptr->HasSubAlgorithms();
7755 : }
7756 :
7757 : /************************************************************************/
7758 : /* GDALAlgorithmGetSubAlgorithmNames() */
7759 : /************************************************************************/
7760 :
7761 : /** Get the names of registered algorithms.
7762 : *
7763 : * @param hAlg Handle to an algorithm. Must NOT be null.
7764 : * @return a NULL terminated list of names, which must be destroyed with
7765 : * CSLDestroy()
7766 : * @since 3.11
7767 : */
7768 791 : char **GDALAlgorithmGetSubAlgorithmNames(GDALAlgorithmH hAlg)
7769 : {
7770 791 : VALIDATE_POINTER1(hAlg, __func__, nullptr);
7771 791 : return CPLStringList(hAlg->ptr->GetSubAlgorithmNames()).StealList();
7772 : }
7773 :
7774 : /************************************************************************/
7775 : /* GDALAlgorithmInstantiateSubAlgorithm() */
7776 : /************************************************************************/
7777 :
7778 : /** Instantiate an algorithm by its name (or its alias).
7779 : *
7780 : * @param hAlg Handle to an algorithm. Must NOT be null.
7781 : * @param pszSubAlgName Algorithm name. Must NOT be null.
7782 : * @return an handle to the algorithm (to be freed with GDALAlgorithmRelease),
7783 : * or NULL if the algorithm does not exist or another error occurred.
7784 : * @since 3.11
7785 : */
7786 9115 : GDALAlgorithmH GDALAlgorithmInstantiateSubAlgorithm(GDALAlgorithmH hAlg,
7787 : const char *pszSubAlgName)
7788 : {
7789 9115 : VALIDATE_POINTER1(hAlg, __func__, nullptr);
7790 9115 : VALIDATE_POINTER1(pszSubAlgName, __func__, nullptr);
7791 18230 : auto subAlg = hAlg->ptr->InstantiateSubAlgorithm(pszSubAlgName);
7792 : return subAlg
7793 18230 : ? std::make_unique<GDALAlgorithmHS>(std::move(subAlg)).release()
7794 18230 : : nullptr;
7795 : }
7796 :
7797 : /************************************************************************/
7798 : /* GDALAlgorithmParseCommandLineArguments() */
7799 : /************************************************************************/
7800 :
7801 : /** Parse a command line argument, which does not include the algorithm
7802 : * name, to set the value of corresponding arguments.
7803 : *
7804 : * @param hAlg Handle to an algorithm. Must NOT be null.
7805 : * @param papszArgs NULL-terminated list of arguments, not including the algorithm name.
7806 : * @return true if successful, false otherwise
7807 : * @since 3.11
7808 : */
7809 :
7810 356 : bool GDALAlgorithmParseCommandLineArguments(GDALAlgorithmH hAlg,
7811 : CSLConstList papszArgs)
7812 : {
7813 356 : VALIDATE_POINTER1(hAlg, __func__, false);
7814 356 : return hAlg->ptr->ParseCommandLineArguments(CPLStringList(papszArgs));
7815 : }
7816 :
7817 : /************************************************************************/
7818 : /* GDALAlgorithmGetActualAlgorithm() */
7819 : /************************************************************************/
7820 :
7821 : /** Return the actual algorithm that is going to be invoked, when the
7822 : * current algorithm has sub-algorithms.
7823 : *
7824 : * Only valid after GDALAlgorithmParseCommandLineArguments() has been called.
7825 : *
7826 : * Note that the lifetime of the returned algorithm does not exceed the one of
7827 : * the hAlg instance that owns it.
7828 : *
7829 : * @param hAlg Handle to an algorithm. Must NOT be null.
7830 : * @return an handle to the algorithm (to be freed with GDALAlgorithmRelease).
7831 : * @since 3.11
7832 : */
7833 929 : GDALAlgorithmH GDALAlgorithmGetActualAlgorithm(GDALAlgorithmH hAlg)
7834 : {
7835 929 : VALIDATE_POINTER1(hAlg, __func__, nullptr);
7836 929 : return GDALAlgorithmHS::FromRef(hAlg->ptr->GetActualAlgorithm()).release();
7837 : }
7838 :
7839 : /************************************************************************/
7840 : /* GDALAlgorithmRun() */
7841 : /************************************************************************/
7842 :
7843 : /** Execute the algorithm, starting with ValidateArguments() and then
7844 : * calling RunImpl().
7845 : *
7846 : * This function must be called at most once per instance.
7847 : *
7848 : * @param hAlg Handle to an algorithm. Must NOT be null.
7849 : * @param pfnProgress Progress callback. May be null.
7850 : * @param pProgressData Progress callback user data. May be null.
7851 : * @return true if successful, false otherwise
7852 : * @since 3.11
7853 : */
7854 :
7855 2882 : bool GDALAlgorithmRun(GDALAlgorithmH hAlg, GDALProgressFunc pfnProgress,
7856 : void *pProgressData)
7857 : {
7858 2882 : VALIDATE_POINTER1(hAlg, __func__, false);
7859 2882 : return hAlg->ptr->Run(pfnProgress, pProgressData);
7860 : }
7861 :
7862 : /************************************************************************/
7863 : /* GDALAlgorithmFinalize() */
7864 : /************************************************************************/
7865 :
7866 : /** Complete any pending actions, and return the final status.
7867 : * This is typically useful for algorithm that generate an output dataset.
7868 : *
7869 : * Note that this function does *NOT* release memory associated with the
7870 : * algorithm. GDALAlgorithmRelease() must still be called afterwards.
7871 : *
7872 : * @param hAlg Handle to an algorithm. Must NOT be null.
7873 : * @return true if successful, false otherwise
7874 : * @since 3.11
7875 : */
7876 :
7877 970 : bool GDALAlgorithmFinalize(GDALAlgorithmH hAlg)
7878 : {
7879 970 : VALIDATE_POINTER1(hAlg, __func__, false);
7880 970 : return hAlg->ptr->Finalize();
7881 : }
7882 :
7883 : /************************************************************************/
7884 : /* GDALAlgorithmGetUsageAsJSON() */
7885 : /************************************************************************/
7886 :
7887 : /** Return the usage of the algorithm as a JSON-serialized string.
7888 : *
7889 : * This can be used to dynamically generate interfaces to algorithms.
7890 : *
7891 : * @param hAlg Handle to an algorithm. Must NOT be null.
7892 : * @return a string that must be freed with CPLFree()
7893 : * @since 3.11
7894 : */
7895 6 : char *GDALAlgorithmGetUsageAsJSON(GDALAlgorithmH hAlg)
7896 : {
7897 6 : VALIDATE_POINTER1(hAlg, __func__, nullptr);
7898 6 : return CPLStrdup(hAlg->ptr->GetUsageAsJSON().c_str());
7899 : }
7900 :
7901 : /************************************************************************/
7902 : /* GDALAlgorithmGetArgNames() */
7903 : /************************************************************************/
7904 :
7905 : /** Return the list of available argument names.
7906 : *
7907 : * @param hAlg Handle to an algorithm. Must NOT be null.
7908 : * @return a NULL terminated list of names, which must be destroyed with
7909 : * CSLDestroy()
7910 : * @since 3.11
7911 : */
7912 16351 : char **GDALAlgorithmGetArgNames(GDALAlgorithmH hAlg)
7913 : {
7914 16351 : VALIDATE_POINTER1(hAlg, __func__, nullptr);
7915 32702 : CPLStringList list;
7916 358284 : for (const auto &arg : hAlg->ptr->GetArgs())
7917 341933 : list.AddString(arg->GetName().c_str());
7918 16351 : return list.StealList();
7919 : }
7920 :
7921 : /************************************************************************/
7922 : /* GDALAlgorithmGetArg() */
7923 : /************************************************************************/
7924 :
7925 : /** Return an argument from its name.
7926 : *
7927 : * The lifetime of the returned object does not exceed the one of hAlg.
7928 : *
7929 : * @param hAlg Handle to an algorithm. Must NOT be null.
7930 : * @param pszArgName Argument name. Must NOT be null.
7931 : * @return an argument that must be released with GDALAlgorithmArgRelease(),
7932 : * or nullptr in case of error
7933 : * @since 3.11
7934 : */
7935 342858 : GDALAlgorithmArgH GDALAlgorithmGetArg(GDALAlgorithmH hAlg,
7936 : const char *pszArgName)
7937 : {
7938 342858 : VALIDATE_POINTER1(hAlg, __func__, nullptr);
7939 342858 : VALIDATE_POINTER1(pszArgName, __func__, nullptr);
7940 685716 : auto arg = hAlg->ptr->GetArg(pszArgName, /* suggestionAllowed = */ true,
7941 342858 : /* isConst = */ true);
7942 342858 : if (!arg)
7943 3 : return nullptr;
7944 342855 : return std::make_unique<GDALAlgorithmArgHS>(arg).release();
7945 : }
7946 :
7947 : /************************************************************************/
7948 : /* GDALAlgorithmGetArgNonConst() */
7949 : /************************************************************************/
7950 :
7951 : /** Return an argument from its name, possibly allowing creation of user-provided
7952 : * argument if the algorithm allow it.
7953 : *
7954 : * The lifetime of the returned object does not exceed the one of hAlg.
7955 : *
7956 : * @param hAlg Handle to an algorithm. Must NOT be null.
7957 : * @param pszArgName Argument name. Must NOT be null.
7958 : * @return an argument that must be released with GDALAlgorithmArgRelease(),
7959 : * or nullptr in case of error
7960 : * @since 3.12
7961 : */
7962 10102 : GDALAlgorithmArgH GDALAlgorithmGetArgNonConst(GDALAlgorithmH hAlg,
7963 : const char *pszArgName)
7964 : {
7965 10102 : VALIDATE_POINTER1(hAlg, __func__, nullptr);
7966 10102 : VALIDATE_POINTER1(pszArgName, __func__, nullptr);
7967 20204 : auto arg = hAlg->ptr->GetArg(pszArgName, /* suggestionAllowed = */ true,
7968 10102 : /* isConst = */ false);
7969 10102 : if (!arg)
7970 2 : return nullptr;
7971 10100 : return std::make_unique<GDALAlgorithmArgHS>(arg).release();
7972 : }
7973 :
7974 : /************************************************************************/
7975 : /* GDALAlgorithmGetArgDependencies() */
7976 : /************************************************************************/
7977 :
7978 : /** Return the list of argument names the specified argument depends on.
7979 : *
7980 : * This includes both regular dependencies and mutual dependencies.
7981 : *
7982 : * @param hAlg Handle to an algorithm. Must NOT be null.
7983 : * @param pszArgName Argument name. Must NOT be null.
7984 : * @return a NULL terminated list of names, which must be destroyed with
7985 : * CSLDestroy()
7986 : * @since 3.11
7987 : */
7988 7 : char **GDALAlgorithmGetArgDependencies(GDALAlgorithmH hAlg,
7989 : const char *pszArgName)
7990 : {
7991 7 : VALIDATE_POINTER1(hAlg, __func__, nullptr);
7992 7 : VALIDATE_POINTER1(pszArgName, __func__, nullptr);
7993 7 : return CPLStringList(hAlg->ptr->GetArgDependencies(pszArgName)).StealList();
7994 : }
7995 :
7996 : /************************************************************************/
7997 : /* GDALAlgorithmArgRelease() */
7998 : /************************************************************************/
7999 :
8000 : /** Release a handle to an argument.
8001 : *
8002 : * @since 3.11
8003 : */
8004 352955 : void GDALAlgorithmArgRelease(GDALAlgorithmArgH hArg)
8005 : {
8006 352955 : delete hArg;
8007 352955 : }
8008 :
8009 : /************************************************************************/
8010 : /* GDALAlgorithmArgGetName() */
8011 : /************************************************************************/
8012 :
8013 : /** Return the name of an argument.
8014 : *
8015 : * @param hArg Handle to an argument. Must NOT be null.
8016 : * @return argument name whose lifetime is bound to hArg and which must not
8017 : * be freed.
8018 : * @since 3.11
8019 : */
8020 19561 : const char *GDALAlgorithmArgGetName(GDALAlgorithmArgH hArg)
8021 : {
8022 19561 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8023 19561 : return hArg->ptr->GetName().c_str();
8024 : }
8025 :
8026 : /************************************************************************/
8027 : /* GDALAlgorithmArgGetType() */
8028 : /************************************************************************/
8029 :
8030 : /** Get the type of an argument
8031 : *
8032 : * @param hArg Handle to an argument. Must NOT be null.
8033 : * @since 3.11
8034 : */
8035 435506 : GDALAlgorithmArgType GDALAlgorithmArgGetType(GDALAlgorithmArgH hArg)
8036 : {
8037 435506 : VALIDATE_POINTER1(hArg, __func__, GAAT_STRING);
8038 435506 : return hArg->ptr->GetType();
8039 : }
8040 :
8041 : /************************************************************************/
8042 : /* GDALAlgorithmArgGetDescription() */
8043 : /************************************************************************/
8044 :
8045 : /** Return the description of an argument.
8046 : *
8047 : * @param hArg Handle to an argument. Must NOT be null.
8048 : * @return argument description whose lifetime is bound to hArg and which must not
8049 : * be freed.
8050 : * @since 3.11
8051 : */
8052 86470 : const char *GDALAlgorithmArgGetDescription(GDALAlgorithmArgH hArg)
8053 : {
8054 86470 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8055 86470 : return hArg->ptr->GetDescription().c_str();
8056 : }
8057 :
8058 : /************************************************************************/
8059 : /* GDALAlgorithmArgGetShortName() */
8060 : /************************************************************************/
8061 :
8062 : /** Return the short name, or empty string if there is none
8063 : *
8064 : * @param hArg Handle to an argument. Must NOT be null.
8065 : * @return short name whose lifetime is bound to hArg and which must not
8066 : * be freed.
8067 : * @since 3.11
8068 : */
8069 1 : const char *GDALAlgorithmArgGetShortName(GDALAlgorithmArgH hArg)
8070 : {
8071 1 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8072 1 : return hArg->ptr->GetShortName().c_str();
8073 : }
8074 :
8075 : /************************************************************************/
8076 : /* GDALAlgorithmArgGetAliases() */
8077 : /************************************************************************/
8078 :
8079 : /** Return the aliases (potentially none)
8080 : *
8081 : * @param hArg Handle to an argument. Must NOT be null.
8082 : * @return a NULL terminated list of names, which must be destroyed with
8083 : * CSLDestroy()
8084 :
8085 : * @since 3.11
8086 : */
8087 163263 : char **GDALAlgorithmArgGetAliases(GDALAlgorithmArgH hArg)
8088 : {
8089 163263 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8090 163263 : return CPLStringList(hArg->ptr->GetAliases()).StealList();
8091 : }
8092 :
8093 : /************************************************************************/
8094 : /* GDALAlgorithmArgGetMetaVar() */
8095 : /************************************************************************/
8096 :
8097 : /** Return the "meta-var" hint.
8098 : *
8099 : * By default, the meta-var value is the long name of the argument in
8100 : * upper case.
8101 : *
8102 : * @param hArg Handle to an argument. Must NOT be null.
8103 : * @return meta-var hint whose lifetime is bound to hArg and which must not
8104 : * be freed.
8105 : * @since 3.11
8106 : */
8107 1 : const char *GDALAlgorithmArgGetMetaVar(GDALAlgorithmArgH hArg)
8108 : {
8109 1 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8110 1 : return hArg->ptr->GetMetaVar().c_str();
8111 : }
8112 :
8113 : /************************************************************************/
8114 : /* GDALAlgorithmArgGetCategory() */
8115 : /************************************************************************/
8116 :
8117 : /** Return the argument category
8118 : *
8119 : * GAAC_COMMON, GAAC_BASE, GAAC_ADVANCED, GAAC_ESOTERIC or a custom category.
8120 : *
8121 : * @param hArg Handle to an argument. Must NOT be null.
8122 : * @return category whose lifetime is bound to hArg and which must not
8123 : * be freed.
8124 : * @since 3.11
8125 : */
8126 1 : const char *GDALAlgorithmArgGetCategory(GDALAlgorithmArgH hArg)
8127 : {
8128 1 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8129 1 : return hArg->ptr->GetCategory().c_str();
8130 : }
8131 :
8132 : /************************************************************************/
8133 : /* GDALAlgorithmArgIsPositional() */
8134 : /************************************************************************/
8135 :
8136 : /** Return if the argument is a positional one.
8137 : *
8138 : * @param hArg Handle to an argument. Must NOT be null.
8139 : * @since 3.11
8140 : */
8141 1 : bool GDALAlgorithmArgIsPositional(GDALAlgorithmArgH hArg)
8142 : {
8143 1 : VALIDATE_POINTER1(hArg, __func__, false);
8144 1 : return hArg->ptr->IsPositional();
8145 : }
8146 :
8147 : /************************************************************************/
8148 : /* GDALAlgorithmArgIsRequired() */
8149 : /************************************************************************/
8150 :
8151 : /** Return whether the argument is required. Defaults to false.
8152 : *
8153 : * @param hArg Handle to an argument. Must NOT be null.
8154 : * @since 3.11
8155 : */
8156 163263 : bool GDALAlgorithmArgIsRequired(GDALAlgorithmArgH hArg)
8157 : {
8158 163263 : VALIDATE_POINTER1(hArg, __func__, false);
8159 163263 : return hArg->ptr->IsRequired();
8160 : }
8161 :
8162 : /************************************************************************/
8163 : /* GDALAlgorithmArgGetMinCount() */
8164 : /************************************************************************/
8165 :
8166 : /** Return the minimum number of values for the argument.
8167 : *
8168 : * Defaults to 0.
8169 : * Only applies to list type of arguments.
8170 : *
8171 : * @param hArg Handle to an argument. Must NOT be null.
8172 : * @since 3.11
8173 : */
8174 1 : int GDALAlgorithmArgGetMinCount(GDALAlgorithmArgH hArg)
8175 : {
8176 1 : VALIDATE_POINTER1(hArg, __func__, 0);
8177 1 : return hArg->ptr->GetMinCount();
8178 : }
8179 :
8180 : /************************************************************************/
8181 : /* GDALAlgorithmArgGetMaxCount() */
8182 : /************************************************************************/
8183 :
8184 : /** Return the maximum number of values for the argument.
8185 : *
8186 : * Defaults to 1 for scalar types, and INT_MAX for list types.
8187 : * Only applies to list type of arguments.
8188 : *
8189 : * @param hArg Handle to an argument. Must NOT be null.
8190 : * @since 3.11
8191 : */
8192 1 : int GDALAlgorithmArgGetMaxCount(GDALAlgorithmArgH hArg)
8193 : {
8194 1 : VALIDATE_POINTER1(hArg, __func__, 0);
8195 1 : return hArg->ptr->GetMaxCount();
8196 : }
8197 :
8198 : /************************************************************************/
8199 : /* GDALAlgorithmArgGetPackedValuesAllowed() */
8200 : /************************************************************************/
8201 :
8202 : /** Return whether, for list type of arguments, several values, space
8203 : * separated, may be specified. That is "--foo=bar,baz".
8204 : * The default is true.
8205 : *
8206 : * @param hArg Handle to an argument. Must NOT be null.
8207 : * @since 3.11
8208 : */
8209 1 : bool GDALAlgorithmArgGetPackedValuesAllowed(GDALAlgorithmArgH hArg)
8210 : {
8211 1 : VALIDATE_POINTER1(hArg, __func__, false);
8212 1 : return hArg->ptr->GetPackedValuesAllowed();
8213 : }
8214 :
8215 : /************************************************************************/
8216 : /* GDALAlgorithmArgGetRepeatedArgAllowed() */
8217 : /************************************************************************/
8218 :
8219 : /** Return whether, for list type of arguments, the argument may be
8220 : * repeated. That is "--foo=bar --foo=baz".
8221 : * The default is true.
8222 : *
8223 : * @param hArg Handle to an argument. Must NOT be null.
8224 : * @since 3.11
8225 : */
8226 1 : bool GDALAlgorithmArgGetRepeatedArgAllowed(GDALAlgorithmArgH hArg)
8227 : {
8228 1 : VALIDATE_POINTER1(hArg, __func__, false);
8229 1 : return hArg->ptr->GetRepeatedArgAllowed();
8230 : }
8231 :
8232 : /************************************************************************/
8233 : /* GDALAlgorithmArgGetChoices() */
8234 : /************************************************************************/
8235 :
8236 : /** Return the allowed values (as strings) for the argument.
8237 : *
8238 : * Only honored for GAAT_STRING and GAAT_STRING_LIST types.
8239 : *
8240 : * @param hArg Handle to an argument. Must NOT be null.
8241 : * @return a NULL terminated list of names, which must be destroyed with
8242 : * CSLDestroy()
8243 :
8244 : * @since 3.11
8245 : */
8246 1 : char **GDALAlgorithmArgGetChoices(GDALAlgorithmArgH hArg)
8247 : {
8248 1 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8249 1 : return CPLStringList(hArg->ptr->GetChoices()).StealList();
8250 : }
8251 :
8252 : /************************************************************************/
8253 : /* GDALAlgorithmArgGetMetadataItem() */
8254 : /************************************************************************/
8255 :
8256 : /** Return the values of the metadata item of an argument.
8257 : *
8258 : * @param hArg Handle to an argument. Must NOT be null.
8259 : * @param pszItem Name of the item. Must NOT be null.
8260 : * @return a NULL terminated list of values, which must be destroyed with
8261 : * CSLDestroy()
8262 :
8263 : * @since 3.11
8264 : */
8265 63 : char **GDALAlgorithmArgGetMetadataItem(GDALAlgorithmArgH hArg,
8266 : const char *pszItem)
8267 : {
8268 63 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8269 63 : VALIDATE_POINTER1(pszItem, __func__, nullptr);
8270 63 : const auto pVecOfStrings = hArg->ptr->GetMetadataItem(pszItem);
8271 63 : return pVecOfStrings ? CPLStringList(*pVecOfStrings).StealList() : nullptr;
8272 : }
8273 :
8274 : /************************************************************************/
8275 : /* GDALAlgorithmArgIsExplicitlySet() */
8276 : /************************************************************************/
8277 :
8278 : /** Return whether the argument value has been explicitly set with Set()
8279 : *
8280 : * @param hArg Handle to an argument. Must NOT be null.
8281 : * @since 3.11
8282 : */
8283 723 : bool GDALAlgorithmArgIsExplicitlySet(GDALAlgorithmArgH hArg)
8284 : {
8285 723 : VALIDATE_POINTER1(hArg, __func__, false);
8286 723 : return hArg->ptr->IsExplicitlySet();
8287 : }
8288 :
8289 : /************************************************************************/
8290 : /* GDALAlgorithmArgHasDefaultValue() */
8291 : /************************************************************************/
8292 :
8293 : /** Return if the argument has a declared default value.
8294 : *
8295 : * @param hArg Handle to an argument. Must NOT be null.
8296 : * @since 3.11
8297 : */
8298 2 : bool GDALAlgorithmArgHasDefaultValue(GDALAlgorithmArgH hArg)
8299 : {
8300 2 : VALIDATE_POINTER1(hArg, __func__, false);
8301 2 : return hArg->ptr->HasDefaultValue();
8302 : }
8303 :
8304 : /************************************************************************/
8305 : /* GDALAlgorithmArgGetDefaultAsBoolean() */
8306 : /************************************************************************/
8307 :
8308 : /** Return the argument default value as a integer.
8309 : *
8310 : * Must only be called on arguments whose type is GAAT_BOOLEAN
8311 : *
8312 : * GDALAlgorithmArgHasDefaultValue() must be called to determine if the
8313 : * argument has a default value.
8314 : *
8315 : * @param hArg Handle to an argument. Must NOT be null.
8316 : * @since 3.12
8317 : */
8318 3 : bool GDALAlgorithmArgGetDefaultAsBoolean(GDALAlgorithmArgH hArg)
8319 : {
8320 3 : VALIDATE_POINTER1(hArg, __func__, false);
8321 3 : if (hArg->ptr->GetType() != GAAT_BOOLEAN)
8322 : {
8323 1 : CPLError(CE_Failure, CPLE_AppDefined,
8324 : "%s must only be called on arguments of type GAAT_BOOLEAN",
8325 : __func__);
8326 1 : return false;
8327 : }
8328 2 : return hArg->ptr->GetDefault<bool>();
8329 : }
8330 :
8331 : /************************************************************************/
8332 : /* GDALAlgorithmArgGetDefaultAsString() */
8333 : /************************************************************************/
8334 :
8335 : /** Return the argument default value as a string.
8336 : *
8337 : * Must only be called on arguments whose type is GAAT_STRING.
8338 : *
8339 : * GDALAlgorithmArgHasDefaultValue() must be called to determine if the
8340 : * argument has a default value.
8341 : *
8342 : * @param hArg Handle to an argument. Must NOT be null.
8343 : * @return string whose lifetime is bound to hArg and which must not
8344 : * be freed.
8345 : * @since 3.11
8346 : */
8347 3 : const char *GDALAlgorithmArgGetDefaultAsString(GDALAlgorithmArgH hArg)
8348 : {
8349 3 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8350 3 : if (hArg->ptr->GetType() != GAAT_STRING)
8351 : {
8352 2 : CPLError(CE_Failure, CPLE_AppDefined,
8353 : "%s must only be called on arguments of type GAAT_STRING",
8354 : __func__);
8355 2 : return nullptr;
8356 : }
8357 1 : return hArg->ptr->GetDefault<std::string>().c_str();
8358 : }
8359 :
8360 : /************************************************************************/
8361 : /* GDALAlgorithmArgGetDefaultAsInteger() */
8362 : /************************************************************************/
8363 :
8364 : /** Return the argument default value as a integer.
8365 : *
8366 : * Must only be called on arguments whose type is GAAT_INTEGER
8367 : *
8368 : * GDALAlgorithmArgHasDefaultValue() must be called to determine if the
8369 : * argument has a default value.
8370 : *
8371 : * @param hArg Handle to an argument. Must NOT be null.
8372 : * @since 3.12
8373 : */
8374 3 : int GDALAlgorithmArgGetDefaultAsInteger(GDALAlgorithmArgH hArg)
8375 : {
8376 3 : VALIDATE_POINTER1(hArg, __func__, 0);
8377 3 : if (hArg->ptr->GetType() != GAAT_INTEGER)
8378 : {
8379 2 : CPLError(CE_Failure, CPLE_AppDefined,
8380 : "%s must only be called on arguments of type GAAT_INTEGER",
8381 : __func__);
8382 2 : return 0;
8383 : }
8384 1 : return hArg->ptr->GetDefault<int>();
8385 : }
8386 :
8387 : /************************************************************************/
8388 : /* GDALAlgorithmArgGetDefaultAsDouble() */
8389 : /************************************************************************/
8390 :
8391 : /** Return the argument default value as a double.
8392 : *
8393 : * Must only be called on arguments whose type is GAAT_REAL
8394 : *
8395 : * GDALAlgorithmArgHasDefaultValue() must be called to determine if the
8396 : * argument has a default value.
8397 : *
8398 : * @param hArg Handle to an argument. Must NOT be null.
8399 : * @since 3.12
8400 : */
8401 3 : double GDALAlgorithmArgGetDefaultAsDouble(GDALAlgorithmArgH hArg)
8402 : {
8403 3 : VALIDATE_POINTER1(hArg, __func__, 0);
8404 3 : if (hArg->ptr->GetType() != GAAT_REAL)
8405 : {
8406 2 : CPLError(CE_Failure, CPLE_AppDefined,
8407 : "%s must only be called on arguments of type GAAT_REAL",
8408 : __func__);
8409 2 : return 0;
8410 : }
8411 1 : return hArg->ptr->GetDefault<double>();
8412 : }
8413 :
8414 : /************************************************************************/
8415 : /* GDALAlgorithmArgGetDefaultAsStringList() */
8416 : /************************************************************************/
8417 :
8418 : /** Return the argument default value as a string list.
8419 : *
8420 : * Must only be called on arguments whose type is GAAT_STRING_LIST.
8421 : *
8422 : * GDALAlgorithmArgHasDefaultValue() must be called to determine if the
8423 : * argument has a default value.
8424 : *
8425 : * @param hArg Handle to an argument. Must NOT be null.
8426 : * @return a NULL terminated list of names, which must be destroyed with
8427 : * CSLDestroy()
8428 :
8429 : * @since 3.12
8430 : */
8431 3 : char **GDALAlgorithmArgGetDefaultAsStringList(GDALAlgorithmArgH hArg)
8432 : {
8433 3 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8434 3 : if (hArg->ptr->GetType() != GAAT_STRING_LIST)
8435 : {
8436 2 : CPLError(CE_Failure, CPLE_AppDefined,
8437 : "%s must only be called on arguments of type GAAT_STRING_LIST",
8438 : __func__);
8439 2 : return nullptr;
8440 : }
8441 2 : return CPLStringList(hArg->ptr->GetDefault<std::vector<std::string>>())
8442 1 : .StealList();
8443 : }
8444 :
8445 : /************************************************************************/
8446 : /* GDALAlgorithmArgGetDefaultAsIntegerList() */
8447 : /************************************************************************/
8448 :
8449 : /** Return the argument default value as a integer list.
8450 : *
8451 : * Must only be called on arguments whose type is GAAT_INTEGER_LIST.
8452 : *
8453 : * GDALAlgorithmArgHasDefaultValue() must be called to determine if the
8454 : * argument has a default value.
8455 : *
8456 : * @param hArg Handle to an argument. Must NOT be null.
8457 : * @param[out] pnCount Pointer to the number of values in the list. Must NOT be null.
8458 : * @since 3.12
8459 : */
8460 3 : const int *GDALAlgorithmArgGetDefaultAsIntegerList(GDALAlgorithmArgH hArg,
8461 : size_t *pnCount)
8462 : {
8463 3 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8464 3 : VALIDATE_POINTER1(pnCount, __func__, nullptr);
8465 3 : if (hArg->ptr->GetType() != GAAT_INTEGER_LIST)
8466 : {
8467 2 : CPLError(
8468 : CE_Failure, CPLE_AppDefined,
8469 : "%s must only be called on arguments of type GAAT_INTEGER_LIST",
8470 : __func__);
8471 2 : *pnCount = 0;
8472 2 : return nullptr;
8473 : }
8474 1 : const auto &val = hArg->ptr->GetDefault<std::vector<int>>();
8475 1 : *pnCount = val.size();
8476 1 : return val.data();
8477 : }
8478 :
8479 : /************************************************************************/
8480 : /* GDALAlgorithmArgGetDefaultAsDoubleList() */
8481 : /************************************************************************/
8482 :
8483 : /** Return the argument default value as a real list.
8484 : *
8485 : * Must only be called on arguments whose type is GAAT_REAL_LIST.
8486 : *
8487 : * GDALAlgorithmArgHasDefaultValue() must be called to determine if the
8488 : * argument has a default value.
8489 : *
8490 : * @param hArg Handle to an argument. Must NOT be null.
8491 : * @param[out] pnCount Pointer to the number of values in the list. Must NOT be null.
8492 : * @since 3.12
8493 : */
8494 3 : const double *GDALAlgorithmArgGetDefaultAsDoubleList(GDALAlgorithmArgH hArg,
8495 : size_t *pnCount)
8496 : {
8497 3 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8498 3 : VALIDATE_POINTER1(pnCount, __func__, nullptr);
8499 3 : if (hArg->ptr->GetType() != GAAT_REAL_LIST)
8500 : {
8501 2 : CPLError(CE_Failure, CPLE_AppDefined,
8502 : "%s must only be called on arguments of type GAAT_REAL_LIST",
8503 : __func__);
8504 2 : *pnCount = 0;
8505 2 : return nullptr;
8506 : }
8507 1 : const auto &val = hArg->ptr->GetDefault<std::vector<double>>();
8508 1 : *pnCount = val.size();
8509 1 : return val.data();
8510 : }
8511 :
8512 : /************************************************************************/
8513 : /* GDALAlgorithmArgIsHidden() */
8514 : /************************************************************************/
8515 :
8516 : /** Return whether the argument is hidden (for GDAL internal use)
8517 : *
8518 : * This is an alias for GDALAlgorithmArgIsHiddenForCLI() &&
8519 : * GDALAlgorithmArgIsHiddenForAPI().
8520 : *
8521 : * @param hArg Handle to an argument. Must NOT be null.
8522 : * @since 3.12
8523 : */
8524 1 : bool GDALAlgorithmArgIsHidden(GDALAlgorithmArgH hArg)
8525 : {
8526 1 : VALIDATE_POINTER1(hArg, __func__, false);
8527 1 : return hArg->ptr->IsHidden();
8528 : }
8529 :
8530 : /************************************************************************/
8531 : /* GDALAlgorithmArgIsHiddenForCLI() */
8532 : /************************************************************************/
8533 :
8534 : /** Return whether the argument must not be mentioned in CLI usage.
8535 : *
8536 : * For example, "output-value" for "gdal raster info", which is only
8537 : * meant when the algorithm is used from a non-CLI context.
8538 : *
8539 : * @param hArg Handle to an argument. Must NOT be null.
8540 : * @since 3.11
8541 : */
8542 1 : bool GDALAlgorithmArgIsHiddenForCLI(GDALAlgorithmArgH hArg)
8543 : {
8544 1 : VALIDATE_POINTER1(hArg, __func__, false);
8545 1 : return hArg->ptr->IsHiddenForCLI();
8546 : }
8547 :
8548 : /************************************************************************/
8549 : /* GDALAlgorithmArgIsHiddenForAPI() */
8550 : /************************************************************************/
8551 :
8552 : /** Return whether the argument must not be mentioned in the context of an
8553 : * API use.
8554 : * Said otherwise, if it is only for CLI usage.
8555 : *
8556 : * For example "--help"
8557 : *
8558 : * @param hArg Handle to an argument. Must NOT be null.
8559 : * @since 3.12
8560 : */
8561 221893 : bool GDALAlgorithmArgIsHiddenForAPI(GDALAlgorithmArgH hArg)
8562 : {
8563 221893 : VALIDATE_POINTER1(hArg, __func__, false);
8564 221893 : return hArg->ptr->IsHiddenForAPI();
8565 : }
8566 :
8567 : /************************************************************************/
8568 : /* GDALAlgorithmArgIsOnlyForCLI() */
8569 : /************************************************************************/
8570 :
8571 : /** Return whether the argument must not be mentioned in the context of an
8572 : * API use.
8573 : * Said otherwise, if it is only for CLI usage.
8574 : *
8575 : * For example "--help"
8576 : *
8577 : * @param hArg Handle to an argument. Must NOT be null.
8578 : * @since 3.11
8579 : * @deprecated Use GDALAlgorithmArgIsHiddenForAPI() instead.
8580 : */
8581 0 : bool GDALAlgorithmArgIsOnlyForCLI(GDALAlgorithmArgH hArg)
8582 : {
8583 0 : VALIDATE_POINTER1(hArg, __func__, false);
8584 0 : return hArg->ptr->IsHiddenForAPI();
8585 : }
8586 :
8587 : /************************************************************************/
8588 : /* GDALAlgorithmArgIsAvailableInPipelineStep() */
8589 : /************************************************************************/
8590 :
8591 : /** Return whether the argument is available in a pipeline step.
8592 : *
8593 : * If false, it is only available in standalone mode.
8594 : *
8595 : * @param hArg Handle to an argument. Must NOT be null.
8596 : * @since 3.13
8597 : */
8598 2 : bool GDALAlgorithmArgIsAvailableInPipelineStep(GDALAlgorithmArgH hArg)
8599 : {
8600 2 : VALIDATE_POINTER1(hArg, __func__, false);
8601 2 : return hArg->ptr->IsAvailableInPipelineStep();
8602 : }
8603 :
8604 : /************************************************************************/
8605 : /* GDALAlgorithmArgIsInput() */
8606 : /************************************************************************/
8607 :
8608 : /** Indicate whether the value of the argument is read-only during the
8609 : * execution of the algorithm.
8610 : *
8611 : * Default is true.
8612 : *
8613 : * @param hArg Handle to an argument. Must NOT be null.
8614 : * @since 3.11
8615 : */
8616 219105 : bool GDALAlgorithmArgIsInput(GDALAlgorithmArgH hArg)
8617 : {
8618 219105 : VALIDATE_POINTER1(hArg, __func__, false);
8619 219105 : return hArg->ptr->IsInput();
8620 : }
8621 :
8622 : /************************************************************************/
8623 : /* GDALAlgorithmArgIsOutput() */
8624 : /************************************************************************/
8625 :
8626 : /** Return whether (at least part of) the value of the argument is set
8627 : * during the execution of the algorithm.
8628 : *
8629 : * For example, "output-value" for "gdal raster info"
8630 : * Default is false.
8631 : * An argument may return both IsInput() and IsOutput() as true.
8632 : * For example the "gdal raster convert" algorithm consumes the dataset
8633 : * name of its "output" argument, and sets the dataset object during its
8634 : * execution.
8635 : *
8636 : * @param hArg Handle to an argument. Must NOT be null.
8637 : * @since 3.11
8638 : */
8639 122804 : bool GDALAlgorithmArgIsOutput(GDALAlgorithmArgH hArg)
8640 : {
8641 122804 : VALIDATE_POINTER1(hArg, __func__, false);
8642 122804 : return hArg->ptr->IsOutput();
8643 : }
8644 :
8645 : /************************************************************************/
8646 : /* GDALAlgorithmArgGetDatasetType() */
8647 : /************************************************************************/
8648 :
8649 : /** Get which type of dataset is allowed / generated.
8650 : *
8651 : * Binary-or combination of GDAL_OF_RASTER, GDAL_OF_VECTOR and
8652 : * GDAL_OF_MULTIDIM_RASTER.
8653 : * Only applies to arguments of type GAAT_DATASET or GAAT_DATASET_LIST.
8654 : *
8655 : * @param hArg Handle to an argument. Must NOT be null.
8656 : * @since 3.11
8657 : */
8658 2 : GDALArgDatasetType GDALAlgorithmArgGetDatasetType(GDALAlgorithmArgH hArg)
8659 : {
8660 2 : VALIDATE_POINTER1(hArg, __func__, 0);
8661 2 : return hArg->ptr->GetDatasetType();
8662 : }
8663 :
8664 : /************************************************************************/
8665 : /* GDALAlgorithmArgGetDatasetInputFlags() */
8666 : /************************************************************************/
8667 :
8668 : /** Indicates which components among name and dataset are accepted as
8669 : * input, when this argument serves as an input.
8670 : *
8671 : * If the GADV_NAME bit is set, it indicates a dataset name is accepted as
8672 : * input.
8673 : * If the GADV_OBJECT bit is set, it indicates a dataset object is
8674 : * accepted as input.
8675 : * If both bits are set, the algorithm can accept either a name or a dataset
8676 : * object.
8677 : * Only applies to arguments of type GAAT_DATASET or GAAT_DATASET_LIST.
8678 : *
8679 : * @param hArg Handle to an argument. Must NOT be null.
8680 : * @return string whose lifetime is bound to hAlg and which must not
8681 : * be freed.
8682 : * @since 3.11
8683 : */
8684 2 : int GDALAlgorithmArgGetDatasetInputFlags(GDALAlgorithmArgH hArg)
8685 : {
8686 2 : VALIDATE_POINTER1(hArg, __func__, 0);
8687 2 : return hArg->ptr->GetDatasetInputFlags();
8688 : }
8689 :
8690 : /************************************************************************/
8691 : /* GDALAlgorithmArgGetDatasetOutputFlags() */
8692 : /************************************************************************/
8693 :
8694 : /** Indicates which components among name and dataset are modified,
8695 : * when this argument serves as an output.
8696 : *
8697 : * If the GADV_NAME bit is set, it indicates a dataset name is generated as
8698 : * output (that is the algorithm will generate the name. Rarely used).
8699 : * If the GADV_OBJECT bit is set, it indicates a dataset object is
8700 : * generated as output, and available for use after the algorithm has
8701 : * completed.
8702 : * Only applies to arguments of type GAAT_DATASET or GAAT_DATASET_LIST.
8703 : *
8704 : * @param hArg Handle to an argument. Must NOT be null.
8705 : * @return string whose lifetime is bound to hAlg and which must not
8706 : * be freed.
8707 : * @since 3.11
8708 : */
8709 2 : int GDALAlgorithmArgGetDatasetOutputFlags(GDALAlgorithmArgH hArg)
8710 : {
8711 2 : VALIDATE_POINTER1(hArg, __func__, 0);
8712 2 : return hArg->ptr->GetDatasetOutputFlags();
8713 : }
8714 :
8715 : /************************************************************************/
8716 : /* GDALAlgorithmArgGetMutualExclusionGroup() */
8717 : /************************************************************************/
8718 :
8719 : /** Return the name of the mutual exclusion group to which this argument
8720 : * belongs to.
8721 : *
8722 : * Or empty string if it does not belong to any exclusion group.
8723 : *
8724 : * @param hArg Handle to an argument. Must NOT be null.
8725 : * @return string whose lifetime is bound to hArg and which must not
8726 : * be freed.
8727 : * @since 3.11
8728 : */
8729 1 : const char *GDALAlgorithmArgGetMutualExclusionGroup(GDALAlgorithmArgH hArg)
8730 : {
8731 1 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8732 1 : return hArg->ptr->GetMutualExclusionGroup().c_str();
8733 : }
8734 :
8735 : /************************************************************************/
8736 : /* GDALAlgorithmArgGetMutualDependencyGroup() */
8737 : /************************************************************************/
8738 :
8739 : /** Return the name of the mutual dependency group to which this argument
8740 : * belongs to.
8741 : *
8742 : * Or empty string if it does not belong to any dependency group.
8743 : *
8744 : * @param hArg Handle to an argument. Must NOT be null.
8745 : * @return string whose lifetime is bound to hArg and which must not
8746 : * be freed.
8747 : * @since 3.13
8748 : */
8749 5 : const char *GDALAlgorithmArgGetMutualDependencyGroup(GDALAlgorithmArgH hArg)
8750 : {
8751 5 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8752 5 : return hArg->ptr->GetMutualDependencyGroup().c_str();
8753 : }
8754 :
8755 : /************************************************************************/
8756 : /* GDALAlgorithmArgGetDirectDependencies() */
8757 : /************************************************************************/
8758 :
8759 : /** Return the list of names of arguments that this argument depends on.
8760 : *
8761 : * This is not necessarily a symmetric relationship.
8762 : * If argument A depends on argument B, it doesn't mean that B depends on A.
8763 : * Mutual dependency groups are a special case of dependencies,
8764 : * where all arguments of the group depend on each other and are not
8765 : * returned by this method.
8766 : *
8767 : * @param hArg Handle to an argument. Must NOT be null.
8768 : * @return a NULL terminated list of names, which must be destroyed with
8769 : * CSLDestroy()
8770 : * @since 3.13
8771 : */
8772 7 : char **GDALAlgorithmArgGetDirectDependencies(GDALAlgorithmArgH hArg)
8773 : {
8774 7 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8775 7 : return CPLStringList(hArg->ptr->GetDirectDependencies()).StealList();
8776 : }
8777 :
8778 : /************************************************************************/
8779 : /* GDALAlgorithmArgGetAsBoolean() */
8780 : /************************************************************************/
8781 :
8782 : /** Return the argument value as a boolean.
8783 : *
8784 : * Must only be called on arguments whose type is GAAT_BOOLEAN.
8785 : *
8786 : * @param hArg Handle to an argument. Must NOT be null.
8787 : * @since 3.11
8788 : */
8789 8 : bool GDALAlgorithmArgGetAsBoolean(GDALAlgorithmArgH hArg)
8790 : {
8791 8 : VALIDATE_POINTER1(hArg, __func__, false);
8792 8 : if (hArg->ptr->GetType() != GAAT_BOOLEAN)
8793 : {
8794 1 : CPLError(CE_Failure, CPLE_AppDefined,
8795 : "%s must only be called on arguments of type GAAT_BOOLEAN",
8796 : __func__);
8797 1 : return false;
8798 : }
8799 7 : return hArg->ptr->Get<bool>();
8800 : }
8801 :
8802 : /************************************************************************/
8803 : /* GDALAlgorithmArgGetAsString() */
8804 : /************************************************************************/
8805 :
8806 : /** Return the argument value as a string.
8807 : *
8808 : * Must only be called on arguments whose type is GAAT_STRING.
8809 : *
8810 : * @param hArg Handle to an argument. Must NOT be null.
8811 : * @return string whose lifetime is bound to hArg and which must not
8812 : * be freed.
8813 : * @since 3.11
8814 : */
8815 377 : const char *GDALAlgorithmArgGetAsString(GDALAlgorithmArgH hArg)
8816 : {
8817 377 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8818 377 : if (hArg->ptr->GetType() != GAAT_STRING)
8819 : {
8820 1 : CPLError(CE_Failure, CPLE_AppDefined,
8821 : "%s must only be called on arguments of type GAAT_STRING",
8822 : __func__);
8823 1 : return nullptr;
8824 : }
8825 376 : return hArg->ptr->Get<std::string>().c_str();
8826 : }
8827 :
8828 : /************************************************************************/
8829 : /* GDALAlgorithmArgGetAsDatasetValue() */
8830 : /************************************************************************/
8831 :
8832 : /** Return the argument value as a GDALArgDatasetValueH.
8833 : *
8834 : * Must only be called on arguments whose type is GAAT_DATASET
8835 : *
8836 : * @param hArg Handle to an argument. Must NOT be null.
8837 : * @return handle to a GDALArgDatasetValue that must be released with
8838 : * GDALArgDatasetValueRelease(). The lifetime of that handle does not exceed
8839 : * the one of hArg.
8840 : * @since 3.11
8841 : */
8842 3241 : GDALArgDatasetValueH GDALAlgorithmArgGetAsDatasetValue(GDALAlgorithmArgH hArg)
8843 : {
8844 3241 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8845 3241 : if (hArg->ptr->GetType() != GAAT_DATASET)
8846 : {
8847 1 : CPLError(CE_Failure, CPLE_AppDefined,
8848 : "%s must only be called on arguments of type GAAT_DATASET",
8849 : __func__);
8850 1 : return nullptr;
8851 : }
8852 3240 : return std::make_unique<GDALArgDatasetValueHS>(
8853 6480 : &(hArg->ptr->Get<GDALArgDatasetValue>()))
8854 3240 : .release();
8855 : }
8856 :
8857 : /************************************************************************/
8858 : /* GDALAlgorithmArgGetAsInteger() */
8859 : /************************************************************************/
8860 :
8861 : /** Return the argument value as a integer.
8862 : *
8863 : * Must only be called on arguments whose type is GAAT_INTEGER
8864 : *
8865 : * @param hArg Handle to an argument. Must NOT be null.
8866 : * @since 3.11
8867 : */
8868 26 : int GDALAlgorithmArgGetAsInteger(GDALAlgorithmArgH hArg)
8869 : {
8870 26 : VALIDATE_POINTER1(hArg, __func__, 0);
8871 26 : if (hArg->ptr->GetType() != GAAT_INTEGER)
8872 : {
8873 1 : CPLError(CE_Failure, CPLE_AppDefined,
8874 : "%s must only be called on arguments of type GAAT_INTEGER",
8875 : __func__);
8876 1 : return 0;
8877 : }
8878 25 : return hArg->ptr->Get<int>();
8879 : }
8880 :
8881 : /************************************************************************/
8882 : /* GDALAlgorithmArgGetAsDouble() */
8883 : /************************************************************************/
8884 :
8885 : /** Return the argument value as a double.
8886 : *
8887 : * Must only be called on arguments whose type is GAAT_REAL
8888 : *
8889 : * @param hArg Handle to an argument. Must NOT be null.
8890 : * @since 3.11
8891 : */
8892 8 : double GDALAlgorithmArgGetAsDouble(GDALAlgorithmArgH hArg)
8893 : {
8894 8 : VALIDATE_POINTER1(hArg, __func__, 0);
8895 8 : if (hArg->ptr->GetType() != GAAT_REAL)
8896 : {
8897 1 : CPLError(CE_Failure, CPLE_AppDefined,
8898 : "%s must only be called on arguments of type GAAT_REAL",
8899 : __func__);
8900 1 : return 0;
8901 : }
8902 7 : return hArg->ptr->Get<double>();
8903 : }
8904 :
8905 : /************************************************************************/
8906 : /* GDALAlgorithmArgGetAsStringList() */
8907 : /************************************************************************/
8908 :
8909 : /** Return the argument value as a string list.
8910 : *
8911 : * Must only be called on arguments whose type is GAAT_STRING_LIST.
8912 : *
8913 : * @param hArg Handle to an argument. Must NOT be null.
8914 : * @return a NULL terminated list of names, which must be destroyed with
8915 : * CSLDestroy()
8916 :
8917 : * @since 3.11
8918 : */
8919 4 : char **GDALAlgorithmArgGetAsStringList(GDALAlgorithmArgH hArg)
8920 : {
8921 4 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8922 4 : if (hArg->ptr->GetType() != GAAT_STRING_LIST)
8923 : {
8924 1 : CPLError(CE_Failure, CPLE_AppDefined,
8925 : "%s must only be called on arguments of type GAAT_STRING_LIST",
8926 : __func__);
8927 1 : return nullptr;
8928 : }
8929 6 : return CPLStringList(hArg->ptr->Get<std::vector<std::string>>())
8930 3 : .StealList();
8931 : }
8932 :
8933 : /************************************************************************/
8934 : /* GDALAlgorithmArgGetAsIntegerList() */
8935 : /************************************************************************/
8936 :
8937 : /** Return the argument value as a integer list.
8938 : *
8939 : * Must only be called on arguments whose type is GAAT_INTEGER_LIST.
8940 : *
8941 : * @param hArg Handle to an argument. Must NOT be null.
8942 : * @param[out] pnCount Pointer to the number of values in the list. Must NOT be null.
8943 : * @since 3.11
8944 : */
8945 8 : const int *GDALAlgorithmArgGetAsIntegerList(GDALAlgorithmArgH hArg,
8946 : size_t *pnCount)
8947 : {
8948 8 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8949 8 : VALIDATE_POINTER1(pnCount, __func__, nullptr);
8950 8 : if (hArg->ptr->GetType() != GAAT_INTEGER_LIST)
8951 : {
8952 1 : CPLError(
8953 : CE_Failure, CPLE_AppDefined,
8954 : "%s must only be called on arguments of type GAAT_INTEGER_LIST",
8955 : __func__);
8956 1 : *pnCount = 0;
8957 1 : return nullptr;
8958 : }
8959 7 : const auto &val = hArg->ptr->Get<std::vector<int>>();
8960 7 : *pnCount = val.size();
8961 7 : return val.data();
8962 : }
8963 :
8964 : /************************************************************************/
8965 : /* GDALAlgorithmArgGetAsDoubleList() */
8966 : /************************************************************************/
8967 :
8968 : /** Return the argument value as a real list.
8969 : *
8970 : * Must only be called on arguments whose type is GAAT_REAL_LIST.
8971 : *
8972 : * @param hArg Handle to an argument. Must NOT be null.
8973 : * @param[out] pnCount Pointer to the number of values in the list. Must NOT be null.
8974 : * @since 3.11
8975 : */
8976 8 : const double *GDALAlgorithmArgGetAsDoubleList(GDALAlgorithmArgH hArg,
8977 : size_t *pnCount)
8978 : {
8979 8 : VALIDATE_POINTER1(hArg, __func__, nullptr);
8980 8 : VALIDATE_POINTER1(pnCount, __func__, nullptr);
8981 8 : if (hArg->ptr->GetType() != GAAT_REAL_LIST)
8982 : {
8983 1 : CPLError(CE_Failure, CPLE_AppDefined,
8984 : "%s must only be called on arguments of type GAAT_REAL_LIST",
8985 : __func__);
8986 1 : *pnCount = 0;
8987 1 : return nullptr;
8988 : }
8989 7 : const auto &val = hArg->ptr->Get<std::vector<double>>();
8990 7 : *pnCount = val.size();
8991 7 : return val.data();
8992 : }
8993 :
8994 : /************************************************************************/
8995 : /* GDALAlgorithmArgSetAsBoolean() */
8996 : /************************************************************************/
8997 :
8998 : /** Set the value for a GAAT_BOOLEAN argument.
8999 : *
9000 : * It cannot be called several times for a given argument.
9001 : * Validation checks and other actions are run.
9002 : *
9003 : * @param hArg Handle to an argument. Must NOT be null.
9004 : * @param value value.
9005 : * @return true if success.
9006 : * @since 3.11
9007 : */
9008 :
9009 731 : bool GDALAlgorithmArgSetAsBoolean(GDALAlgorithmArgH hArg, bool value)
9010 : {
9011 731 : VALIDATE_POINTER1(hArg, __func__, false);
9012 731 : return hArg->ptr->Set(value);
9013 : }
9014 :
9015 : /************************************************************************/
9016 : /* GDALAlgorithmArgSetAsString() */
9017 : /************************************************************************/
9018 :
9019 : /** Set the value for a GAAT_STRING argument.
9020 : *
9021 : * It cannot be called several times for a given argument.
9022 : * Validation checks and other actions are run.
9023 : *
9024 : * @param hArg Handle to an argument. Must NOT be null.
9025 : * @param value value (may be null)
9026 : * @return true if success.
9027 : * @since 3.11
9028 : */
9029 :
9030 3257 : bool GDALAlgorithmArgSetAsString(GDALAlgorithmArgH hArg, const char *value)
9031 : {
9032 3257 : VALIDATE_POINTER1(hArg, __func__, false);
9033 3257 : return hArg->ptr->Set(value ? value : "");
9034 : }
9035 :
9036 : /************************************************************************/
9037 : /* GDALAlgorithmArgSetAsInteger() */
9038 : /************************************************************************/
9039 :
9040 : /** Set the value for a GAAT_INTEGER (or GAAT_REAL) argument.
9041 : *
9042 : * It cannot be called several times for a given argument.
9043 : * Validation checks and other actions are run.
9044 : *
9045 : * @param hArg Handle to an argument. Must NOT be null.
9046 : * @param value value.
9047 : * @return true if success.
9048 : * @since 3.11
9049 : */
9050 :
9051 473 : bool GDALAlgorithmArgSetAsInteger(GDALAlgorithmArgH hArg, int value)
9052 : {
9053 473 : VALIDATE_POINTER1(hArg, __func__, false);
9054 473 : return hArg->ptr->Set(value);
9055 : }
9056 :
9057 : /************************************************************************/
9058 : /* GDALAlgorithmArgSetAsDouble() */
9059 : /************************************************************************/
9060 :
9061 : /** Set the value for a GAAT_REAL argument.
9062 : *
9063 : * It cannot be called several times for a given argument.
9064 : * Validation checks and other actions are run.
9065 : *
9066 : * @param hArg Handle to an argument. Must NOT be null.
9067 : * @param value value.
9068 : * @return true if success.
9069 : * @since 3.11
9070 : */
9071 :
9072 263 : bool GDALAlgorithmArgSetAsDouble(GDALAlgorithmArgH hArg, double value)
9073 : {
9074 263 : VALIDATE_POINTER1(hArg, __func__, false);
9075 263 : return hArg->ptr->Set(value);
9076 : }
9077 :
9078 : /************************************************************************/
9079 : /* GDALAlgorithmArgSetAsDatasetValue() */
9080 : /************************************************************************/
9081 :
9082 : /** Set the value for a GAAT_DATASET argument.
9083 : *
9084 : * It cannot be called several times for a given argument.
9085 : * Validation checks and other actions are run.
9086 : *
9087 : * @param hArg Handle to an argument. Must NOT be null.
9088 : * @param value Handle to a GDALArgDatasetValue. Must NOT be null.
9089 : * @return true if success.
9090 : * @since 3.11
9091 : */
9092 2 : bool GDALAlgorithmArgSetAsDatasetValue(GDALAlgorithmArgH hArg,
9093 : GDALArgDatasetValueH value)
9094 : {
9095 2 : VALIDATE_POINTER1(hArg, __func__, false);
9096 2 : VALIDATE_POINTER1(value, __func__, false);
9097 2 : return hArg->ptr->SetFrom(*(value->ptr));
9098 : }
9099 :
9100 : /************************************************************************/
9101 : /* GDALAlgorithmArgSetDataset() */
9102 : /************************************************************************/
9103 :
9104 : /** Set dataset object, increasing its reference counter.
9105 : *
9106 : * @param hArg Handle to an argument. Must NOT be null.
9107 : * @param hDS Dataset object. May be null.
9108 : * @return true if success.
9109 : * @since 3.11
9110 : */
9111 :
9112 2 : bool GDALAlgorithmArgSetDataset(GDALAlgorithmArgH hArg, GDALDatasetH hDS)
9113 : {
9114 2 : VALIDATE_POINTER1(hArg, __func__, false);
9115 2 : return hArg->ptr->Set(GDALDataset::FromHandle(hDS));
9116 : }
9117 :
9118 : /************************************************************************/
9119 : /* GDALAlgorithmArgSetAsStringList() */
9120 : /************************************************************************/
9121 :
9122 : /** Set the value for a GAAT_STRING_LIST argument.
9123 : *
9124 : * It cannot be called several times for a given argument.
9125 : * Validation checks and other actions are run.
9126 : *
9127 : * @param hArg Handle to an argument. Must NOT be null.
9128 : * @param value value as a NULL terminated list (may be null)
9129 : * @return true if success.
9130 : * @since 3.11
9131 : */
9132 :
9133 815 : bool GDALAlgorithmArgSetAsStringList(GDALAlgorithmArgH hArg, CSLConstList value)
9134 : {
9135 815 : VALIDATE_POINTER1(hArg, __func__, false);
9136 815 : return hArg->ptr->Set(
9137 1630 : static_cast<std::vector<std::string>>(CPLStringList(value)));
9138 : }
9139 :
9140 : /************************************************************************/
9141 : /* GDALAlgorithmArgSetAsIntegerList() */
9142 : /************************************************************************/
9143 :
9144 : /** Set the value for a GAAT_INTEGER_LIST argument.
9145 : *
9146 : * It cannot be called several times for a given argument.
9147 : * Validation checks and other actions are run.
9148 : *
9149 : * @param hArg Handle to an argument. Must NOT be null.
9150 : * @param nCount Number of values in pnValues.
9151 : * @param pnValues Pointer to an array of integer values of size nCount.
9152 : * @return true if success.
9153 : * @since 3.11
9154 : */
9155 105 : bool GDALAlgorithmArgSetAsIntegerList(GDALAlgorithmArgH hArg, size_t nCount,
9156 : const int *pnValues)
9157 : {
9158 105 : VALIDATE_POINTER1(hArg, __func__, false);
9159 105 : return hArg->ptr->Set(std::vector<int>(pnValues, pnValues + nCount));
9160 : }
9161 :
9162 : /************************************************************************/
9163 : /* GDALAlgorithmArgSetAsDoubleList() */
9164 : /************************************************************************/
9165 :
9166 : /** Set the value for a GAAT_REAL_LIST argument.
9167 : *
9168 : * It cannot be called several times for a given argument.
9169 : * Validation checks and other actions are run.
9170 : *
9171 : * @param hArg Handle to an argument. Must NOT be null.
9172 : * @param nCount Number of values in pnValues.
9173 : * @param pnValues Pointer to an array of double values of size nCount.
9174 : * @return true if success.
9175 : * @since 3.11
9176 : */
9177 159 : bool GDALAlgorithmArgSetAsDoubleList(GDALAlgorithmArgH hArg, size_t nCount,
9178 : const double *pnValues)
9179 : {
9180 159 : VALIDATE_POINTER1(hArg, __func__, false);
9181 159 : return hArg->ptr->Set(std::vector<double>(pnValues, pnValues + nCount));
9182 : }
9183 :
9184 : /************************************************************************/
9185 : /* GDALAlgorithmArgSetDatasets() */
9186 : /************************************************************************/
9187 :
9188 : /** Set dataset objects to a GAAT_DATASET_LIST argument, increasing their reference counter.
9189 : *
9190 : * @param hArg Handle to an argument. Must NOT be null.
9191 : * @param nCount Number of values in pnValues.
9192 : * @param pahDS Pointer to an array of dataset of size nCount.
9193 : * @return true if success.
9194 : * @since 3.11
9195 : */
9196 :
9197 1339 : bool GDALAlgorithmArgSetDatasets(GDALAlgorithmArgH hArg, size_t nCount,
9198 : GDALDatasetH *pahDS)
9199 : {
9200 1339 : VALIDATE_POINTER1(hArg, __func__, false);
9201 2678 : std::vector<GDALArgDatasetValue> values;
9202 2704 : for (size_t i = 0; i < nCount; ++i)
9203 : {
9204 1365 : values.emplace_back(GDALDataset::FromHandle(pahDS[i]));
9205 : }
9206 1339 : return hArg->ptr->Set(std::move(values));
9207 : }
9208 :
9209 : /************************************************************************/
9210 : /* GDALAlgorithmArgSetDatasetNames() */
9211 : /************************************************************************/
9212 :
9213 : /** Set dataset names to a GAAT_DATASET_LIST argument.
9214 : *
9215 : * @param hArg Handle to an argument. Must NOT be null.
9216 : * @param names Dataset names as a NULL terminated list (may be null)
9217 : * @return true if success.
9218 : * @since 3.11
9219 : */
9220 :
9221 816 : bool GDALAlgorithmArgSetDatasetNames(GDALAlgorithmArgH hArg, CSLConstList names)
9222 : {
9223 816 : VALIDATE_POINTER1(hArg, __func__, false);
9224 1632 : std::vector<GDALArgDatasetValue> values;
9225 1703 : for (size_t i = 0; names[i]; ++i)
9226 : {
9227 887 : values.emplace_back(names[i]);
9228 : }
9229 816 : return hArg->ptr->Set(std::move(values));
9230 : }
9231 :
9232 : /************************************************************************/
9233 : /* GDALArgDatasetValueCreate() */
9234 : /************************************************************************/
9235 :
9236 : /** Instantiate an empty GDALArgDatasetValue
9237 : *
9238 : * @return new handle to free with GDALArgDatasetValueRelease()
9239 : * @since 3.11
9240 : */
9241 1 : GDALArgDatasetValueH GDALArgDatasetValueCreate()
9242 : {
9243 1 : return std::make_unique<GDALArgDatasetValueHS>().release();
9244 : }
9245 :
9246 : /************************************************************************/
9247 : /* GDALArgDatasetValueRelease() */
9248 : /************************************************************************/
9249 :
9250 : /** Release a handle to a GDALArgDatasetValue
9251 : *
9252 : * @since 3.11
9253 : */
9254 3241 : void GDALArgDatasetValueRelease(GDALArgDatasetValueH hValue)
9255 : {
9256 3241 : delete hValue;
9257 3241 : }
9258 :
9259 : /************************************************************************/
9260 : /* GDALArgDatasetValueGetName() */
9261 : /************************************************************************/
9262 :
9263 : /** Return the name component of the GDALArgDatasetValue
9264 : *
9265 : * @param hValue Handle to a GDALArgDatasetValue. Must NOT be null.
9266 : * @return string whose lifetime is bound to hAlg and which must not
9267 : * be freed.
9268 : * @since 3.11
9269 : */
9270 1 : const char *GDALArgDatasetValueGetName(GDALArgDatasetValueH hValue)
9271 : {
9272 1 : VALIDATE_POINTER1(hValue, __func__, nullptr);
9273 1 : return hValue->ptr->GetName().c_str();
9274 : }
9275 :
9276 : /************************************************************************/
9277 : /* GDALArgDatasetValueGetDatasetRef() */
9278 : /************************************************************************/
9279 :
9280 : /** Return the dataset component of the GDALArgDatasetValue.
9281 : *
9282 : * This does not modify the reference counter, hence the lifetime of the
9283 : * returned object is not guaranteed to exceed the one of hValue.
9284 : *
9285 : * @param hValue Handle to a GDALArgDatasetValue. Must NOT be null.
9286 : * @since 3.11
9287 : */
9288 3 : GDALDatasetH GDALArgDatasetValueGetDatasetRef(GDALArgDatasetValueH hValue)
9289 : {
9290 3 : VALIDATE_POINTER1(hValue, __func__, nullptr);
9291 3 : return GDALDataset::ToHandle(hValue->ptr->GetDatasetRef());
9292 : }
9293 :
9294 : /************************************************************************/
9295 : /* GDALArgDatasetValueGetDatasetIncreaseRefCount() */
9296 : /************************************************************************/
9297 :
9298 : /** Return the dataset component of the GDALArgDatasetValue, and increase its
9299 : * reference count if not null. Once done with the dataset, the caller should
9300 : * call GDALReleaseDataset().
9301 : *
9302 : * @param hValue Handle to a GDALArgDatasetValue. Must NOT be null.
9303 : * @since 3.11
9304 : */
9305 : GDALDatasetH
9306 1101 : GDALArgDatasetValueGetDatasetIncreaseRefCount(GDALArgDatasetValueH hValue)
9307 : {
9308 1101 : VALIDATE_POINTER1(hValue, __func__, nullptr);
9309 1101 : return GDALDataset::ToHandle(hValue->ptr->GetDatasetIncreaseRefCount());
9310 : }
9311 :
9312 : /************************************************************************/
9313 : /* GDALArgDatasetValueSetName() */
9314 : /************************************************************************/
9315 :
9316 : /** Set dataset name
9317 : *
9318 : * @param hValue Handle to a GDALArgDatasetValue. Must NOT be null.
9319 : * @param pszName Dataset name. May be null.
9320 : * @since 3.11
9321 : */
9322 :
9323 1379 : void GDALArgDatasetValueSetName(GDALArgDatasetValueH hValue,
9324 : const char *pszName)
9325 : {
9326 1379 : VALIDATE_POINTER0(hValue, __func__);
9327 1379 : hValue->ptr->Set(pszName ? pszName : "");
9328 : }
9329 :
9330 : /************************************************************************/
9331 : /* GDALArgDatasetValueSetDataset() */
9332 : /************************************************************************/
9333 :
9334 : /** Set dataset object, increasing its reference counter.
9335 : *
9336 : * @param hValue Handle to a GDALArgDatasetValue. Must NOT be null.
9337 : * @param hDS Dataset object. May be null.
9338 : * @since 3.11
9339 : */
9340 :
9341 746 : void GDALArgDatasetValueSetDataset(GDALArgDatasetValueH hValue,
9342 : GDALDatasetH hDS)
9343 : {
9344 746 : VALIDATE_POINTER0(hValue, __func__);
9345 746 : hValue->ptr->Set(GDALDataset::FromHandle(hDS));
9346 : }
|