What is @turf/length?
@turf/length is a module from the Turf.js library that allows you to calculate the length of a GeoJSON LineString or MultiLineString. It is useful for geographic data analysis and manipulation, particularly when working with spatial data in web applications.
What are @turf/length's main functionalities?
Calculate Length of LineString
This feature allows you to calculate the length of a LineString in the specified units (e.g., miles, kilometers). The code sample demonstrates how to create a LineString and calculate its length in miles.
const turf = require('@turf/length');
const line = {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[-77.031669, 38.878605],
[-77.029609, 38.881946],
[-77.020339, 38.884084],
[-77.025661, 38.885821]
]
}
};
const length = turf(line, {units: 'miles'});
console.log(length);
Calculate Length of MultiLineString
This feature allows you to calculate the length of a MultiLineString in the specified units (e.g., kilometers). The code sample demonstrates how to create a MultiLineString and calculate its length in kilometers.
const turf = require('@turf/length');
const multiLine = {
"type": "Feature",
"geometry": {
"type": "MultiLineString",
"coordinates": [
[
[-77.031669, 38.878605],
[-77.029609, 38.881946]
],
[
[-77.020339, 38.884084],
[-77.025661, 38.885821]
]
]
}
};
const length = turf(multiLine, {units: 'kilometers'});
console.log(length);
Other packages similar to @turf/length
geolib
Geolib is a library for geospatial calculations, including distance and length calculations. It provides similar functionality to @turf/length but also includes additional features like calculating the distance between two points, finding the center of a collection of points, and more. It is more versatile but may be more complex to use for simple length calculations.
geodesy
Geodesy is a library for geodesic calculations, including distance and length calculations. It offers similar functionality to @turf/length but focuses more on precise geodesic calculations, which can be useful for applications requiring high accuracy. It is more specialized and may be overkill for simple length calculations.
@turf/length
length
Takes a GeoJSON and measures its length in the specified units, (Multi)Point's distance are ignored.
Parameters
-
geojson
Feature<(LineString | MultiLineString)> GeoJSON to measure
-
options
Object Optional parameters (optional, default {}
)
options.units
string can be degrees, radians, miles, or kilometers (optional, default kilometers
)
Examples
var line = turf.lineString([[115, -32], [131, -22], [143, -25], [150, -34]]);
var length = turf.length(line, {units: 'miles'});
var addToMap = [line];
line.properties.distance = length;
Returns number length of GeoJSON
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/length
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf