I have two layers named Layer1 and Layer2 containing some features. Layer2 features are overlapped on Layer1 features.

Using python I want to check if a feature exists on layer2 when i select a feature in layer1. Is there any method to do that?

  • Can anyone help me with this please !!! – Virat ABD Aug 17 '18 at 13:15

If you just want a confirmation, this code will tell you. If you want to do further things, here you have the base to start with. You haven't mentioned the size of your layers. You might encounter some performance issues for big files.

from shapely.geometry import shape, MultiPolygon
import fiona

layer1 = fiona.open('layer1.shp')
layer2 = fiona.open('layer2.shp')
multi_polygon = MultiPolygon([shape(feat['geometry']) for feat in layer2])

for feature in layer1:
    geometry1 = shape(feature['geometry'])
    if geometry1.intersects(multi_polygon):
        print('Feature {} intersects layer 2'.format(feature))
  • Thanks for your help , but its not working in my plugin.I have line features in my layer so is there any changes to make in that code – Virat ABD Aug 22 '18 at 5:35
  • Then change MultiPolygon for MultiLineString – ImanolUr Aug 22 '18 at 8:18
  • above code you used feat['geometry'] in this geometry refers to field of layer right ? – Virat ABD Aug 30 '18 at 9:27

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.