I have a drains layer (polygon) around which I have created buffers of varying distances. I want to find the intersection between the different buffer layers and pipes (line) layer and accordingly assign score in a field. I have used the following code:

pathName = os.path.dirname(unicode(drainLayer.dataProvider().dataSourceUri()))
for filename in os.listdir(pathName):
    if filename.endswith('1.shp') and ('buf' in filename):
        #name = ntpath.basename(filename).split('.')[0]
        buf = QgsVectorLayer(pathName,filename,"ogr")
        for f1 in waterLayer.getFeatures():
            for f2 in buf.getFeatures():
                if f1.geometry().intersects(f2.geometry()):
for filename in os.listdir(pathName):
    if filename.endswith('2.shp') and ('buf' in filename):
        #name = ntpath.basename(filename).split('.')[0]
        buf2 = QgsVectorLayer(pathName,filename,"ogr")
        for f1 in waterLayer.getFeatures():
            for f2 in buf2.getFeatures():
                if (f1.geometry().crosses(f2.geometry())):
                    f1['drainScore'] =1

Wherever the intersections are complete it gives the correct output. But wherever there are partial intersections, it does not identify them. I have used crosses,touches and intersects method in the second for loop.

I have also tried processing algorithms like qgis:intersection, qgis:joinattributesbylocation (with crosses as PREDICATE) but none of them recognize partial intersections. When I use the vector tools of intersection and join attributes by location from QGIS 3 desktop, it gives the correct output, i.e., it recognizes partial as well as complete intersection. I have attached an image of the kind of partial intersection I want the code to recognize.

enter image description here

  • i had additionally tried clip and difference tools of QGIS also. I observed the same result, QGIS tools gave different outputs as compared to corresponding processing algorithms in python. I had to use a round about way to solve the problem. Am i missing something when using tools and processing.run ? – rao Mar 19 '19 at 12:05

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.