1
legend=iface.legendInterface()
layers=legend.layers()
layer1=layers[0]
layer2=layers[2]
pointCounter=0
for pointFeat in layer1.getFeatures():
    pointGeom=pointFeat.geometry()
    for polyFeat in layer2.getFeatures():
       polyGeom=polyFeat.geometry()
       if pointGeom.disjoint(polyGeom):
          layers[0].select(pointGeom.id())
          pointCounter=pointCounter+1

print pointCounter

not getting the correct output for disjoint

Expected Result The result I got

  • What result are you expecting? – JGH Nov 29 '17 at 12:10
  • @JGH when I try the same disjoint query with QGIS spatial query I get 2 features as result , when I tried with the above code I get 33574 features as result. Layer1 is point layer and layer2 is polygon layer – drama Nov 29 '17 at 12:28
  • In the script, you are counting the number of polygons being disjointed from every point, so you can expect a count of roughly numberOfPoints * (numberOfPolygons -1) – JGH Nov 29 '17 at 12:42
2

The code you wrote counts the number of polygons being disjointed from every point, so assuming the points are in only one polygon, you can expect to find a count of number of points * (number of polygons -1 ).

If you want to count the number of points being disjointed from the entire layer, i.e. from ALL polygons, you would need to do the opposite: discard any point that intersects at least one polygon, and record the ones that do not intersect any polygon. Using your code, it could be modified to the following. Note that there is probably other (better) ways to do it on the entire layer directly.

legend=iface.legendInterface()
layers=legend.layers()
layer1=layers[0]
layer2=layers[2]
pointCounter=0
for pointFeat in layer1.getFeatures():
    pointGeom=pointFeat.geometry()
    intersected = False #reset the boolean flag
    for polyFeat in layer2.getFeatures():
       polyGeom=polyFeat.geometry()
       if pointGeom.intersects(polyGeom):
          intersected = True  #point and polygon do intersect
          break #no need to check other polygons


     #Now that every polygons have been tested, deduct if the point is disjointed or not
     if intersected == False
          layers[0].select(pointGeom.id())
          pointCounter=pointCounter+1

print pointCounter

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.