-
Notifications
You must be signed in to change notification settings - Fork 944
/
index.js
184 lines (166 loc) · 5.88 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { center } from "@turf/center";
import jsts from "@turf/jsts";
import { geomEach, featureEach } from "@turf/meta";
import { geoAzimuthalEquidistant } from "d3-geo";
import {
feature,
featureCollection,
radiansToLength,
lengthToRadians,
earthRadius,
} from "@turf/helpers";
const { BufferOp, GeoJSONReader, GeoJSONWriter } = jsts;
/**
* Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, and degrees.
*
* When using a negative radius, the resulting geometry may be invalid if
* it's too small compared to the radius magnitude. If the input is a
* FeatureCollection, only valid members will be returned in the output
* FeatureCollection - i.e., the output collection may have fewer members than
* the input, or even be empty.
*
* @function
* @param {FeatureCollection|Geometry|Feature<any>} geojson input to be buffered
* @param {number} radius distance to draw the buffer (negative values are allowed)
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units="kilometers"] any of the options supported by turf units
* @param {number} [options.steps=8] number of steps
* @returns {FeatureCollection|Feature<Polygon|MultiPolygon>|undefined} buffered features
* @example
* var point = turf.point([-90.548630, 14.616599]);
* var buffered = turf.buffer(point, 500, {units: 'miles'});
*
* //addToMap
* var addToMap = [point, buffered]
*/
function buffer(geojson, radius, options) {
// Optional params
options = options || {};
// use user supplied options or default values
var units = options.units || "kilometers";
var steps = options.steps || 8;
// validation
if (!geojson) throw new Error("geojson is required");
if (typeof options !== "object") throw new Error("options must be an object");
if (typeof steps !== "number") throw new Error("steps must be an number");
// Allow negative buffers ("erosion") or zero-sized buffers ("repair geometry")
if (radius === undefined) throw new Error("radius is required");
if (steps <= 0) throw new Error("steps must be greater than 0");
var results = [];
switch (geojson.type) {
case "GeometryCollection":
geomEach(geojson, function (geometry) {
var buffered = bufferFeature(geometry, radius, units, steps);
if (buffered) results.push(buffered);
});
return featureCollection(results);
case "FeatureCollection":
featureEach(geojson, function (feature) {
var multiBuffered = bufferFeature(feature, radius, units, steps);
if (multiBuffered) {
featureEach(multiBuffered, function (buffered) {
if (buffered) results.push(buffered);
});
}
});
return featureCollection(results);
}
return bufferFeature(geojson, radius, units, steps);
}
/**
* Buffer single Feature/Geometry
*
* @private
* @param {Feature<any>} geojson input to be buffered
* @param {number} radius distance to draw the buffer
* @param {string} [units='kilometers'] any of the options supported by turf units
* @param {number} [steps=8] number of steps
* @returns {Feature<Polygon|MultiPolygon>} buffered feature
*/
function bufferFeature(geojson, radius, units, steps) {
var properties = geojson.properties || {};
var geometry = geojson.type === "Feature" ? geojson.geometry : geojson;
// Geometry Types faster than jsts
if (geometry.type === "GeometryCollection") {
var results = [];
geomEach(geojson, function (geometry) {
var buffered = bufferFeature(geometry, radius, units, steps);
if (buffered) results.push(buffered);
});
return featureCollection(results);
}
// Project GeoJSON to Azimuthal Equidistant projection (convert to Meters)
var projection = defineProjection(geometry);
var projected = {
type: geometry.type,
coordinates: projectCoords(geometry.coordinates, projection),
};
// JSTS buffer operation
var reader = new GeoJSONReader();
var geom = reader.read(projected);
var distance = radiansToLength(lengthToRadians(radius, units), "meters");
var buffered = BufferOp.bufferOp(geom, distance, steps);
var writer = new GeoJSONWriter();
buffered = writer.write(buffered);
// Detect if empty geometries
if (coordsIsNaN(buffered.coordinates)) return undefined;
// Unproject coordinates (convert to Degrees)
var result = {
type: buffered.type,
coordinates: unprojectCoords(buffered.coordinates, projection),
};
return feature(result, properties);
}
/**
* Coordinates isNaN
*
* @private
* @param {Array<any>} coords GeoJSON Coordinates
* @returns {boolean} if NaN exists
*/
function coordsIsNaN(coords) {
if (Array.isArray(coords[0])) return coordsIsNaN(coords[0]);
return isNaN(coords[0]);
}
/**
* Project coordinates to projection
*
* @private
* @param {Array<any>} coords to project
* @param {GeoProjection} proj D3 Geo Projection
* @returns {Array<any>} projected coordinates
*/
function projectCoords(coords, proj) {
if (typeof coords[0] !== "object") return proj(coords);
return coords.map(function (coord) {
return projectCoords(coord, proj);
});
}
/**
* Un-Project coordinates to projection
*
* @private
* @param {Array<any>} coords to un-project
* @param {GeoProjection} proj D3 Geo Projection
* @returns {Array<any>} un-projected coordinates
*/
function unprojectCoords(coords, proj) {
if (typeof coords[0] !== "object") return proj.invert(coords);
return coords.map(function (coord) {
return unprojectCoords(coord, proj);
});
}
/**
* Define Azimuthal Equidistant projection
*
* @private
* @param {Geometry|Feature<any>} geojson Base projection on center of GeoJSON
* @returns {GeoProjection} D3 Geo Azimuthal Equidistant Projection
*/
function defineProjection(geojson) {
var coords = center(geojson).geometry.coordinates;
var rotation = [-coords[0], -coords[1]];
return geoAzimuthalEquidistant().rotate(rotation).scale(earthRadius);
}
export { buffer };
export default buffer;