Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/turf-interpolate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ function interpolate<T extends Grid = "square">(
throw new Error("zValue is missing");
}
if (d === 0) {
// Use the exact input value instead of distance weighting.
sw = 1;
zw = zValue;
return false;
}
var w = 1.0 / Math.pow(d, weight);
sw += w;
Expand Down
99 changes: 99 additions & 0 deletions packages/turf-interpolate/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ test("turf-interpolate -- throws errors", (t) => {
/zValue is missing/,
"zValue is missing"
);
t.throws(
() =>
interpolate(featureCollection([point([0, 0])]), 1000, {
gridType: "point",
bbox: [-1, -1, 1, 1],
}),
/zValue is missing/,
"an exact match still requires a zValue"
);
t.end();
});

Expand All @@ -110,6 +119,96 @@ test("turf-interpolate -- zValue from 3rd coordinate", (t) => {
t.end();
});

test("turf-interpolate -- exact match preserves the input value", (t) => {
for (const elevation of [42, 0, -10]) {
for (const matchIndex of [0, 1, 2]) {
const samples = [
point([1, 1], { elevation: 100 }),
point([-1, 1], { elevation: 200 }),
];
samples.splice(matchIndex, 0, point([0, 0, 999], { elevation }));
const points = featureCollection(samples);
const original = JSON.stringify(points);
const result = interpolate(points, 1000, {
gridType: "point",
bbox: [-1, -1, 1, 1],
});

t.equal(result.features.length, 1, "one grid point");
t.deepEqual(result.features[0].geometry.coordinates, [0, 0]);
t.equal(
result.features[0].properties.elevation,
elevation,
`exact value ${elevation} at input index ${matchIndex}`
);
t.equal(JSON.stringify(points), original, "input is not modified");
}
}
t.end();
});

test("turf-interpolate -- exact match with different weights", (t) => {
const points = featureCollection([
point([1, 1], { elevation: 100 }),
point([0, 0], { elevation: 42 }),
]);
for (const weight of [0.5, 2, 0, -1]) {
for (const features of [points.features, [...points.features].reverse()]) {
const result = interpolate(featureCollection(features), 1000, {
gridType: "point",
bbox: [-1, -1, 1, 1],
weight,
});
t.equal(result.features[0].properties.elevation, 42, `weight ${weight}`);
}
}
t.end();
});

test("turf-interpolate -- exact match uses the third coordinate fallback", (t) => {
const result = interpolate(featureCollection([point([0, 0, -10])]), 1000, {
gridType: "point",
bbox: [-1, -1, 1, 1],
property: "temperature",
});
t.equal(result.features[0].properties.temperature, -10);
t.end();
});

test("turf-interpolate -- exact match at a polygon centroid", (t) => {
const result = interpolate(
featureCollection([
point([1, 1], { elevation: 100 }),
point([0, 0], { elevation: 42 }),
]),
200,
{ bbox: [-1, -1, 1, 1] }
);
t.equal(result.features.length, 1, "one square grid cell");
t.equal(result.features[0].properties.elevation, 42);
t.end();
});

test("turf-interpolate -- nonmatching points retain distance weighting", (t) => {
const points = featureCollection([
point([-1, 0], { elevation: 20 }),
point([1, 0], { elevation: 40 }),
]);
for (const weight of [0, 1, 2, -1]) {
const result = interpolate(points, 1000, {
gridType: "point",
bbox: [-1, -1, 1, 1],
weight,
});
t.equal(
round(result.features[0].properties.elevation),
30,
`weight ${weight}`
);
}
t.end();
});

// style result
function colorize(grid, property, name) {
property = property || "elevation";
Expand Down