[Qgis-developer] How to iterate over the vertices of a geometry from python

Nathan Woodrow madmanwoo at gmail.com
Sun May 13 05:10:42 PDT 2012


Sure.

You can use:

for v in geom.asPolyline():
    print v.x(), v.y()

asPolyline returns QVector<QgsPoint> which is
just a List of QgsPoints in Python.  Access like normal

- Nathan

On Sun, May 13, 2012 at 9:48 PM, Ricardo Filipe Soares Garcia da
<ricardo.garcia.silva at gmail.com> wrote:
> Hi list
>
> I'm trying to translate and rotate a geometry using python, in order
> to create parallel lines.
>
> For the translation I could use QgsGeometry's translate() method, but
> for the rotation I haven't found a ready-made solution. So my current
> strategy is to individually rotate each vertex, according to a
> specified angle and length. For this i'd like to iterate over the
> geometry's vertices.
>
> The problem seems to be that the api methods that return vertex
> coordinates (vertexAt(), adjacentVertices, ...) always return a value
> even if the specified vertex index is wrong. For example, if I use a
> geometry with 5 vertices and call:
>
> geometry.vertexAt(100)
>
> -> (0,0)
>
> I cannot know if there is a vertex placed at the (0, 0) coordinates or
> if there is no vertex at all.
> Is there some way to iterate over a geometry's vertices? Or maybe get
> an exception when asking for an invalid one?
>
> Thanks
>
>
> --
> ___________________________ ___ __
> Ricardo Garcia Silva
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the Qgis-developer mailing list