Skip to content
Open
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
6 changes: 3 additions & 3 deletions packages/turf-clusters-dbscan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ function clustersDbscan(
(neighbor) => {
const neighborIndex = neighbor.index;
const neighborPoint = points.features[neighborIndex];
const distanceInKm = distance(point, neighborPoint, {
units: "kilometers",
const neighborDistance = distance(point, neighborPoint, {
units: options.units,
});
return distanceInKm <= maxDistance;
return neighborDistance <= maxDistance;
}
);
};
Expand Down
21 changes: 20 additions & 1 deletion packages/turf-clusters-dbscan/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { writeJsonFileSync } from "write-json-file";
import { centroid } from "@turf/centroid";
import chromatism from "chromatism";
import concaveman from "concaveman";
import { point, polygon, featureCollection } from "@turf/helpers";
import {
point,
polygon,
featureCollection,
lengthToDegrees,
} from "@turf/helpers";
import { clusterReduce, clusterEach } from "@turf/clusters";
import { coordAll, featureEach } from "@turf/meta";
import { clustersDbscan } from "./index.js";
Expand Down Expand Up @@ -154,3 +159,17 @@ test("clusters-dbscan -- allow input mutation", (t) => {
t.equal(oldPoints.features[1].properties.cluster, 1, "cluster is 1");
t.end();
});

test("clusters-dbscan -- maxDistance honors non-kilometer units", (t) => {
// 400 m east + 400 m north of the origin is ~566 m away — outside a 400 m
// radius — so with a 400 m maxDistance the two points must be noise.
const offset = lengthToDegrees(400, "meters"); // at the equator, east == north
const fc = featureCollection([point([0, 0]), point([offset, offset])]);

const clustered = clustersDbscan(fc, 400, { units: "meters", minPoints: 2 });
t.true(
clustered.features.every((f) => f.properties.dbscan === "noise"),
"points ~566 m apart are noise when maxDistance is 400 meters"
);
t.end();
});