Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mbullington/turf-diameter
An unofficial Turf function for calculating the polygon diameter of a Feature.
https://github.com/mbullington/turf-diameter
algorithm geojson javascript turfjs
Last synced: about 1 month ago
JSON representation
An unofficial Turf function for calculating the polygon diameter of a Feature.
- Host: GitHub
- URL: https://github.com/mbullington/turf-diameter
- Owner: mbullington
- License: other
- Created: 2019-06-07T05:23:07.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T23:36:13.000Z (almost 2 years ago)
- Last Synced: 2024-05-09T14:42:42.781Z (6 months ago)
- Topics: algorithm, geojson, javascript, turfjs
- Language: JavaScript
- Size: 1.61 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# turf-diameter
An unofficial Turf module for calculating the [polygon diameter](http://mathworld.wolfram.com/PolygonDiameter.html) of
a Feature or FeatureCollection.## API
#### Table of Contents
- [dtheta](#dtheta)
- [Parameters](#parameters)
- [diameter](#diameter)
- [Parameters](#parameters-1)
- [Examples](#examples)### dtheta
Returns the counter-clockwise angle between
`{ coords[a], coords[a + 1] }` to `{ coords[b], coords[b + 1] }`#### Parameters
- `coords` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<\[[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number), [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)]>** List of GeoJSON coordinates
- `a` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** starting index of the first ray
- `b` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** starting index of the second rayReturns **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** counter-clockwise angle between 0 and `2 * pi`
### diameter
Takes a [Feature](Feature) or [FeatureCollection](FeatureCollection) and returns the
[polygon diameter](http://mathworld.wolfram.com/PolygonDiameter.html).Internally, this uses the rotating calipers method and pseudocode
described [here](https://www.tvhoang.com/articles/2018/12/rotating-calipers).
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](https://github.com/mapbox/concaveman).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.#### Parameters
- `geojson` **GeoJSON** input Feature or FeatureCollection
#### Examples
```javascript
const 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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** the polygon diameter of the GeoJSON