Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I know there would be no tool in arcpy module as such but mignt have in third party modules. I however tried taking the vertices of the polygon and moving using the below formula. but did not get the correct output coordinate values and polygon thus generated is wierd.

I have tried this function

def RotateAxis(AnchorX,AnchorY,inputx,inputy, WindDirection):
    x = inputx - AnchorX
    y = inputy - AnchorY
    resultx = (x * math.cos(WindDirection)) - (y * math.sin(WindDirection)) + AnchorX
    resulty = (x * math.sin(WindDirection)) + (y * math.cos(WindDirection)) + AnchorY
    return (resultx,resulty)

where Anchor is the origin and WindDirection is the angle which I tried taking in radians as well as degrees but could ot get correct results.

I tried with projected coordinates that didn't work either. Is there any suggestion?

share|improve this question
    
Please tell us precisely how you could not get correct results. What were the inputs and what were the outputs? –  whuber Apr 17 '12 at 6:04
    
To quickly answer one of your questions: angular units are radians. –  Mike T Apr 18 '12 at 2:59
add comment

1 Answer

You may use the following function:

def Rotate2D(pts,cnt,ang=pi/4):
    '''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian'''
    return dot(pts-cnt,ar([[cos(ang),sin(ang)],[-sin(ang),cos(ang)]]))+cnt

It works well as can be seen in the following figures: about the anchor point on (0,0):
enter image description here

about the anchor point on (1,0):
enter image description here

about the anchor point on (0.5,0.5):
enter image description here

Update:
Well here is the full code, you must be able to generate the exact results as here:

from __future__ import division                 #to avoid integer devision problem
import scipy
import pylab

#just for fun making further development easier and with joy
pi     = scipy.pi
dot    = scipy.dot
sin    = scipy.sin
cos    = scipy.cos
ar     = scipy.array
rand   = scipy.rand
arange = scipy.arange
plot   = pylab.plot
show   = pylab.show
axis   = pylab.axis
grid   = pylab.grid
title  = pylab.title
rad    = lambda ang: ang*pi/180                 #lovely lambda: degree to radian

#the function
def Rotate2D(pts,cnt,ang=pi/4):
    '''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian'''
    return dot(pts-cnt,ar([[cos(ang),sin(ang)],[-sin(ang),cos(ang)]]))+cnt

#the code for test
pts = ar([[0,0],[1,0],[1,1],[0.5,1.5],[0,1]])
plot(*pts.T,lw=5,color='k')                     #points (poly) to be rotated
for ang in arange(0,2*pi,pi/8):
    ots = Rotate2D(pts,ar([0.5,0.5]),ang)       #the results
    plot(*ots.T)
axis('image')
grid(True)
title('Rotate2D about a point')
show()

The results are:
enter image description here
enter image description here

Good luck!

Appendices:

>>> pts
array([[ 0. ,  0. ],
       [ 1. ,  0. ],
       [ 1. ,  1. ],
       [ 0.5,  1.5],
       [ 0. ,  1. ]])
>>> pts.shape
(5, 2)
>>> anchor = ar([0.5,0.5])
>>> anchor
array([ 0.5,  0.5])
>>> ots = Rotate2D(pts,anchor,ang=rad(45))
>>> ots
array([[ 0.5       , -0.20710678],
       [ 1.20710678,  0.5       ],
       [ 0.5       ,  1.20710678],
       [-0.20710678,  1.20710678],
       [-0.20710678,  0.5       ]])
>>> ots.shape
(5, 2)
>>> '''a single point'''
'a single point'
>>> pts = ar([3.1,1.3])
>>> pts.shape
(2,)
>>> ots = Rotate2D(pts,anchor,ang=rad(30))
>>> ots
array([ 2.35166605,  2.49282032])
>>> ots.shape
(2,)
>>> dts = ots.reshape(-1,2)
>>> dts
array([[ 2.35166605,  2.49282032]])
>>> dts.shape
(1, 2)
share|improve this answer
    
Note that ar is array (numpy or scipy) and also note that cnt is an array i.e., array([0,0]). All these are necessary to avoid problems with multiplication of lists in Python. –  Developer Apr 17 '12 at 13:57
    
I could not get it to work. I have a list of points and one anchor point I would like to get the rotated location of the points calculated per the angle specified. –  Harry Apr 17 '12 at 16:45
    
I have used this code: –  Harry Apr 17 '12 at 16:46
    
Once more, @Harry: exactly how does your attempt at a solution not "work"? We need something to go on here if we're going to do any better than guessing! –  whuber Apr 17 '12 at 16:58
    
def RotateAxis(AnchorX,AnchorY,inputx,inputy, WindDirection): x = inputx - AnchorX y = inputy - AnchorY resultx1 = x * cos(radians(WindDirection)) resultx2 = y * sin(radians(WindDirection)) resultx = resultx1 + resultx2 + AnchorX resulty1 = x * sin(radians(WindDirection)) resulty2 = y * cos(radians(WindDirection)) resulty = resulty1 + resulty2 + AnchorY return (resultx,resulty) –  Harry Apr 17 '12 at 20:44
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.