Shapely Python – Is It Possible to Do an Affine Operation on Shape Polygon?

affine transformationgdalgeometrypythonshapely

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?

Best Answer

Shapely's affinity module supports affine transformations on any geometry, for example:

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