turf geojson measurement: working versions (tested 2026-10-18)
Measure GeoJSON offline with turf: distance in kilometres, polygon area, point-in-polygon, bbox, centroid and a point along a line, asserting values with tolerance.
passed6 passed · 0 failed2026-10-18 03:58 UTC- Status
- passed
- Last passed
- 2026-10-18 03:58 UTC
- Tests
- 6 / 6
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
@turf/turf@7.4.0@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 39095442401
The code
Exactly the files that passed. Your agent can pull them with get_recipe("turf-geojson-measure-buffer") (install), or see them on GitHub.
package.json
{
"name": "turf-geojson-measure-buffer",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"@turf/turf": "7.4.0"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/geo.ts
import {
along,
area,
bbox,
booleanPointInPolygon,
centroid,
distance,
lineString,
multiPoint,
point,
polygon,
type Feature,
type Polygon,
} from "@turf/turf";
export type LatLon = [number, number];
export function distanceKm(from: LatLon, to: LatLon): number {
return distance(point(from), point(to), { units: "kilometers" });
}
export function unitSquare(): Feature<Polygon> {
return polygon([[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]);
}
export function areaKm2(poly: Feature<Polygon>): number {
return area(poly) / 1_000_000;
}
export function contains(coord: LatLon, poly: Feature<Polygon>): boolean {
return booleanPointInPolygon(point(coord), poly);
}
export function bounds(feature: Feature<Polygon>): number[] {
return bbox(feature);
}
export function center(coords: LatLon[]): LatLon {
const c = centroid(multiPoint(coords));
return [c.geometry.coordinates[0], c.geometry.coordinates[1]];
}
export function alongLine(from: LatLon, to: LatLon, km: number): LatLon {
const p = along(lineString([from, to]), km, { units: "kilometers" });
return [p.geometry.coordinates[0], p.geometry.coordinates[1]];
}
test/geo.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import {
alongLine,
areaKm2,
bounds,
center,
contains,
distanceKm,
unitSquare,
} from "../src/geo.ts";
function close(actual: number, expected: number, tol: number): void {
assert.ok(
Math.abs(actual - expected) <= tol,
`expected ${actual} within ${tol} of ${expected}`,
);
}
test("distance between points in kilometers", () => {
close(distanceKm([0, 0], [0, 1]), 111.195, 0.01);
close(distanceKm([-0.1276, 51.5074], [2.3522, 48.8566]), 343.5, 2);
});
test("polygon area in square kilometers", () => {
close(areaKm2(unitSquare()), 12364, 5);
});
test("booleanPointInPolygon for inside and outside points", () => {
const square = unitSquare();
assert.equal(contains([0.5, 0.5], square), true);
assert.equal(contains([1.5, 1.5], square), false);
});
test("bbox returns the min and max corners", () => {
assert.deepEqual(bounds(unitSquare()), [0, 0, 1, 1]);
});
test("centroid averages multipoint coordinates", () => {
assert.deepEqual(center([[0, 0], [2, 0], [2, 2], [0, 2]]), [1, 1]);
});
test("along a lineString at a given distance", () => {
const p = alongLine([0, 0], [1, 0], 50);
close(p[0], 0.44966, 0.002);
close(p[1], 0, 1e-9);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.