Skip to content

Python bindings: add gdal.Run() to run GDAL algorithms #12150

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

Merged
merged 2 commits into from
Apr 17, 2025

Conversation

rouault
Copy link
Member

@rouault rouault commented Apr 15, 2025

Fixes #11832

CC @dbaston

@rouault rouault added enhancement python Pull requests that update Python code funded through GSP Work funded through the GDAL Sponsorship Program gdal_cli Anything related to the new 3.11 "gdal" CLI frontend labels Apr 15, 2025
@rouault rouault added this to the 3.11.0 milestone Apr 15, 2025
@rouault rouault force-pushed the fix_11832 branch 2 times, most recently from 1d13b2e to 1dfbb23 Compare April 15, 2025 17:57
@coveralls
Copy link
Collaborator

coveralls commented Apr 15, 2025

Coverage Status

coverage: 70.691% (+0.004%) from 70.687%
when pulling b8c483b on rouault:fix_11832
into f401ea8 on OSGeo:master.

@dbaston
Copy link
Member

dbaston commented Apr 16, 2025

I see the "run" function doing three separate things:

  • providing a simplified interface to call an algorithm
  • providing a way to collect all of an algorithm's outputs
  • providing a means to automatically clean up any output datasets (via Finalize)

A user may want to do any combination of these, so I would look at separating them. Maybe something like:

gdal.run -> constructs an algorithm, runs it, and returns it
with gdal.run -> same as above, but calls Finalize at end of block
alg.outputs(parse_json=True) -> returns a (possibly empty) dict of an algorithm's outputs, exception if alg has not run yet
alg.output(parse_json=True) -> returns the single output or exception if count != 1

@rouault
Copy link
Member Author

rouault commented Apr 16, 2025

Thanks for all the suggestions @dbaston. They are now implemented

@rouault rouault added python bindings and removed python Pull requests that update Python code labels Apr 16, 2025
@rouault rouault force-pushed the fix_11832 branch 3 times, most recently from 2451f1e to b8c483b Compare April 16, 2025 21:59
@rouault
Copy link
Member Author

rouault commented Apr 17, 2025

I've also added the capability of doing gdal.Run("gdal", "info", input="in.tif"), that is use *args trickery to collect unnamed arguments and assume they are the path. I find that slightly more user friendly

@rouault rouault changed the title Python bindings: add gdal.run() context manager to run GDAL algorithms Python bindings: add gdal.Run() to run GDAL algorithms Apr 17, 2025
@rouault rouault merged commit 2948270 into OSGeo:master Apr 17, 2025
35 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement funded through GSP Work funded through the GDAL Sponsorship Program gdal_cli Anything related to the new 3.11 "gdal" CLI frontend python bindings
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python bindings: more intuitive way of invoking new GDAL cli algorithms ?
3 participants