[Qgis-developer] Pyton Script from Qgis Console Help

vinayan vinayan123 at gmail.com
Tue Mar 20 12:14:24 EDT 2012


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20120320/ed17a2f5/attachment.html


More information about the Qgis-developer mailing list