@turf/helpers 6.1.0 → 6.1.4

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/index.js CHANGED
@@ -616,7 +616,7 @@ exports.convertArea = convertArea;
616
616
  * //=false
617
617
  */
618
618
  function isNumber(num) {
619
- return !isNaN(num) && num !== null && !Array.isArray(num);
619
+ return !isNaN(num) && num !== null && !Array.isArray(num) && !/^\s*$/.test(num);
620
620
  }
621
621
  exports.isNumber = isNumber;
622
622
  /**
@@ -0,0 +1,240 @@
1
+ // Type definitions for geojson 7946.0
2
+ // Project: https://geojson.org/
3
+ // Definitions by: Jacob Bruun <https://github.com/cobster>
4
+ // Arne Schubert <https://github.com/atd-schubert>
5
+ // Jeff Jacobson <https://github.com/JeffJacobson>
6
+ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7
+ // TypeScript Version: 2.3
8
+
9
+ // Note: as of the RFC 7946 version of GeoJSON, Coordinate Reference Systems
10
+ // are no longer supported. (See https://tools.ietf.org/html/rfc7946#appendix-B)}
11
+
12
+ // export as namespace GeoJSON;
13
+
14
+ /**
15
+ * GeometryTypes
16
+ *
17
+ * https://tools.ietf.org/html/rfc7946#section-1.4
18
+ * The valid values for the "type" property of GeoJSON geometry objects.
19
+ */
20
+ export type GeometryTypes = "Point" |
21
+ "LineString" |
22
+ "Polygon" |
23
+ "MultiPoint" |
24
+ "MultiLineString" |
25
+ "MultiPolygon" |
26
+ "GeometryCollection";
27
+
28
+ export type CollectionTypes = "FeatureCollection" | "GeometryCollection";
29
+
30
+ /**
31
+ * Types
32
+ *
33
+ * https://tools.ietf.org/html/rfc7946#section-1.4
34
+ * The value values for the "type" property of GeoJSON Objects.
35
+ */
36
+ export type Types = "Feature" | GeometryTypes | CollectionTypes;
37
+
38
+ /**
39
+ * Bounding box
40
+ *
41
+ * https://tools.ietf.org/html/rfc7946#section-5
42
+ * A GeoJSON object MAY have a member named "bbox" to include information on the coordinate range for its Geometries, Features, or FeatureCollections.
43
+ * The value of the bbox member MUST be an array of length 2*n where n is the number of dimensions represented in the contained geometries,
44
+ * with all axes of the most southwesterly point followed by all axes of the more northeasterly point.
45
+ * The axes order of a bbox follows the axes order of geometries.
46
+ */
47
+ export type BBox2d = [number, number, number, number];
48
+ export type BBox3d = [number, number, number, number, number, number];
49
+ export type BBox = BBox2d | BBox3d;
50
+
51
+ /**
52
+ * Id
53
+ *
54
+ * https://tools.ietf.org/html/rfc7946#section-3.2
55
+ * If a Feature has a commonly used identifier, that identifier SHOULD be included as a member of
56
+ * the Feature object with the name "id", and the value of this member is either a JSON string or number.
57
+ */
58
+ export type Id = string | number;
59
+
60
+ /**
61
+ * Position
62
+ *
63
+ * https://tools.ietf.org/html/rfc7946#section-3.1.1
64
+ * Array should contain between two and three elements.
65
+ * The previous GeoJSON specification allowed more elements (e.g., which could be used to represent M values),
66
+ * but the current specification only allows X, Y, and (optionally) Z to be defined.
67
+ */
68
+ export type Position = number[]; // [number, number] | [number, number, number];
69
+
70
+ /**
71
+ * Properties
72
+ *
73
+ * https://tools.ietf.org/html/rfc7946#section-3.2
74
+ * A Feature object has a member with the name "properties".
75
+ * The value of the properties member is an object (any JSON object or a JSON null value).
76
+ */
77
+ export type Properties = { [name: string]: any; } | null;
78
+
79
+ /**
80
+ * Geometries
81
+ */
82
+ export type Geometries = Point |
83
+ LineString |
84
+ Polygon |
85
+ MultiPoint |
86
+ MultiLineString |
87
+ MultiPolygon;
88
+
89
+ /**
90
+ * GeoJSON Object
91
+ *
92
+ * https://tools.ietf.org/html/rfc7946#section-3
93
+ * The GeoJSON specification also allows [foreign members](https://tools.ietf.org/html/rfc7946#section-6.1)
94
+ * Developers should use "&" type in TypeScript or extend the interface to add these foreign members.
95
+ */
96
+ export interface GeoJSONObject {
97
+ // Don't include foreign members directly into this type def.
98
+ // in order to preserve type safety.
99
+ // [key: string]: any;
100
+ /**
101
+ * Specifies the type of GeoJSON object.
102
+ */
103
+ type: string;
104
+ /**
105
+ * Bounding box of the coordinate range of the object's Geometries, Features, or Feature Collections.
106
+ * https://tools.ietf.org/html/rfc7946#section-5
107
+ */
108
+ bbox?: BBox;
109
+ }
110
+
111
+ /**
112
+ * Geometry Object
113
+ *
114
+ * https://tools.ietf.org/html/rfc7946#section-3
115
+ */
116
+ export interface GeometryObject extends GeoJSONObject {
117
+ type: GeometryTypes;
118
+ }
119
+
120
+ /**
121
+ * Geometry
122
+ *
123
+ * https://tools.ietf.org/html/rfc7946#section-3
124
+ */
125
+ export interface Geometry extends GeoJSONObject {
126
+ coordinates: Position |
127
+ Position[] |
128
+ Position[][] |
129
+ Position[][][];
130
+ }
131
+
132
+ /**
133
+ * Point Geometry Object
134
+ *
135
+ * https://tools.ietf.org/html/rfc7946#section-3.1.2
136
+ */
137
+ export interface Point extends GeometryObject {
138
+ type: "Point";
139
+ coordinates: Position;
140
+ }
141
+
142
+ /**
143
+ * MultiPoint Geometry Object
144
+ *
145
+ * https://tools.ietf.org/html/rfc7946#section-3.1.3
146
+ */
147
+ export interface MultiPoint extends GeometryObject {
148
+ type: "MultiPoint";
149
+ coordinates: Position[];
150
+ }
151
+
152
+ /**
153
+ * LineString Geometry Object
154
+ *
155
+ * https://tools.ietf.org/html/rfc7946#section-3.1.4
156
+ */
157
+ export interface LineString extends GeometryObject {
158
+ type: "LineString";
159
+ coordinates: Position[];
160
+ }
161
+
162
+ /**
163
+ * MultiLineString Geometry Object
164
+ *
165
+ * https://tools.ietf.org/html/rfc7946#section-3.1.5
166
+ */
167
+ export interface MultiLineString extends GeometryObject {
168
+ type: "MultiLineString";
169
+ coordinates: Position[][];
170
+ }
171
+
172
+ /**
173
+ * Polygon Geometry Object
174
+ *
175
+ * https://tools.ietf.org/html/rfc7946#section-3.1.6
176
+ */
177
+ export interface Polygon extends GeometryObject {
178
+ type: "Polygon";
179
+ coordinates: Position[][];
180
+ }
181
+
182
+ /**
183
+ * MultiPolygon Geometry Object
184
+ *
185
+ * https://tools.ietf.org/html/rfc7946#section-3.1.7
186
+ */
187
+ export interface MultiPolygon extends GeometryObject {
188
+ type: "MultiPolygon";
189
+ coordinates: Position[][][];
190
+ }
191
+
192
+ /**
193
+ * GeometryCollection
194
+ *
195
+ * https://tools.ietf.org/html/rfc7946#section-3.1.8
196
+ *
197
+ * A GeoJSON object with type "GeometryCollection" is a Geometry object.
198
+ * A GeometryCollection has a member with the name "geometries".
199
+ * The value of "geometries" is an array. Each element of this array is a GeoJSON Geometry object.
200
+ * It is possible for this array to be empty.
201
+ */
202
+ export interface GeometryCollection extends GeometryObject {
203
+ type: "GeometryCollection";
204
+ geometries: Array<Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon>;
205
+ }
206
+
207
+ /**
208
+ * Feature
209
+ *
210
+ * https://tools.ietf.org/html/rfc7946#section-3.2
211
+ * A Feature object represents a spatially bounded thing.
212
+ * Every Feature object is a GeoJSON object no matter where it occurs in a GeoJSON text.
213
+ */
214
+ export interface Feature<G = Geometry | GeometryCollection, P = Properties> extends GeoJSONObject {
215
+ type: "Feature";
216
+ geometry: G;
217
+ /**
218
+ * A value that uniquely identifies this feature in a
219
+ * https://tools.ietf.org/html/rfc7946#section-3.2.
220
+ */
221
+ id?: Id;
222
+ /**
223
+ * Properties associated with this feature.
224
+ */
225
+ properties: P;
226
+ }
227
+
228
+ /**
229
+ * Feature Collection
230
+ *
231
+ * https://tools.ietf.org/html/rfc7946#section-3.3
232
+ * A GeoJSON object with the type "FeatureCollection" is a FeatureCollection object.
233
+ * A FeatureCollection object has a member with the name "features".
234
+ * The value of "features" is a JSON array. Each element of the array is a Feature object as defined above.
235
+ * It is possible for this array to be empty.
236
+ */
237
+ export interface FeatureCollection<G = Geometry | GeometryCollection, P = Properties> extends GeoJSONObject {
238
+ type: "FeatureCollection";
239
+ features: Array<Feature<G, P>>;
240
+ }
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@turf/helpers",
3
- "version": "6.1.0",
3
+ "version": "6.1.4",
4
4
  "description": "turf helpers module",
