[Qgis-user] Number of vertices

Martin Bain Martin.Bain at lismore.nsw.gov.au
Sun Apr 23 22:09:00 PDT 2017


Nice! Thanks Nyall

-----Original Message-----
From: Nyall Dawson [mailto:nyall.dawson at gmail.com] 
Sent: Monday, 24 April 2017 3:07 PM
To: Martin Bain <Martin.Bain at lismore.nsw.gov.au>
Cc: QGIS User List <qgis-user at lists.osgeo.org>
Subject: Re: [Qgis-user] Number of vertices

On 24 April 2017 at 11:52, Martin Bain <Martin.Bain at lismore.nsw.gov.au> wrote:
> Hi Anke,
>
> The asPolyline() function of a geometry returns a list of points.  You 
> can then simply get the length of the list:
>
>
>
>
>
> lyr=iface.activeLayer()
>
> sel=lyr.selectedFeatures()
>
> obj=sel[0].geometry()
>
> verts=obj.asPolyline()
>
> print verts
>
> [(526002,6.81305e+06), (526011,6.81309e+06), (526016,6.81311e+06)]
>
>
>
> print len(verts)
>
> 3

While this will work, it's quite slow as it requires copying all the geometry's points into a new list. A faster approach is:

feature.geometry().geometry().nCoordinates()

In case you're wondering, the extra ".geometry()" gives you access to the QgsAbstractGeometry interface containing all the low level methods for working with geometry primitives.

Nyall


>
>
>
> Cheers,
>
> Martin.
>
>
>
> From: Qgis-user [mailto:qgis-user-bounces at lists.osgeo.org] On Behalf 
> Of Anke zur Gölden
> Sent: Sunday, 23 April 2017 4:39 PM
> To: QGIS User List <qgis-user at lists.osgeo.org>
> Subject: [Qgis-user] Number of vertices
>
>
>
> Good morning
>
>
>
> is there a way to get the number of vertices for a Polyline?
>
> I am looking for the python method to get if possible
>
>
>
> thx
>
> Anke
>
>
>
>
> This email is intended for the named recipient only. The information 
> it contains may be confidential. If you are not the intended recipient 
> you must not reproduce or distribute any part of this email, disclose 
> its contents to any other party or take any action in reliance on it. 
> If you have received this email in error, please contact the sender 
> immediately and delete the message.
>
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

This email is intended for the named recipient only.  The information it contains may be confidential.  If you are not the intended recipient you must not reproduce or distribute any part of this email, disclose its contents to any other party or take any action in reliance on it.  If you have received this email in error, please contact the sender immediately and delete the message.


More information about the Qgis-user mailing list