@turf/helpers 4.5.2 → 4.7.3

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.
Files changed (4) hide show
  1. package/README.md +72 -12
  2. package/index.d.ts +32 -15
  3. package/index.js +126 -42
  4. package/package.json +3 -3
package/README.md CHANGED
@@ -7,7 +7,9 @@ Wraps a GeoJSON [Geometry](http://geojson.org/geojson-spec.html#geometry) in a G
7
7
  **Parameters**
8
8
 
9
9
  - `geometry` **[Geometry](http://geojson.org/geojson-spec.html#geometry)** input geometry
10
- - `properties` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** properties
10
+ - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object of key-value pairs to add as properties (optional, default `{}`)
11
+ - `bbox` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>]** BBox [west, south, east, north]
12
+ - `id` **\[([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))]** Identifier
11
13
 
12
14
  **Examples**
13
15
 
@@ -24,6 +26,30 @@ var feature = turf.feature(geometry);
24
26
 
25
27
  Returns **[Feature](http://geojson.org/geojson-spec.html#feature-objects)** a GeoJSON Feature
26
28
 
29
+ # geometry
30
+
31
+ Creates a GeoJSON [Geometry](http://geojson.org/geojson-spec.html#geometry) from a Geometry string type & coordinates.
32
+ For GeometryCollection type use `helpers.geometryCollection`
33
+
34
+ **Parameters**
35
+
36
+ - `type` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Geometry Type
37
+ - `coordinates` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** Coordinates
38
+ - `bbox` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>]** BBox [west, south, east, north]
39
+
40
+ **Examples**
41
+
42
+ ```javascript
43
+ var type = 'Point';
44
+ var coordinates = [110, 50];
45
+
46
+ var geometry = turf.geometry(type, coordinates);
47
+
48
+ //=geometry
49
+ ```
50
+
51
+ Returns **[Geometry](http://geojson.org/geojson-spec.html#geometry)** a GeoJSON Geometry
52
+
27
53
  # point
28
54
 
29
55
  Takes coordinates and properties (optional) and returns a new [Point](http://geojson.org/geojson-spec.html#point) feature.
@@ -31,8 +57,9 @@ Takes coordinates and properties (optional) and returns a new [Point](http://geo
31
57
  **Parameters**
32
58
 
33
59
  - `coordinates` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** longitude, latitude position (each in decimal degrees)
34
- - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object that is used as the [Feature](http://geojson.org/geojson-spec.html#feature-objects)'s
35
- properties
60
+ - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object of key-value pairs to add as properties (optional, default `{}`)
61
+ - `bbox` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>]** BBox [west, south, east, north]
62
+ - `id` **\[([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))]** Identifier
36
63
 
37
64
  **Examples**
38
65
 
@@ -51,7 +78,9 @@ Takes an array of LinearRings and optionally an [Object](https://developer.mozil
51
78
  **Parameters**
52
79
 
53
80
  - `coordinates` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>>>** an array of LinearRings
54
- - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** a properties object
81
+ - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object of key-value pairs to add as properties (optional, default `{}`)
82
+ - `bbox` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>]** BBox [west, south, east, north]
83
+ - `id` **\[([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))]** Identifier
55
84
 
56
85
  **Examples**
57
86
 
@@ -80,7 +109,9 @@ coordinate array. Properties can be added optionally.
80
109
  **Parameters**
81
110
 
82
111
  - `coordinates` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>>** an array of Positions
83
- - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object of key-value pairs to add as properties
112
+ - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object of key-value pairs to add as properties (optional, default `{}`)
113
+ - `bbox` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>]** BBox [west, south, east, north]
114
+ - `id` **\[([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))]** Identifier
84
115
 
85
116
  **Examples**
86
117
 
@@ -114,6 +145,8 @@ Takes one or more [Features](http://geojson.org/geojson-spec.html#feature-object
114
145
  **Parameters**
115
146
 
116
147
  - `features` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Feature](http://geojson.org/geojson-spec.html#feature-objects)>** input features
148
+ - `bbox` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>]** BBox [west, south, east, north]
149
+ - `id` **\[([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))]** Identifier
117
150
 
118
151
  **Examples**
119
152
 
@@ -139,7 +172,9 @@ coordinate array. Properties can be added optionally.
139
172
  **Parameters**
140
173
 
141
174
  - `coordinates` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>>>** an array of LineStrings
142
- - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object of key-value pairs to add as properties
175
+ - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object of key-value pairs to add as properties (optional, default `{}`)
176
+ - `bbox` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>]** BBox [west, south, east, north]
177
+ - `id` **\[([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))]** Identifier
143
178
 
144
179
  **Examples**
145
180
 
@@ -161,7 +196,9 @@ coordinate array. Properties can be added optionally.
161
196
  **Parameters**
162
197
 
163
198
  - `coordinates` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>>** an array of Positions
164
- - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object of key-value pairs to add as properties
199
+ - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object of key-value pairs to add as properties (optional, default `{}`)
200
+ - `bbox` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>]** BBox [west, south, east, north]
201
+ - `id` **\[([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))]** Identifier
165
202
 
166
203
  **Examples**
167
204
 
@@ -183,7 +220,9 @@ coordinate array. Properties can be added optionally.
183
220
  **Parameters**
184
221
 
185
222
  - `coordinates` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>>>>** an array of Polygons
186
- - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object of key-value pairs to add as properties
223
+ - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object of key-value pairs to add as properties (optional, default `{}`)
224
+ - `bbox` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>]** BBox [west, south, east, north]
225
+ - `id` **\[([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))]** Identifier
187
226
 
188
227
  **Examples**
189
228
 
@@ -204,8 +243,10 @@ coordinate array. Properties can be added optionally.
204
243
 
205
244
  **Parameters**
206
245
 
207
- - `geometries` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<{Geometry}>** an array of GeoJSON Geometries
208
- - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object of key-value pairs to add as properties
246
+ - `geometries` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Geometry](http://geojson.org/geojson-spec.html#geometry)>** an array of GeoJSON Geometries
247
+ - `properties` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** an Object of key-value pairs to add as properties (optional, default `{}`)
248
+ - `bbox` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>]** BBox [west, south, east, north]
249
+ - `id` **\[([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))]** Identifier
209
250
 
