@turf/helpers 7.0.0-alpha.2 → 7.1.0-alpha.7

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.
@@ -1,3 +1,23 @@
1
+ import { Feature, Point, Position, LineString, MultiLineString, Polygon, MultiPolygon, FeatureCollection, Geometry, GeometryCollection, GeometryObject, GeoJsonProperties, BBox, MultiPoint } from 'geojson';
2
+
3
+ /**
4
+ * Id
5
+ *
6
+ * https://tools.ietf.org/html/rfc7946#section-3.2
7
+ * If a Feature has a commonly used identifier, that identifier SHOULD be included as a member of
8
+ * the Feature object with the name "id", and the value of this member is either a JSON string or number.
9
+ *
10
+ * Should be contributed to @types/geojson
11
+ */
12
+ type Id = string | number;
13
+
14
+ type Coord = Feature<Point> | Point | Position;
15
+ type Units = "meters" | "metres" | "millimeters" | "millimetres" | "centimeters" | "centimetres" | "kilometers" | "kilometres" | "miles" | "nauticalmiles" | "inches" | "yards" | "feet" | "radians" | "degrees";
16
+ type AreaUnits = Exclude<Units, "radians" | "degrees"> | "acres" | "hectares";
17
+ type Grid = "point" | "square" | "hex" | "triangle";
18
+ type Corners = "sw" | "se" | "nw" | "ne" | "center" | "centroid";
19
+ type Lines = LineString | MultiLineString | Polygon | MultiPolygon;
20
+ type AllGeoJSON = Feature | FeatureCollection | Geometry | GeometryCollection;
1
21
  /**
2
22
  * @module helpers
3
23
  */
@@ -7,7 +27,7 @@
7
27
  * @memberof helpers
8
28
  * @type {number}
9
29
  */
10
- export const earthRadius = 6371008.8;
30
+ declare const earthRadius = 6371008.8;
11
31
  /**
12
32
  * Unit of measurement factors using a spherical (non-ellipsoid) earth radius.
13
33
  *
@@ -16,23 +36,7 @@ export const earthRadius = 6371008.8;
16
36
  * @memberof helpers
17
37
  * @type {Object}
18
38
  */
19
- export const factors = {
20
- centimeters: earthRadius * 100,
21
- centimetres: earthRadius * 100,
22
- degrees: 360 / (2 * Math.PI),
23
- feet: earthRadius * 3.28084,
24
- inches: earthRadius * 39.37,
25
- kilometers: earthRadius / 1000,
26
- kilometres: earthRadius / 1000,
27
- meters: earthRadius,
28
- metres: earthRadius,
29
- miles: earthRadius / 1609.344,
30
- millimeters: earthRadius * 1000,
31
- millimetres: earthRadius * 1000,
32
- nauticalmiles: earthRadius / 1852,
33
- radians: 1,
34
- yards: earthRadius * 1.0936,
35
- };
39
+ declare const factors: Record<Units, number>;
36
40
  /**
37
41
 
38
42
  * Area of measurement factors based on 1 square meter.
@@ -40,23 +44,7 @@ export const factors = {
40
44
  * @memberof helpers
41
45
  * @type {Object}
42
46
  */
43
- export const areaFactors = {
44
- acres: 0.000247105,
45
- centimeters: 10000,
46
- centimetres: 10000,
47
- feet: 10.763910417,
48
- hectares: 0.0001,
49
- inches: 1550.003100006,
50
- kilometers: 0.000001,
51
- kilometres: 0.000001,
52
- meters: 1,
53
- metres: 1,
54
- miles: 3.86e-7,
55
- nauticalmiles: 2.9155334959812285e-7,
56
- millimeters: 1000000,
57
- millimetres: 1000000,
58
- yards: 1.195990046,
59
- };
47
+ declare const areaFactors: Record<AreaUnits, number>;
60
48
  /**
61
49
  * Wraps a GeoJSON {@link Geometry} in a GeoJSON {@link Feature}.
62
50
  *
@@ -77,18 +65,10 @@ export const areaFactors = {
77
65
  *
78
66
  * //=feature
79
67
  */
