I have three layers:

  • One point layer consisting of residents
  • One polygon layer with buffers of the first layer (one polygon for every point)
  • Another point layer consisting of containers

I use QGIS 3.4.1

It is possible that multiple container points are within a buffer polygon. My goal is to select all container points within a certain buffer with PyQGIS. Hence, I want to know which containers are near a certain resident. I use the following code:

from osgeo import ogr

#Open datalayers
inw = r"SomePath"
afc = r"SomePath"
inw_buff = r"SomePath"

#set-up for residents
driver_inw = ogr.GetDriverByName("ESRI Shapefile")
dataSource_inw = driver_inw.Open(inw,0)
layer_inw = dataSource_inw.GetLayer()
layerDef_inw = layer_inw.GetLayerDefn()

#set-up for containers
driver_afc = ogr.GetDriverByName("ESRI Shapefile")
dataSource_afc = driver_afc.Open(afc,0)
layer_afc = dataSource_afc.GetLayer()
layerDef_afc = layer_afc.GetLayerDefn()

#set-up for buffers
driver_buff = ogr.GetDriverByName("ESRI Shapefile")
dataSource_buff = driver_buff.Open(inw_buff,0)
layer_buff = dataSource_buff.GetLayer()
layerDef_buff = layer_buff.GetLayerDefn()

#some coordinates from the residents point layer
for f in layer_inw:
    coordx_inw = f[7]
    coordy_inw = f[8]
    ID_inw = f[9]

#find the corresponding buffer polygon
for y in layer_buff:
    if y[9] == ID_inw:
        temp_buff = y

processing.run("native:selectbylocation", {'INPUT':afc,'PREDICATE':[6],'INTERSECT':temp_buff,'METHOD':0})

This gives me the following error:

Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 77, in <module>
File "C:/PROGRA~1/QGIS3~1.4/apps/qgis/./python/plugins\processing\tools\general.py", line 96, in run
return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
File "C:/PROGRA~1/QGIS3~1.4/apps/qgis/./python/plugins\processing\core\Processing.py", line 139, in runAlgorithm
raise QgsProcessingException(msg)
_core.QgsProcessingException: Niet mogelijk uit te voeren algoritme
Kon bronlaag voor INTERSECT niet laden: ongeldige waarde

Translated from Dutch to English it says that the value for INTERSECT is invalid. I looked it up and it does indeed not except a feature class. I tried to place the attributes of the temp_buff into a freshly made QgsVectorLayer, but so far no success.

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.