-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Port gdalcalc with formula excaping fix #6984
Port gdalcalc with formula excaping fix #6984
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!! So nice to see this resurrected.
@@ -176,7 +176,7 @@ def loadAlgorithms(self): | |||
warp(), | |||
# rasterize(), | |||
# ExtractProjection(), | |||
# gdalcalc(), | |||
gdalcalc(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#pedantic , but we've started sorting these imports and algorithms
self.tr('Calculation in gdalnumeric syntax using +-/* or any numpy array functions (i.e. logical_and())'), | ||
'A*2', | ||
optional=False)) | ||
self.addParameter( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be a number input - copy https://github.com/qgis/QGIS/blob/master/python/plugins/processing/algs/gdal/merge.py#L85
self.addParameter(ParameterString(self.EXTRA, | ||
self.tr('Additional creation parameters'), '', optional=True)) | ||
self.addOutput(OutputRaster(self.OUTPUT, self.tr('Calculated'))) | ||
self.addParameter( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set the metadata for this param to get the nice widget - e.g. copy https://github.com/qgis/QGIS/blob/master/python/plugins/processing/algs/gdal/merge.py#L93
@@ -105,24 +172,32 @@ def group(self): | |||
def groupId(self): | |||
return 'rastermiscellaneous' | |||
|
|||
def commandName(self): | |||
if isWindows(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commandName doesn't include the .py extension - this should always return 'gdal_calc'
noData = self.getParameterValue(self.NO_DATA) | ||
if noData is not None: | ||
noData = str(noData) | ||
if not parameters: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't be needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise would fail test https://github.com/qgis/QGIS/blob/master/python/plugins/processing/tests/GdalAlgorithmsTest.py#L112
or the test should be adapted to consider other type of processing exceptions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you implement the other changes I've listed here then that test should pass ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I see, tnx
# extra = str(extra) | ||
#debug = self.getParameterValue(parameters, self.DEBUG) | ||
formula = self.parameterAsString(parameters, self.FORMULA, context) | ||
noData = self.parameterAsDouble(parameters, self.NO_DATA, context) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you need to handle noData = 0, so copy this logic:
https://github.com/qgis/QGIS/blob/master/python/plugins/processing/algs/gdal/proximity.py#L145
|
||
arguments = [] | ||
arguments.append('--calc') | ||
arguments.append('"' + formula + '"') | ||
arguments.append('--calc "{}"'.format(formula) ) | ||
arguments.append('--format') | ||
arguments.append(GdalUtils.getFormatShortNameFromFilename(out)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here use QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if self.getParameterValue(self.BAND_A): | ||
arguments.append('--A_band ' + self.getParameterValue(self.BAND_A)) | ||
if self.getParameterValue(self.INPUT_B): | ||
arguments.append(self.parameterAsLayer(parameters, self.INPUT_A, context).source()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a bit more safety:
layer_a = self.parameterAsRasterLayer(parameters, self.INPUT_A, context)
if layer_a is None:
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT_A))
arguments.append(layer_a.source())
arguments.append(self.parameterAsLayer(parameters, self.INPUT_A, context).source()) | ||
if self.parameterAsString(parameters, self.BAND_A, context): | ||
arguments.append('--A_band ' + self.parameterAsString(parameters, self.BAND_A, context)) | ||
if self.parameterAsLayer(parameters, self.INPUT_B, context): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the optional layers you would go:
if self.INPUT_B in parameters and parameters[self.INPUT_B] is not None:
layer_b = self.parameterAsRasterLayer(parameters, self.INPUT_B, context)
if layer_b is None:
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT_B))
arguments.append(layer_b.source())
'A*2', | ||
optional=False)) | ||
self.addParameter( | ||
QgsProcessingParameterString( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a numeric parameter, not string
return ['gdal_calc', GdalUtils.escapeAndJoin(arguments)] | ||
else: | ||
return ['gdal_calc.py', GdalUtils.escapeAndJoin(arguments)] | ||
return ['gdal_calc.py', GdalUtils.escapeAndJoin(arguments)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need to use gdal_calc (no .py) outside of windows? Other gdal algs still use the non .py command names on Linux.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no idea if it's a problem with my compied installation... but I receive a "gdal_calc" not found if I use self.commandName(), that's running test locally. No idea if it change on travis, it should be strange. btw I try to set again self.commandName and see what append to travis.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry I was referring running the alg. Tests just check commandConsole return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GDAL command:
gdal_calc --calc "A*2" --format GTiff --type Float32 -A /home/ginetto/PROGRAMMING/GIS/GISDATA/qgis_sample_data/raster/landcover.img --A_band 1 --outfile /tmp/processing_b75de98ae82149cea15c40d81f2d6b96/c8207372386d409d87ef0351a3718727/OUTPUT.tif
GDAL command output:
/bin/sh: 1: gdal_calc: not found
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, confirmed on Fedora - gdal_calc.py works, gdal_calc doesn't
Perfect - much appreciated!! |
thx @nyalldawson for the review |
@nyalldawson should I have to produce a fix for the commandName() result depending on win or not? (adding .py)? or it's a packaging problem? No idea why travis is able to run the command and a compiled version do not! |
I'd wait and see. It looks ok to me as is, so let's just see if anyone reports issues. |
I'll ask to @gioman if he can verify any issue on his different platforms |
so I can't explain because it pass tests on docker! any idea @3nids ? |
Description
Ported missing gdal processing alg gdalcalc (wrapper to wdal_calc) to 3.X. Console tests included.
fix promoted inside GeoMove [1] project for Cartolab [2] (A Coruña university)
[1] http://cartolab.udc.es/geomove/
[2] http://cartolab.udc.es
Checklist
fixes #11111
in the commit message next to the description[FEATURE]
in the commit message[needs-docs]
in the commit message and contain sufficient information in the commit message to be documentedscripts/prepare-commit.sh
script before each commit