What is @turf/transform-rotate?
@turf/transform-rotate is a module from the Turf.js library that allows you to rotate geometries by a specified angle around a given pivot point. This can be useful for various geospatial data manipulations, such as aligning features or creating rotated versions of existing geometries.
What are @turf/transform-rotate's main functionalities?
Rotate a Point
This feature allows you to rotate a point geometry by a specified angle. In this example, a point at coordinates [0, 0] is rotated by 45 degrees.
const turf = require('@turf/turf');
const point = turf.point([0, 0]);
const rotatedPoint = turf.transformRotate(point, 45);
console.log(rotatedPoint);
Rotate a Polygon
This feature allows you to rotate a polygon geometry by a specified angle. In this example, a square polygon is rotated by 90 degrees.
const turf = require('@turf/turf');
const polygon = turf.polygon([[
[0, 0],
[1, 0],
[1, 1],
[0, 1],
[0, 0]
]]);
const rotatedPolygon = turf.transformRotate(polygon, 90);
console.log(rotatedPolygon);
Rotate around a Custom Pivot
This feature allows you to rotate a geometry around a custom pivot point. In this example, a line is rotated by 45 degrees around the pivot point [1, 1].
const turf = require('@turf/turf');
const line = turf.lineString([[0, 0], [2, 2]]);
const pivot = [1, 1];
const rotatedLine = turf.transformRotate(line, 45, { pivot });
console.log(rotatedLine);
Other packages similar to @turf/transform-rotate
jsts
JSTS (JavaScript Topology Suite) is a library for performing various geometric operations, including transformations like rotation. It offers a more comprehensive set of geometric operations compared to @turf/transform-rotate, but it may be more complex to use for simple tasks.
geolib
Geolib is a library for geospatial calculations and transformations. It provides functions for rotating points and other geometries, similar to @turf/transform-rotate, but it also includes a broader range of geospatial utilities such as distance calculations and bounding box operations.
@turf/transform-rotate
transformRotate
Rotates any geojson Feature or Geometry of a specified angle, around its centroid
or a given pivot
point.
Parameters
-
geojson
GeoJSON object to be rotated
-
angle
number of rotation in decimal degrees, positive clockwise
-
options
Object Optional parameters (optional, default {}
)
options.pivot
Coord point around which the rotation will be performed (optional, default 'centroid'
)options.mutate
boolean allows GeoJSON input to be mutated (significant performance increase if true) (optional, default false
)
Examples
const poly = turf.polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]);
const options = {pivot: [0, 25]};
const rotatedPoly = turf.transformRotate(poly, 10, options);
const addToMap = [poly, rotatedPoly];
rotatedPoly.properties = {stroke: '#F00', 'stroke-width': 4};
Returns GeoJSON the rotated GeoJSON feature
This module is part of the Turfjs project, an open source module collection dedicated to geographic algorithms. It is maintained in the Turfjs/turf repository, where you can create PRs and issues.
Installation
Install this single module individually:
$ npm install @turf/transform-rotate
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf