gdal icon indicating copy to clipboard operation
gdal copied to clipboard

gdal raster calc: add a --dialect=muparser|builtin, where builtin can be used to compute a single band from all bands of a single input dataset

Open rouault opened this issue 6 months ago • 8 comments

CC @dbaston

rouault avatar Jun 10 '25 16:06 rouault

@rouault This is along the lines of what I was thinking with this comment: https://github.com/OSGeo/gdal/pull/12406#discussion_r2094547998

But rather than an argument --pixel-function that is mutually exclusive with --calc, I would consider something like --dialect [muparser/builtin] that can be eventually extended to --dialect python

dbaston avatar Jun 10 '25 17:06 dbaston

But rather than an argument --pixel-function that is mutually exclusive with --calc, I would consider something like --dialect [muparser/builtin] that can be eventually extended to --dialect python

done

rouault avatar Jun 10 '25 17:06 rouault

Isn't this behavior different from the muparser dialect in a fundamental way, in that with muparser N bands of input will produce N bands of output, but with --dialect builtin, N bands of input will produce 1 band of output?

Both modes seem useful but I think that behavior should remain independent of the dialect.

So if X is a 3-band raster, gdal calc --dialect muparser --calc "sum(X)" currently creates a 3-band raster. But you could also imagine a --flatten switch, in which it creates a 1-band raster with the sum of the three input bands.

Similarly,, if X and Y are 3-band rasters, gdal calc --dialect builtin --calc sum could create a 3-band raster whose bands represent X1 + Y1, X2 + Y2, X3 + Y3.

dbaston avatar Jun 10 '25 21:06 dbaston

But you could also imagine a --flatten switch, in which it creates a 1-band raster with the sum of the three input bands.

Excellent suggestion. Thanks for challenging my proposals! Now implemented

rouault avatar Jun 11 '25 15:06 rouault

Nice! I have been tied up on other things but hope to look at this soon.

dbaston avatar Jun 12 '25 01:06 dbaston

With muparser "sum(X, Y)" we get X[1] + Y[1], X[2] + Y[2], but with builtin "sum" we are getting X[1] + Y[1], Y[2]

any suggestion on what we should do ?

rouault avatar Jun 16 '25 19:06 rouault

I implemented the muparser behavior intentionally to match what numpy would do in this situation. That's why the inputs must have the same number or bands, or a single band.

dbaston avatar Jun 16 '25 19:06 dbaston

I implemented the muparser behavior intentionally to match what numpy would do in this situation. That's why the inputs must have the same number or bands, or a single band.

interesting. Didn't know that numpy would do this. --dialect=builtin updated to have same behavior

rouault avatar Jun 17 '25 10:06 rouault

We don't need source names for --dialect builtin, so https://github.com/rouault/gdal/pull/65 removes that requirement

dbaston avatar Jun 17 '25 18:06 dbaston

Additional suggestions in https://github.com/rouault/gdal/pull/68, https://github.com/rouault/gdal/pull/67

dbaston avatar Jun 18 '25 01:06 dbaston