[Qgis-developer] Pyton Script from Qgis Console Help

ASAHI Kosuke asahi at hcc.co.jp
Tue Mar 20 23:26:55 EDT 2012


Hi Vinayan

I can run your script on qgis 1.7.4,
but I change few points adapt to script runner plugin(see following link).
 
http://spatialgalaxy.net/2012/01/29/script-runner-a-plugin-to-run-python-scripts-in-qgis/

Please run this script on script runner.
  http://d.hatena.ne.jp/waigani/files/rotate.py?d=download

If you have crash again, I need more infomation (OS? version? etc..).

(2012/03/21 1:14), vinayan wrote:
> Hi all,
>
> I just read about running python scrips from Qgis Console(article by
> Gary Sherman
> http://spatialgalaxy.net/2012/01/27/qgis-running-scripts-in-the-python-console/).
> I am trying rotate each point in a layer based on a center point. I
> managed some code by copying from the CadTools plugin(by Stefan
> Ziegler). Whenever i run this from qgis console, it crashes the
> application. Can somebody point me in the right direction? Please find
> the code below. I have to admit that i started python only yesterday.
>
> Note: I am not sure if this is the place to ask this. But i have been
> inspired by reading
>
>
> ------------------------------------------------------------------------------------------------------------------------------------
> #!/usr/bin/env Python
> """Load all shapefiles in a given directory.
> This script (iteratefeature.py) runs from the QGIS Python console.
>  From the console, use:
> from iteratefeature import Looper
> ldr = Looper(qgis.utils.iface)
> ldr.LoopXY()
>
> """
> from glob import glob
> from os import path
> from qgis.core import *
> import math
>
> class Looper:
> def __init__(self, iface):
> """Initialize using the qgis.utils.iface
> object passed from the console.
>
> """
> self.iface = iface
>
>
> def LoopXY(self):
>
> basePoint=QgsPoint(-121.3140824, 38.63196648)
> rotAngle=45
>
> mc = self.iface.mapCanvas()
> layer = mc.layer(0)
> provider = layer.dataProvider()
>
> feat = QgsFeature()
> provider.select()
>
>
> while(provider.nextFeature(feat)):
> #layer.startEditing()
> geometry = feat.geometry()
> geom1=rotate(geometry,basePoint,rotAngle * math.pi / 180)
> layer.changeGeometry(feat.id(), geom1)
> #feat.setGeometry(geom1)
> #print "X Coord %d: " %geometry.asPoint().x()
> #print "Y Coord %d: " %geometry.asPoint().y()
> layer.commitChanges()
> print "updated %d: " %feat.id()
> print
>
> # Rotate script
> def rotate(geometry, point, angle):
>
> if angle == 0 or angle == 2 * math.pi or angle == -2 * math.pi:
> return geom
>
> p0 = geometry.asPoint()
> p1 = QgsPoint(p0.x() - point.x(), p0.y() - point.y())
> p2 = rotatePoint(p1, angle)
> p3 = QgsPoint(point.x() + p2.x(), point.y() + p2.y())
> return QgsGeometry().fromPoint(p3)
>
>
> # Rotates a single point (centre 0/0).
> # (c) Stefan Ziegler
> def rotatePoint(point, angle):
> x = math.cos(angle)*point.x() - math.sin(angle)*point.y()
> y = math.sin(angle)*point.x() + math.cos(angle)*point.y()
> return QgsPoint(x, y)
>
>
> ----------------------------------------------------------------------------------------------------------------------------
>
> Regards
> Vinayan
>
>
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer


-- 
ASAHI Kosuke



More information about the Qgis-developer mailing list