How to import GDAL embedded in new Fiona wheels

240 Views Asked by At

Since october 2022, Fiona's wheels include GDAL (according to the releases documentations). Many packages refer to GDAL using this command, but it won't work :

from osgeo import gdal

For instance, I've just loaded an environment using poetry (and python 3.9.15 on Linux) :

poetry new dummy
cd dummy
poetry add geopandas richdem

Calling this script will throw an error:

import richdem as rd
dem = rd.LoadGDAL("dummy")

Exception: richdem.LoadGDAL() requires GDAL.

I don't think this troubles arises specifically from richdem, but it will be linked to any third package using from osgeo import gdal. Indeed, this is the command which is loaded in richdem's __init__.py file.

I also tried to load Fiona at first (as it patches GDAL environment variables at launch), but it doesn't change anything.

Note :

If I now install gdal (same version as the one included in my 1.8.22 Fiona, ie. gdal 3.4.3), then :

  • rd.LoadGDAL("dummy") triggers a correct RuntimeError: dummy: No such file or directory
  • rd.LoadGDAL("a/real/tif_file.tif") triggers a ModuleNotFoundError: No module named '_gdal_array'

I think this is a distinct problem; besides, I now have two (same) distributions of GDAL on my pc, which can't be good.

So the question is : how could I fix the from osgeo import gdal from third party packages calling for GDAL (if GDAL is embedded in Fiona) ?

1

There are 1 best solutions below

0
tgrandje On

Long story short : you can't simply switch from osgeo to Fiona.

In fact, Fiona doesn't includes GDAL python package (ie. the GDAL python bindings) but the shared library :

From Sean Gillies :

Fiona's wheels contain a GDAL shared library (libgdal.dll or .so or .dylib) and its own library dependencies (libproj, etc). By GDAL, we mean the C library.

Beware also of trying to use both GDAL (ie. python bindings and Fiona) at the same time :

From Sean Gillies :

It's not a great solution, I agree, and not only because you'll have two copies of libgdal. The Fiona project is not promoting it as a solution at all. In https://rasterio.readthedocs.io/en/latest/topics/switch.html#mutual-incompatibilities, I've warned users against this and I should probably do the same for Fiona to make it more clear.

More detailed info on fiona.groups.io; following that discussion, an issue was opened on github Fiona's repo.