An unofficial Turf function for calculating the polygon diameter of a Feature.
OTHER License
An unofficial Turf module for calculating the polygon diameter of a Feature or FeatureCollection.
Returns the counter-clockwise angle between
{ coords[a], coords[a + 1] }
to { coords[b], coords[b + 1] }
coords
Array<[number, number]> List of GeoJSON coordinatesa
Number starting index of the first rayb
Number starting index of the second rayReturns Number counter-clockwise angle between 0 and 2 * pi
Takes a Feature or FeatureCollection and returns the polygon diameter.
Internally, this uses the rotating calipers method and pseudocode described here. The concepts from the article are based off of the following paper: Shamos, Michael (1978). "Computational Geometry" (PDF). Yale University. pp. 76–81.
The rotating calipers method requires a convex hull so we use mapbox's concaveman,
which runs in O(n * log n)
time as stated here.
The algorithm in the paper runs in about O(n) time, making this entire algorithm
run in O(n)
+ O(n * log n)
= O(n * log n)
time.
geojson
GeoJSON input Feature or FeatureCollectionconst points = turf.featureCollection([
turf.point([10.195312, 43.755225]),
turf.point([10.404052, 43.8424511]),
turf.point([10.579833, 43.659924]),
turf.point([10.360107, 43.516688]),
turf.point([10.14038, 43.588348]),
turf.point([10.195312, 43.755225])
]);
const len = diameter(points);
Returns Number the polygon diameter of the GeoJSON