<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body text="#000000" bgcolor="#ffffff">
<font face="Helvetica, Arial, sans-serif">Hi all,<br>
<br>
I just read about running python scrips from Qgis Console(article
by Gary Sherman
<a class="moz-txt-link-freetext" href="http://spatialgalaxy.net/2012/01/27/qgis-running-scripts-in-the-python-console/">http://spatialgalaxy.net/2012/01/27/qgis-running-scripts-in-the-python-console/</a>).
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 </font><font
face="Helvetica, Arial, sans-serif">Stefan Ziegler)</font><font
face="Helvetica, Arial, sans-serif">. 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.<br>
<br>
Note: I am not sure if this is the place to ask this. But i have
been inspired by reading <br>
<br>
<br>
------------------------------------------------------------------------------------------------------------------------------------<br>
#!/usr/bin/env Python<br>
"""Load all shapefiles in a given directory.<br>
This script (iteratefeature.py) runs from the QGIS Python
console.<br>
From the console, use:<br>
from iteratefeature import Looper<br>
ldr = Looper(qgis.utils.iface)<br>
ldr.LoopXY()<br>
<br>
"""<br>
from glob import glob<br>
from os import path<br>
from qgis.core import *<br>
import math<br>
<br>
class Looper:<br>
def __init__(self, iface):<br>
"""Initialize using the qgis.utils.iface <br>
object passed from the console.<br>
<br>
"""<br>
self.iface = iface<br>
<br>
<br>
def LoopXY(self):<br>
<br>
basePoint=QgsPoint(-121.3140824, 38.63196648)<br>
rotAngle=45<br>
<br>
mc = self.iface.mapCanvas()<br>
layer = mc.layer(0)<br>
provider = layer.dataProvider()<br>
<br>
feat = QgsFeature()<br>
provider.select()<br>
<br>
<br>
while(provider.nextFeature(feat)):<br>
#layer.startEditing()<br>
geometry = feat.geometry()<br>
geom1=rotate(geometry,basePoint,rotAngle * math.pi / 180)<br>
layer.changeGeometry(feat.id(), geom1)<br>
#feat.setGeometry(geom1)<br>
#print "X Coord %d: " %geometry.asPoint().x()<br>
#print "Y Coord %d: " %geometry.asPoint().y()<br>
layer.commitChanges()<br>
print "updated %d: " %feat.id()<br>
print<br>
<br>
# Rotate script<br>
def rotate(geometry, point, angle):<br>
<br>
if angle == 0 or angle == 2 * math.pi or angle == -2 *
math.pi:<br>
return geom<br>
<br>
p0 = geometry.asPoint()<br>
p1 = QgsPoint(p0.x() - point.x(), p0.y() - point.y())<br>
p2 = rotatePoint(p1, angle)<br>
p3 = QgsPoint(point.x() + p2.x(), point.y() + p2.y())<br>
return QgsGeometry().fromPoint(p3)<br>
<br>
<br>
# Rotates a single point (centre 0/0).<br>
# (c) Stefan Ziegler<br>
def rotatePoint(point, angle):<br>
x = math.cos(angle)*point.x() - math.sin(angle)*point.y()<br>
y = math.sin(angle)*point.x() + math.cos(angle)*point.y()<br>
return QgsPoint(x, y)<br>
<br>
<br>
----------------------------------------------------------------------------------------------------------------------------<br>
<br>
Regards<br>
Vinayan<br>
<br>
</font>
</body>
</html>