210
251
  **Examples**
211
252
 
@@ -237,10 +278,10 @@ Round number to precision
237
278
  **Examples**
238
279
 
239
280
  ```javascript
240
- round(120.4321)
281
+ turf.round(120.4321)
241
282
  //=120
242
283
 
243
- round(120.4321, 2)
284
+ turf.round(120.4321, 2)
244
285
  //=120.43
245
286
  ```
246
287
 
@@ -339,6 +380,25 @@ Valid units: kilometers, kilometres, meters, metres, centimetres, millimeter, ac
339
380
 
340
381
  Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the converted distance
341
382
 
383
+ # isNumber
384
+
385
+ isNumber
386
+
387
+ **Parameters**
388
+
389
+ - `num` **Any** Number to validate
390
+
391
+ **Examples**
392
+
393
+ ```javascript
394
+ turf.isNumber(123)
395
+ //=true
396
+ turf.isNumber('foo')
397
+ //=false
398
+ ```
399
+
400
+ Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true/false
401
+
342
402
  <!-- This file is automatically generated. Please don't edit it directly:
343
403
  if you find an error, edit the source file (likely index.js), and re-run
344
404
  ./scripts/generate-readmes in the turf project. -->
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- /// <reference types="geojson" />
1
+ /// <reference types='geojson' />
2
2
 
3
+ export type Id = string|number
3
4
  export type Points = GeoJSON.FeatureCollection<GeoJSON.Point>;
4
5
  export type Point = GeoJSON.Feature<GeoJSON.Point>;