80
- export function feature(geom, properties, options = {}) {
81
- const feat = { type: "Feature" };
82
- if (options.id === 0 || options.id) {
83
- feat.id = options.id;
84
- }
85
- if (options.bbox) {
86
- feat.bbox = options.bbox;
87
- }
88
- feat.properties = properties || {};
89
- feat.geometry = geom;
90
- return feat;
91
- }
68
+ declare function feature<G extends GeometryObject = Geometry, P extends GeoJsonProperties = GeoJsonProperties>(geom: G | null, properties?: P, options?: {
69
+ bbox?: BBox;
70
+ id?: Id;
71
+ }): Feature<G, P>;
92
72
  /**
93
73
  * Creates a GeoJSON {@link Geometry} from a Geometry string type & coordinates.
94
74
  * For GeometryCollection type use `helpers.geometryCollection`
@@ -104,24 +84,7 @@ export function feature(geom, properties, options = {}) {
104
84
  * var geometry = turf.geometry(type, coordinates);
105
85
  * // => geometry
106
86
  */
107
- export function geometry(type, coordinates, _options = {}) {
108
- switch (type) {
109
- case "Point":
110
- return point(coordinates).geometry;
111
- case "LineString":
112
- return lineString(coordinates).geometry;
113
- case "Polygon":
114
- return polygon(coordinates).geometry;
115
- case "MultiPoint":
116
- return multiPoint(coordinates).geometry;
117
- case "MultiLineString":
118
- return multiLineString(coordinates).geometry;
119
- case "MultiPolygon":
120
- return multiPolygon(coordinates).geometry;
121
- default:
122
- throw new Error(type + " is invalid");
123
- }
124
- }
87
+ declare function geometry(type: "Point" | "LineString" | "Polygon" | "MultiPoint" | "MultiLineString" | "MultiPolygon", coordinates: any[], _options?: Record<string, never>): Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon;
125
88
  /**
126
89
  * Creates a {@link Point} {@link Feature} from a Position.
127
90
  *
@@ -137,25 +100,10 @@ export function geometry(type, coordinates, _options = {}) {
137
100
  *
138
101
  * //=point
139
102
  */
140
- export function point(coordinates, properties, options = {}) {
141
- if (!coordinates) {
142
- throw new Error("coordinates is required");
143
- }
144
- if (!Array.isArray(coordinates)) {
145
- throw new Error("coordinates must be an Array");
146
- }
147
- if (coordinates.length < 2) {
148
- throw new Error("coordinates must be at least 2 numbers long");
149
- }
150
- if (!isNumber(coordinates[0]) || !isNumber(coordinates[1])) {
151
- throw new Error("coordinates must contain numbers");
152
- }
153
- const geom = {
154
- type: "Point",
155
- coordinates,
156
- };
157
- return feature(geom, properties, options);
158
- }
103
+ declare function point<P extends GeoJsonProperties = GeoJsonProperties>(coordinates: Position, properties?: P, options?: {
104
+ bbox?: BBox;
105
+ id?: Id;
106
+ }): Feature<Point, P>;
159
107
  /**
160
108
  * Creates a {@link Point} {@link FeatureCollection} from an Array of Point coordinates.
161
109
  *
@@ -176,11 +124,10 @@ export function point(coordinates, properties, options = {}) {
176
124
  *
177
125
  * //=points
178
126
  */
179
- export function points(coordinates, properties, options = {}) {
180
- return featureCollection(coordinates.map((coords) => {
181
- return point(coords, properties);
182
- }), options);
183
- }
127
+ declare function points<P extends GeoJsonProperties = GeoJsonProperties>(coordinates: Position[], properties?: P, options?: {
128
+ bbox?: BBox;
129
+ id?: Id;
130
+ }): FeatureCollection<Point, P>;
184
131
  /**
185
132
  * Creates a {@link Polygon} {@link Feature} from an Array of LinearRings.
186
133
  *
@@ -196,27 +143,10 @@ export function points(coordinates, properties, options = {}) {
196
143
  *
197
144
  * //=polygon
198
145
  */
