Skip to content

Commit 8f5c407

Browse files
authored
Added bbox parameter to @turf/interpolate (#2768)
Expands the interpolation field beyond the default bbox of the input data points.
1 parent bf588dd commit 8f5c407

File tree

9 files changed

+148156
-5
lines changed

9 files changed

+148156
-5
lines changed

packages/turf-interpolate/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Takes a set of points and estimates their 'property' values on a grid using the
1616
* `options.property` **[string][6]** the property name in `points` from which z-values will be pulled, zValue fallbacks to 3rd coordinate if no property exists. (optional, default `'elevation'`)
1717
* `options.units` **[string][6]** used in calculating cellSize, can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
1818
* `options.weight` **[number][4]** exponent regulating the distance-decay weighting (optional, default `1`)
19+
* `options.bbox` **[BBox][7]** Bounding Box Array \[west, south, east, north] associated with the FeatureCollection. (optional, default `bbox(points)`)
1920

2021
### Examples
2122

@@ -33,7 +34,7 @@ var grid = turf.interpolate(points, 100, options);
3334
var addToMap = [grid];
3435
```
3536

36-
Returns **[FeatureCollection][2]<([Point][3] | [Polygon][7])>** grid of points or polygons with interpolated 'property'
37+
Returns **[FeatureCollection][2]<([Point][3] | [Polygon][8])>** grid of points or polygons with interpolated 'property'
3738

3839
[1]: https://en.wikipedia.org/wiki/Inverse_distance_weighting
3940

@@ -47,7 +48,9 @@ Returns **[FeatureCollection][2]<([Point][3] | [Polygon][7])>** grid of points o
4748

4849
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
4950

50-
[7]: https://tools.ietf.org/html/rfc7946#section-3.1.6
51+
[7]: https://tools.ietf.org/html/rfc7946#section-5
52+
53+
[8]: https://tools.ietf.org/html/rfc7946#section-3.1.6
5154

5255
<!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->
5356

packages/turf-interpolate/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Point, Polygon, FeatureCollection } from "geojson";
1+
import { Point, Polygon, FeatureCollection, BBox } from "geojson";
22
import { Units, Grid } from "@turf/helpers";
33

44
/**
@@ -12,6 +12,7 @@ declare function interpolate(
1212
property?: string;
1313
units?: Units;
1414
weight?: number;
15+
bbox?: BBox;
1516
}
1617
): FeatureCollection<Point>;
1718
declare function interpolate(
@@ -22,6 +23,7 @@ declare function interpolate(
2223
property?: string;
2324
units?: Units;
2425
weight?: number;
26+
bbox?: BBox;
2527
}
2628
): FeatureCollection<Polygon>;
2729

packages/turf-interpolate/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { centroid } from "@turf/centroid";
66
import { squareGrid } from "@turf/square-grid";
77
import { triangleGrid } from "@turf/triangle-grid";
88
import { clone } from "@turf/clone";
9-
import { featureCollection } from "@turf/helpers";
9+
import { featureCollection, validateBBox } from "@turf/helpers";
1010
import { featureEach } from "@turf/meta";
1111
import { collectionOf } from "@turf/invariant";
1212

@@ -21,6 +21,7 @@ import { collectionOf } from "@turf/invariant";
2121
* @param {string} [options.property='elevation'] the property name in `points` from which z-values will be pulled, zValue fallbacks to 3rd coordinate if no property exists.
2222
* @param {string} [options.units='kilometers'] used in calculating cellSize, can be degrees, radians, miles, or kilometers
2323
* @param {number} [options.weight=1] exponent regulating the distance-decay weighting
24+
* @param {BBox} [options.bbox=bbox(points)] Bounding Box Array [west, south, east, north] associated with the FeatureCollection.
2425
* @returns {FeatureCollection<Point|Polygon>} grid of points or polygons with interpolated 'property'
2526
* @example
2627
* var points = turf.randomPoint(30, {bbox: [50, 30, 70, 50]});
@@ -42,6 +43,7 @@ function interpolate(points, cellSize, options) {
4243
var gridType = options.gridType;
4344
var property = options.property;
4445
var weight = options.weight;
46+
var box = options.bbox;
4547

4648
// validation
4749
if (!points) throw new Error("points is required");
@@ -55,7 +57,8 @@ function interpolate(points, cellSize, options) {
5557
gridType = gridType || "square";
5658
weight = weight || 1;
5759

58-
var box = bbox(points);
60+
box = box ?? bbox(points);
61+
validateBBox(box);
5962
var grid;
6063
switch (gridType) {
6164
case "point":
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"type": "FeatureCollection",
3+
"properties": {
4+
"property": "value",
5+
"gridType": "square",
6+
"weight": 0.5,
7+
"cellSize": 1,
8+
"bbox": [9.416249084472656, 45.3391764115696, 9.031605529785156, 45.63689620055365]
9+
},
10+
"features": [
11+
{
12+
"type": "Feature",
13+
"properties": {
14+
"value": 4
15+
},
16+
"geometry": {
17+
"type": "Point",
18+
"coordinates": [9.155731201171875, 45.47216977418841]
19+
}
20+
},
21+
{
22+
"type": "Feature",
23+
"properties": {
24+
"value": 99
25+
},
26+
"geometry": {
27+
"type": "Point",
28+
"coordinates": [9.195213317871094, 45.53689620055365]
29+
}
30+
},
31+
{
32+
"type": "Feature",
33+
"properties": {
34+
"value": 10
35+
},
36+
"geometry": {
37+
"type": "Point",
38+
"coordinates": [9.175300598144531, 45.49912810913339]
39+
}
40+
},
41+
{
42+
"type": "Feature",
43+
"properties": {
44+
"value": 6
45+
},
46+
"geometry": {
47+
"type": "Point",
48+
"coordinates": [9.231605529785156, 45.49190839157102]
49+
}
50+
},
51+
{
52+
"type": "Feature",
53+
"properties": {
54+
"value": 7
55+
},
56+
"geometry": {
57+
"type": "Point",
58+
"coordinates": [9.116249084472656, 45.4391764115696]
59+
}
60+
}
61+
]
62+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"type": "FeatureCollection",
3+
"properties": {
4+
"property": "value",
5+
"weight": 0.5,
6+
"gridType": "square",
7+
"cellSize": 0.5,
8+
"units": "kilometers",
9+
"bbox": [9.416249084472656, 45.3391764115696, 9.031605529785156, 45.63689620055365]
10+
},
11+
"features": [
12+
{
13+
"type": "Feature",
14+
"properties": {
15+
"value": 4
16+
},
17+
"geometry": {
18+
"type": "Point",
19+
"coordinates": [9.155731201171875, 45.47216977418841]
20+
}
21+
},
22+
{
23+
"type": "Feature",
24+
"properties": {
25+
"value": 99
26+
},
27+
"geometry": {
28+
"type": "Point",
29+
"coordinates": [9.195213317871094, 45.53689620055365]
30+
}
31+
},
32+
{
33+
"type": "Feature",
34+
"properties": {
35+
"value": 10
36+
},
37+
"geometry": {
38+
"type": "Point",
39+
"coordinates": [9.175300598144531, 45.49912810913339]
40+
}
41+
},
42+
{
43+
"type": "Feature",
44+
"properties": {
45+
"value": 6
46+
},
47+
"geometry": {
48+
"type": "Point",
49+
"coordinates": [9.231605529785156, 45.49190839157102]
50+
}
51+
},
52+
{
53+
"type": "Feature",
54+
"properties": {
55+
"value": 7
56+
},
57+
"geometry": {
58+
"type": "Point",
59+
"coordinates": [9.116249084472656, 45.4391764115696]
60+
}
61+
}
62+
]
63+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"type": "FeatureCollection",
3+
"properties": {
4+
"weight": 2,
5+
"gridType": "hex",
6+
"cellSize": 0.5,
7+
"units": "miles",
8+
"bbox": [-6.357258206107161, 53.39023949422162, -6.186614219650437, 53.31219701461626]
9+
},
10+
"features": [
11+
{
12+
"type": "Feature",
13+
"properties": {},
14+
"geometry": {
15+
"type": "Point",
16+
"coordinates": [-6.3288116455078125, 53.355879304922276, 2]
17+
}
18+
},
19+
{
20+
"type": "Feature",
21+
"properties": {},
22+
"geometry": {
23+
"type": "Point",
24+
"coordinates": [-6.2615203857421875, 53.38087096356977, 5]
25+
}
26+
},
27+
{
28+
"type": "Feature",
29+
"properties": {},
30+
"geometry": {
31+
"type": "Point",
32+
"coordinates": [-6.248474121093749, 53.31979992850456, 14]
33+
}
34+
},
35+
{
36+
"type": "Feature",
37+
"properties": {},
38+
"geometry": {
39+
"type": "Point",
40+
"coordinates": [-6.2697601318359375, 53.352190769802725, 9]
41+
}
42+
},
43+
{
44+
"type": "Feature",
45+
"properties": {},
46+
"geometry": {
47+
"type": "Point",
48+
"coordinates": [-6.2505340576171875, 53.3648943803576, 11]
49+
}
50+
}
51+
]
52+
}

0 commit comments

Comments
 (0)