I have a code where I would like to be able to use both GeoPandas and PyQGIS. However, it seems that including GeoPandas is causing issues in the output of PyQGIS.

Here is a small illustration using NYC OpenData school districts and census blocks

import geopandas
import os
import sys

# Add to Python Path

# Initialize QGIS
from qgis.core import *
from qgis.gui import *
qgs = QgsApplication([], False)
QgsApplication.setPrefixPath("/Applications/QGIS.app/Contents/MacOS", True)

# Setup Processing
from PyQt4 import QtCore, QtGui
import processing
  # create dummy iface
class DummyInterface(object):
    def __init__(self):
        self.destCrs = None
    def __getattr__(self, *args, **kwargs):
        def dummy(*args, **kwargs):
            return DummyInterface()
        return dummy
    def __iter__(self):
        return self
    def next(self):
        raise StopIteration
    def layers(self):
        # simulate iface.legendInterface().layers()
        return QgsMapLayerRegistry.instance().mapLayers().values()
iface = DummyInterface()
  # initalise processing plugin with dummy iface object
plugin = processing.classFactory(iface)

# Set paths
districts = 'School Districts/geo_export_cdfc9016-ca65-4c45-8423-7acc709a42a2.shp'
blocks = '2010 Census Blocks/geo_export_ae8a0155-ccdc-4a7b-a42e-6707282035cc.shp'
district32 = 'School Districts/district32.shp'
outfile = 'clipped_blocks.shp'

# Select school district 32 and save as new file
districts = QgsVectorLayer(districts,'districts',"ogr")
query = '"school_dis"=32'
selection = districts.getFeatures(QgsFeatureRequest().setFilterExpression(query))
districts.setSelectedFeatures([k.id() for k in selection])
_writer = QgsVectorFileWriter.writeAsVectorFormat(districts,district32,"utf-8",None,"ESRI Shapefile",True)

# Clip blocks with school district 32


If I run this code without the import geopandas, then I get this clipping: Clip_NoGeoPandas which is correct. But if I do it with the import geopandas, then I get this incorrect clipping: Clip_GeoPandas

It seems that simply by importing GeoPandas, it messes up the QGIS processing algorithms. This keeps happening in my code. No errors are thrown, and it's sort of random which processes work and which ones don't. I've had issues with "qgis:clip" and "qgis:dissolve", but other ones like "qgis:deleteduplicategeometries" and "grass7:v.clean" seem to work fine.

Is there an issue with the way I've setup the PyQGIS processing or are these two just fundamentally incompatible? Ideally I'd like to be able to use them in the same code as sometimes it's easier to do a task in GeoPandas than PyQGIS and vice versa.

I am running this on a Mac (OS ver. 10.13) with QGIS 2.18.15, Python 2.17.14 and GeoPandas 0.3.0.

  • 1
    Suggest you try updating to QGIS 2.18.20 and/or submit a ticket on the QGIS Issue Tracker. Might also be worth seeing if import shapely has the same effect. Shapely is the geometry library GeoPandas uses. Shapely is built on GEOS which I think QGIS also makes use of under the hood. Might be some library version incompatibility going on between QGIS GEOS and Shapely/GeoPandas GEOS...??? Could also try making import geopandas the very last import, after qgis, PyQT4, processing etc. to force qgis binary libs, ie GEOS, to get loaded first. – user2856 Jun 14 '18 at 4:21
  • Making import geopandas as the last import fixes this error. import shapely doesn't cause any issues. However, when I made GeoPandas the last import in my larger code, I still had some issues (e.g. with merging and the field calculator). – mcc9092 Jun 14 '18 at 18:50
  • Might be another library incompatibility then. I just automatically assumed shapely/geos because of the geometry. – user2856 Jun 14 '18 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.