199
- export function polygon(coordinates, properties, options = {}) {
200
- for (const ring of coordinates) {
201
- if (ring.length < 4) {
202
- throw new Error("Each LinearRing of a Polygon must have 4 or more Positions.");
203
- }
204
- if (ring[ring.length - 1].length !== ring[0].length) {
205
- throw new Error("First and last Position are not equivalent.");
206
- }
207
- for (let j = 0; j < ring[ring.length - 1].length; j++) {
208
- // Check if first point of Polygon contains two numbers
209
- if (ring[ring.length - 1][j] !== ring[0][j]) {
210
- throw new Error("First and last Position are not equivalent.");
211
- }
212
- }
213
- }
214
- const geom = {
215
- type: "Polygon",
216
- coordinates,
217
- };
218
- return feature(geom, properties, options);
219
- }
146
+ declare function polygon<P extends GeoJsonProperties = GeoJsonProperties>(coordinates: Position[][], properties?: P, options?: {
147
+ bbox?: BBox;
148
+ id?: Id;
149
+ }): Feature<Polygon, P>;
220
150
  /**
221
151
  * Creates a {@link Polygon} {@link FeatureCollection} from an Array of Polygon coordinates.
222
152
  *
@@ -235,11 +165,10 @@ export function polygon(coordinates, properties, options = {}) {
235
165
  *
236
166
  * //=polygons
237
167
  */
238
- export function polygons(coordinates, properties, options = {}) {
239
- return featureCollection(coordinates.map((coords) => {
240
- return polygon(coords, properties);
241
- }), options);
242
- }
168
+ declare function polygons<P extends GeoJsonProperties = GeoJsonProperties>(coordinates: Position[][][], properties?: P, options?: {
169
+ bbox?: BBox;
170
+ id?: Id;
171
+ }): FeatureCollection<Polygon, P>;
243
172
  /**
244
173
  * Creates a {@link LineString} {@link Feature} from an Array of Positions.
245
174
  *
@@ -257,16 +186,10 @@ export function polygons(coordinates, properties, options = {}) {
257
186
  * //=linestring1
258
187
  * //=linestring2
259
188
  */
260
- export function lineString(coordinates, properties, options = {}) {
261
- if (coordinates.length < 2) {
262
- throw new Error("coordinates must be an array of two or more positions");
263
- }
264
- const geom = {
265
- type: "LineString",
266
- coordinates,
267
- };
268
- return feature(geom, properties, options);
269
- }
189
+ declare function lineString<P extends GeoJsonProperties = GeoJsonProperties>(coordinates: Position[], properties?: P, options?: {
190
+ bbox?: BBox;
191
+ id?: Id;
192
+ }): Feature<LineString, P>;
270
193
  /**
271
194
  * Creates a {@link LineString} {@link FeatureCollection} from an Array of LineString coordinates.
272
195
  *
@@ -286,11 +209,10 @@ export function lineString(coordinates, properties, options = {}) {
286
209
  *
287
210
  * //=linestrings
288
211
  */
289
- export function lineStrings(coordinates, properties, options = {}) {
290
- return featureCollection(coordinates.map((coords) => {
291
- return lineString(coords, properties);
292
- }), options);
293
- }
212
+ declare function lineStrings<P extends GeoJsonProperties = GeoJsonProperties>(coordinates: Position[][], properties?: P, options?: {
213
+ bbox?: BBox;
214
+ id?: Id;
215
+ }): FeatureCollection<LineString, P>;
294
216
  /**
295
217
  * Takes one or more {@link Feature|Features} and creates a {@link FeatureCollection}.
296
218
  *
@@ -313,17 +235,10 @@ export function lineStrings(coordinates, properties, options = {}) {
313
235
  *
314
236
  * //=collection
315
237
  */
316
- export function featureCollection(features, options = {}) {
317
- const fc = { type: "FeatureCollection" };
318
- if (options.id) {
319
- fc.id = options.id;
320
- }
321
- if (options.bbox) {
322
- fc.bbox = options.bbox;
323
- }
324
- fc.features = features;
325
- return fc;
326
- }
238
+ declare function featureCollection<G extends GeometryObject = Geometry, P extends GeoJsonProperties = GeoJsonProperties>(features: Array<Feature<G, P>>, options?: {
239
+ bbox?: BBox;
240
+ id?: Id;
241
+ }): FeatureCollection<G, P>;
327
242
  /**
328
243
  * Creates a {@link Feature<MultiLineString>} based on a
329
244
  * coordinate array. Properties can be added optionally.
@@ -341,13 +256,10 @@ export function featureCollection(features, options = {}) {
341
256
  *
342
257
  * //=multiLine
343
258
  */
344
- export function multiLineString(coordinates, properties, options = {}) {
345
- const geom = {
346
- type: "MultiLineString",
347
- coordinates,
348
- };
349
- return feature(geom, properties, options);
350
- }
259
+ declare function multiLineString<P extends GeoJsonProperties = GeoJsonProperties>(coordinates: Position[][], properties?: P, options?: {
260
+ bbox?: BBox;
261
+ id?: Id;
262
+ }): Feature<MultiLineString, P>;
351
263
  /**
352
264
  * Creates a {@link Feature<MultiPoint>} based on a
353
265
  * coordinate array. Properties can be added optionally.
@@ -365,13 +277,10 @@ export function multiLineString(coordinates, properties, options = {}) {
365
277
  *
366
278
  * //=multiPt
367
279
  */
368
- export function multiPoint(coordinates, properties, options = {}) {
369
- const geom = {
370
- type: "MultiPoint",
371
- coordinates,
372
- };
373
- return feature(geom, properties, options);
374
- }
280
+ declare function multiPoint<P extends GeoJsonProperties = GeoJsonProperties>(coordinates: Position[], properties?: P, options?: {
281
+ bbox?: BBox;
282
+ id?: Id;
283
+ }): Feature<MultiPoint, P>;
375
284
  /**
376
285
  * Creates a {@link Feature<MultiPolygon>} based on a
377
286
  * coordinate array. Properties can be added optionally.
@@ -390,13 +299,10 @@ export function multiPoint(coordinates, properties, options = {}) {
390
299
  * //=multiPoly
391
300
  *
392
301
  */
393
- export function multiPolygon(coordinates, properties, options = {}) {
394
- const geom = {
395
- type: "MultiPolygon",
396
- coordinates,
397
- };
398
- return feature(geom, properties, options);
399
- }
302
+ declare function multiPolygon<P extends GeoJsonProperties = GeoJsonProperties>(coordinates: Position[][][], properties?: P, options?: {
303
+ bbox?: BBox;
304
+ id?: Id;
305
+ }): Feature<MultiPolygon, P>;
400
306
  /**
401
307
  * Creates a {@link Feature<GeometryCollection>} based on a
402
308
  * coordinate array. Properties can be added optionally.
@@ -415,13 +321,10 @@ export function multiPolygon(coordinates, properties, options = {}) {
415
321
  *
416
322
  * // => collection
417
323
  */
418
- export function geometryCollection(geometries, properties, options = {}) {
419
- const geom = {
420
- type: "GeometryCollection",
421
- geometries,
422
- };
423
- return feature(geom, properties, options);
424
- }
324
+ declare function geometryCollection<P extends GeoJsonProperties = GeoJsonProperties>(geometries: Array<Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon>, properties?: P, options?: {
325
+ bbox?: BBox;
326
+ id?: Id;
327
+ }): Feature<GeometryCollection, P>;
425
328
  /**
426
329
  * Round number to precision
427
330
  *
@@ -435,13 +338,7 @@ export function geometryCollection(geometries, properties, options = {}) {
435
338
  * turf.round(120.4321, 2)
436
339
  * //=120.43
437
340
  */
438
- export function round(num, precision = 0) {
439
- if (precision && !(precision >= 0)) {
440
- throw new Error("precision must be a positive number");
441
- }
442
- const multiplier = Math.pow(10, precision || 0);
443
- return Math.round(num * multiplier) / multiplier;
444
- }
341
+ declare function round(num: number, precision?: number): number;
445
342
  /**
446
343
  * Convert a distance measurement (assuming a spherical Earth) from radians to a more friendly unit.
447
344
  * Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
@@ -452,13 +349,7 @@ export function round(num, precision = 0) {
452
349
  * meters, kilometres, kilometers.
453
350
  * @returns {number} distance
454
351
  */