5
5
  "main": "index",
6
6
  "types": "index.d.ts",
7
7
  "files": [
8
8
  "index.js",
9
- "index.ts",
10
- "index.d.ts"
9
+ "index.d.ts",
10
+ "lib"
11
11
  ],
12
12
  "scripts": {
13
13
  "prepare": "tsc",
@@ -31,7 +31,8 @@
31
31
  "Tom MacWright <@tmcw>",
32
32
  "Stefano Borghi <@stebogit>",
33
33
  "Denis Carriere <@DenisCarriere>",
34
- "William Nordmann <@wnordmann>"
34
+ "William Nordmann <@wnordmann>",
35
+ "Vamshi Palreddy <@vamshi29292>"
35
36
  ],
36
37
  "license": "MIT",
37
38
  "bugs": {
@@ -41,6 +42,8 @@
41
42
  "devDependencies": {
42
43
  "benchmark": "*",
43
44
  "tape": "*",
44
- "typescript": "*"
45
+ "typescript": "*",
46
+ "tslint": "*",
47
+ "@types/tape": "*"
45
48
  }
46
49
  }
package/index.ts DELETED
@@ -1,762 +0,0 @@
1
- import {
2
- BBox, CollectionTypes, Feature, FeatureCollection,
3
- GeoJSONObject, Geometries, Geometry, GeometryCollection, GeometryObject, GeometryTypes,
4
- Id, LineString, MultiLineString, MultiPoint,
5
- MultiPolygon, Point, Polygon, Position,
6
- Properties, Types,
7
- } from "./lib/geojson";
8
- export {
9
- Id, Properties, BBox, Position,
10
- Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon,
11
- GeometryObject, GeoJSONObject, GeometryCollection, Geometry,
12
- GeometryTypes, Types, CollectionTypes, Geometries,
13
- Feature, FeatureCollection,
14
- };
15
-
16
- // TurfJS Combined Types
17
- export type Coord = Feature<Point> | Point | Position;
18
-
19
- // TurfJS String Types
20
- export type Units = "meters" | "millimeters" | "centimeters" |
21
- "kilometers" | "acres" | "miles" | "nauticalmiles" |
22
- "inches" | "yards" | "feet" | "radians" | "degrees";
23
- export type Grid = "point" | "square" | "hex" | "triangle";
24
- export type Corners = "sw" | "se" | "nw" | "ne" | "center" | "centroid";
25
-
26
- export type Lines = LineString | MultiLineString | Polygon | MultiPolygon;
27
- export type AllGeoJSON = Feature | FeatureCollection | Geometry | GeometryCollection;
28
-
29
- /**
30
- * @module helpers
31
- */
32
-
33
- /**
34
- * Earth Radius used with the Harvesine formula and approximates using a spherical (non-ellipsoid) Earth.
35
- *
36
- * @memberof helpers
37
- * @type {number}
38
- */
39
- export let earthRadius = 6371008.8;
40
-
41
- /**
42
- * Unit of measurement factors using a spherical (non-ellipsoid) earth radius.
43
- *
44
- * @memberof helpers
45
- * @type {Object}
46
- */
47
- export let factors: {[key: string]: number} = {
48
- centimeters: earthRadius * 100,
49
- centimetres: earthRadius * 100,
50
- degrees: earthRadius / 111325,
51
- feet: earthRadius * 3.28084,
52
- inches: earthRadius * 39.370,
53
- kilometers: earthRadius / 1000,
54
- kilometres: earthRadius / 1000,
55
- meters: earthRadius,
56
- metres: earthRadius,
57
- miles: earthRadius / 1609.344,
58
- millimeters: earthRadius * 1000,
59
- millimetres: earthRadius * 1000,
60
- nauticalmiles: earthRadius / 1852,
61
- radians: 1,
62
- yards: earthRadius / 1.0936,
63
- };
64
-
65
- /**
66
- * Units of measurement factors based on 1 meter.
67
- *
68
- * @memberof helpers
69
- * @type {Object}
70
- */
71
- export let unitsFactors: {[key: string]: number} = {
72
- centimeters: 100,
73
- centimetres: 100,
74
- degrees: 1 / 111325,
75
- feet: 3.28084,
76
- inches: 39.370,
77
- kilometers: 1 / 1000,
78
- kilometres: 1 / 1000,
79
- meters: 1,
80
- metres: 1,
81
- miles: 1 / 1609.344,
82
- millimeters: 1000,
83
- millimetres: 1000,
84
- nauticalmiles: 1 / 1852,
85
- radians: 1 / earthRadius,
86
- yards: 1 / 1.0936,
87
- };
88
-
89
- /**
90
- * Area of measurement factors based on 1 square meter.
91
- *
92
- * @memberof helpers
93
- * @type {Object}
94
- */
95
- export let areaFactors: any = {
96
- acres: 0.000247105,
97
- centimeters: 10000,
98
- centimetres: 10000,
99
- feet: 10.763910417,
100
- inches: 1550.003100006,
101
- kilometers: 0.000001,
102
- kilometres: 0.000001,
103
- meters: 1,
104
- metres: 1,
105
- miles: 3.86e-7,
106
- millimeters: 1000000,
107
- millimetres: 1000000,
108
- yards: 1.195990046,
109
- };
110
-
111
- /**
112
- * Wraps a GeoJSON {@link Geometry} in a GeoJSON {@link Feature}.
113
- *
114
- * @name feature
115
- * @param {Geometry} geometry input geometry
116
- * @param {Object} [properties={}] an Object of key-value pairs to add as properties
117
- * @param {Object} [options={}] Optional Parameters
118
- * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
119
- * @param {string|number} [options.id] Identifier associated with the Feature
120
- * @returns {Feature} a GeoJSON Feature
121
- * @example
122
- * var geometry = {
123
- * "type": "Point",
124
- * "coordinates": [110, 50]
125
- * };
126
- *
127
- * var feature = turf.feature(geometry);
128
- *
129
- * //=feature
130
- */
131
- export function feature<G = Geometry, P = Properties>(
132
- geom: G,
133
- properties?: P,
134
- options: {bbox?: BBox, id?: Id} = {},
135
- ): Feature<G, P> {
136
- const feat: any = {type: "Feature"};
137
- if (options.id === 0 || options.id) { feat.id = options.id; }
138
- if (options.bbox) { feat.bbox = options.bbox; }
139
- feat.properties = properties || {};
140
- feat.geometry = geom;
141
- return feat;
142
- }
143
-
144
- /**
145
- * Creates a GeoJSON {@link Geometry} from a Geometry string type & coordinates.
146
- * For GeometryCollection type use `helpers.geometryCollection`
147
- *
148
- * @name geometry
149
- * @param {string} type Geometry Type
150
- * @param {Array<any>} coordinates Coordinates
151
- * @param {Object} [options={}] Optional Parameters
152
- * @returns {Geometry} a GeoJSON Geometry
153
- * @example
154
- * var type = "Point";
155
- * var coordinates = [110, 50];
156
- * var geometry = turf.geometry(type, coordinates);
157
- * // => geometry
158
- */
159
- export function geometry(
160
- type: "Point" | "LineString" | "Polygon" | "MultiPoint" | "MultiLineString" | "MultiPolygon",
161
- coordinates: any[],
162
- options: {} = {},
163
- ) {
164
- switch (type) {
165
- case "Point": return point(coordinates).geometry;
166
- case "LineString": return lineString(coordinates).geometry;
167
- case "Polygon": return polygon(coordinates).geometry;
168
- case "MultiPoint": return multiPoint(coordinates).geometry;
169
- case "MultiLineString": return multiLineString(coordinates).geometry;
170
- case "MultiPolygon": return multiPolygon(coordinates).geometry;
171
- default: throw new Error(type + " is invalid");
172
- }
173
- }
174
-
175
- /**
176
- * Creates a {@link Point} {@link Feature} from a Position.
177
- *
178
- * @name point
179
- * @param {Array<number>} coordinates longitude, latitude position (each in decimal degrees)
180
- * @param {Object} [properties={}] an Object of key-value pairs to add as properties
181
- * @param {Object} [options={}] Optional Parameters
182
- * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
183
- * @param {string|number} [options.id] Identifier associated with the Feature
184
- * @returns {Feature<Point>} a Point feature
185
- * @example
186
- * var point = turf.point([-75.343, 39.984]);
187
- *
188
- * //=point
189
- */
190
- export function point<P = Properties>(
191
- coordinates: Position,
192
- properties?: P,
193
- options: {bbox?: BBox, id?: Id} = {},
194
- ): Feature<Point, P> {
195
- const geom: Point = {
196
- type: "Point",
197
- coordinates,
198
- };
199
- return feature(geom, properties, options);
200
- }
201
-
202
- /**
203
- * Creates a {@link Point} {@link FeatureCollection} from an Array of Point coordinates.
204
- *
205
- * @name points
206
- * @param {Array<Array<number>>} coordinates an array of Points
207
- * @param {Object} [properties={}] Translate these properties to each Feature
208
- * @param {Object} [options={}] Optional Parameters
209
- * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north]
210
- * associated with the FeatureCollection
211
- * @param {string|number} [options.id] Identifier associated with the FeatureCollection
212
- * @returns {FeatureCollection<Point>} Point Feature
213
- * @example
214
- * var points = turf.points([
215
- * [-75, 39],
216
- * [-80, 45],
217
- * [-78, 50]
218
- * ]);
219
- *
220
- * //=points
221
- */
222
- export function points<P = Properties>(
223
- coordinates: Position[],
224
- properties?: P,
225
- options: {bbox?: BBox, id?: Id} = {},
226
- ): FeatureCollection<Point, P> {
227
- return featureCollection(coordinates.map((coords) => {
228
- return point(coords, properties);
229
- }), options);
230
- }
231
-
232
- /**
233
- * Creates a {@link Polygon} {@link Feature} from an Array of LinearRings.
234
- *
235
- * @name polygon
236
- * @param {Array<Array<Array<number>>>} coordinates an array of LinearRings
237
- * @param {Object} [properties={}] an Object of key-value pairs to add as properties
238
- * @param {Object} [options={}] Optional Parameters
239
- * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
240
- * @param {string|number} [options.id] Identifier associated with the Feature
241
- * @returns {Feature<Polygon>} Polygon Feature
242
- * @example
243
- * var polygon = turf.polygon([[[-5, 52], [-4, 56], [-2, 51], [-7, 54], [-5, 52]]], { name: 'poly1' });
244
- *
245
- * //=polygon
246
- */
247
- export function polygon<P = Properties>(
248
- coordinates: Position[][],
249
- properties?: P,
250
- options: {bbox?: BBox, id?: Id} = {},
251
- ): Feature<Polygon, P> {
252
- for (const ring of coordinates) {
253
- if (ring.length < 4) {
254
- throw new Error("Each LinearRing of a Polygon must have 4 or more Positions.");
255
- }
256
- for (let j = 0; j < ring[ring.length - 1].length; j++) {
257
- // Check if first point of Polygon contains two numbers
258
- if (ring[ring.length - 1][j] !== ring[0][j]) {
259
- throw new Error("First and last Position are not equivalent.");
260
- }
261
- }
262
- }
263
- const geom: Polygon = {
264
- type: "Polygon",
265
- coordinates,
266
- };
267
- return feature(geom, properties, options);
268
- }
269
-
270
- /**
271
- * Creates a {@link Polygon} {@link FeatureCollection} from an Array of Polygon coordinates.
272
- *
273
- * @name polygons
274
- * @param {Array<Array<Array<Array<number>>>>} coordinates an array of Polygon coordinates
275
- * @param {Object} [properties={}] an Object of key-value pairs to add as properties
276
- * @param {Object} [options={}] Optional Parameters
277
- * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
278
- * @param {string|number} [options.id] Identifier associated with the FeatureCollection
279
- * @returns {FeatureCollection<Polygon>} Polygon FeatureCollection
280
- * @example
281
- * var polygons = turf.polygons([
282
- * [[[-5, 52], [-4, 56], [-2, 51], [-7, 54], [-5, 52]]],
283
- * [[[-15, 42], [-14, 46], [-12, 41], [-17, 44], [-15, 42]]],
284
- * ]);
285
- *
286
- * //=polygons
287
- */
288
- export function polygons<P = Properties>(
289
- coordinates: Position[][][],
290
- properties?: P,
291
- options: {bbox?: BBox, id?: Id} = {},
292
- ): FeatureCollection<Polygon, P> {
293
- return featureCollection(coordinates.map((coords) => {
294
- return polygon(coords, properties);
295
- }), options);
296
- }
297
-
298
- /**
299
- * Creates a {@link LineString} {@link Feature} from an Array of Positions.
300
- *
301
- * @name lineString
302
- * @param {Array<Array<number>>} coordinates an array of Positions
303
- * @param {Object} [properties={}] an Object of key-value pairs to add as properties
304
- * @param {Object} [options={}] Optional Parameters
305
- * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
306
- * @param {string|number} [options.id] Identifier associated with the Feature
307
- * @returns {Feature<LineString>} LineString Feature
308
- * @example
309
- * var linestring1 = turf.lineString([[-24, 63], [-23, 60], [-25, 65], [-20, 69]], {name: 'line 1'});
310
- * var linestring2 = turf.lineString([[-14, 43], [-13, 40], [-15, 45], [-10, 49]], {name: 'line 2'});
311
- *
312
- * //=linestring1
313
- * //=linestring2
314
- */
315
- export function lineString<P = Properties>(
316
- coordinates: Position[],
317
- properties?: P,
318
- options: {bbox?: BBox, id?: Id} = {},
319
- ): Feature<LineString, P> {
320
- if (coordinates.length < 2) { throw new Error("coordinates must be an array of two or more positions"); }
321
- const geom: LineString = {
322
- type: "LineString",
323
- coordinates,
324
- };
325
- return feature(geom, properties, options);
326
- }
327
-
328
- /**
329
- * Creates a {@link LineString} {@link FeatureCollection} from an Array of LineString coordinates.
330
- *
331
- * @name lineStrings
332
- * @param {Array<Array<Array<number>>>} coordinates an array of LinearRings
333
- * @param {Object} [properties={}] an Object of key-value pairs to add as properties
334
- * @param {Object} [options={}] Optional Parameters
335
- * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north]
336
- * associated with the FeatureCollection
337
- * @param {string|number} [options.id] Identifier associated with the FeatureCollection
338
- * @returns {FeatureCollection<LineString>} LineString FeatureCollection
339
- * @example
340
- * var linestrings = turf.lineStrings([
341
- * [[-24, 63], [-23, 60], [-25, 65], [-20, 69]],
342
- * [[-14, 43], [-13, 40], [-15, 45], [-10, 49]]
343
- * ]);
344
- *
345
- * //=linestrings
346
- */
347
- export function lineStrings<P = Properties>(
348
- coordinates: Position[][],
349
- properties?: P,
350
- options: {bbox?: BBox, id?: Id} = {},
351
- ): FeatureCollection<LineString, P> {
352
- return featureCollection(coordinates.map((coords) => {
353
- return lineString(coords, properties);
354
- }), options);
355
- }
356
-
357
- /**
358
- * Takes one or more {@link Feature|Features} and creates a {@link FeatureCollection}.
359
- *
360
- * @name featureCollection
361
- * @param {Feature[]} features input features
362
- * @param {Object} [options={}] Optional Parameters
363
- * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
364
- * @param {string|number} [options.id] Identifier associated with the Feature
365
- * @returns {FeatureCollection} FeatureCollection of Features
366
- * @example
367
- * var locationA = turf.point([-75.343, 39.984], {name: 'Location A'});
368
- * var locationB = turf.point([-75.833, 39.284], {name: 'Location B'});
369
- * var locationC = turf.point([-75.534, 39.123], {name: 'Location C'});
370
- *
371
- * var collection = turf.featureCollection([
372
- * locationA,
373
- * locationB,
374
- * locationC
375
- * ]);
376
- *
377
- * //=collection
378
- */
379
- export function featureCollection<G = Geometry, P = Properties>(
380
- features: Array<Feature<G, P>>,
381
- options: {bbox?: BBox, id?: Id} = {},
382
- ): FeatureCollection<G, P> {
383
- const fc: any = {type: "FeatureCollection"};
384
- if (options.id) { fc.id = options.id; }
385
- if (options.bbox) { fc.bbox = options.bbox; }
386
- fc.features = features;
387
- return fc;
388
- }
389
-
390
- /**
391
- * Creates a {@link Feature<MultiLineString>} based on a
392
- * coordinate array. Properties can be added optionally.
393
- *
394
- * @name multiLineString
395
- * @param {Array<Array<Array<number>>>} coordinates an array of LineStrings
396
- * @param {Object} [properties={}] an Object of key-value pairs to add as properties
397
- * @param {Object} [options={}] Optional Parameters
398
- * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
399
- * @param {string|number} [options.id] Identifier associated with the Feature
400
- * @returns {Feature<MultiLineString>} a MultiLineString feature
401
- * @throws {Error} if no coordinates are passed
402
- * @example
403
- * var multiLine = turf.multiLineString([[[0,0],[10,10]]]);
404
- *
405
- * //=multiLine
406
- */
407
- export function multiLineString<P = Properties>(
408
- coordinates: Position[][],
409
- properties?: P,
410
- options: {bbox?: BBox, id?: Id} = {},
411
- ): Feature<MultiLineString, P> {
412
- const geom: MultiLineString = {
413
- type: "MultiLineString",
414
- coordinates,
415
- };
416
- return feature(geom, properties, options);
417
- }
418
-
419
- /**
420
- * Creates a {@link Feature<MultiPoint>} based on a
421
- * coordinate array. Properties can be added optionally.
422
- *
423
- * @name multiPoint
424
- * @param {Array<Array<number>>} coordinates an array of Positions
425
- * @param {Object} [properties={}] an Object of key-value pairs to add as properties
426
- * @param {Object} [options={}] Optional Parameters
427
- * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
428
- * @param {string|number} [options.id] Identifier associated with the Feature
429
- * @returns {Feature<MultiPoint>} a MultiPoint feature
430
- * @throws {Error} if no coordinates are passed
431
- * @example
432
- * var multiPt = turf.multiPoint([[0,0],[10,10]]);
433
- *
434
- * //=multiPt
435
- */
436
- export function multiPoint<P = Properties>(
437
- coordinates: Position[],
438
- properties?: P,
439
- options: {bbox?: BBox, id?: Id} = {},
440
- ): Feature<MultiPoint, P> {
441
- const geom: MultiPoint = {
442
- type: "MultiPoint",
443
- coordinates,
444
- };
445
- return feature(geom, properties, options);
446
- }
447
-
448
- /**
449
- * Creates a {@link Feature<MultiPolygon>} based on a
450
- * coordinate array. Properties can be added optionally.
451
- *
452
- * @name multiPolygon
453
- * @param {Array<Array<Array<Array<number>>>>} coordinates an array of Polygons
454
- * @param {Object} [properties={}] an Object of key-value pairs to add as properties
455
- * @param {Object} [options={}] Optional Parameters
456
- * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
457
- * @param {string|number} [options.id] Identifier associated with the Feature
458
- * @returns {Feature<MultiPolygon>} a multipolygon feature
459
- * @throws {Error} if no coordinates are passed
460
- * @example
461
- * var multiPoly = turf.multiPolygon([[[[0,0],[0,10],[10,10],[10,0],[0,0]]]]);
462
- *
463
- * //=multiPoly
464
- *
465
- */
466
- export function multiPolygon<P = Properties>(
467
- coordinates: Position[][][],
468
- properties?: P,
469
- options: {bbox?: BBox, id?: Id} = {},
470
- ): Feature<MultiPolygon, P> {
471
- const geom: MultiPolygon = {
472
- type: "MultiPolygon",
473
- coordinates,
474
- };
475
- return feature(geom, properties, options);
476
- }
477
-
478
- /**
479
- * Creates a {@link Feature<GeometryCollection>} based on a
480
- * coordinate array. Properties can be added optionally.
481
- *
482
- * @name geometryCollection
483
- * @param {Array<Geometry>} geometries an array of GeoJSON Geometries
484
- * @param {Object} [properties={}] an Object of key-value pairs to add as properties
485
- * @param {Object} [options={}] Optional Parameters
486
- * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
487
- * @param {string|number} [options.id] Identifier associated with the Feature
488
- * @returns {Feature<GeometryCollection>} a GeoJSON GeometryCollection Feature
489
- * @example
490
- * var pt = turf.geometry("Point", [100, 0]);
491
- * var line = turf.geometry("LineString", [[101, 0], [102, 1]]);
492
- * var collection = turf.geometryCollection([pt, line]);
493
- *
494
- * // => collection
495
- */
496
- export function geometryCollection<P = Properties>(
497
- geometries: Array<Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon>,
498
- properties?: P,
499
- options: {bbox?: BBox, id?: Id} = {},
500
- ): Feature<GeometryCollection, P> {
501
- const geom: GeometryCollection = {
502
- type: "GeometryCollection",
503
- geometries,
504
- };
505
- return feature(geom, properties, options);
506
- }
507
-
508
- /**
509
- * Round number to precision
510
- *
511
- * @param {number} num Number
512
- * @param {number} [precision=0] Precision
513
- * @returns {number} rounded number
514
- * @example
515
- * turf.round(120.4321)
516
- * //=120
517
- *
518
- * turf.round(120.4321, 2)
519
- * //=120.43
520
- */
521
- export function round(num: number, precision = 0): number {
522
- if (precision && !(precision >= 0)) { throw new Error("precision must be a positive number"); }
523
- const multiplier = Math.pow(10, precision || 0);
524
- return Math.round(num * multiplier) / multiplier;
525
- }
526
-
527
- /**
528
- * Convert a distance measurement (assuming a spherical Earth) from radians to a more friendly unit.
529
- * Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
530
- *
531
- * @name radiansToLength
532
- * @param {number} radians in radians across the sphere
533
- * @param {string} [units="kilometers"] can be degrees, radians, miles, or kilometers inches, yards, metres,
534
- * meters, kilometres, kilometers.
535
- * @returns {number} distance
536
- */
537
- export function radiansToLength(radians: number, units: Units = "kilometers"): number {
538
- const factor = factors[units];
539
- if (!factor) { throw new Error(units + " units is invalid"); }
540
- return radians * factor;
541
- }
542
-
543
- /**
544
- * Convert a distance measurement (assuming a spherical Earth) from a real-world unit into radians
545
- * Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
546
- *
547
- * @name lengthToRadians
548
- * @param {number} distance in real units
549
- * @param {string} [units="kilometers"] can be degrees, radians, miles, or kilometers inches, yards, metres,
550
- * meters, kilometres, kilometers.
551
- * @returns {number} radians
552
- */
553
- export function lengthToRadians(distance: number, units: Units = "kilometers"): number {
554
- const factor = factors[units];
555
- if (!factor) { throw new Error(units + " units is invalid"); }
556
- return distance / factor;
557
- }
558
-
559
- /**
560
- * Convert a distance measurement (assuming a spherical Earth) from a real-world unit into degrees
561
- * Valid units: miles, nauticalmiles, inches, yards, meters, metres, centimeters, kilometres, feet
562
- *
563
- * @name lengthToDegrees
564
- * @param {number} distance in real units
565
- * @param {string} [units="kilometers"] can be degrees, radians, miles, or kilometers inches, yards, metres,
566
- * meters, kilometres, kilometers.
567
- * @returns {number} degrees
568
- */
569
- export function lengthToDegrees(distance: number, units?: Units): number {
570
- return radiansToDegrees(lengthToRadians(distance, units));
571
- }
572
-
573
- /**
574
- * Converts any bearing angle from the north line direction (positive clockwise)
575
- * and returns an angle between 0-360 degrees (positive clockwise), 0 being the north line
576
- *
577
- * @name bearingToAzimuth
578
- * @param {number} bearing angle, between -180 and +180 degrees
579
- * @returns {number} angle between 0 and 360 degrees
580
- */
581
- export function bearingToAzimuth(bearing: number): number {
582
- let angle = bearing % 360;
583
- if (angle < 0) { angle += 360; }
584
- return angle;
585
- }
586
-
587
- /**
588
- * Converts an angle in radians to degrees
589
- *
590
- * @name radiansToDegrees
591
- * @param {number} radians angle in radians
592
- * @returns {number} degrees between 0 and 360 degrees
593
- */
594
- export function radiansToDegrees(radians: number): number {
595
- const degrees = radians % (2 * Math.PI);
596
- return degrees * 180 / Math.PI;
597
- }
598
-
599
- /**
600
- * Converts an angle in degrees to radians
601
- *
602
- * @name degreesToRadians
603
- * @param {number} degrees angle between 0 and 360 degrees
604
- * @returns {number} angle in radians
605
- */
606
- export function degreesToRadians(degrees: number): number {
607
- const radians = degrees % 360;
608
- return radians * Math.PI / 180;
609
- }
610
-
611
- /**
612
- * Converts a length to the requested unit.
613
- * Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
614
- *
615
- * @param {number} length to be converted
616
- * @param {Units} [originalUnit="kilometers"] of the length
617
- * @param {Units} [finalUnit="kilometers"] returned unit
618
- * @returns {number} the converted length
619
- */
620
- export function convertLength(
621
- length: number,
622
- originalUnit: Units = "kilometers",
623
- finalUnit: Units = "kilometers",
624
- ): number {
625
- if (!(length >= 0)) { throw new Error("length must be a positive number"); }
626
- return radiansToLength(lengthToRadians(length, originalUnit), finalUnit);
627
- }
628
-
629
- /**
630
- * Converts a area to the requested unit.
631
- * Valid units: kilometers, kilometres, meters, metres, centimetres, millimeters, acres, miles, yards, feet, inches
632
- * @param {number} area to be converted
633
- * @param {Units} [originalUnit="meters"] of the distance
634
- * @param {Units} [finalUnit="kilometers"] returned unit
635
- * @returns {number} the converted distance
636
- */
637
- export function convertArea(area: number, originalUnit: Units = "meters", finalUnit: Units = "kilometers"): number {
638
- if (!(area >= 0)) { throw new Error("area must be a positive number"); }
639
-
640
- const startFactor = areaFactors[originalUnit];
641
- if (!startFactor) { throw new Error("invalid original units"); }
642
-
643
- const finalFactor = areaFactors[finalUnit];
644
- if (!finalFactor) { throw new Error("invalid final units"); }
645
-
646
- return (area / startFactor) * finalFactor;
647
- }
648
-
649
- /**
650
- * isNumber
651
- *
652
- * @param {*} num Number to validate
653
- * @returns {boolean} true/false
654
- * @example
655
- * turf.isNumber(123)
656
- * //=true
657
- * turf.isNumber('foo')
658
- * //=false
659
- */
660
- export function isNumber(num: any): boolean {
661
- return !isNaN(num) && num !== null && !Array.isArray(num);
662
- }
663
-
664
- /**
665
- * isObject
666
- *
667
- * @param {*} input variable to validate
668
- * @returns {boolean} true/false
669
- * @example
670
- * turf.isObject({elevation: 10})
671
- * //=true
672
- * turf.isObject('foo')
673
- * //=false
674
- */
675
- export function isObject(input: any): boolean {
676
- return (!!input) && (input.constructor === Object);
677
- }
678
-
679
- /**
680
- * Validate BBox
681
- *
682
- * @private
683
- * @param {Array<number>} bbox BBox to validate
684
- * @returns {void}
685
- * @throws Error if BBox is not valid
686
- * @example
687
- * validateBBox([-180, -40, 110, 50])
688
- * //=OK
689
- * validateBBox([-180, -40])
690
- * //=Error
691
- * validateBBox('Foo')
692
- * //=Error
693
- * validateBBox(5)
694
- * //=Error
695
- * validateBBox(null)
696
- * //=Error
697
- * validateBBox(undefined)
698
- * //=Error
699
- */
700
- export function validateBBox(bbox: any): void {
701
- if (!bbox) { throw new Error("bbox is required"); }
702
- if (!Array.isArray(bbox)) { throw new Error("bbox must be an Array"); }
703
- if (bbox.length !== 4 && bbox.length !== 6) { throw new Error("bbox must be an Array of 4 or 6 numbers"); }
704
- bbox.forEach((num) => {
705
- if (!isNumber(num)) { throw new Error("bbox must only contain numbers"); }
706
- });
707
- }
708
-
709
- /**
710
- * Validate Id
711
- *
712
- * @private
713
- * @param {string|number} id Id to validate
714
- * @returns {void}
715
- * @throws Error if Id is not valid
716
- * @example
717
- * validateId([-180, -40, 110, 50])
718
- * //=Error
719
- * validateId([-180, -40])
720
- * //=Error
721
- * validateId('Foo')
722
- * //=OK
723
- * validateId(5)
724
- * //=OK
725
- * validateId(null)
726
- * //=Error
727
- * validateId(undefined)
728
- * //=Error
729
- */
730
- export function validateId(id: any): void {
731
- if (!id) { throw new Error("id is required"); }
732
- if (["string", "number"].indexOf(typeof id) === -1) { throw new Error("id must be a number or a string"); }
733
- }
734
-
735
- // Deprecated methods
736
- export function radians2degrees(): void {
737
- throw new Error("method has been renamed to `radiansToDegrees`");
738
- }
739
-
740
- export function degrees2radians(): void {
741
- throw new Error("method has been renamed to `degreesToRadians`");
742
- }
743
-
744
- export function distanceToDegrees(): void {
745
- throw new Error("method has been renamed to `lengthToDegrees`");
746
- }
747
-
748
- export function distanceToRadians(): void {
749
- throw new Error("method has been renamed to `lengthToRadians`");
750
- }
751
-
752
- export function radiansToDistance(): void {
753
- throw new Error("method has been renamed to `radiansToLength`");
754
- }
755
-
756
- export function bearingToAngle(): void {
757
- throw new Error("method has been renamed to `bearingToAzimuth`");
758
- }
759
-
760
- export function convertDistance(): void {
761
- throw new Error("method has been renamed to `convertLength`");
762
- }