Skip to content
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

Reusable python scripts (now gdal_calc.py) #121

Closed
wants to merge 5 commits into from

Conversation

pierstitus
Copy link

The same way as the GDAL utilities are made available as library (https://trac.osgeo.org/gdal/wiki/rfc59.1_utilities_as_a_library) it would be nice have the python utilities also available for calling directly from python. Judging from Stack Exchange there is quite some interest in this possibility.

I now implemented an interface for gdal_calc.py, so it can be used from python for example as
import gdal_calc
gdal_calc.Calc("-log10(A)", outfile="output.tif", A="raster.tif", A_band=1)

This could be done for the other python utilities as well. For some (gdal_edit and gdal_pansharpen) there is already a very basic (not documented) interface which just takes the command line arguments, it would be nice to make them more pythonic too.

There are some open questions:

  1. Naming, whether to use gdal_calc, calc or Calc. gdal_calc is most recognizable, calc is python default style for functions, Calc would be like the rest of GDAL. So now I chose for the last option.
  2. Whether or not include it in osgeo.gdal, so it can be accessed in the same way as the other tools. I'd say it would be a good thing, maybe in a try: clause for the case that they are not installed or not on the path or so. In ubuntu /usr/bin is in the python path by default, I don't know how that is on other systems (windows/geo4w?).

Furthermore I fixed a bug which came in in GDAL 2.0, namely that functions are not directly available for use in expressions anymore, so for example "log10(A)" fails. This is the first commit and should be merged to version 2.0 and 2.1 also.

… clean way

commit 4b6a415 (30-01-2015) removed it in a cleanup (accidentally),
       so for examle 'log10(A)' didn't work (in gdal 2.0/2.1)
Some other points:
- raise exceptions on errors
- --quiet option to suppress progress messages
@rouault
Copy link
Member

rouault commented Apr 19, 2016

Would be good if autotest/pyscripts/test_gdal_calc.py could be enhanced to demonstrate the bug fix for functions in expressions, and the use of gdal_calc.Calc()

Old default was confusing and could easily be used accidentally
For numpy functions and new python interface
@pierstitus
Copy link
Author

Ok, added tests.

Also while testing found that the default Float32 NoDataValue can be very confusing, so changed that to the more standard maximum float

Any ideas about including the Calc() funtion in the osgeo.gdal namespace?

kwrobot pushed a commit to aashish24/gdal-svn that referenced this pull request Apr 20, 2016
…gain, now in a clean way, to fix 2.0 regression that made for examle 'log10(A)' to no longer work (patch by Piers Titus van der Torren, OSGeo/gdal#121)

git-svn-id: https://svn.osgeo.org/gdal/trunk/gdal@34034 f0d54148-0727-0410-94bb-9a71ac55c965
@rouault
Copy link
Member

rouault commented Apr 20, 2016

trunk r34034, branches/2.1 r34035, branches/2.0 r34036 "gdal_calc.py: Add * from gdalnumeric to gdal_calc.py eval namespace again, now in a clean way, to fix 2.0 regression that made for examle 'log10(A)' to no longer work (patch by Piers Titus van der Torren, #121)"

trunk r34037 "gdal_calc.py: Add function to gdal_calc.py to make it callable from python (gdal_calc.Calc()), raise exceptions on errors, --quiet option to suppress progress messages, change default Float32 NoDataValue to more standard 3.402823466E+38 (patch by Piers Titus van der Torren, #121)"

Not sure about how/f including Calc() into osgeo.gdal. Perhaps should be raised on gdal-dev mailing list

@rouault rouault closed this Apr 20, 2016
kwrobot pushed a commit to aashish24/gdal-svn that referenced this pull request Apr 20, 2016
…ython (gdal_calc.Calc()), raise exceptions on errors, --quiet option to suppress progress messages, change default Float32 NoDataValue to more standard 3.402823466E+38 (patch by Piers Titus van der Torren, OSGeo/gdal#121)

git-svn-id: https://svn.osgeo.org/gdal/trunk/gdal@34037 f0d54148-0727-0410-94bb-9a71ac55c965
@alavenant
Copy link

How transform a gdal_calc.py command line to gdal_calc.calc().

My command :
gdal_calc.py -A tmp_highest.vrt -B tmp_lowest.vrt --outfile=tmp_artefacts_mask.tif --calc=((A-B)>=50) --NoDataValue=0 --type=Byte

I try :
gdal_calc.Calc('-A tmp_highest.vrt -B tmp_lowest.vrt', outfile='tmp_artefacts_mask.tif', calc=((A-B)>=50), NoDataValue=0, type='Byte')

I have "Calc() got multiple values for argument 'calc'"

Thanks

@pierstitus
Copy link
Author

@alavenant You should provide the filenames in a pythonic way too, and the calculation should be a string:

gdal_calc.Calc(A='tmp_highest.vrt', B='tmp_lowest.vrt', outfile='tmp_artefacts_mask.tif', calc='((A-B)>=50)', NoDataValue=0, type='Byte')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants