What is @turf/transform-translate?
@turf/transform-translate is a module from the Turf.js library that allows you to translate GeoJSON features by a specified distance along a given direction. This can be useful for tasks such as moving geometries, creating buffers, or simulating movement.
What are @turf/transform-translate's main functionalities?
Translate a Point
This feature allows you to translate a point by a specified distance and direction. In this example, a point is translated 100 meters at a 45-degree angle.
const turf = require('@turf/turf');
const point = turf.point([-75.343, 39.984]);
const translatedPoint = turf.transformTranslate(point, 100, 45);
console.log(translatedPoint);
Translate a Polygon
This feature allows you to translate a polygon by a specified distance and direction. In this example, a polygon is translated 200 meters at a 90-degree angle.
const turf = require('@turf/turf');
const polygon = turf.polygon([[
[-75.343, 39.984],
[-75.534, 39.123],
[-75.123, 39.123],
[-75.343, 39.984]
]]);
const translatedPolygon = turf.transformTranslate(polygon, 200, 90);
console.log(translatedPolygon);
Translate a LineString
This feature allows you to translate a LineString by a specified distance and direction. In this example, a LineString is translated 50 meters at a 180-degree angle.
const turf = require('@turf/turf');
const line = turf.lineString([[-75.343, 39.984], [-75.534, 39.123]]);
const translatedLine = turf.transformTranslate(line, 50, 180);
console.log(translatedLine);
Other packages similar to @turf/transform-translate
geolib
Geolib is a library for geospatial calculations. It provides functions for distance calculations, finding points within a radius, and more. While it does not directly offer translation of geometries, it can be used to calculate new positions based on distance and bearing.
jsts
JSTS (JavaScript Topology Suite) is a library for performing various geometric operations. It includes functions for buffering, union, intersection, and more. While it does not specifically focus on translation, it offers a wide range of geometric manipulations.
proj4js
Proj4js is a library for converting coordinates between different coordinate systems. While it does not provide translation functionalities, it is useful for transforming coordinates which can be a part of more complex geospatial workflows.
@turf/transform-translate
transformTranslate
Moves any geojson Feature or Geometry of a specified distance along a Rhumb Line
on the provided direction angle.
Parameters
-
geojson
(GeoJSON | GeometryCollection) object to be translated
-
distance
number length of the motion; negative values determine motion in opposite direction
-
direction
number of the motion; angle from North in decimal degrees, positive clockwise
-
options
Object Optional parameters (optional, default {}
)
options.units
Units in which distance
will be express; miles, kilometers, degrees, or radians (optional, default 'kilometers'
)options.zTranslation
number length of the vertical motion, same unit of distance (optional, default 0
)options.mutate
boolean allows GeoJSON input to be mutated (significant performance increase if true) (optional, default false
)
Examples
var poly = turf.polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]);
var translatedPoly = turf.transformTranslate(poly, 100, 35);
var addToMap = [poly, translatedPoly];
translatedPoly.properties = {stroke: '#F00', 'stroke-width': 4};
Returns (GeoJSON | GeometryCollection) the translated GeoJSON object
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-translate
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf