# Python, shapely library: is it possible to do an affine operation on shape polygon?

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?

-

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.

-
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
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
@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

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)

-

Grass can do it using v.transform.

-

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

-

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.

-