@turf/interpolate 7.3.1 → 7.3.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.
- package/dist/cjs/index.cjs +32 -17
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +28 -20
- package/dist/esm/index.d.ts +28 -20
- package/dist/esm/index.js +32 -17
- package/dist/esm/index.js.map +1 -1
- package/package.json +17 -15
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
|
|
2
2
|
var _bbox = require('@turf/bbox');
|
|
3
3
|
var _hexgrid = require('@turf/hex-grid');
|
|
4
4
|
var _pointgrid = require('@turf/point-grid');
|
|
@@ -11,21 +11,25 @@ var _helpers = require('@turf/helpers');
|
|
|
11
11
|
var _meta = require('@turf/meta');
|
|
12
12
|
var _invariant = require('@turf/invariant');
|
|
13
13
|
function interpolate(points, cellSize, options) {
|
|
14
|
+
var _a, _b, _c, _d;
|
|
14
15
|
options = options || {};
|
|
15
|
-
if (typeof options !== "object")
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
if (typeof options !== "object") {
|
|
17
|
+
throw new Error("options is invalid");
|
|
18
|
+
}
|
|
19
|
+
if (!points) {
|
|
20
|
+
throw new Error("points is required");
|
|
21
|
+
}
|
|
21
22
|
_invariant.collectionOf.call(void 0, points, "Point", "input must contain Points");
|
|
22
|
-
if (!cellSize)
|
|
23
|
-
|
|
23
|
+
if (!cellSize) {
|
|
24
|
+
throw new Error("cellSize is required");
|
|
25
|
+
}
|
|
26
|
+
var gridType = (_a = options.gridType) != null ? _a : "square";
|
|
27
|
+
var property = (_b = options.property) != null ? _b : "elevation";
|
|
28
|
+
var weight = (_c = options.weight) != null ? _c : 1;
|
|
29
|
+
var box = (_d = options.bbox) != null ? _d : _bbox.bbox.call(void 0, points);
|
|
30
|
+
if (weight !== void 0 && typeof weight !== "number") {
|
|
24
31
|
throw new Error("weight must be a number");
|
|
25
|
-
|
|
26
|
-
gridType = gridType || "square";
|
|
27
|
-
weight = weight || 1;
|
|
28
|
-
box = box != null ? box : _bbox.bbox.call(void 0, points);
|
|
32
|
+
}
|
|
29
33
|
_helpers.validateBBox.call(void 0, box);
|
|
30
34
|
var grid;
|
|
31
35
|
switch (gridType) {
|
|
@@ -50,21 +54,32 @@ function interpolate(points, cellSize, options) {
|
|
|
50
54
|
}
|
|
51
55
|
var results = [];
|
|
52
56
|
_meta.featureEach.call(void 0, grid, function(gridFeature) {
|
|
57
|
+
var _a2;
|
|
53
58
|
var zw = 0;
|
|
54
59
|
var sw = 0;
|
|
55
60
|
_meta.featureEach.call(void 0, points, function(point) {
|
|
61
|
+
var _a3;
|
|
56
62
|
var gridPoint = gridType === "point" ? gridFeature : _centroid.centroid.call(void 0, gridFeature);
|
|
57
63
|
var d = _distance.distance.call(void 0, gridPoint, point, options);
|
|
58
64
|
var zValue;
|
|
59
|
-
if (property !== void 0)
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (
|
|
65
|
+
if (property !== void 0) {
|
|
66
|
+
zValue = (_a3 = point.properties) == null ? void 0 : _a3[property];
|
|
67
|
+
}
|
|
68
|
+
if (zValue === void 0) {
|
|
69
|
+
zValue = point.geometry.coordinates[2];
|
|
70
|
+
}
|
|
71
|
+
if (zValue === void 0) {
|
|
72
|
+
throw new Error("zValue is missing");
|
|
73
|
+
}
|
|
74
|
+
if (d === 0) {
|
|
75
|
+
zw = zValue;
|
|
76
|
+
}
|
|
63
77
|
var w = 1 / Math.pow(d, weight);
|
|
64
78
|
sw += w;
|
|
65
79
|
zw += w * zValue;
|
|
66
80
|
});
|
|
67
81
|
var newFeature = _clone.clone.call(void 0, gridFeature);
|
|
82
|
+
(_a2 = newFeature.properties) != null ? _a2 : newFeature.properties = {};
|
|
68
83
|
newFeature.properties[property] = zw / sw;
|
|
69
84
|
results.push(newFeature);
|
|
70
85
|
});
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/turf/turf/packages/turf-interpolate/dist/cjs/index.cjs","../../index.
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/turf/turf/packages/turf-interpolate/dist/cjs/index.cjs","../../index.ts"],"names":["_a"],"mappings":"AAAA;ACAA,kCAAqB;AACrB,yCAAwB;AACxB,6CAA0B;AAC1B,0CAAyB;AACzB,0CAAyB;AACzB,+CAA2B;AAC3B,mDAA6B;AAC7B,oCAAsB;AACtB,wCAA6D;AAC7D,kCAA4B;AAC5B,4CAA6B;AA6B7B,SAAS,WAAA,CACP,MAAA,EACA,QAAA,EACA,OAAA,EAOwD;AAjD1D,EAAA,IAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA;AAmDE,EAAA,QAAA,EAAU,QAAA,GAAW,CAAC,CAAA;AAGtB,EAAA,GAAA,CAAI,OAAO,QAAA,IAAY,QAAA,EAAU;AAC/B,IAAA,MAAM,IAAI,KAAA,CAAM,oBAAoB,CAAA;AAAA,EACtC;AACA,EAAA,GAAA,CAAI,CAAC,MAAA,EAAQ;AACX,IAAA,MAAM,IAAI,KAAA,CAAM,oBAAoB,CAAA;AAAA,EACtC;AACA,EAAA,qCAAA,MAAa,EAAQ,OAAA,EAAS,2BAA2B,CAAA;AACzD,EAAA,GAAA,CAAI,CAAC,QAAA,EAAU;AACb,IAAA,MAAM,IAAI,KAAA,CAAM,sBAAsB,CAAA;AAAA,EACxC;AAEA,EAAA,IAAI,SAAA,EAAA,CAAW,GAAA,EAAA,OAAA,CAAQ,QAAA,EAAA,GAAR,KAAA,EAAA,GAAA,EAAoB,QAAA;AACnC,EAAA,IAAI,SAAA,EAAA,CAAW,GAAA,EAAA,OAAA,CAAQ,QAAA,EAAA,GAAR,KAAA,EAAA,GAAA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AAC3B,IAAA,UAAA,CAAW,UAAA,CAAW,QAAQ,EAAA,EAAI,GAAA,EAAK,EAAA;AACvC,IAAA,OAAA,CAAQ,IAAA,CAAK,UAAU,CAAA;AAAA,EACzB,CAAC,CAAA;AACD,EAAA,OAAO,wCAAA,OAAyB,CAAA;AAGlC;AAGA,IAAO,cAAA,EAAQ,WAAA;ADnDf;AACE;AACA;AACF,mEAAC","file":"/home/runner/work/turf/turf/packages/turf-interpolate/dist/cjs/index.cjs","sourcesContent":[null,"import { bbox } from \"@turf/bbox\";\nimport { hexGrid } from \"@turf/hex-grid\";\nimport { pointGrid } from \"@turf/point-grid\";\nimport { distance } from \"@turf/distance\";\nimport { centroid } from \"@turf/centroid\";\nimport { squareGrid } from \"@turf/square-grid\";\nimport { triangleGrid } from \"@turf/triangle-grid\";\nimport { clone } from \"@turf/clone\";\nimport { featureCollection, Grid, Units, validateBBox } from \"@turf/helpers\";\nimport { featureEach } from \"@turf/meta\";\nimport { collectionOf } from \"@turf/invariant\";\nimport { BBox, Feature, FeatureCollection, Point, Polygon } from \"geojson\";\n\n/**\n * Takes a set of points and estimates their 'property' values on a grid using the [Inverse Distance Weighting (IDW) method](https://en.wikipedia.org/wiki/Inverse_distance_weighting).\n *\n * @function\n * @param {FeatureCollection<Point>} points with known value\n * @param {number} cellSize the distance across each grid point\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.gridType='square'] defines the output format based on a Grid Type (options: 'square' | 'point' | 'hex' | 'triangle')\n * @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.\n * @param {Units} [options.units='kilometers'] used in calculating cellSize. Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units}.\n * @param {number} [options.weight=1] exponent regulating the distance-decay weighting\n * @param {BBox} [options.bbox=bbox(points)] Bounding Box Array [west, south, east, north] associated with the FeatureCollection.\n * @returns {FeatureCollection<Point|Polygon>} grid of points or polygons with interpolated 'property'\n * @example\n * var points = turf.randomPoint(30, {bbox: [50, 30, 70, 50]});\n *\n * // add a random property to each point\n * turf.featureEach(points, function(point) {\n * point.properties.solRad = Math.random() * 50;\n * });\n * var options = {gridType: 'points', property: 'solRad', units: 'miles'};\n * var grid = turf.interpolate(points, 100, options);\n *\n * //addToMap\n * var addToMap = [grid];\n */\nfunction interpolate<T extends Grid = \"square\">(\n points: FeatureCollection<Point>,\n cellSize: number,\n options?: {\n gridType?: T;\n property?: string;\n units?: Units;\n weight?: number;\n bbox?: BBox;\n }\n): FeatureCollection<T extends \"point\" ? Point : Polygon> {\n // Optional parameters\n options = options || {};\n\n // Validation pre-options parsing\n if (typeof options !== \"object\") {\n throw new Error(\"options is invalid\");\n }\n if (!points) {\n throw new Error(\"points is required\");\n }\n collectionOf(points, \"Point\", \"input must contain Points\");\n if (!cellSize) {\n throw new Error(\"cellSize is required\");\n }\n\n var gridType = options.gridType ?? \"square\";\n var property = options.property ?? \"elevation\";\n var weight = options.weight ?? 1;\n var box = options.bbox ?? bbox(points);\n\n // validation post options-parsing\n if (weight !== undefined && typeof weight !== \"number\") {\n throw new Error(\"weight must be a number\");\n }\n\n validateBBox(box);\n\n var grid;\n switch (gridType) {\n case \"point\":\n case \"points\":\n grid = pointGrid(box, cellSize, options);\n break;\n case \"square\":\n case \"squares\":\n grid = squareGrid(box, cellSize, options);\n break;\n case \"hex\":\n case \"hexes\":\n grid = hexGrid(box, cellSize, options);\n break;\n case \"triangle\":\n case \"triangles\":\n grid = triangleGrid(box, cellSize, options);\n break;\n default:\n throw new Error(\"invalid gridType\");\n }\n var results: Feature<Point | Polygon>[] = [];\n featureEach<Point | Polygon>(grid, function (gridFeature) {\n var zw = 0;\n var sw = 0;\n // calculate the distance from each input point to the grid points\n featureEach(points, function (point) {\n var gridPoint =\n gridType === \"point\"\n ? (gridFeature as Feature<Point>)\n : centroid(gridFeature);\n var d = distance(gridPoint, point, options);\n var zValue;\n // property has priority for zValue, fallbacks to 3rd coordinate from geometry\n if (property !== undefined) {\n zValue = point.properties?.[property];\n }\n if (zValue === undefined) {\n zValue = point.geometry.coordinates[2];\n }\n if (zValue === undefined) {\n throw new Error(\"zValue is missing\");\n }\n if (d === 0) {\n zw = zValue;\n }\n var w = 1.0 / Math.pow(d, weight);\n sw += w;\n zw += w * zValue;\n });\n // write interpolated value for each grid point\n var newFeature = clone(gridFeature);\n newFeature.properties ??= {};\n newFeature.properties[property] = zw / sw;\n results.push(newFeature);\n });\n return featureCollection(results) as FeatureCollection<\n T extends \"point\" ? Point : Polygon\n >;\n}\n\nexport { interpolate };\nexport default interpolate;\n"]}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -1,30 +1,38 @@
|
|
|
1
|
+
import { Grid, Units } from '@turf/helpers';
|
|
1
2
|
import { FeatureCollection, Point, BBox, Polygon } from 'geojson';
|
|
2
|
-
import { Units, Grid } from '@turf/helpers';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Takes a set of points and estimates their 'property' values on a grid using the [Inverse Distance Weighting (IDW) method](https://en.wikipedia.org/wiki/Inverse_distance_weighting).
|
|
6
|
+
*
|
|
7
|
+
* @function
|
|
8
|
+
* @param {FeatureCollection<Point>} points with known value
|
|
9
|
+
* @param {number} cellSize the distance across each grid point
|
|
10
|
+
* @param {Object} [options={}] Optional parameters
|
|
11
|
+
* @param {string} [options.gridType='square'] defines the output format based on a Grid Type (options: 'square' | 'point' | 'hex' | 'triangle')
|
|
12
|
+
* @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.
|
|
13
|
+
* @param {Units} [options.units='kilometers'] used in calculating cellSize. Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units}.
|
|
14
|
+
* @param {number} [options.weight=1] exponent regulating the distance-decay weighting
|
|
15
|
+
* @param {BBox} [options.bbox=bbox(points)] Bounding Box Array [west, south, east, north] associated with the FeatureCollection.
|
|
16
|
+
* @returns {FeatureCollection<Point|Polygon>} grid of points or polygons with interpolated 'property'
|
|
17
|
+
* @example
|
|
18
|
+
* var points = turf.randomPoint(30, {bbox: [50, 30, 70, 50]});
|
|
19
|
+
*
|
|
20
|
+
* // add a random property to each point
|
|
21
|
+
* turf.featureEach(points, function(point) {
|
|
22
|
+
* point.properties.solRad = Math.random() * 50;
|
|
23
|
+
* });
|
|
24
|
+
* var options = {gridType: 'points', property: 'solRad', units: 'miles'};
|
|
25
|
+
* var grid = turf.interpolate(points, 100, options);
|
|
26
|
+
*
|
|
27
|
+
* //addToMap
|
|
28
|
+
* var addToMap = [grid];
|
|
6
29
|
*/
|
|
7
|
-
declare function interpolate(
|
|
8
|
-
|
|
9
|
-
cellSize: number,
|
|
10
|
-
options?: {
|
|
11
|
-
gridType?: "point";
|
|
30
|
+
declare function interpolate<T extends Grid = "square">(points: FeatureCollection<Point>, cellSize: number, options?: {
|
|
31
|
+
gridType?: T;
|
|
12
32
|
property?: string;
|
|
13
33
|
units?: Units;
|
|
14
34
|
weight?: number;
|
|
15
35
|
bbox?: BBox;
|
|
16
|
-
|
|
17
|
-
): FeatureCollection<Point>;
|
|
18
|
-
declare function interpolate(
|
|
19
|
-
points: FeatureCollection<Point>,
|
|
20
|
-
cellSize: number,
|
|
21
|
-
options?: {
|
|
22
|
-
gridType?: Grid;
|
|
23
|
-
property?: string;
|
|
24
|
-
units?: Units;
|
|
25
|
-
weight?: number;
|
|
26
|
-
bbox?: BBox;
|
|
27
|
-
}
|
|
28
|
-
): FeatureCollection<Polygon>;
|
|
36
|
+
}): FeatureCollection<T extends "point" ? Point : Polygon>;
|
|
29
37
|
|
|
30
38
|
export { interpolate as default, interpolate };
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,30 +1,38 @@
|
|
|
1
|
+
import { Grid, Units } from '@turf/helpers';
|
|
1
2
|
import { FeatureCollection, Point, BBox, Polygon } from 'geojson';
|
|
2
|
-
import { Units, Grid } from '@turf/helpers';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Takes a set of points and estimates their 'property' values on a grid using the [Inverse Distance Weighting (IDW) method](https://en.wikipedia.org/wiki/Inverse_distance_weighting).
|
|
6
|
+
*
|
|
7
|
+
* @function
|
|
8
|
+
* @param {FeatureCollection<Point>} points with known value
|
|
9
|
+
* @param {number} cellSize the distance across each grid point
|
|
10
|
+
* @param {Object} [options={}] Optional parameters
|
|
11
|
+
* @param {string} [options.gridType='square'] defines the output format based on a Grid Type (options: 'square' | 'point' | 'hex' | 'triangle')
|
|
12
|
+
* @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.
|
|
13
|
+
* @param {Units} [options.units='kilometers'] used in calculating cellSize. Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units}.
|
|
14
|
+
* @param {number} [options.weight=1] exponent regulating the distance-decay weighting
|
|
15
|
+
* @param {BBox} [options.bbox=bbox(points)] Bounding Box Array [west, south, east, north] associated with the FeatureCollection.
|
|
16
|
+
* @returns {FeatureCollection<Point|Polygon>} grid of points or polygons with interpolated 'property'
|
|
17
|
+
* @example
|
|
18
|
+
* var points = turf.randomPoint(30, {bbox: [50, 30, 70, 50]});
|
|
19
|
+
*
|
|
20
|
+
* // add a random property to each point
|
|
21
|
+
* turf.featureEach(points, function(point) {
|
|
22
|
+
* point.properties.solRad = Math.random() * 50;
|
|
23
|
+
* });
|
|
24
|
+
* var options = {gridType: 'points', property: 'solRad', units: 'miles'};
|
|
25
|
+
* var grid = turf.interpolate(points, 100, options);
|
|
26
|
+
*
|
|
27
|
+
* //addToMap
|
|
28
|
+
* var addToMap = [grid];
|
|
6
29
|
*/
|
|
7
|
-
declare function interpolate(
|
|
8
|
-
|
|
9
|
-
cellSize: number,
|
|
10
|
-
options?: {
|
|
11
|
-
gridType?: "point";
|
|
30
|
+
declare function interpolate<T extends Grid = "square">(points: FeatureCollection<Point>, cellSize: number, options?: {
|
|
31
|
+
gridType?: T;
|
|
12
32
|
property?: string;
|
|
13
33
|
units?: Units;
|
|
14
34
|
weight?: number;
|
|
15
35
|
bbox?: BBox;
|
|
16
|
-
|
|
17
|
-
): FeatureCollection<Point>;
|
|
18
|
-
declare function interpolate(
|
|
19
|
-
points: FeatureCollection<Point>,
|
|
20
|
-
cellSize: number,
|
|
21
|
-
options?: {
|
|
22
|
-
gridType?: Grid;
|
|
23
|
-
property?: string;
|
|
24
|
-
units?: Units;
|
|
25
|
-
weight?: number;
|
|
26
|
-
bbox?: BBox;
|
|
27
|
-
}
|
|
28
|
-
): FeatureCollection<Polygon>;
|
|
36
|
+
}): FeatureCollection<T extends "point" ? Point : Polygon>;
|
|
29
37
|
|
|
30
38
|
export { interpolate as default, interpolate };
|
package/dist/esm/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// index.
|
|
1
|
+
// index.ts
|
|
2
2
|
import { bbox } from "@turf/bbox";
|
|
3
3
|
import { hexGrid } from "@turf/hex-grid";
|
|
4
4
|
import { pointGrid } from "@turf/point-grid";
|
|
@@ -11,21 +11,25 @@ import { featureCollection, validateBBox } from "@turf/helpers";
|
|
|
11
11
|
import { featureEach } from "@turf/meta";
|
|
12
12
|
import { collectionOf } from "@turf/invariant";
|
|
13
13
|
function interpolate(points, cellSize, options) {
|
|
14
|
+
var _a, _b, _c, _d;
|
|
14
15
|
options = options || {};
|
|
15
|
-
if (typeof options !== "object")
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
if (typeof options !== "object") {
|
|
17
|
+
throw new Error("options is invalid");
|
|
18
|
+
}
|
|
19
|
+
if (!points) {
|
|
20
|
+
throw new Error("points is required");
|
|
21
|
+
}
|
|
21
22
|
collectionOf(points, "Point", "input must contain Points");
|
|
22
|
-
if (!cellSize)
|
|
23
|
-
|
|
23
|
+
if (!cellSize) {
|
|
24
|
+
throw new Error("cellSize is required");
|
|
25
|
+
}
|
|
26
|
+
var gridType = (_a = options.gridType) != null ? _a : "square";
|
|
27
|
+
var property = (_b = options.property) != null ? _b : "elevation";
|
|
28
|
+
var weight = (_c = options.weight) != null ? _c : 1;
|
|
29
|
+
var box = (_d = options.bbox) != null ? _d : bbox(points);
|
|
30
|
+
if (weight !== void 0 && typeof weight !== "number") {
|
|
24
31
|
throw new Error("weight must be a number");
|
|
25
|
-
|
|
26
|
-
gridType = gridType || "square";
|
|
27
|
-
weight = weight || 1;
|
|
28
|
-
box = box != null ? box : bbox(points);
|
|
32
|
+
}
|
|
29
33
|
validateBBox(box);
|
|
30
34
|
var grid;
|
|
31
35
|
switch (gridType) {
|
|
@@ -50,21 +54,32 @@ function interpolate(points, cellSize, options) {
|
|
|
50
54
|
}
|
|
51
55
|
var results = [];
|
|
52
56
|
featureEach(grid, function(gridFeature) {
|
|
57
|
+
var _a2;
|
|
53
58
|
var zw = 0;
|
|
54
59
|
var sw = 0;
|
|
55
60
|
featureEach(points, function(point) {
|
|
61
|
+
var _a3;
|
|
56
62
|
var gridPoint = gridType === "point" ? gridFeature : centroid(gridFeature);
|
|
57
63
|
var d = distance(gridPoint, point, options);
|
|
58
64
|
var zValue;
|
|
59
|
-
if (property !== void 0)
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (
|
|
65
|
+
if (property !== void 0) {
|
|
66
|
+
zValue = (_a3 = point.properties) == null ? void 0 : _a3[property];
|
|
67
|
+
}
|
|
68
|
+
if (zValue === void 0) {
|
|
69
|
+
zValue = point.geometry.coordinates[2];
|
|
70
|
+
}
|
|
71
|
+
if (zValue === void 0) {
|
|
72
|
+
throw new Error("zValue is missing");
|
|
73
|
+
}
|
|
74
|
+
if (d === 0) {
|
|
75
|
+
zw = zValue;
|
|
76
|
+
}
|
|
63
77
|
var w = 1 / Math.pow(d, weight);
|
|
64
78
|
sw += w;
|
|
65
79
|
zw += w * zValue;
|
|
66
80
|
});
|
|
67
81
|
var newFeature = clone(gridFeature);
|
|
82
|
+
(_a2 = newFeature.properties) != null ? _a2 : newFeature.properties = {};
|
|
68
83
|
newFeature.properties[property] = zw / sw;
|
|
69
84
|
results.push(newFeature);
|
|
70
85
|
});
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../index.
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import { bbox } from \"@turf/bbox\";\nimport { hexGrid } from \"@turf/hex-grid\";\nimport { pointGrid } from \"@turf/point-grid\";\nimport { distance } from \"@turf/distance\";\nimport { centroid } from \"@turf/centroid\";\nimport { squareGrid } from \"@turf/square-grid\";\nimport { triangleGrid } from \"@turf/triangle-grid\";\nimport { clone } from \"@turf/clone\";\nimport { featureCollection, Grid, Units, validateBBox } from \"@turf/helpers\";\nimport { featureEach } from \"@turf/meta\";\nimport { collectionOf } from \"@turf/invariant\";\nimport { BBox, Feature, FeatureCollection, Point, Polygon } from \"geojson\";\n\n/**\n * Takes a set of points and estimates their 'property' values on a grid using the [Inverse Distance Weighting (IDW) method](https://en.wikipedia.org/wiki/Inverse_distance_weighting).\n *\n * @function\n * @param {FeatureCollection<Point>} points with known value\n * @param {number} cellSize the distance across each grid point\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.gridType='square'] defines the output format based on a Grid Type (options: 'square' | 'point' | 'hex' | 'triangle')\n * @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.\n * @param {Units} [options.units='kilometers'] used in calculating cellSize. Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units}.\n * @param {number} [options.weight=1] exponent regulating the distance-decay weighting\n * @param {BBox} [options.bbox=bbox(points)] Bounding Box Array [west, south, east, north] associated with the FeatureCollection.\n * @returns {FeatureCollection<Point|Polygon>} grid of points or polygons with interpolated 'property'\n * @example\n * var points = turf.randomPoint(30, {bbox: [50, 30, 70, 50]});\n *\n * // add a random property to each point\n * turf.featureEach(points, function(point) {\n * point.properties.solRad = Math.random() * 50;\n * });\n * var options = {gridType: 'points', property: 'solRad', units: 'miles'};\n * var grid = turf.interpolate(points, 100, options);\n *\n * //addToMap\n * var addToMap = [grid];\n */\nfunction interpolate<T extends Grid = \"square\">(\n points: FeatureCollection<Point>,\n cellSize: number,\n options?: {\n gridType?: T;\n property?: string;\n units?: Units;\n weight?: number;\n bbox?: BBox;\n }\n): FeatureCollection<T extends \"point\" ? Point : Polygon> {\n // Optional parameters\n options = options || {};\n\n // Validation pre-options parsing\n if (typeof options !== \"object\") {\n throw new Error(\"options is invalid\");\n }\n if (!points) {\n throw new Error(\"points is required\");\n }\n collectionOf(points, \"Point\", \"input must contain Points\");\n if (!cellSize) {\n throw new Error(\"cellSize is required\");\n }\n\n var gridType = options.gridType ?? \"square\";\n var property = options.property ?? \"elevation\";\n var weight = options.weight ?? 1;\n var box = options.bbox ?? bbox(points);\n\n // validation post options-parsing\n if (weight !== undefined && typeof weight !== \"number\") {\n throw new Error(\"weight must be a number\");\n }\n\n validateBBox(box);\n\n var grid;\n switch (gridType) {\n case \"point\":\n case \"points\":\n grid = pointGrid(box, cellSize, options);\n break;\n case \"square\":\n case \"squares\":\n grid = squareGrid(box, cellSize, options);\n break;\n case \"hex\":\n case \"hexes\":\n grid = hexGrid(box, cellSize, options);\n break;\n case \"triangle\":\n case \"triangles\":\n grid = triangleGrid(box, cellSize, options);\n break;\n default:\n throw new Error(\"invalid gridType\");\n }\n var results: Feature<Point | Polygon>[] = [];\n featureEach<Point | Polygon>(grid, function (gridFeature) {\n var zw = 0;\n var sw = 0;\n // calculate the distance from each input point to the grid points\n featureEach(points, function (point) {\n var gridPoint =\n gridType === \"point\"\n ? (gridFeature as Feature<Point>)\n : centroid(gridFeature);\n var d = distance(gridPoint, point, options);\n var zValue;\n // property has priority for zValue, fallbacks to 3rd coordinate from geometry\n if (property !== undefined) {\n zValue = point.properties?.[property];\n }\n if (zValue === undefined) {\n zValue = point.geometry.coordinates[2];\n }\n if (zValue === undefined) {\n throw new Error(\"zValue is missing\");\n }\n if (d === 0) {\n zw = zValue;\n }\n var w = 1.0 / Math.pow(d, weight);\n sw += w;\n zw += w * zValue;\n });\n // write interpolated value for each grid point\n var newFeature = clone(gridFeature);\n newFeature.properties ??= {};\n newFeature.properties[property] = zw / sw;\n results.push(newFeature);\n });\n return featureCollection(results) as FeatureCollection<\n T extends \"point\" ? Point : Polygon\n >;\n}\n\nexport { interpolate };\nexport default interpolate;\n"],"mappings":";AAAA,SAAS,YAAY;AACrB,SAAS,eAAe;AACxB,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB;AACzB,SAAS,gBAAgB;AACzB,SAAS,kBAAkB;AAC3B,SAAS,oBAAoB;AAC7B,SAAS,aAAa;AACtB,SAAS,mBAAgC,oBAAoB;AAC7D,SAAS,mBAAmB;AAC5B,SAAS,oBAAoB;AA6B7B,SAAS,YACP,QACA,UACA,SAOwD;AAjD1D;AAmDE,YAAU,WAAW,CAAC;AAGtB,MAAI,OAAO,YAAY,UAAU;AAC/B,UAAM,IAAI,MAAM,oBAAoB;AAAA,EACtC;AACA,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI,MAAM,oBAAoB;AAAA,EACtC;AACA,eAAa,QAAQ,SAAS,2BAA2B;AACzD,MAAI,CAAC,UAAU;AACb,UAAM,IAAI,MAAM,sBAAsB;AAAA,EACxC;AAEA,MAAI,YAAW,aAAQ,aAAR,YAAoB;AACnC,MAAI,YAAW,aAAQ,aAAR,YAAoB;AACnC,MAAI,UAAS,aAAQ,WAAR,YAAkB;AAC/B,MAAI,OAAM,aAAQ,SAAR,YAAgB,KAAK,MAAM;AAGrC,MAAI,WAAW,UAAa,OAAO,WAAW,UAAU;AACtD,UAAM,IAAI,MAAM,yBAAyB;AAAA,EAC3C;AAEA,eAAa,GAAG;AAEhB,MAAI;AACJ,UAAQ,UAAU;AAAA,IAChB,KAAK;AAAA,IACL,KAAK;AACH,aAAO,UAAU,KAAK,UAAU,OAAO;AACvC;AAAA,IACF,KAAK;AAAA,IACL,KAAK;AACH,aAAO,WAAW,KAAK,UAAU,OAAO;AACxC;AAAA,IACF,KAAK;AAAA,IACL,KAAK;AACH,aAAO,QAAQ,KAAK,UAAU,OAAO;AACrC;AAAA,IACF,KAAK;AAAA,IACL,KAAK;AACH,aAAO,aAAa,KAAK,UAAU,OAAO;AAC1C;AAAA,IACF;AACE,YAAM,IAAI,MAAM,kBAAkB;AAAA,EACtC;AACA,MAAI,UAAsC,CAAC;AAC3C,cAA6B,MAAM,SAAU,aAAa;AAnG5D,QAAAA;AAoGI,QAAI,KAAK;AACT,QAAI,KAAK;AAET,gBAAY,QAAQ,SAAU,OAAO;AAvGzC,UAAAA;AAwGM,UAAI,YACF,aAAa,UACR,cACD,SAAS,WAAW;AAC1B,UAAI,IAAI,SAAS,WAAW,OAAO,OAAO;AAC1C,UAAI;AAEJ,UAAI,aAAa,QAAW;AAC1B,kBAASA,MAAA,MAAM,eAAN,gBAAAA,IAAmB;AAAA,MAC9B;AACA,UAAI,WAAW,QAAW;AACxB,iBAAS,MAAM,SAAS,YAAY,CAAC;AAAA,MACvC;AACA,UAAI,WAAW,QAAW;AACxB,cAAM,IAAI,MAAM,mBAAmB;AAAA,MACrC;AACA,UAAI,MAAM,GAAG;AACX,aAAK;AAAA,MACP;AACA,UAAI,IAAI,IAAM,KAAK,IAAI,GAAG,MAAM;AAChC,YAAM;AACN,YAAM,IAAI;AAAA,IACZ,CAAC;AAED,QAAI,aAAa,MAAM,WAAW;AAClC,KAAAA,MAAA,WAAW,eAAX,OAAAA,MAAA,WAAW,aAAe,CAAC;AAC3B,eAAW,WAAW,QAAQ,IAAI,KAAK;AACvC,YAAQ,KAAK,UAAU;AAAA,EACzB,CAAC;AACD,SAAO,kBAAkB,OAAO;AAGlC;AAGA,IAAO,gBAAQ;","names":["_a"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/interpolate",
|
|
3
|
-
"version": "7.3.
|
|
3
|
+
"version": "7.3.3",
|
|
4
4
|
"description": "Creates an interpolated grid of points using the Inverse Distance Weighting method.",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@turf/truncate": "7.3.
|
|
57
|
+
"@turf/truncate": "7.3.3",
|
|
58
58
|
"@types/benchmark": "^2.1.5",
|
|
59
59
|
"@types/tape": "^5.8.1",
|
|
60
60
|
"benchmark": "^2.1.4",
|
|
@@ -63,21 +63,23 @@
|
|
|
63
63
|
"tape": "^5.9.0",
|
|
64
64
|
"tsup": "^8.4.0",
|
|
65
65
|
"tsx": "^4.19.4",
|
|
66
|
+
"typescript": "^5.8.3",
|
|
66
67
|
"write-json-file": "^6.0.0"
|
|
67
68
|
},
|
|
68
69
|
"dependencies": {
|
|
69
|
-
"@turf/bbox": "7.3.
|
|
70
|
-
"@turf/centroid": "7.3.
|
|
71
|
-
"@turf/clone": "7.3.
|
|
72
|
-
"@turf/distance": "7.3.
|
|
73
|
-
"@turf/helpers": "7.3.
|
|
74
|
-
"@turf/hex-grid": "7.3.
|
|
75
|
-
"@turf/invariant": "7.3.
|
|
76
|
-
"@turf/meta": "7.3.
|
|
77
|
-
"@turf/point-grid": "7.3.
|
|
78
|
-
"@turf/square-grid": "7.3.
|
|
79
|
-
"@turf/triangle-grid": "7.3.
|
|
80
|
-
"@types/geojson": "^7946.0.10"
|
|
70
|
+
"@turf/bbox": "7.3.3",
|
|
71
|
+
"@turf/centroid": "7.3.3",
|
|
72
|
+
"@turf/clone": "7.3.3",
|
|
73
|
+
"@turf/distance": "7.3.3",
|
|
74
|
+
"@turf/helpers": "7.3.3",
|
|
75
|
+
"@turf/hex-grid": "7.3.3",
|
|
76
|
+
"@turf/invariant": "7.3.3",
|
|
77
|
+
"@turf/meta": "7.3.3",
|
|
78
|
+
"@turf/point-grid": "7.3.3",
|
|
79
|
+
"@turf/square-grid": "7.3.3",
|
|
80
|
+
"@turf/triangle-grid": "7.3.3",
|
|
81
|
+
"@types/geojson": "^7946.0.10",
|
|
82
|
+
"tslib": "^2.8.1"
|
|
81
83
|
},
|
|
82
|
-
"gitHead": "
|
|
84
|
+
"gitHead": "fa0e2da8ce02d9a82720eae922f89c9386596e04"
|
|
83
85
|
}
|