Skip to main content
2 of 9
more explanation on math
Mike T
  • 42.4k
  • 10
  • 129
  • 190

You can do this in PostGIS using ST_Affine. The functionality to rotate around an arbitrary point was added to ST_Rotate for the upcoming 2.0 release.

If you have an earlier version (like PostGIS 1.5), you can add these functions:

CREATE OR REPLACE FUNCTION st_rotate(geometry, double precision, geometry)
  RETURNS geometry AS
'SELECT ST_Affine($1,  cos($2), -sin($2), 0,  sin($2),  cos($2), 0, 0, 0, 1, ST_X($3) - cos($2) * ST_X($3) + sin($2) * ST_Y($3), ST_Y($3) - sin($2) * ST_X($3) - cos($2) * ST_Y($3), 0)'
  LANGUAGE sql IMMUTABLE STRICT
  COST 100;
COMMENT ON FUNCTION st_rotate(geometry, double precision, geometry) IS 'args: geomA, rotRadians, pointOrigin - Rotate a geometry rotRadians counter-clockwise about an origin.';

CREATE OR REPLACE FUNCTION st_rotate(geometry, double precision, double precision, double precision)
  RETURNS geometry AS
'SELECT ST_Affine($1,  cos($2), -sin($2), 0,  sin($2),  cos($2), 0, 0, 0, 1,    $3 - cos($2) * $3 + sin($2) * $4, $4 - sin($2) * $3 - cos($2) * $4, 0)'
  LANGUAGE sql IMMUTABLE STRICT
  COST 100;
COMMENT ON FUNCTION st_rotate(geometry, double precision, double precision, double precision) IS 'args: geomA, rotRadians, x0, y0 - Rotate a geometry rotRadians counter-clockwise about an origin.';

See examples at ST_Rotate to get an idea on how to use it to rotate a geometry around an x, y point.

Because we all like math, the transformation matrix from the above functions is represented as:

[ cos(θ)  | -sin(θ)  ||  x - cos(θ) * x + sin(θ) * y ]
[ sin(θ)  |  cos(θ)  ||  y - sin(θ) * x - cos(θ) * y ]

Where θ is the counter-clockwise rotation about an origin, in radians, x is the Easting/Longitude of the origin point, and y is the Northing/Latitude.

This math could possibly be adapted to any affine transformation tool. But I'm not sure if it could be adapted to qgsAffine ...

Mike T
  • 42.4k
  • 10
  • 129
  • 190