455
- export function radiansToLength(radians, units = "kilometers") {
456
- const factor = factors[units];
457
- if (!factor) {
458
- throw new Error(units + " units is invalid");
459
- }
460
- return radians * factor;
461
- }
352
+ declare function radiansToLength(radians: number, units?: Units): number;
462
353
  /**
463
354
  * Convert a distance measurement (assuming a spherical Earth) from a real-world unit into radians
464
355
  * Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
@@ -469,13 +360,7 @@ export function radiansToLength(radians, units = "kilometers") {
469
360
  * meters, kilometres, kilometers.
470
361
  * @returns {number} radians
471
362
  */
472
- export function lengthToRadians(distance, units = "kilometers") {
473
- const factor = factors[units];
474
- if (!factor) {
475
- throw new Error(units + " units is invalid");
476
- }
477
- return distance / factor;
478
- }
363
+ declare function lengthToRadians(distance: number, units?: Units): number;
479
364
  /**
480
365
  * Convert a distance measurement (assuming a spherical Earth) from a real-world unit into degrees
481
366
  * Valid units: miles, nauticalmiles, inches, yards, meters, metres, centimeters, kilometres, feet
@@ -486,9 +371,7 @@ export function lengthToRadians(distance, units = "kilometers") {
486
371
  * meters, kilometres, kilometers.
487
372
  * @returns {number} degrees
488
373
  */
489
- export function lengthToDegrees(distance, units) {
490
- return radiansToDegrees(lengthToRadians(distance, units));
491
- }
374
+ declare function lengthToDegrees(distance: number, units?: Units): number;
492
375
  /**
493
376
  * Converts any bearing angle from the north line direction (positive clockwise)
494
377
  * and returns an angle between 0-360 degrees (positive clockwise), 0 being the north line
@@ -497,13 +380,7 @@ export function lengthToDegrees(distance, units) {
497
380
  * @param {number} bearing angle, between -180 and +180 degrees
498
381
  * @returns {number} angle between 0 and 360 degrees
499
382
  */
500
- export function bearingToAzimuth(bearing) {
501
- let angle = bearing % 360;
502
- if (angle < 0) {
503
- angle += 360;
504
- }
505
- return angle;
506
- }
383
+ declare function bearingToAzimuth(bearing: number): number;
507
384
  /**
508
385
  * Converts an angle in radians to degrees
509
386
  *
@@ -511,10 +388,7 @@ export function bearingToAzimuth(bearing) {
511
388
  * @param {number} radians angle in radians
512
389
  * @returns {number} degrees between 0 and 360 degrees
513
390
  */
514
- export function radiansToDegrees(radians) {
515
- const degrees = radians % (2 * Math.PI);
516
- return (degrees * 180) / Math.PI;
517
- }
391
+ declare function radiansToDegrees(radians: number): number;
518
392
  /**
519
393
  * Converts an angle in degrees to radians
520
394
  *
@@ -522,10 +396,7 @@ export function radiansToDegrees(radians) {
522
396
  * @param {number} degrees angle between 0 and 360 degrees
523
397
  * @returns {number} angle in radians
524
398
  */
525
- export function degreesToRadians(degrees) {
526
- const radians = degrees % 360;
527
- return (radians * Math.PI) / 180;
528
- }
399
+ declare function degreesToRadians(degrees: number): number;
529
400
  /**
530
401
  * Converts a length to the requested unit.
531
402
  * Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
@@ -535,12 +406,7 @@ export function degreesToRadians(degrees) {
535
406
  * @param {Units} [finalUnit="kilometers"] returned unit
536
407
  * @returns {number} the converted length
537
408
  */
