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.

  • 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 '17 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 '17 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. – Rakesh Giri Apr 6 '17 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 '17 at 14:18
  • 1
    @Joseph thank you so much for your comment, It's working fine now. – Rakesh Giri Apr 7 '17 at 8:57
5

This is an easier alternative.

layer = iface.activeLayer()   
expr = QgsExpression( "num_geometries( $geometry ) > 1" )
it = layer.getFeatures( QgsFeatureRequest( expr ) )    
ids = [i.id() for i in it]  

Now you can know how many features are multi-part:

print 'There are {} multipart features in this layer'.format(len(ids))

And even select multi-part features:

layer.setSelectedFeatures( ids )
  • @Carrillo thank you so much for your comments, Script is working fine now, and script shared by you also looks easy to work with. – Rakesh Giri Apr 7 '17 at 8:56

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.