-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathgdalalg_raster_calc.cpp
More file actions
1193 lines (1068 loc) · 40.8 KB
/
gdalalg_raster_calc.cpp
File metadata and controls
1193 lines (1068 loc) · 40.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
* Project: GDAL
* Purpose: "gdal raster calc" subcommand
* Author: Daniel Baston
*
******************************************************************************
* Copyright (c) 2025, ISciences LLC
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "gdalalg_raster_calc.h"
#include "../frmts/vrt/gdal_vrt.h"
#include "../frmts/vrt/vrtdataset.h"
#include "cpl_float.h"
#include "cpl_vsi_virtual.h"
#include "gdal_priv.h"
#include "gdal_utils.h"
#include "vrtdataset.h"
#include <algorithm>
#include <optional>
//! @cond Doxygen_Suppress
#ifndef _
#define _(x) (x)
#endif
struct GDALCalcOptions
{
GDALDataType dstType{GDT_Unknown};
bool checkCRS{true};
bool checkExtent{true};
};
static bool MatchIsCompleteVariableNameWithNoIndex(const std::string &str,
size_t from, size_t to)
{
if (to < str.size())
{
// If the character after the end of the match is:
// * alphanumeric or _ : we've matched only part of a variable name
// * [ : we've matched a variable that already has an index
// * ( : we've matched a function name
if (std::isalnum(str[to]) || str[to] == '_' || str[to] == '[' ||
str[to] == '(')
{
return false;
}
}
if (from > 0)
{
// If the character before the start of the match is alphanumeric or _,
// we've matched only part of a variable name.
if (std::isalnum(str[from - 1]) || str[from - 1] == '_')
{
return false;
}
}
return true;
}
/**
* Add a band subscript to all instances of a specified variable that
* do not already have such a subscript. For example, "X" would be
* replaced with "X[3]" but "X[1]" would be left untouched.
*/
static std::string SetBandIndices(const std::string &origExpression,
const std::string &variable, int band,
bool &expressionChanged)
{
std::string expression = origExpression;
expressionChanged = false;
std::string::size_type seekPos = 0;
auto pos = expression.find(variable, seekPos);
while (pos != std::string::npos)
{
auto end = pos + variable.size();
if (MatchIsCompleteVariableNameWithNoIndex(expression, pos, end))
{
// No index specified for variable
expression = expression.substr(0, pos + variable.size()) + '[' +
std::to_string(band) + ']' + expression.substr(end);
expressionChanged = true;
}
seekPos = end;
pos = expression.find(variable, seekPos);
}
return expression;
}
static bool PosIsAggregateFunctionArgument(const std::string &expression,
size_t pos)
{
// If this position is a function argument, we should be able to
// scan backwards for a ( and find only variable names, literals or commas.
while (pos != 0)
{
const char c = expression[pos];
if (c == '(')
{
pos--;
break;
}
if (!(isspace(c) || isalnum(c) || c == ',' || c == '.' || c == '[' ||
c == ']' || c == '_'))
{
return false;
}
pos--;
}
// Now what we've found the (, the preceding characters should be an
// aggregate function name
if (pos < 2)
{
return false;
}
if (STARTS_WITH_CI(expression.c_str() + (pos - 2), "avg") ||
STARTS_WITH_CI(expression.c_str() + (pos - 2), "sum") ||
STARTS_WITH_CI(expression.c_str() + (pos - 2), "min") ||
STARTS_WITH_CI(expression.c_str() + (pos - 2), "max"))
{
return true;
}
return false;
}
/**
* Replace X by X[1],X[2],...X[n]
*/
static std::string
SetBandIndicesFlattenedExpression(const std::string &origExpression,
const std::string &variable, int nBands)
{
std::string expression = origExpression;
std::string::size_type seekPos = 0;
auto pos = expression.find(variable, seekPos);
while (pos != std::string::npos)
{
auto end = pos + variable.size();
if (MatchIsCompleteVariableNameWithNoIndex(expression, pos, end) &&
PosIsAggregateFunctionArgument(expression, pos))
{
std::string newExpr = expression.substr(0, pos);
for (int i = 1; i <= nBands; ++i)
{
if (i > 1)
newExpr += ',';
newExpr += variable;
newExpr += '[';
newExpr += std::to_string(i);
newExpr += ']';
}
const size_t oldExprSize = expression.size();
newExpr += expression.substr(end);
expression = std::move(newExpr);
end += expression.size() - oldExprSize;
}
seekPos = end;
pos = expression.find(variable, seekPos);
}
return expression;
}
struct SourceProperties
{
int nBands{0};
int nX{0};
int nY{0};
bool hasGT{false};
GDALGeoTransform gt{};
OGRSpatialReferenceRefCountedPtr srs{};
std::vector<std::optional<double>> noData{};
GDALDataType eDT{GDT_Unknown};
};
static std::optional<SourceProperties>
UpdateSourceProperties(SourceProperties &out, const std::string &dsn,
const GDALCalcOptions &options)
{
SourceProperties source;
bool srsMismatch = false;
bool extentMismatch = false;
bool dimensionMismatch = false;
{
std::unique_ptr<GDALDataset> ds(
GDALDataset::Open(dsn.c_str(), GDAL_OF_RASTER));
if (!ds)
{
CPLError(CE_Failure, CPLE_AppDefined, "Failed to open %s",
dsn.c_str());
return std::nullopt;
}
source.nBands = ds->GetRasterCount();
source.nX = ds->GetRasterXSize();
source.nY = ds->GetRasterYSize();
source.noData.resize(source.nBands);
if (options.checkExtent)
{
ds->GetGeoTransform(source.gt);
}
if (options.checkCRS && out.srs)
{
const OGRSpatialReference *srs = ds->GetSpatialRef();
srsMismatch = srs && !srs->IsSame(out.srs.get());
}
// Store the source data type if it is the same for all bands in the source
bool bandsHaveSameType = true;
for (int i = 1; i <= source.nBands; ++i)
{
GDALRasterBand *band = ds->GetRasterBand(i);
if (i == 1)
{
source.eDT = band->GetRasterDataType();
}
else if (bandsHaveSameType &&
source.eDT != band->GetRasterDataType())
{
source.eDT = GDT_Unknown;
bandsHaveSameType = false;
}
int success;
double noData = band->GetNoDataValue(&success);
if (success)
{
source.noData[i - 1] = noData;
}
}
}
if (source.nX != out.nX || source.nY != out.nY)
{
dimensionMismatch = true;
}
if (source.gt.xorig != out.gt.xorig || source.gt.xrot != out.gt.xrot ||
source.gt.yorig != out.gt.yorig || source.gt.yrot != out.gt.yrot)
{
extentMismatch = true;
}
if (source.gt.xscale != out.gt.xscale || source.gt.yscale != out.gt.yscale)
{
// Resolutions are different. Are the extents the same?
double xmaxOut =
out.gt.xorig + out.nX * out.gt.xscale + out.nY * out.gt.xrot;
double yminOut =
out.gt.yorig + out.nX * out.gt.yrot + out.nY * out.gt.yscale;
double xmax = source.gt.xorig + source.nX * source.gt.xscale +
source.nY * source.gt.xrot;
double ymin = source.gt.yorig + source.nX * source.gt.yrot +
source.nY * source.gt.yscale;
// Max allowable extent misalignment, expressed as fraction of a pixel
constexpr double EXTENT_RTOL = 1e-3;
if (std::abs(xmax - xmaxOut) >
EXTENT_RTOL * std::abs(source.gt.xscale) ||
std::abs(ymin - yminOut) > EXTENT_RTOL * std::abs(source.gt.yscale))
{
extentMismatch = true;
}
}
if (options.checkExtent && extentMismatch)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Input extents are inconsistent.");
return std::nullopt;
}
if (!options.checkExtent && dimensionMismatch)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Inputs do not have the same dimensions.");
return std::nullopt;
}
// Find a common resolution
if (source.nX > out.nX)
{
auto dx = CPLGreatestCommonDivisor(out.gt.xscale, source.gt.xscale);
if (dx == 0)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Failed to find common resolution for inputs.");
return std::nullopt;
}
out.nX = static_cast<int>(
std::round(static_cast<double>(out.nX) * out.gt.xscale / dx));
out.gt.xscale = dx;
}
if (source.nY > out.nY)
{
auto dy = CPLGreatestCommonDivisor(out.gt.yscale, source.gt.yscale);
if (dy == 0)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Failed to find common resolution for inputs.");
return std::nullopt;
}
out.nY = static_cast<int>(
std::round(static_cast<double>(out.nY) * out.gt.yscale / dy));
out.gt.yscale = dy;
}
if (srsMismatch)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Input spatial reference systems are inconsistent.");
return std::nullopt;
}
return source;
}
/** Create XML nodes for one or more derived bands resulting from the evaluation
* of a single expression
*
* @param pszVRTFilename VRT filename
* @param root VRTDataset node to which the band nodes should be added
* @param bandType the type of the band(s) to create
* @param nXOut Number of columns in VRT dataset
* @param nYOut Number of rows in VRT dataset
* @param expression Expression for which band(s) should be added
* @param dialect Expression dialect
* @param flatten Generate a single band output raster per expression, even if
* input datasets are multiband.
* @param noDataText nodata value to use for the created band, or "none", or ""
* @param pixelFunctionArguments Pixel function arguments.
* @param sources Mapping of source names to DSNs
* @param sourceProps Mapping of source names to properties
* @param fakeSourceFilename If not empty, used instead of real input filenames.
* @return true if the band(s) were added, false otherwise
*/
static bool
CreateDerivedBandXML(const char *pszVRTFilename, CPLXMLNode *root, int nXOut,
int nYOut, GDALDataType bandType,
const std::string &expression, const std::string &dialect,
bool flatten, const std::string &noDataText,
const std::vector<std::string> &pixelFunctionArguments,
const std::map<std::string, std::string> &sources,
const std::map<std::string, SourceProperties> &sourceProps,
const std::string &fakeSourceFilename)
{
int nOutBands = 1; // By default, each expression produces a single output
// band. When processing the expression below, we may
// discover that the expression produces multiple bands,
// in which case this will be updated.
for (int nOutBand = 1; nOutBand <= nOutBands; nOutBand++)
{
// Copy the expression for each output band, because we may modify it
// when adding band indices (e.g., X -> X[1]) to the variables in the
// expression.
std::string bandExpression = expression;
CPLXMLNode *band = CPLCreateXMLNode(root, CXT_Element, "VRTRasterBand");
CPLAddXMLAttributeAndValue(band, "subClass", "VRTDerivedRasterBand");
if (bandType == GDT_Unknown)
{
bandType = GDT_Float64;
}
CPLAddXMLAttributeAndValue(band, "dataType",
GDALGetDataTypeName(bandType));
std::optional<double> dstNoData;
bool autoSelectNoDataValue = false;
if (noDataText.empty())
{
autoSelectNoDataValue = true;
}
else if (noDataText != "none")
{
if (auto parsed = cpl::strict_parse<double>(noDataText);
parsed.has_value())
{
dstNoData = parsed.value();
}
else
{
CPLError(CE_Failure, CPLE_AppDefined,
"Invalid NoData value: %s", noDataText.c_str());
return false;
}
}
for (const auto &[source_name, dsn] : sources)
{
auto it = sourceProps.find(source_name);
CPLAssert(it != sourceProps.end());
const auto &props = it->second;
bool expressionAppliedPerBand = false;
if (dialect == "builtin")
{
expressionAppliedPerBand = !flatten;
}
else
{
const int nDefaultInBand = std::min(props.nBands, nOutBand);
if (flatten)
{
bandExpression = SetBandIndicesFlattenedExpression(
bandExpression, source_name, props.nBands);
}
bandExpression =
SetBandIndices(bandExpression, source_name, nDefaultInBand,
expressionAppliedPerBand);
}
if (expressionAppliedPerBand)
{
if (nOutBands <= 1)
{
nOutBands = props.nBands;
}
else if (props.nBands != 1 && props.nBands != nOutBands)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Expression cannot operate on all bands of "
"rasters with incompatible numbers of bands "
"(source %s has %d bands but expected to have "
"1 or %d bands).",
source_name.c_str(), props.nBands, nOutBands);
return false;
}
}
// Create a source for each input band that is used in
// the expression.
for (int nInBand = 1; nInBand <= props.nBands; nInBand++)
{
CPLString inBandVariable;
if (dialect == "builtin")
{
if (!flatten && props.nBands >= 2 && nInBand != nOutBand)
continue;
}
else
{
inBandVariable.Printf("%s[%d]", source_name.c_str(),
nInBand);
if (bandExpression.find(inBandVariable) ==
std::string::npos)
{
continue;
}
}
const std::optional<double> &srcNoData =
props.noData[nInBand - 1];
CPLXMLNode *source = CPLCreateXMLNode(
band, CXT_Element,
srcNoData.has_value() ? "ComplexSource" : "SimpleSource");
if (!inBandVariable.empty())
{
CPLAddXMLAttributeAndValue(source, "name",
inBandVariable.c_str());
}
CPLXMLNode *sourceFilename =
CPLCreateXMLNode(source, CXT_Element, "SourceFilename");
if (fakeSourceFilename.empty())
{
std::string osSourceFilename = dsn;
bool bRelativeToVRT = false;
if (pszVRTFilename[0])
{
std::tie(osSourceFilename, bRelativeToVRT) =
VRTSimpleSource::ComputeSourceNameAndRelativeFlag(
CPLGetPathSafe(pszVRTFilename).c_str(), dsn);
}
CPLAddXMLAttributeAndValue(sourceFilename, "relativeToVRT",
bRelativeToVRT ? "1" : "0");
CPLCreateXMLNode(sourceFilename, CXT_Text,
osSourceFilename.c_str());
}
else
{
CPLCreateXMLNode(sourceFilename, CXT_Text,
fakeSourceFilename.c_str());
}
CPLXMLNode *sourceBand =
CPLCreateXMLNode(source, CXT_Element, "SourceBand");
CPLCreateXMLNode(sourceBand, CXT_Text,
std::to_string(nInBand).c_str());
if (srcNoData.has_value())
{
CPLXMLNode *srcNoDataNode =
CPLCreateXMLNode(source, CXT_Element, "NODATA");
std::string srcNoDataText =
CPLSPrintf("%.17g", srcNoData.value());
CPLCreateXMLNode(srcNoDataNode, CXT_Text,
srcNoDataText.c_str());
if (autoSelectNoDataValue && !dstNoData.has_value())
{
dstNoData = srcNoData;
}
}
if (fakeSourceFilename.empty())
{
CPLXMLNode *srcRect =
CPLCreateXMLNode(source, CXT_Element, "SrcRect");
CPLAddXMLAttributeAndValue(srcRect, "xOff", "0");
CPLAddXMLAttributeAndValue(srcRect, "yOff", "0");
CPLAddXMLAttributeAndValue(
srcRect, "xSize", std::to_string(props.nX).c_str());
CPLAddXMLAttributeAndValue(
srcRect, "ySize", std::to_string(props.nY).c_str());
CPLXMLNode *dstRect =
CPLCreateXMLNode(source, CXT_Element, "DstRect");
CPLAddXMLAttributeAndValue(dstRect, "xOff", "0");
CPLAddXMLAttributeAndValue(dstRect, "yOff", "0");
CPLAddXMLAttributeAndValue(dstRect, "xSize",
std::to_string(nXOut).c_str());
CPLAddXMLAttributeAndValue(dstRect, "ySize",
std::to_string(nYOut).c_str());
}
}
if (dstNoData.has_value())
{
if (!GDALIsValueExactAs(dstNoData.value(), bandType))
{
CPLError(
CE_Failure, CPLE_AppDefined,
"Band output type %s cannot represent NoData value %g",
GDALGetDataTypeName(bandType), dstNoData.value());
return false;
}
CPLXMLNode *noDataNode =
CPLCreateXMLNode(band, CXT_Element, "NoDataValue");
CPLString dstNoDataText =
CPLSPrintf("%.17g", dstNoData.value());
CPLCreateXMLNode(noDataNode, CXT_Text, dstNoDataText.c_str());
}
}
CPLXMLNode *pixelFunctionType =
CPLCreateXMLNode(band, CXT_Element, "PixelFunctionType");
CPLXMLNode *arguments =
CPLCreateXMLNode(band, CXT_Element, "PixelFunctionArguments");
if (dialect == "builtin")
{
CPLCreateXMLNode(pixelFunctionType, CXT_Text, expression.c_str());
}
else
{
CPLCreateXMLNode(pixelFunctionType, CXT_Text, "expression");
CPLAddXMLAttributeAndValue(arguments, "dialect", "muparser");
// Add the expression as a last step, because we may modify the
// expression as we iterate through the bands.
CPLAddXMLAttributeAndValue(arguments, "expression",
bandExpression.c_str());
}
if (!pixelFunctionArguments.empty())
{
const CPLStringList args(pixelFunctionArguments);
for (const auto &[key, value] : cpl::IterateNameValue(args))
{
CPLAddXMLAttributeAndValue(arguments, key, value);
}
}
}
return true;
}
static bool ParseSourceDescriptors(const std::vector<std::string> &inputs,
std::map<std::string, std::string> &datasets,
std::string &firstSourceName,
bool requireSourceNames)
{
for (size_t iInput = 0; iInput < inputs.size(); iInput++)
{
const std::string &input = inputs[iInput];
std::string name;
const auto pos = input.find('=');
if (pos == std::string::npos)
{
if (requireSourceNames && inputs.size() > 1)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Inputs must be named when more than one input is "
"provided.");
return false;
}
name = "X";
if (iInput > 0)
{
name += std::to_string(iInput);
}
}
else
{
name = input.substr(0, pos);
}
// Check input name is legal
for (size_t i = 0; i < name.size(); ++i)
{
const char c = name[i];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
// ok
}
else if (c == '_' || (c >= '0' && c <= '9'))
{
if (i == 0)
{
// Reserved constants in MuParser start with an underscore
CPLError(
CE_Failure, CPLE_AppDefined,
"Name '%s' is illegal because it starts with a '%c'",
name.c_str(), c);
return false;
}
}
else
{
CPLError(CE_Failure, CPLE_AppDefined,
"Name '%s' is illegal because character '%c' is not "
"allowed",
name.c_str(), c);
return false;
}
}
std::string dsn =
(pos == std::string::npos) ? input : input.substr(pos + 1);
if (!dsn.empty() && dsn.front() == '[' && dsn.back() == ']')
{
dsn = "{\"type\":\"gdal_streamed_alg\", \"command_line\":\"gdal "
"raster pipeline " +
CPLString(dsn.substr(1, dsn.size() - 2))
.replaceAll('\\', "\\\\")
.replaceAll('"', "\\\"") +
"\"}";
}
if (datasets.find(name) != datasets.end())
{
CPLError(CE_Failure, CPLE_AppDefined,
"An input with name '%s' has already been provided",
name.c_str());
return false;
}
datasets[name] = std::move(dsn);
if (iInput == 0)
{
firstSourceName = std::move(name);
}
}
return true;
}
static bool ReadFileLists(const std::vector<GDALArgDatasetValue> &inputDS,
std::vector<std::string> &inputFilenames)
{
for (const auto &dsVal : inputDS)
{
const auto &input = dsVal.GetName();
if (!input.empty() && input[0] == '@')
{
auto f =
VSIVirtualHandleUniquePtr(VSIFOpenL(input.c_str() + 1, "r"));
if (!f)
{
CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s",
input.c_str() + 1);
return false;
}
while (const char *filename = CPLReadLineL(f.get()))
{
inputFilenames.push_back(filename);
}
}
else
{
inputFilenames.push_back(input);
}
}
return true;
}
/** Creates a VRT datasource with one or more derived raster bands containing
* results of an expression.
*
* To make this work with muparser (which does not support vector types), we
* do a simple parsing of the expression internally, transforming it into
* multiple expressions with explicit band indices. For example, for a two-band
* raster "X", the expression "X + 3" will be transformed into "X[1] + 3" and
* "X[2] + 3". The use of brackets is for readability only; as far as the
* expression engine is concerned, the variables "X[1]" and "X[2]" have nothing
* to do with each other.
*
* @param pszVRTFilename VRT filename
* @param inputs A list of sources, expressed as NAME=DSN
* @param expressions A list of expressions to be evaluated
* @param dialect Expression dialect
* @param flatten Generate a single band output raster per expression, even if
* input datasets are multiband.
* @param noData NoData values to use for output bands, or "none", or ""
* @param pixelFunctionArguments Pixel function arguments.
* @param options flags controlling which checks should be performed on the inputs
* @param[out] maxSourceBands Maximum number of bands in source dataset(s)
* @param fakeSourceFilename If not empty, used instead of real input filenames.
*
* @return a newly created VRTDataset, or nullptr on error
*/
static std::unique_ptr<GDALDataset> GDALCalcCreateVRTDerived(
const char *pszVRTFilename, const std::vector<std::string> &inputs,
const std::vector<std::string> &expressions, const std::string &dialect,
bool flatten, const std::string &noData,
const std::vector<std::vector<std::string>> &pixelFunctionArguments,
const GDALCalcOptions &options, int &maxSourceBands,
const std::string &fakeSourceFilename = std::string())
{
if (inputs.empty())
{
return nullptr;
}
std::map<std::string, std::string> sources;
std::string firstSource;
bool requireSourceNames = dialect != "builtin";
if (!ParseSourceDescriptors(inputs, sources, firstSource,
requireSourceNames))
{
return nullptr;
}
// Use the first source provided to determine properties of the output
const char *firstDSN = sources[firstSource].c_str();
maxSourceBands = 0;
// Read properties from the first source
SourceProperties out;
{
std::unique_ptr<GDALDataset> ds(
GDALDataset::Open(firstDSN, GDAL_OF_RASTER));
if (!ds)
{
CPLError(CE_Failure, CPLE_AppDefined, "Failed to open %s",
firstDSN);
return nullptr;
}
out.nX = ds->GetRasterXSize();
out.nY = ds->GetRasterYSize();
out.nBands = 1;
out.srs =
OGRSpatialReferenceRefCountedPtr::makeClone(ds->GetSpatialRef());
out.hasGT = ds->GetGeoTransform(out.gt) == CE_None;
}
CPLXMLTreeCloser root(CPLCreateXMLNode(nullptr, CXT_Element, "VRTDataset"));
maxSourceBands = 0;
// Collect properties of the different sources, and verity them for
// consistency.
std::map<std::string, SourceProperties> sourceProps;
for (const auto &[source_name, dsn] : sources)
{
// TODO avoid opening the first source twice.
auto props = UpdateSourceProperties(out, dsn, options);
if (props.has_value())
{
maxSourceBands = std::max(maxSourceBands, props->nBands);
sourceProps[source_name] = std::move(props.value());
}
else
{
return nullptr;
}
}
size_t iExpr = 0;
for (const auto &origExpression : expressions)
{
GDALDataType bandType = options.dstType;
// If output band type has not been specified, set it equal to the
// input band type for certain pixel functions, if the inputs have
// a consistent band type.
if (bandType == GDT_Unknown && dialect == "builtin" &&
(origExpression == "min" || origExpression == "max" ||
origExpression == "mode"))
{
for (const auto &[_, props] : sourceProps)
{
if (bandType == GDT_Unknown)
{
bandType = props.eDT;
}
else if (props.eDT == GDT_Unknown || props.eDT != bandType)
{
bandType = GDT_Unknown;
break;
}
}
}
if (!CreateDerivedBandXML(pszVRTFilename, root.get(), out.nX, out.nY,
bandType, origExpression, dialect, flatten,
noData, pixelFunctionArguments[iExpr],
sources, sourceProps, fakeSourceFilename))
{
return nullptr;
}
++iExpr;
}
// CPLDebug("VRT", "%s", CPLSerializeXMLTree(root.get()));
auto ds = fakeSourceFilename.empty()
? std::make_unique<VRTDataset>(out.nX, out.nY)
: std::make_unique<VRTDataset>(1, 1);
if (ds->XMLInit(root.get(), pszVRTFilename[0]
? CPLGetPathSafe(pszVRTFilename).c_str()
: "") != CE_None)
{
return nullptr;
};
if (out.hasGT)
{
ds->SetGeoTransform(out.gt);
}
if (out.srs)
{
ds->SetSpatialRef(out.srs.get());
}
return ds;
}
/************************************************************************/
/* GDALRasterCalcAlgorithm::GDALRasterCalcAlgorithm() */
/************************************************************************/
GDALRasterCalcAlgorithm::GDALRasterCalcAlgorithm(bool standaloneStep) noexcept
: GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
ConstructorOptions()
.SetStandaloneStep(standaloneStep)
.SetAddDefaultArguments(false)
.SetAutoOpenInputDatasets(false)
.SetInputDatasetInputFlags(GADV_NAME)
.SetInputDatasetMetaVar("INPUTS")
.SetInputDatasetMaxCount(INT_MAX))
{
AddRasterInputArgs(false, false);
if (standaloneStep)
{
AddProgressArg();
AddRasterOutputArgs(false);
}
AddOutputDataTypeArg(&m_type);
AddArg("no-check-crs", 0,
_("Do not check consistency of input coordinate reference systems"),
&m_noCheckCRS)
.AddHiddenAlias("no-check-srs");
AddArg("no-check-extent", 0, _("Do not check consistency of input extents"),
&m_noCheckExtent);
AddArg("propagate-nodata", 0,
_("Whether to set pixels to the output NoData value if any of the "
"input pixels is NoData"),
&m_propagateNoData);
AddArg("calc", 0, _("Expression(s) to evaluate"), &m_expr)
.SetRequired()
.SetPackedValuesAllowed(false)
.SetMinCount(1)
.SetAutoCompleteFunction(
[this](const std::string ¤tValue)
{
std::vector<std::string> ret;
if (m_dialect == "builtin")
{
if (currentValue.find('(') == std::string::npos)
return VRTDerivedRasterBand::GetPixelFunctionNames();
}
return ret;
});
AddArg("dialect", 0, _("Expression dialect"), &m_dialect)
.SetDefault(m_dialect)
.SetChoices("muparser", "builtin");
AddArg("flatten", 0,
_("Generate a single band output raster per expression, even if "
"input datasets are multiband"),
&m_flatten);
AddNodataArg(&m_nodata, true);
// This is a hidden option only used by test_gdalalg_raster_calc_expression_rewriting()
// for now
AddArg("no-check-expression", 0,
_("Whether to skip expression validity checks for virtual format "
"output"),
&m_noCheckExpression)
.SetHidden();
AddValidationAction(
[this]()
{
GDALPipelineStepRunContext ctxt;
return m_noCheckExpression || !IsGDALGOutput() || RunStep(ctxt);
});
}
/************************************************************************/
/* GDALRasterCalcAlgorithm::RunImpl() */
/************************************************************************/
bool GDALRasterCalcAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
void *pProgressData)
{
GDALPipelineStepRunContext stepCtxt;
stepCtxt.m_pfnProgress = pfnProgress;
stepCtxt.m_pProgressData = pProgressData;
return RunPreStepPipelineValidations() && RunStep(stepCtxt);
}
/************************************************************************/
/* GDALRasterCalcAlgorithm::RunStep() */
/************************************************************************/
bool GDALRasterCalcAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt)
{
CPLAssert(!m_outputDataset.GetDatasetRef());
GDALCalcOptions options;
options.checkExtent = !m_noCheckExtent;
options.checkCRS = !m_noCheckCRS;
if (!m_type.empty())
{
options.dstType = GDALGetDataTypeByName(m_type.c_str());
}
std::vector<std::string> inputFilenames;
if (!ReadFileLists(m_inputDataset, inputFilenames))
{
return false;
}
std::vector<std::vector<std::string>> pixelFunctionArgs;
if (m_dialect == "builtin")
{
for (std::string &expr : m_expr)
{
const CPLStringList aosTokens(
CSLTokenizeString2(expr.c_str(), "()",