5
6
  export type MultiPoints = GeoJSON.FeatureCollection<GeoJSON.MultiPoint>;
@@ -17,64 +18,75 @@ export type LineStringFeatures = LineString | LineStrings | MultiLineString | Mu
17
18
  export type PolygonFeatures = Polygon | Polygons | MultiPolygon | MultiPolygons | GeoJSON.Polygon | GeoJSON.MultiPolygon
18
19
  export type Features<Geom extends GeometryObject> = GeoJSON.FeatureCollection<Geom>;
19
20
  export type Feature<Geom extends GeometryObject> = GeoJSON.Feature<Geom>;
20
- export type Units = "miles" | "nauticalmiles" | "degrees" | "radians" | "inches" | "yards" | "meters" | "metres" | "kilometers" | "kilometres";
21
+ export type Units = 'miles' | 'nauticalmiles' | 'degrees' | 'radians' | 'inches' | 'yards' | 'meters' | 'metres' | 'kilometers' | 'kilometres';
21
22
  export type BBox = [number, number, number, number];
22
23
  export type GeometryObject = GeoJSON.GeometryObject;
23
24
  export type GeometryCollection = GeoJSON.GeometryCollection;
24
25
  export type Geoms = GeoJSON.Point | GeoJSON.LineString | GeoJSON.Polygon | GeoJSON.MultiPoint | GeoJSON.MultiLineString | GeoJSON.MultiPolygon;
26
+ export type GeometryTypes = 'Point' | 'LineString' | 'Polygon' | 'MultiPoint' | 'MultiLineString' | 'MultiPolygon';
25
27
 
26
28
  export interface FeatureGeometryCollection extends GeoJSON.Feature<any> {
27
29
  geometry: GeometryCollection
28
30
  }
29
31
 
32
+ export interface FeatureCollection {
33
+ <Geom extends Geoms>(features: Feature<Geom>[], bbox?: BBox, id?: Id): Features<Geom>;
34
+ (features: Feature<any>[], bbox?: BBox, id?: Id): Features<any>;
35
+ }
36
+
37
+ export interface Properties {
38
+ [key: string]: any
39
+ }
40
+
30
41
  /**
31
42
  * http://turfjs.org/docs/#feature
32
43
  */
33
- export function feature<Geom extends GeometryObject>(geometry: Geom, properties?: any): Feature<Geom>;
44
+ export function feature<Geom extends GeometryObject>(geometry: Geom, properties?: Properties, bbox?: BBox, id?: Id): Feature<Geom>;
45
+
46
+ /**
47
+ * http://turfjs.org/docs/#geometry
48
+ */
49
+ export function geometry(type: GeometryTypes, coordinates: any[], bbox?: BBox): GeometryObject;
34
50
 
35
51
  /**
36
52
  * http://turfjs.org/docs/#point
37
53
  */
38
- export function point(coordinates: Position, properties?: any): Point;
54
+ export function point(coordinates: Position, properties?: Properties, bbox?: BBox, id?: Id): Point;
39
55
 
40
56
  /**
41
57
  * http://turfjs.org/docs/#polygon
42
58
  */
43
- export function polygon(coordinates: Position[][], properties?: any): Polygon;
59
+ export function polygon(coordinates: Position[][], properties?: Properties, bbox?: BBox, id?: Id): Polygon;
44
60
 
45
61
  /**
46
62
  * http://turfjs.org/docs/#linestring
47
63
  */
48
- export function lineString(coordinates: Position[], properties?: any): LineString;
64
+ export function lineString(coordinates: Position[], properties?: Properties, bbox?: BBox, id?: Id): LineString;
49
65
 
50
66
  /**
51
67
  * http://turfjs.org/docs/#featurecollection
52
68
  */
