-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
77 lines (69 loc) · 2.08 KB
/
index.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// http://stackoverflow.com/questions/839899/how-do-i-calculate-a-point-on-a-circles-circumference
// radians = degrees * (pi/180)
// https://github.com/bjornharrtell/jsts/blob/master/examples/buffer.html
var featurecollection = require('turf-featurecollection');
var jsts = require('jsts');
var normalize = require('geojson-normalize');
/**
* Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, and degrees.
*
* @module turf/buffer
* @category transformation
* @param {(Feature|FeatureCollection)} feature input to be buffered
* @param {Number} distance distance to draw the buffer
* @param {String} unit 'miles', 'feet', 'kilometers', 'meters', or 'degrees'
* @return {FeatureCollection<Polygon>|FeatureCollection<MultiPolygon>|Polygon|MultiPolygon} buffered features
*
* @example
* var pt = {
* "type": "Feature",
* "properties": {},
* "geometry": {
* "type": "Point",
* "coordinates": [-90.548630, 14.616599]
* }
* };
* var unit = 'miles';
*
* var buffered = turf.buffer(pt, 500, unit);
* var result = turf.featurecollection([buffered, pt]);
*
* //=result
*/
module.exports = function(feature, radius, units) {
switch (units) {
case 'miles':
radius = radius / 69.047;
break;
case 'feet':
radius = radius / 364568.0;
break;
case 'kilometers':
radius = radius / 111.12;
break;
case 'meters':
case 'metres':
radius = radius / 111120.0;
break;
case 'degrees':
break;
}
var fc = normalize(feature);
var buffered = normalize(featurecollection(fc.features.map(function(f) {
return bufferOp(f, radius);
})));
if(buffered.features.length > 1) return buffered;
else if(buffered.features.length === 1) return buffered.features[0];
};
var bufferOp = function(feature, radius) {
var reader = new jsts.io.GeoJSONReader();
var geom = reader.read(JSON.stringify(feature.geometry));
var buffered = geom.buffer(radius);
var parser = new jsts.io.GeoJSONParser();
buffered = parser.write(buffered);
return {
type: 'Feature',
geometry: buffered,
properties: {}
};
};