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 am trying to create a rotated rectangle in lat/lon coordinates.

Basically I have the coordinates of the center and rotation angle around the center.

Is it possible to do using, shapely or gdal, or something else?

thanks

share|improve this question
add comment

5 Answers

up vote 3 down vote accepted

Unfortunately no. Shapely is basically an extension of GEOS, which does not offer any affine transformation abilities. If you are wondering if "how is this done in PostGIS?", they have this capability in liblwgeom, which is a general lightweight geometry function library (DB independent).

The most direct way to rotate (x,y) coordinates is to dig into the mathematics a bit, and use from math import cos, sin. If you are rotating about a non-origin (0,0) coordinate, then you need to add some +/- offsets to transform your coordinates before performing the trig functions.


Update:

As of Shapely version 1.2.17 there is an affinity module to do just this.

from shapely import affinity
from shapely.geometry import LineString

# Example geometry
line = LineString([(1, 3), (1, 1), (4, 1)])

# Rotate 30 degrees CCW from origin at the center of bbox
line_rot_center = affinity.rotate(line, 30, 'center')

# Rotate from origin at (1, 1)
line_rot_11 = affinity.rotate(line, 30, (1, 1))

See help(affinity) for documentation.

share|improve this answer
    
Unfortunately, a rotation in (lat, lon) is not performed with an affine transformation. You either have to project the data and rotate the projected coordinates or else perform a spherical rotation. –  whuber Aug 11 '11 at 15:05
1  
Oh right, I missed the (lat, lon) part of the question. I've never seen a affine transformation function built for geographic inputs, so the tips you provided are essential. –  Mike T Aug 11 '11 at 22:27
    
Since v1.2.17, Shapely has had affine transformations, although they're not documented yet - github.com/Toblerity/Shapely/blob/master/shapely/affinity.py –  Alex L Apr 28 '13 at 7:49
    
Also see the author's blog: sgillies.net/blog/1168/shapely-1-2-17 –  Alex L Apr 28 '13 at 7:50
1  
@AlexL type help(shapely.affinity). Actually I'm nearly done preparing the manual update for the module. –  Mike T Apr 28 '13 at 10:19
show 1 more comment

I don't see any features in Python or Shapely to do so but you can do this with JTS (Java Topology Suite) http://tsusiatsoftware.net/jts/main.html (GEOS, the lib used by Shapely is a port of JTS but JTS have more functions)

You can download the soft here http://sourceforge.net/projects/jts-topo-suite/ and launch testbuilder.sh or testbuilder.bat depending on your OS to play for discovering. After you understood, the basics you will have to do the same without GUI but with Java programming only.

http://geoscript.org/ may help you to make the glue beetween python code and java (because one implementation use Jython)

share|improve this answer
add comment

Grass can do it using v.transform.

share|improve this answer
add comment

You can use the gdal python bindings to do affine transformations http://www.gdal.org/ogr/osr_tutorial.html

Since it is built on top of proj/geos/gdal you can do the same things as in shapely + more

share|improve this answer
add comment

Sextante has a tool that might work. It is called Transform, under the "Tools for Vector layers". To run the command, define the angle, anchor point, scale factor and translation. Sextante is an extension to gvsig, openjump, udig, others. It also runs from the command-line.

share|improve this answer
add comment

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.