53
- interface featureCollection {
54
- <Geom extends Geoms>(features: Feature<Geom>[]): Features<Geom>;
55
- (features: Feature<any>[]): Features<any>;
56
- }
57
- export const featureCollection: featureCollection;
69
+ export const featureCollection: FeatureCollection;
58
70
 
59
71
  /**
60
72
  * http://turfjs.org/docs/#multilinestring
61
73
  */
62
- export function multiLineString(coordinates: Position[][], properties?: any): MultiLineString;
74
+ export function multiLineString(coordinates: Position[][], properties?: Properties, bbox?: BBox, id?: Id): MultiLineString;
63
75
 
64
76
  /**
65
77
  * http://turfjs.org/docs/#multipoint
66
78
  */
67
- export function multiPoint(coordinates: Position[], properties?: any): MultiPoint;
79
+ export function multiPoint(coordinates: Position[], properties?: Properties, bbox?: BBox, id?: Id): MultiPoint;
68
80
 
69
81
  /**
70
82
  * http://turfjs.org/docs/#multipolygon
71
83
  */
72
- export function multiPolygon(coordinates: Position[][][], properties?: any): MultiPolygon;
84
+ export function multiPolygon(coordinates: Position[][][], properties?: Properties, bbox?: BBox, id?: Id): MultiPolygon;
73
85
 
74
86
  /**
75
87
  * http://turfjs.org/docs/#geometrycollection
76
88
  */
77
- export function geometryCollection(geometries: GeometryObject[], properties?: any): FeatureGeometryCollection;
89
+ export function geometryCollection(geometries: GeometryObject[], properties?: Properties, bbox?: BBox, id?: Id): FeatureGeometryCollection;
78
90
 
79
91
  /**
80
92
  * http://turfjs.org/docs/#radianstodistance
@@ -120,3 +132,8 @@ export function convertDistance(distance: number, originalUnit: Units, finalUnit
120
132
  * http://turfjs.org/docs/#convertarea
121
133
  */
122
134
  export function convertArea(area: number, originalUnit?: Units, finalUnit?: Units): number
135
+
136
+ /**
137
+ * http://turfjs.org/docs/#isnumber
138
+ */
139
+ export function isNumber(num: any): boolean
package/index.js CHANGED
@@ -3,7 +3,9 @@
3
3
  *
4
4
  * @name feature
5
5
  * @param {Geometry} geometry input geometry
6
- * @param {Object} properties properties
6
+ * @param {Object} [properties={}] an Object of key-value pairs to add as properties
7
+ * @param {Array<number>} [bbox] BBox [west, south, east, north]
8
+ * @param {string|number} [id] Identifier
7
9
  * @returns {Feature} a GeoJSON Feature
8
10
  * @example
