Clarification on usage with QGIS

Ari Meyer

Note: I originally submitted this as a github issue, as I'd like to see an update to the documentation regarding using Rasterio with QGIS (and perhaps for integrating with any apps that use GDAL).

We're looking to simplify our code for a QGIS plugin to use Rasterio/Shapely in place of GDAL/OGR. Shapely is included in the standard QGIS distribution, but Rasterio is not (not sure why). In any case, I found on :
GDAL’s bindings (gdal for the rest of this document) and Rasterio are not entirely compatible and should not, without a great deal of care, be imported and used in a single Python program. The reason is that the dynamic library they each load (these are C extension modules, remember), on Linux, gdal.dll on Windows, has a number of global objects and the two modules take different approaches to managing these objects.

Does this imply that we may have problems if we try to port the GDAL Python code of our QGIS plugin to Rasterio that we may run into problems, as QGIS is built on GDAL? Can you add a section to clarifying interoperability with QGIS?

Ari Meyer

FYI, Sean Gillies responded on :

I'm certain that until rasterio is included with QGIS, shared library compatibility ("DLL Hell") will be a problem for your plugin. For a compiled program like QGIS, it's fairly simple to make sure that only one instance of a library is loaded, this is the job of the linker. In a Python program, there's no built in way to prevent conflicts when different extension modules cause different instances of the same shared library to be loaded. If I were writing QGIS plugins, I would try to stick to using

Ari Meyer

Looking through the older messages, it seems like integration with QGIS, in particular, is a common enough use case that it really should be discussed separately in the docs.  I will be making a request to the QGIS team that Rasterio be included in the core distribution.  A couple questions for the group:

1)  Does anyone have experience writing QGIS plugins using Rasterio?  If so, did you run into any problems, particularly those related to issue I pointed to above?

2)  Is there a technical reason why Shapely was included in QGIS but Rasterio was not?


Ratcliff, Christina (A&F, Waite Campus)

I've successfully developed the PAT plugin for QGIS using rasterio,  geopandas (uses fiona, shapely). All of these libraries are available in the osgeo4w installer through the advanced install. You will also have to ensure the correct GDAL dll is also installed for the version of rasterio you are using.

My method was to create a windows bat file containing the relevant installation which then can be run as administrator to install the libraries. This will use a command line install of rasterio, geopandas and GDAL dll via OSGeo4W.

Have a look at the following osgeo4w and QGIS  tickets for answers to some of the issues I came across.

I hope this helps.

Ari Meyer

That's exactly what I hoped to hear, Christina -- thank you so much!

One question: are there any other libraries commonly used in the QGIS space that depend upon a particular version of GDAL?  I'm hoping I won't be facing any version conflicts with this setup.

Ratcliff, Christina (A&F, Waite Campus)

Ari, I've been running the Geopandas (pandas, shapely fiona), RasterIO configuration since early 2019, and as long as you have access to those gdal backwards compatibility dll's you should be fine.

With libraries depending on GDAL, where possible use those available in osgeo4w. Osgeo4w will then properly install and get them talking to both GDAL and QGIS.

Good luck.

Ari Meyer

Much appreciated, Christina.  Will try this out.