538
- export function convertLength(length, originalUnit = "kilometers", finalUnit = "kilometers") {
539
- if (!(length >= 0)) {
540
- throw new Error("length must be a positive number");
541
- }
542
- return radiansToLength(lengthToRadians(length, originalUnit), finalUnit);
543
- }
409
+ declare function convertLength(length: number, originalUnit?: Units, finalUnit?: Units): number;
544
410
  /**
545
411
  * Converts a area to the requested unit.
546
412
  * Valid units: kilometers, kilometres, meters, metres, centimetres, millimeters, acres, miles, yards, feet, inches, hectares
@@ -549,20 +415,7 @@ export function convertLength(length, originalUnit = "kilometers", finalUnit = "
549
415
  * @param {Units} [finalUnit="kilometers"] returned unit
550
416
  * @returns {number} the converted area
551
417
  */
552
- export function convertArea(area, originalUnit = "meters", finalUnit = "kilometers") {
553
- if (!(area >= 0)) {
554
- throw new Error("area must be a positive number");
555
- }
556
- const startFactor = areaFactors[originalUnit];
557
- if (!startFactor) {
558
- throw new Error("invalid original units");
559
- }
560
- const finalFactor = areaFactors[finalUnit];
561
- if (!finalFactor) {
562
- throw new Error("invalid final units");
563
- }
564
- return (area / startFactor) * finalFactor;
565
- }
418
+ declare function convertArea(area: number, originalUnit?: AreaUnits, finalUnit?: AreaUnits): number;
566
419
  /**
567
420
  * isNumber
568
421
  *
@@ -574,9 +427,7 @@ export function convertArea(area, originalUnit = "meters", finalUnit = "kilomete
574
427
  * turf.isNumber('foo')
575
428
  * //=false
576
429
  */
577
- export function isNumber(num) {
578
- return !isNaN(num) && num !== null && !Array.isArray(num);
579
- }
430
+ declare function isNumber(num: any): boolean;
580
431
  /**
581
432
  * isObject
582
433
  *
@@ -588,9 +439,7 @@ export function isNumber(num) {
588
439
  * turf.isObject('foo')
589
440
  * //=false
590
441
  */
591
- export function isObject(input) {
592
- return input !== null && typeof input === "object" && !Array.isArray(input);
593
- }
442
+ declare function isObject(input: any): boolean;
594
443
  /**
595
444
  * Validate BBox
596
445
  *
@@ -612,22 +461,7 @@ export function isObject(input) {
612
461
  * validateBBox(undefined)
613
462
  * //=Error
614
463
  */
615
- export function validateBBox(bbox) {
616
- if (!bbox) {
617
- throw new Error("bbox is required");
618
- }
619
- if (!Array.isArray(bbox)) {
620
- throw new Error("bbox must be an Array");
621
- }
622
- if (bbox.length !== 4 && bbox.length !== 6) {
623
- throw new Error("bbox must be an Array of 4 or 6 numbers");
624
- }
625
- bbox.forEach((num) => {
626
- if (!isNumber(num)) {
627
- throw new Error("bbox must only contain numbers");
628
- }
629
- });
630
- }
464
+ declare function validateBBox(bbox: any): void;
631
465
  /**
632
466
  * Validate Id
633
467
  *
@@ -649,11 +483,6 @@ export function validateBBox(bbox) {
649
483
  * validateId(undefined)
650
484
  * //=Error
651
485
  */
652
- export function validateId(id) {
653
- if (!id) {
654
- throw new Error("id is required");
655
- }
656
- if (["string", "number"].indexOf(typeof id) === -1) {
657
- throw new Error("id must be a number or a string");
658
- }
659
- }
486
+ declare function validateId(id: any): void;
487
+
488
+ export { type AllGeoJSON, type AreaUnits, type Coord, type Corners, type Grid, type Id, type Lines, type Units, areaFactors, bearingToAzimuth, convertArea, convertLength, degreesToRadians, earthRadius, factors, feature, featureCollection, geometry, geometryCollection, isNumber, isObject, lengthToDegrees, lengthToRadians, lineString, lineStrings, multiLineString, multiPoint, multiPolygon, point, points, polygon, polygons, radiansToDegrees, radiansToLength, round, validateBBox, validateId };