9
11
  * var geometry = {
@@ -15,14 +17,56 @@
15
17
  *
16
18
  * //=feature
17
19
  */
18
- function feature(geometry, properties) {
19
- if (!geometry) throw new Error('No geometry passed');
20
-
21
- return {
22
- type: 'Feature',
23
- properties: properties || {},
24
- geometry: geometry
25
- };
20
+ function feature(geometry, properties, bbox, id) {
21
+ if (geometry === undefined) throw new Error('geometry is required');
22
+ if (properties && properties.constructor !== Object) throw new Error('properties must be an Object');
23
+ if (bbox && bbox.length !== 4) throw new Error('bbox must be an Array of 4 numbers');
24
+ if (id && ['string', 'number'].indexOf(typeof id) === -1) throw new Error('id must be a number or a string');
25
+
26
+ var feat = {type: 'Feature'};
27
+ if (id) feat.id = id;
28
+ if (bbox) feat.bbox = bbox;
29
+ feat.properties = properties || {};
30
+ feat.geometry = geometry;
31
+ return feat;
32
+ }
33
+
34
+ /**
35
+ * Creates a GeoJSON {@link Geometry} from a Geometry string type & coordinates.
36
+ * For GeometryCollection type use `helpers.geometryCollection`
37
+ *
38
+ * @name geometry
39
+ * @param {string} type Geometry Type
40
+ * @param {Array<number>} coordinates Coordinates
41
+ * @param {Array<number>} [bbox] BBox [west, south, east, north]
42
+ * @returns {Geometry} a GeoJSON Geometry
43
+ * @example
44
+ * var type = 'Point';
45
+ * var coordinates = [110, 50];
46
+ *
47
+ * var geometry = turf.geometry(type, coordinates);
48
+ *
49
+ * //=geometry
50
+ */
51
+ function geometry(type, coordinates, bbox) {
52
+ // Validation
53
+ if (!type) throw new Error('type is required');
54
+ if (!coordinates) throw new Error('coordinates is required');
55
+ if (!Array.isArray(coordinates)) throw new Error('coordinates must be an Array');
56
+ if (bbox && bbox.length !== 4) throw new Error('bbox must be an Array of 4 numbers');
57
+
58
+ var geom;
59
+ switch (type) {
60
+ case 'Point': geom = point(coordinates).geometry; break;
61
+ case 'LineString': geom = lineString(coordinates).geometry; break;
62
+ case 'Polygon': geom = polygon(coordinates).geometry; break;
63
+ case 'MultiPoint': geom = multiPoint(coordinates).geometry; break;
64
+ case 'MultiLineString': geom = multiLineString(coordinates).geometry; break;
65
+ case 'MultiPolygon': geom = multiPolygon(coordinates).geometry; break;
66
+ default: throw new Error(type + ' is invalid');
67
+ }
68
+ if (bbox) geom.bbox = bbox;
69
+ return geom;
26
70
  }
27
71
 
28
72
  /**
@@ -30,24 +74,25 @@ function feature(geometry, properties) {
30
74
  *
31
75
  * @name point
32
76
  * @param {Array<number>} coordinates longitude, latitude position (each in decimal degrees)
33
- * @param {Object=} properties an Object that is used as the {@link Feature}'s
34
- * properties
77
+ * @param {Object} [properties={}] an Object of key-value pairs to add as properties
78
+ * @param {Array<number>} [bbox] BBox [west, south, east, north]
79
+ * @param {string|number} [id] Identifier
35
80
  * @returns {Feature<Point>} a Point feature
36
81
  * @example
37
82
  * var point = turf.point([-75.343, 39.984]);
38
83
  *
39
84
  * //=point
40
85
  */
41
- function point(coordinates, properties) {
86
+ function point(coordinates, properties, bbox, id) {
42
87
  if (!coordinates) throw new Error('No coordinates passed');
43
88
  if (coordinates.length === undefined) throw new Error('Coordinates must be an array');
44
89
  if (coordinates.length < 2) throw new Error('Coordinates must be at least 2 numbers long');
45
- if (typeof coordinates[0] !== 'number' || typeof coordinates[1] !== 'number') throw new Error('Coordinates must contain numbers');
90
+ if (!isNumber(coordinates[0]) || !isNumber(coordinates[1])) throw new Error('Coordinates must contain numbers');
46
91
 
47
92
  return feature({
48
93
  type: 'Point',
49
94
  coordinates: coordinates
50
- }, properties);
95
+ }, properties, bbox, id);
51
96
  }
52
97
 
53
98
  /**
@@ -55,7 +100,9 @@ function point(coordinates, properties) {
55
100
  *
56
101
  * @name polygon
57
102
  * @param {Array<Array<Array<number>>>} coordinates an array of LinearRings
58
- * @param {Object=} properties a properties object
103
+ * @param {Object} [properties={}] an Object of key-value pairs to add as properties
104
+ * @param {Array<number>} [bbox] BBox [west, south, east, north]
105
+ * @param {string|number} [id] Identifier
59
106
  * @returns {Feature<Polygon>} a Polygon feature
60
107
  * @throws {Error} throw an error if a LinearRing of the polygon has too few positions
61
108
  * or if a LinearRing of the Polygon does not have matching Positions at the beginning & end.
@@ -70,7 +117,7 @@ function point(coordinates, properties) {
70
117
  *
71
118
  * //=polygon
72
119
  */
73
- function polygon(coordinates, properties) {
120
+ function polygon(coordinates, properties, bbox, id) {
74
121
  if (!coordinates) throw new Error('No coordinates passed');
75
122
 
76
123
  for (var i = 0; i < coordinates.length; i++) {
@@ -79,6 +126,8 @@ function polygon(coordinates, properties) {
79
126
  throw new Error('Each LinearRing of a Polygon must have 4 or more Positions.');
80
127
  }
81
128
  for (var j = 0; j < ring[ring.length - 1].length; j++) {
129
+ // Check if first point of Polygon contains two numbers
130
+ if (i === 0 && j === 0 && !isNumber(ring[0][0]) || !isNumber(ring[0][1])) throw new Error('Coordinates must contain numbers');
82
131
  if (ring[ring.length - 1][j] !== ring[0][j]) {
83
132
  throw new Error('First and last Position are not equivalent.');
84
133
  }
@@ -88,7 +137,7 @@ function polygon(coordinates, properties) {
88
137
  return feature({
89
138
  type: 'Polygon',
90
139
  coordinates: coordinates
91
- }, properties);
140
+ }, properties, bbox, id);
92
141
  }
93
142
 
94
143
  /**
@@ -97,7 +146,9 @@ function polygon(coordinates, properties) {
97
146
  *
98
147
  * @name lineString
99
148
  * @param {Array<Array<number>>} coordinates an array of Positions
100
- * @param {Object=} properties an Object of key-value pairs to add as properties
149
+ * @param {Object} [properties={}] an Object of key-value pairs to add as properties
150
+ * @param {Array<number>} [bbox] BBox [west, south, east, north]
151
+ * @param {string|number} [id] Identifier
101
152
  * @returns {Feature<LineString>} a LineString feature
102
153
  * @throws {Error} if no coordinates are passed
103
154
  * @example
@@ -118,14 +169,16 @@ function polygon(coordinates, properties) {
118
169
  *
119
170
  * //=linestring2
120
171
  */
121
- function lineString(coordinates, properties) {
172
+ function lineString(coordinates, properties, bbox, id) {
122
173
  if (!coordinates) throw new Error('No coordinates passed');
123
174
  if (coordinates.length < 2) throw new Error('Coordinates must be an array of two or more positions');
175
+ // Check if first point of LineString contains two numbers
176
+ if (!isNumber(coordinates[0][1]) || !isNumber(coordinates[0][1])) throw new Error('Coordinates must contain numbers');
124
177
 
125
178
  return feature({
126
179
  type: 'LineString',
127
180
  coordinates: coordinates
128
- }, properties);
181
+ }, properties, bbox, id);
129
182
  }
130
183
 
131
184
  /**
@@ -133,6 +186,8 @@ function lineString(coordinates, properties) {
133
186
  *
134
187
  * @name featureCollection
135
188
  * @param {Feature[]} features input features
189
+ * @param {Array<number>} [bbox] BBox [west, south, east, north]
190
+ * @param {string|number} [id] Identifier
136
191
  * @returns {FeatureCollection} a FeatureCollection of input features
137
192
  * @example
138
193
  * var features = [
@@ -145,14 +200,17 @@ function lineString(coordinates, properties) {
145
200
  *
146
201
  * //=collection
147
202
  */
148
- function featureCollection(features) {
203
+ function featureCollection(features, bbox, id) {
149
204
  if (!features) throw new Error('No features passed');
150
205
  if (!Array.isArray(features)) throw new Error('features must be an Array');
151
-
152
- return {
153
- type: 'FeatureCollection',
154
- features: features
155
- };
206
+ if (bbox && bbox.length !== 4) throw new Error('bbox must be an Array of 4 numbers');
207
+ if (id && ['string', 'number'].indexOf(typeof id) === -1) throw new Error('id must be a number or a string');
208
+
209
+ var fc = {type: 'FeatureCollection'};
210
+ if (id) fc.id = id;
211
+ if (bbox) fc.bbox = bbox;
212
+ fc.features = features;
213
+ return fc;
156
214
  }
157
215
 
158
216
  /**
@@ -161,7 +219,9 @@ function featureCollection(features) {
161
219
  *
162
220
  * @name multiLineString
163
221
  * @param {Array<Array<Array<number>>>} coordinates an array of LineStrings
164
- * @param {Object=} properties an Object of key-value pairs to add as properties
222
+ * @param {Object} [properties={}] an Object of key-value pairs to add as properties
223
+ * @param {Array<number>} [bbox] BBox [west, south, east, north]
224
+ * @param {string|number} [id] Identifier
165
225
  * @returns {Feature<MultiLineString>} a MultiLineString feature
166
226
  * @throws {Error} if no coordinates are passed
167
227
  * @example
@@ -169,13 +229,13 @@ function featureCollection(features) {
169
229
  *
170
230
  * //=multiLine
171
231
  */
172
- function multiLineString(coordinates, properties) {
232
+ function multiLineString(coordinates, properties, bbox, id) {
173
233
  if (!coordinates) throw new Error('No coordinates passed');
174
234
 
175
235
  return feature({
176
236
  type: 'MultiLineString',
177
237
  coordinates: coordinates
178
- }, properties);
238
+ }, properties, bbox, id);
179
239
  }
180
240
 
181
241
  /**
@@ -184,7 +244,9 @@ function multiLineString(coordinates, properties) {
184
244
  *
185
245
  * @name multiPoint
186
246
  * @param {Array<Array<number>>} coordinates an array of Positions
187
- * @param {Object=} properties an Object of key-value pairs to add as properties
247
+ * @param {Object} [properties={}] an Object of key-value pairs to add as properties
248
+ * @param {Array<number>} [bbox] BBox [west, south, east, north]
249
+ * @param {string|number} [id] Identifier
188
250
  * @returns {Feature<MultiPoint>} a MultiPoint feature
189
251
  * @throws {Error} if no coordinates are passed
190
252
  * @example
@@ -192,13 +254,13 @@ function multiLineString(coordinates, properties) {
192
254
  *
193
255
  * //=multiPt
194
256
  */
195
- function multiPoint(coordinates, properties) {
257
+ function multiPoint(coordinates, properties, bbox, id) {
196
258
  if (!coordinates) throw new Error('No coordinates passed');
197
259
 
198
260
  return feature({
199
261
  type: 'MultiPoint',
200
262
  coordinates: coordinates
201
- }, properties);
263
+ }, properties, bbox, id);
202
264
  }
203
265
 
204
266
  /**
@@ -207,7 +269,9 @@ function multiPoint(coordinates, properties) {
207
269
  *
208
270
  * @name multiPolygon
209
271
  * @param {Array<Array<Array<Array<number>>>>} coordinates an array of Polygons
210
- * @param {Object=} properties an Object of key-value pairs to add as properties
272
+ * @param {Object} [properties={}] an Object of key-value pairs to add as properties
273
+ * @param {Array<number>} [bbox] BBox [west, south, east, north]
274
+ * @param {string|number} [id] Identifier
211
275
  * @returns {Feature<MultiPolygon>} a multipolygon feature
212
276
  * @throws {Error} if no coordinates are passed
213
277
  * @example
@@ -216,13 +280,13 @@ function multiPoint(coordinates, properties) {
216
280
  * //=multiPoly
217
281
  *
218
282
  */
219
- function multiPolygon(coordinates, properties) {
283
+ function multiPolygon(coordinates, properties, bbox, id) {
220
284
  if (!coordinates) throw new Error('No coordinates passed');
221
285
 
222
286
  return feature({
223
287
  type: 'MultiPolygon',
224
288
  coordinates: coordinates
225
- }, properties);
289
+ }, properties, bbox, id);
226
290
  }
