@turf/helpers 4.7.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -55
- package/index.d.ts +52 -45
- package/index.js +45 -46
- package/main.js +567 -0
- package/package.json +20 -7
package/main.js
ADDED
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Wraps a GeoJSON {@link Geometry} in a GeoJSON {@link Feature}.
|
|
7
|
+
*
|
|
8
|
+
* @name feature
|
|
9
|
+
* @param {Geometry} geometry input geometry
|
|
10
|
+
* @param {Object} [properties={}] an Object of key-value pairs to add as properties
|
|
11
|
+
* @param {Array<number>} [bbox] BBox [west, south, east, north]
|
|
12
|
+
* @param {string|number} [id] Identifier
|
|
13
|
+
* @returns {Feature} a GeoJSON Feature
|
|
14
|
+
* @example
|
|
15
|
+
* var geometry = {
|
|
16
|
+
* "type": "Point",
|
|
17
|
+
* "coordinates": [110, 50]
|
|
18
|
+
* };
|
|
19
|
+
*
|
|
20
|
+
* var feature = turf.feature(geometry);
|
|
21
|
+
*
|
|
22
|
+
* //=feature
|
|
23
|
+
*/
|
|
24
|
+
function feature(geometry, properties, bbox, id) {
|
|
25
|
+
if (geometry === undefined) throw new Error('geometry is required');
|
|
26
|
+
if (properties && properties.constructor !== Object) throw new Error('properties must be an Object');
|
|
27
|
+
if (bbox && bbox.length !== 4) throw new Error('bbox must be an Array of 4 numbers');
|
|
28
|
+
if (id && ['string', 'number'].indexOf(typeof id) === -1) throw new Error('id must be a number or a string');
|
|
29
|
+
|
|
30
|
+
var feat = {type: 'Feature'};
|
|
31
|
+
if (id) feat.id = id;
|
|
32
|
+
if (bbox) feat.bbox = bbox;
|
|
33
|
+
feat.properties = properties || {};
|
|
34
|
+
feat.geometry = geometry;
|
|
35
|
+
return feat;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Creates a GeoJSON {@link Geometry} from a Geometry string type & coordinates.
|
|
40
|
+
* For GeometryCollection type use `helpers.geometryCollection`
|
|
41
|
+
*
|
|
42
|
+
* @name geometry
|
|
43
|
+
* @param {string} type Geometry Type
|
|
44
|
+
* @param {Array<number>} coordinates Coordinates
|
|
45
|
+
* @param {Array<number>} [bbox] BBox [west, south, east, north]
|
|
46
|
+
* @returns {Geometry} a GeoJSON Geometry
|
|
47
|
+
* @example
|
|
48
|
+
* var type = 'Point';
|
|
49
|
+
* var coordinates = [110, 50];
|
|
50
|
+
*
|
|
51
|
+
* var geometry = turf.geometry(type, coordinates);
|
|
52
|
+
*
|
|
53
|
+
* //=geometry
|
|
54
|
+
*/
|
|
55
|
+
function geometry(type, coordinates, bbox) {
|
|
56
|
+
// Validation
|
|
57
|
+
if (!type) throw new Error('type is required');
|
|
58
|
+
if (!coordinates) throw new Error('coordinates is required');
|
|
59
|
+
if (!Array.isArray(coordinates)) throw new Error('coordinates must be an Array');
|
|
60
|
+
if (bbox && bbox.length !== 4) throw new Error('bbox must be an Array of 4 numbers');
|
|
61
|
+
|
|
62
|
+
var geom;
|
|
63
|
+
switch (type) {
|
|
64
|
+
case 'Point': geom = point(coordinates).geometry; break;
|
|
65
|
+
case 'LineString': geom = lineString(coordinates).geometry; break;
|
|
66
|
+
case 'Polygon': geom = polygon(coordinates).geometry; break;
|
|
67
|
+
case 'MultiPoint': geom = multiPoint(coordinates).geometry; break;
|
|
68
|
+
case 'MultiLineString': geom = multiLineString(coordinates).geometry; break;
|
|
69
|
+
case 'MultiPolygon': geom = multiPolygon(coordinates).geometry; break;
|
|
70
|
+
default: throw new Error(type + ' is invalid');
|
|
71
|
+
}
|
|
72
|
+
if (bbox) geom.bbox = bbox;
|
|
73
|
+
return geom;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Takes coordinates and properties (optional) and returns a new {@link Point} feature.
|
|
78
|
+
*
|
|
79
|
+
* @name point
|
|
80
|
+
* @param {Array<number>} coordinates longitude, latitude position (each in decimal degrees)
|
|
81
|
+
* @param {Object} [properties={}] an Object of key-value pairs to add as properties
|
|
82
|
+
* @param {Array<number>} [bbox] BBox [west, south, east, north]
|
|
83
|
+
* @param {string|number} [id] Identifier
|
|
84
|
+
* @returns {Feature<Point>} a Point feature
|
|
85
|
+
* @example
|
|
86
|
+
* var point = turf.point([-75.343, 39.984]);
|
|
87
|
+
*
|
|
88
|
+
* //=point
|
|
89
|
+
*/
|
|
90
|
+
function point(coordinates, properties, bbox, id) {
|
|
91
|
+
if (!coordinates) throw new Error('No coordinates passed');
|
|
92
|
+
if (coordinates.length === undefined) throw new Error('Coordinates must be an array');
|
|
93
|
+
if (coordinates.length < 2) throw new Error('Coordinates must be at least 2 numbers long');
|
|
94
|
+
if (!isNumber(coordinates[0]) || !isNumber(coordinates[1])) throw new Error('Coordinates must contain numbers');
|
|
95
|
+
|
|
96
|
+
return feature({
|
|
97
|
+
type: 'Point',
|
|
98
|
+
coordinates: coordinates
|
|
99
|
+
}, properties, bbox, id);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Takes an array of LinearRings and optionally an {@link Object} with properties and returns a {@link Polygon} feature.
|
|
104
|
+
*
|
|
105
|
+
* @name polygon
|
|
106
|
+
* @param {Array<Array<Array<number>>>} coordinates an array of LinearRings
|
|
107
|
+
* @param {Object} [properties={}] an Object of key-value pairs to add as properties
|
|
108
|
+
* @param {Array<number>} [bbox] BBox [west, south, east, north]
|
|
109
|
+
* @param {string|number} [id] Identifier
|
|
110
|
+
* @returns {Feature<Polygon>} a Polygon feature
|
|
111
|
+
* @throws {Error} throw an error if a LinearRing of the polygon has too few positions
|
|
112
|
+
* or if a LinearRing of the Polygon does not have matching Positions at the beginning & end.
|
|
113
|
+
* @example
|
|
114
|
+
* var polygon = turf.polygon([[
|
|
115
|
+
* [-2.275543, 53.464547],
|
|
116
|
+
* [-2.275543, 53.489271],
|
|
117
|
+
* [-2.215118, 53.489271],
|
|
118
|
+
* [-2.215118, 53.464547],
|
|
119
|
+
* [-2.275543, 53.464547]
|
|
120
|
+
* ]], { name: 'poly1', population: 400});
|
|
121
|
+
*
|
|
122
|
+
* //=polygon
|
|
123
|
+
*/
|
|
124
|
+
function polygon(coordinates, properties, bbox, id) {
|
|
125
|
+
if (!coordinates) throw new Error('No coordinates passed');
|
|
126
|
+
|
|
127
|
+
for (var i = 0; i < coordinates.length; i++) {
|
|
128
|
+
var ring = coordinates[i];
|
|
129
|
+
if (ring.length < 4) {
|
|
130
|
+
throw new Error('Each LinearRing of a Polygon must have 4 or more Positions.');
|
|
131
|
+
}
|
|
132
|
+
for (var j = 0; j < ring[ring.length - 1].length; j++) {
|
|
133
|
+
// Check if first point of Polygon contains two numbers
|
|
134
|
+
if (i === 0 && j === 0 && !isNumber(ring[0][0]) || !isNumber(ring[0][1])) throw new Error('Coordinates must contain numbers');
|
|
135
|
+
if (ring[ring.length - 1][j] !== ring[0][j]) {
|
|
136
|
+
throw new Error('First and last Position are not equivalent.');
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return feature({
|
|
142
|
+
type: 'Polygon',
|
|
143
|
+
coordinates: coordinates
|
|
144
|
+
}, properties, bbox, id);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Creates a {@link LineString} based on a
|
|
149
|
+
* coordinate array. Properties can be added optionally.
|
|
150
|
+
*
|
|
151
|
+
* @name lineString
|
|
152
|
+
* @param {Array<Array<number>>} coordinates an array of Positions
|
|
153
|
+
* @param {Object} [properties={}] an Object of key-value pairs to add as properties
|
|
154
|
+
* @param {Array<number>} [bbox] BBox [west, south, east, north]
|
|
155
|
+
* @param {string|number} [id] Identifier
|
|
156
|
+
* @returns {Feature<LineString>} a LineString feature
|
|
157
|
+
* @throws {Error} if no coordinates are passed
|
|
158
|
+
* @example
|
|
159
|
+
* var linestring1 = turf.lineString([
|
|
160
|
+
* [-21.964416, 64.148203],
|
|
161
|
+
* [-21.956176, 64.141316],
|
|
162
|
+
* [-21.93901, 64.135924],
|
|
163
|
+
* [-21.927337, 64.136673]
|
|
164
|
+
* ]);
|
|
165
|
+
* var linestring2 = turf.lineString([
|
|
166
|
+
* [-21.929054, 64.127985],
|
|
167
|
+
* [-21.912918, 64.134726],
|
|
168
|
+
* [-21.916007, 64.141016],
|
|
169
|
+
* [-21.930084, 64.14446]
|
|
170
|
+
* ], {name: 'line 1', distance: 145});
|
|
171
|
+
*
|
|
172
|
+
* //=linestring1
|
|
173
|
+
*
|
|
174
|
+
* //=linestring2
|
|
175
|
+
*/
|
|
176
|
+
function lineString(coordinates, properties, bbox, id) {
|
|
177
|
+
if (!coordinates) throw new Error('No coordinates passed');
|
|
178
|
+
if (coordinates.length < 2) throw new Error('Coordinates must be an array of two or more positions');
|
|
179
|
+
// Check if first point of LineString contains two numbers
|
|
180
|
+
if (!isNumber(coordinates[0][1]) || !isNumber(coordinates[0][1])) throw new Error('Coordinates must contain numbers');
|
|
181
|
+
|
|
182
|
+
return feature({
|
|
183
|
+
type: 'LineString',
|
|
184
|
+
coordinates: coordinates
|
|
185
|
+
}, properties, bbox, id);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Takes one or more {@link Feature|Features} and creates a {@link FeatureCollection}.
|
|
190
|
+
*
|
|
191
|
+
* @name featureCollection
|
|
192
|
+
* @param {Feature[]} features input features
|
|
193
|
+
* @param {Array<number>} [bbox] BBox [west, south, east, north]
|
|
194
|
+
* @param {string|number} [id] Identifier
|
|
195
|
+
* @returns {FeatureCollection} a FeatureCollection of input features
|
|
196
|
+
* @example
|
|
197
|
+
* var features = [
|
|
198
|
+
* turf.point([-75.343, 39.984], {name: 'Location A'}),
|
|
199
|
+
* turf.point([-75.833, 39.284], {name: 'Location B'}),
|
|
200
|
+
* turf.point([-75.534, 39.123], {name: 'Location C'})
|
|
201
|
+
* ];
|
|
202
|
+
*
|
|
203
|
+
* var collection = turf.featureCollection(features);
|
|
204
|
+
*
|
|
205
|
+
* //=collection
|
|
206
|
+
*/
|
|
207
|
+
function featureCollection(features, bbox, id) {
|
|
208
|
+
if (!features) throw new Error('No features passed');
|
|
209
|
+
if (!Array.isArray(features)) throw new Error('features must be an Array');
|
|
210
|
+
if (bbox && bbox.length !== 4) throw new Error('bbox must be an Array of 4 numbers');
|
|
211
|
+
if (id && ['string', 'number'].indexOf(typeof id) === -1) throw new Error('id must be a number or a string');
|
|
212
|
+
|
|
213
|
+
var fc = {type: 'FeatureCollection'};
|
|
214
|
+
if (id) fc.id = id;
|
|
215
|
+
if (bbox) fc.bbox = bbox;
|
|
216
|
+
fc.features = features;
|
|
217
|
+
return fc;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Creates a {@link Feature<MultiLineString>} based on a
|
|
222
|
+
* coordinate array. Properties can be added optionally.
|
|
223
|
+
*
|
|
224
|
+
* @name multiLineString
|
|
225
|
+
* @param {Array<Array<Array<number>>>} coordinates an array of LineStrings
|
|
226
|
+
* @param {Object} [properties={}] an Object of key-value pairs to add as properties
|
|
227
|
+
* @param {Array<number>} [bbox] BBox [west, south, east, north]
|
|
228
|
+
* @param {string|number} [id] Identifier
|
|
229
|
+
* @returns {Feature<MultiLineString>} a MultiLineString feature
|
|
230
|
+
* @throws {Error} if no coordinates are passed
|
|
231
|
+
* @example
|
|
232
|
+
* var multiLine = turf.multiLineString([[[0,0],[10,10]]]);
|
|
233
|
+
*
|
|
234
|
+
* //=multiLine
|
|
235
|
+
*/
|
|
236
|
+
function multiLineString(coordinates, properties, bbox, id) {
|
|
237
|
+
if (!coordinates) throw new Error('No coordinates passed');
|
|
238
|
+
|
|
239
|
+
return feature({
|
|
240
|
+
type: 'MultiLineString',
|
|
241
|
+
coordinates: coordinates
|
|
242
|
+
}, properties, bbox, id);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Creates a {@link Feature<MultiPoint>} based on a
|
|
247
|
+
* coordinate array. Properties can be added optionally.
|
|
248
|
+
*
|
|
249
|
+
* @name multiPoint
|
|
250
|
+
* @param {Array<Array<number>>} coordinates an array of Positions
|
|
251
|
+
* @param {Object} [properties={}] an Object of key-value pairs to add as properties
|
|
252
|
+
* @param {Array<number>} [bbox] BBox [west, south, east, north]
|
|
253
|
+
* @param {string|number} [id] Identifier
|
|
254
|
+
* @returns {Feature<MultiPoint>} a MultiPoint feature
|
|
255
|
+
* @throws {Error} if no coordinates are passed
|
|
256
|
+
* @example
|
|
257
|
+
* var multiPt = turf.multiPoint([[0,0],[10,10]]);
|
|
258
|
+
*
|
|
259
|
+
* //=multiPt
|
|
260
|
+
*/
|
|
261
|
+
function multiPoint(coordinates, properties, bbox, id) {
|
|
262
|
+
if (!coordinates) throw new Error('No coordinates passed');
|
|
263
|
+
|
|
264
|
+
return feature({
|
|
265
|
+
type: 'MultiPoint',
|
|
266
|
+
coordinates: coordinates
|
|
267
|
+
}, properties, bbox, id);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Creates a {@link Feature<MultiPolygon>} based on a
|
|
272
|
+
* coordinate array. Properties can be added optionally.
|
|
273
|
+
*
|
|
274
|
+
* @name multiPolygon
|
|
275
|
+
* @param {Array<Array<Array<Array<number>>>>} coordinates an array of Polygons
|
|
276
|
+
* @param {Object} [properties={}] an Object of key-value pairs to add as properties
|
|
277
|
+
* @param {Array<number>} [bbox] BBox [west, south, east, north]
|
|
278
|
+
* @param {string|number} [id] Identifier
|
|
279
|
+
* @returns {Feature<MultiPolygon>} a multipolygon feature
|
|
280
|
+
* @throws {Error} if no coordinates are passed
|
|
281
|
+
* @example
|
|
282
|
+
* var multiPoly = turf.multiPolygon([[[[0,0],[0,10],[10,10],[10,0],[0,0]]]]);
|
|
283
|
+
*
|
|
284
|
+
* //=multiPoly
|
|
285
|
+
*
|
|
286
|
+
*/
|
|
287
|
+
function multiPolygon(coordinates, properties, bbox, id) {
|
|
288
|
+
if (!coordinates) throw new Error('No coordinates passed');
|
|
289
|
+
|
|
290
|
+
return feature({
|
|
291
|
+
type: 'MultiPolygon',
|
|
292
|
+
coordinates: coordinates
|
|
293
|
+
}, properties, bbox, id);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Creates a {@link Feature<GeometryCollection>} based on a
|
|
298
|
+
* coordinate array. Properties can be added optionally.
|
|
299
|
+
*
|
|
300
|
+
* @name geometryCollection
|
|
301
|
+
* @param {Array<Geometry>} geometries an array of GeoJSON Geometries
|
|
302
|
+
* @param {Object} [properties={}] an Object of key-value pairs to add as properties
|
|
303
|
+
* @param {Array<number>} [bbox] BBox [west, south, east, north]
|
|
304
|
+
* @param {string|number} [id] Identifier
|
|
305
|
+
* @returns {Feature<GeometryCollection>} a GeoJSON GeometryCollection Feature
|
|
306
|
+
* @example
|
|
307
|
+
* var pt = {
|
|
308
|
+
* "type": "Point",
|
|
309
|
+
* "coordinates": [100, 0]
|
|
310
|
+
* };
|
|
311
|
+
* var line = {
|
|
312
|
+
* "type": "LineString",
|
|
313
|
+
* "coordinates": [ [101, 0], [102, 1] ]
|
|
314
|
+
* };
|
|
315
|
+
* var collection = turf.geometryCollection([pt, line]);
|
|
316
|
+
*
|
|
317
|
+
* //=collection
|
|
318
|
+
*/
|
|
319
|
+
function geometryCollection(geometries, properties, bbox, id) {
|
|
320
|
+
if (!geometries) throw new Error('geometries is required');
|
|
321
|
+
if (!Array.isArray(geometries)) throw new Error('geometries must be an Array');
|
|
322
|
+
|
|
323
|
+
return feature({
|
|
324
|
+
type: 'GeometryCollection',
|
|
325
|
+
geometries: geometries
|
|
326
|
+
}, properties, bbox, id);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// https://en.wikipedia.org/wiki/Great-circle_distance#Radius_for_spherical_Earth
|
|
330
|
+
var factors = {
|
|
331
|
+
miles: 3960,
|
|
332
|
+
nauticalmiles: 3441.145,
|
|
333
|
+
degrees: 57.2957795,
|
|
334
|
+
radians: 1,
|
|
335
|
+
inches: 250905600,
|
|
336
|
+
yards: 6969600,
|
|
337
|
+
meters: 6373000,
|
|
338
|
+
metres: 6373000,
|
|
339
|
+
centimeters: 6.373e+8,
|
|
340
|
+
centimetres: 6.373e+8,
|
|
341
|
+
kilometers: 6373,
|
|
342
|
+
kilometres: 6373,
|
|
343
|
+
feet: 20908792.65
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
var areaFactors = {
|
|
347
|
+
kilometers: 0.000001,
|
|
348
|
+
kilometres: 0.000001,
|
|
349
|
+
meters: 1,
|
|
350
|
+
metres: 1,
|
|
351
|
+
centimetres: 10000,
|
|
352
|
+
millimeter: 1000000,
|
|
353
|
+
acres: 0.000247105,
|
|
354
|
+
miles: 3.86e-7,
|
|
355
|
+
yards: 1.195990046,
|
|
356
|
+
feet: 10.763910417,
|
|
357
|
+
inches: 1550.003100006
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Round number to precision
|
|
362
|
+
*
|
|
363
|
+
* @param {number} num Number
|
|
364
|
+
* @param {number} [precision=0] Precision
|
|
365
|
+
* @returns {number} rounded number
|
|
366
|
+
* @example
|
|
367
|
+
* turf.round(120.4321)
|
|
368
|
+
* //=120
|
|
369
|
+
*
|
|
370
|
+
* turf.round(120.4321, 2)
|
|
371
|
+
* //=120.43
|
|
372
|
+
*/
|
|
373
|
+
function round(num, precision) {
|
|
374
|
+
if (num === undefined || num === null || isNaN(num)) throw new Error('num is required');
|
|
375
|
+
if (precision && !(precision >= 0)) throw new Error('precision must be a positive number');
|
|
376
|
+
var multiplier = Math.pow(10, precision || 0);
|
|
377
|
+
return Math.round(num * multiplier) / multiplier;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Convert a distance measurement (assuming a spherical Earth) from radians to a more friendly unit.
|
|
382
|
+
* Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
|
|
383
|
+
*
|
|
384
|
+
* @name radiansToDistance
|
|
385
|
+
* @param {number} radians in radians across the sphere
|
|
386
|
+
* @param {string} [units="kilometers"] can be degrees, radians, miles, or kilometers inches, yards, metres, meters, kilometres, kilometers.
|
|
387
|
+
* @returns {number} distance
|
|
388
|
+
*/
|
|
389
|
+
function radiansToDistance(radians, units) {
|
|
390
|
+
if (radians === undefined || radians === null) throw new Error('radians is required');
|
|
391
|
+
|
|
392
|
+
if (units && typeof units !== 'string') throw new Error('units must be a string');
|
|
393
|
+
var factor = factors[units || 'kilometers'];
|
|
394
|
+
if (!factor) throw new Error(units + ' units is invalid');
|
|
395
|
+
return radians * factor;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Convert a distance measurement (assuming a spherical Earth) from a real-world unit into radians
|
|
400
|
+
* Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
|
|
401
|
+
*
|
|
402
|
+
* @name distanceToRadians
|
|
403
|
+
* @param {number} distance in real units
|
|
404
|
+
* @param {string} [units=kilometers] can be degrees, radians, miles, or kilometers inches, yards, metres, meters, kilometres, kilometers.
|
|
405
|
+
* @returns {number} radians
|
|
406
|
+
*/
|
|
407
|
+
function distanceToRadians(distance, units) {
|
|
408
|
+
if (distance === undefined || distance === null) throw new Error('distance is required');
|
|
409
|
+
|
|
410
|
+
if (units && typeof units !== 'string') throw new Error('units must be a string');
|
|
411
|
+
var factor = factors[units || 'kilometers'];
|
|
412
|
+
if (!factor) throw new Error(units + ' units is invalid');
|
|
413
|
+
return distance / factor;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Convert a distance measurement (assuming a spherical Earth) from a real-world unit into degrees
|
|
418
|
+
* Valid units: miles, nauticalmiles, inches, yards, meters, metres, centimeters, kilometres, feet
|
|
419
|
+
*
|
|
420
|
+
* @name distanceToDegrees
|
|
421
|
+
* @param {number} distance in real units
|
|
422
|
+
* @param {string} [units=kilometers] can be degrees, radians, miles, or kilometers inches, yards, metres, meters, kilometres, kilometers.
|
|
423
|
+
* @returns {number} degrees
|
|
424
|
+
*/
|
|
425
|
+
function distanceToDegrees(distance, units) {
|
|
426
|
+
return radians2degrees(distanceToRadians(distance, units));
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Converts any bearing angle from the north line direction (positive clockwise)
|
|
431
|
+
* and returns an angle between 0-360 degrees (positive clockwise), 0 being the north line
|
|
432
|
+
*
|
|
433
|
+
* @name bearingToAngle
|
|
434
|
+
* @param {number} bearing angle, between -180 and +180 degrees
|
|
435
|
+
* @returns {number} angle between 0 and 360 degrees
|
|
436
|
+
*/
|
|
437
|
+
function bearingToAngle(bearing) {
|
|
438
|
+
if (bearing === null || bearing === undefined) throw new Error('bearing is required');
|
|
439
|
+
|
|
440
|
+
var angle = bearing % 360;
|
|
441
|
+
if (angle < 0) angle += 360;
|
|
442
|
+
return angle;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Converts an angle in radians to degrees
|
|
447
|
+
*
|
|
448
|
+
* @name radians2degrees
|
|
449
|
+
* @param {number} radians angle in radians
|
|
450
|
+
* @returns {number} degrees between 0 and 360 degrees
|
|
451
|
+
*/
|
|
452
|
+
function radians2degrees(radians) {
|
|
453
|
+
if (radians === null || radians === undefined) throw new Error('radians is required');
|
|
454
|
+
|
|
455
|
+
var degrees = radians % (2 * Math.PI);
|
|
456
|
+
return degrees * 180 / Math.PI;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Converts an angle in degrees to radians
|
|
461
|
+
*
|
|
462
|
+
* @name degrees2radians
|
|
463
|
+
* @param {number} degrees angle between 0 and 360 degrees
|
|
464
|
+
* @returns {number} angle in radians
|
|
465
|
+
*/
|
|
466
|
+
function degrees2radians(degrees) {
|
|
467
|
+
if (degrees === null || degrees === undefined) throw new Error('degrees is required');
|
|
468
|
+
|
|
469
|
+
var radians = degrees % 360;
|
|
470
|
+
return radians * Math.PI / 180;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Converts a distance to the requested unit.
|
|
475
|
+
* Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
|
|
476
|
+
*
|
|
477
|
+
* @param {number} distance to be converted
|
|
478
|
+
* @param {string} originalUnit of the distance
|
|
479
|
+
* @param {string} [finalUnit=kilometers] returned unit
|
|
480
|
+
* @returns {number} the converted distance
|
|
481
|
+
*/
|
|
482
|
+
function convertDistance(distance, originalUnit, finalUnit) {
|
|
483
|
+
if (distance === null || distance === undefined) throw new Error('distance is required');
|
|
484
|
+
if (!(distance >= 0)) throw new Error('distance must be a positive number');
|
|
485
|
+
|
|
486
|
+
var convertedDistance = radiansToDistance(distanceToRadians(distance, originalUnit), finalUnit || 'kilometers');
|
|
487
|
+
return convertedDistance;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Converts a area to the requested unit.
|
|
492
|
+
* Valid units: kilometers, kilometres, meters, metres, centimetres, millimeter, acre, mile, yard, foot, inch
|
|
493
|
+
* @param {number} area to be converted
|
|
494
|
+
* @param {string} [originalUnit=meters] of the distance
|
|
495
|
+
* @param {string} [finalUnit=kilometers] returned unit
|
|
496
|
+
* @returns {number} the converted distance
|
|
497
|
+
*/
|
|
498
|
+
function convertArea(area, originalUnit, finalUnit) {
|
|
499
|
+
if (area === null || area === undefined) throw new Error('area is required');
|
|
500
|
+
if (!(area >= 0)) throw new Error('area must be a positive number');
|
|
501
|
+
|
|
502
|
+
var startFactor = areaFactors[originalUnit || 'meters'];
|
|
503
|
+
if (!startFactor) throw new Error('invalid original units');
|
|
504
|
+
|
|
505
|
+
var finalFactor = areaFactors[finalUnit || 'kilometers'];
|
|
506
|
+
if (!finalFactor) throw new Error('invalid final units');
|
|
507
|
+
|
|
508
|
+
return (area / startFactor) * finalFactor;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* isNumber
|
|
513
|
+
*
|
|
514
|
+
* @param {*} num Number to validate
|
|
515
|
+
* @returns {boolean} true/false
|
|
516
|
+
* @example
|
|
517
|
+
* turf.isNumber(123)
|
|
518
|
+
* //=true
|
|
519
|
+
* turf.isNumber('foo')
|
|
520
|
+
* //=false
|
|
521
|
+
*/
|
|
522
|
+
function isNumber(num) {
|
|
523
|
+
return !isNaN(num) && num !== null && !Array.isArray(num);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* isObject
|
|
528
|
+
*
|
|
529
|
+
* @param {*} input variable to validate
|
|
530
|
+
* @returns {boolean} true/false
|
|
531
|
+
* @example
|
|
532
|
+
* turf.isObject({elevation: 10})
|
|
533
|
+
* //=true
|
|
534
|
+
* turf.isObject('foo')
|
|
535
|
+
* //=false
|
|
536
|
+
*/
|
|
537
|
+
function isObject(input) {
|
|
538
|
+
return (!!input) && (input.constructor === Object);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Earth Radius used with the Harvesine formula and approximates using a spherical (non-ellipsoid) Earth.
|
|
543
|
+
*/
|
|
544
|
+
var earthRadius = 6371008.8;
|
|
545
|
+
|
|
546
|
+
exports.feature = feature;
|
|
547
|
+
exports.geometry = geometry;
|
|
548
|
+
exports.point = point;
|
|
549
|
+
exports.polygon = polygon;
|
|
550
|
+
exports.lineString = lineString;
|
|
551
|
+
exports.featureCollection = featureCollection;
|
|
552
|
+
exports.multiLineString = multiLineString;
|
|
553
|
+
exports.multiPoint = multiPoint;
|
|
554
|
+
exports.multiPolygon = multiPolygon;
|
|
555
|
+
exports.geometryCollection = geometryCollection;
|
|
556
|
+
exports.round = round;
|
|
557
|
+
exports.radiansToDistance = radiansToDistance;
|
|
558
|
+
exports.distanceToRadians = distanceToRadians;
|
|
559
|
+
exports.distanceToDegrees = distanceToDegrees;
|
|
560
|
+
exports.bearingToAngle = bearingToAngle;
|
|
561
|
+
exports.radians2degrees = radians2degrees;
|
|
562
|
+
exports.degrees2radians = degrees2radians;
|
|
563
|
+
exports.convertDistance = convertDistance;
|
|
564
|
+
exports.convertArea = convertArea;
|
|
565
|
+
exports.isNumber = isNumber;
|
|
566
|
+
exports.isObject = isObject;
|
|
567
|
+
exports.earthRadius = earthRadius;
|
package/package.json
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/helpers",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "turf helpers module",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "main",
|
|
6
|
+
"module": "index",
|
|
7
|
+
"jsnext:main": "index",
|
|
6
8
|
"types": "index.d.ts",
|
|
7
9
|
"files": [
|
|
8
10
|
"index.js",
|
|
9
|
-
"index.d.ts"
|
|
11
|
+
"index.d.ts",
|
|
12
|
+
"main.js"
|
|
10
13
|
],
|
|
11
14
|
"scripts": {
|
|
12
|
-
"
|
|
13
|
-
"
|
|
15
|
+
"pretest": "rollup -c ../../rollup.config.js",
|
|
16
|
+
"test": "node -r @std/esm test.js",
|
|
17
|
+
"posttest": "uglifyjs main.js -o main.min.js",
|
|
18
|
+
"bench": "node -r @std/esm bench.js"
|
|
14
19
|
},
|
|
15
20
|
"repository": {
|
|
16
21
|
"type": "git",
|
|
@@ -35,7 +40,15 @@
|
|
|
35
40
|
},
|
|
36
41
|
"homepage": "https://github.com/Turfjs/turf",
|
|
37
42
|
"devDependencies": {
|
|
38
|
-
"benchmark": "
|
|
39
|
-
"
|
|
43
|
+
"benchmark": "*",
|
|
44
|
+
"rollup": "*",
|
|
45
|
+
"tape": "*",
|
|
46
|
+
"@std/esm": "*",
|
|
47
|
+
"uglify-js": "*"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {},
|
|
50
|
+
"@std/esm": {
|
|
51
|
+
"esm": "js",
|
|
52
|
+
"cjs": true
|
|
40
53
|
}
|
|
41
54
|
}
|