[Qgis-developer] How to scale a polygon from pyQgis

Martin Dobias wonder.sk at gmail.com
Fri Feb 6 04:04:23 PST 2015


Hi

>From QGIS 2.8 there is QgsGeometry.transform() method that works with
QTransform. The following code will scale the sample line segment:

from PyQt4.QtGui import QTransform
g = QgsGeometry.fromPolyline([QgsPoint(-1,0),QgsPoint(1,0)])
g.transform( QTransform().scale(2,2) )
print g.asPolyline()

The code will print [(-2,0), (2,0)]

Cheers
Martin


On Fri, Feb 6, 2015 at 6:53 PM, Geo DrinX <geodrinx at gmail.com> wrote:

> Hello,
>
>
> I need to scale a polygon from python code.
>
> An extract of source code is the following :
>
>            for feat in iter:
>               geom = feat.geometry()
>               rect = geom.boundingBox()
>
>               x1 = rect.xMinimum()
>               y1 = rect.yMinimum()
>
>               x2 = rect.xMaximum()
>               y2 = rect.yMinimum()
>
>               xc = (x2 + x1) / 2.0
>               yc = (y2 + y1) / 2.0
>
>                 elem = geom.asPolygon()
>
>                 fat = float(scale) / 100.
>
>                 for i in range(geom.exportToWkt().count(',')):
>
>                             vertex = geom.vertexAt(i)
>
>                             x,y = vertex.x(),
> vertex.y()
>
>                             xR, yR = x-xc , y-yc
>                             xS, yS = xR*fat, yR*fat
>
>                              xT, yT = xS + xc, yS + yc
>
>                              geom.moveVertex(xT, yT, i)
>
>
> Because the polygon do not change, sure I miss something, to save the
> modified element, also if the layer is in editing.
>
> Someone could help me to understand what I wrong ?
>
>
> Thank you
>
> Roberto
>
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20150206/697e7659/attachment.html>


More information about the Qgis-developer mailing list