227
291
 
228
292
  /**
@@ -230,8 +294,10 @@ function multiPolygon(coordinates, properties) {
230
294
  * coordinate array. Properties can be added optionally.
231
295
  *
232
296
  * @name geometryCollection
233
- * @param {Array<{Geometry}>} geometries an array of GeoJSON Geometries
234
- * @param {Object=} properties an Object of key-value pairs to add as properties
297
+ * @param {Array<Geometry>} geometries an array of GeoJSON Geometries
298
+ * @param {Object} [properties={}] an Object of key-value pairs to add as properties
299
+ * @param {Array<number>} [bbox] BBox [west, south, east, north]
300
+ * @param {string|number} [id] Identifier
235
301
  * @returns {Feature<GeometryCollection>} a GeoJSON GeometryCollection Feature
236
302
  * @example
237
303
  * var pt = {
@@ -246,13 +312,14 @@ function multiPolygon(coordinates, properties) {
246
312
  *
247
313
  * //=collection
248
314
  */
249
- function geometryCollection(geometries, properties) {
315
+ function geometryCollection(geometries, properties, bbox, id) {
250
316
  if (!geometries) throw new Error('geometries is required');
317
+ if (!Array.isArray(geometries)) throw new Error('geometries must be an Array');
251
318
 
252
319
  return feature({
253
320
  type: 'GeometryCollection',
254
321
  geometries: geometries
255
- }, properties);
322
+ }, properties, bbox, id);
256
323
  }
