-
Notifications
You must be signed in to change notification settings - Fork 919
/
index.ts
56 lines (52 loc) · 1.76 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { GeoJsonProperties, Feature, Point, Polygon } from "geojson";
import { destination } from "@turf/destination";
import { polygon, Units } from "@turf/helpers";
/**
* Takes a {@link Point} and calculates the circle polygon given a radius in degrees, radians, miles, or kilometers; and steps for precision.
*
* @name circle
* @param {Feature<Point>|number[]} center center point
* @param {number} radius radius of the circle
* @param {Object} [options={}] Optional parameters
* @param {number} [options.steps=64] number of steps
* @param {string} [options.units='kilometers'] miles, kilometers, degrees, or radians
* @param {Object} [options.properties={}] properties
* @returns {Feature<Polygon>} circle polygon
* @example
* var center = [-75.343, 39.984];
* var radius = 5;
* var options = {steps: 10, units: 'kilometers', properties: {foo: 'bar'}};
* var circle = turf.circle(center, radius, options);
*
* //addToMap
* var addToMap = [turf.point(center), circle]
*/
function circle<P extends GeoJsonProperties = GeoJsonProperties>(
center: number[] | Point | Feature<Point, P>,
radius: number,
options: {
steps?: number;
units?: Units;
properties?: P;
} = {}
): Feature<Polygon, P> {
// default params
const steps = options.steps || 64;
const properties: any = options.properties
? options.properties
: !Array.isArray(center) && center.type === "Feature" && center.properties
? center.properties
: {};
// main
const coordinates = [];
for (let i = 0; i < steps; i++) {
coordinates.push(
destination(center, radius, (i * -360) / steps, options).geometry
.coordinates
);
}
coordinates.push(coordinates[0]);
return polygon([coordinates], properties);
}
export { circle };
export default circle;