2

My script is -

# Find all multipart features in the active layer
l = iface.activeLayer()
iter = l.getFeatures()
geoms = []
for feature in iter:
    geom = feature.geometry()
    if geom.isMultipart():
        l.select(feature.id())
        geoms.append(geom)

print 'There are %i multipart features in this layer' % len(geoms)

I don't know the exact problem with this script. Using this script in the Python console should select all multi part features in the active layer but in my case no multi section polygons fetched, while I can easily see multi section polygons exists in my polygon file.

5
  • 1
    Your title should be a summary of what is in the body of your question. I think it would be worth you reviewing this for some ideas on question structure.
    – PolyGeo
    Apr 6, 2017 at 12:04
  • 3
    Your code works fine for me, are you sure your layer is a multipolygon? You can check by typing l.wkbType(). If the value returned is 6 then it is a multipolygon. You can check here for the wkb types.
    – Joseph
    Apr 6, 2017 at 12:05
  • 1
    Joseph, thanks for the answer. I ran l.wkbType() it does'nt give '6' but I can clearly see that there are multi section polygons exist in my file. Apr 6, 2017 at 14:13
  • 1
    @RakeshGiri - Try running Vector > Geometry Tools > Singleparts to multipart then try your code again. Also, if you want to notify someone, use the @ before their name so that they can receive a notification. I just stumbled back to this post by coincidence :)
    – Joseph
    Apr 6, 2017 at 14:18
  • 1
    @Joseph thank you so much for your comment, It's working fine now. Apr 7, 2017 at 8:57

1 Answer 1

8

This is an easier alternative.

layer = iface.activeLayer()   
layer.selectByExpression("num_geometries( $geometry ) > 1")

That's it!


After that, note that you could also know how many features are multi-part:

print('There are {} multipart features in this layer'.format(
    layer.selectedFeatureCount()))
1
  • @Carrillo thank you so much for your comments, Script is working fine now, and script shared by you also looks easy to work with. Apr 7, 2017 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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