257
324
 
258
325
  // https://en.wikipedia.org/wiki/Great-circle_distance#Radius_for_spherical_Earth
@@ -292,10 +359,10 @@ var areaFactors = {
292
359
  * @param {number} [precision=0] Precision
293
360
  * @returns {number} rounded number
294
361
  * @example
295
- * round(120.4321)
362
+ * turf.round(120.4321)
296
363
  * //=120
297
364
  *
298
- * round(120.4321, 2)
365
+ * turf.round(120.4321, 2)
299
366
  * //=120.43
300
367
  */
301
368
  function round(num, precision) {
@@ -435,8 +502,24 @@ function convertArea(area, originalUnit, finalUnit) {
435
502
  return (area / startFactor) * finalFactor;
436
503
  }
437
504
 
505
+ /**
506
+ * isNumber
507
+ *
508
+ * @param {*} num Number to validate
509
+ * @returns {boolean} true/false
510
+ * @example
511
+ * turf.isNumber(123)
512
+ * //=true
513
+ * turf.isNumber('foo')
514
+ * //=false
515
+ */
516
+ function isNumber(num) {
517
+ return !isNaN(num) && num !== null && !Array.isArray(num);
518
+ }
519
+
438
520
  module.exports = {
439
521
  feature: feature,
522
+ geometry: geometry,
440
523
  featureCollection: featureCollection,
441
524
  geometryCollection: geometryCollection,
442
525
  point: point,
@@ -453,5 +536,6 @@ module.exports = {
453
536
  bearingToAngle: bearingToAngle,
454
537
  convertDistance: convertDistance,
455
538
  convertArea: convertArea,
456
- round: round
539
+ round: round,
540
+ isNumber: isNumber
457
541
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/helpers",
3
- "version": "4.5.2",
3
+ "version": "4.7.3",
4
4
  "description": "turf helpers module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "homepage": "https://github.com/Turfjs/turf",
37
37
  "devDependencies": {
38
- "benchmark": "^2.1.4",
39
- "tape": "^4.6.3"
38
+ "benchmark": "2.1.4",
39
+ "tape": "4.8.0"
40
40
  }
41
41
  }