gdal icon indicating copy to clipboard operation
gdal copied to clipboard

Make gdal_calc.py available via the Python API

Open cpaulik opened this issue 6 years ago • 4 comments

It would be neat to be able to access gdal_calc.py via


from osgeo import gdal

gdal.Calc(...)

I've seen that the functions are already split in a sensible way in gdal_calc.py. Would you be open to a merge request that moves the code to an importable location while not changing the CLI?

cpaulik avatar Sep 11 '19 15:09 cpaulik

I'm not sure how you could do what you want to do since that would involve mixing gdal_calc.py with the GDAL Python module. But you should already be able to import gdal_calc, and call gdal_calc.doit()

rouault avatar Sep 11 '19 16:09 rouault

Doing import gdal_calc does not work for me.

gdal_calc.py is located lib/python2.7/site-packages/GDAL-2.4.2-py2.7-linux-x86_64.egg-info/scripts in my environment which is not in the PYTHONPATH

Shouldn't it work to move the needed functions into a module and then import them in https://github.com/OSGeo/gdal/blob/master/gdal/swig/include/python/gdal_python.i#L82 to make them available when the gdal module is generated?

cpaulik avatar Sep 12 '19 08:09 cpaulik

Shouldn't it work to move the needed functions into a module and then import them in https://github.com/OSGeo/gdal/blob/master/gdal/swig/include/python/gdal_python.i#L82 to make them available when the gdal module is generated?

that's a possility. For backward compatibility we'd still want the gdal_calc.py script to be usable (would probably be a few line long script importing the module and excuting gdal.Calc())

rouault avatar Sep 12 '19 22:09 rouault

Working on a task that will really benefit from gdal.Calc() instead of subprocess.call([sys.executable, 'venv/Scripts/gdal_calc.py'... which is really hard to package in an installer. Was the provision of gdal_calc via the Python API achieved so far?

Japhetho avatar Dec 03 '21 06:12 Japhetho