Skip to content
Merged
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
70 changes: 50 additions & 20 deletions packages/turf-clusters-dbscan/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { GeoJsonProperties, FeatureCollection, Point } from "geojson";
import { clone } from "@turf/clone";
import { Units } from "@turf/helpers";
import KDBush from "kdbush";
import * as geokdbush from "geokdbush";
import { distance } from "@turf/distance";
import { degreesToRadians, lengthToDegrees, Units } from "@turf/helpers";
import RBush from "rbush";

/**
* Point classification within the cluster.
Expand Down Expand Up @@ -80,13 +80,11 @@ function clustersDbscan(
// Defaults
const minPoints = options.minPoints || 3;

// Calculate the distance in degrees for region queries
const latDistanceInDegrees = lengthToDegrees(maxDistance, options.units);

// Create a spatial index
const kdIndex = new KDBush(points.features.length);
// Index each point for spatial queries
for (const point of points.features) {
kdIndex.add(point.geometry.coordinates[0], point.geometry.coordinates[1]);
}
kdIndex.finish();
var tree = new RBush(points.features.length);

// Keeps track of whether a point has been visited or not.
var visited = points.features.map((_) => false);
Expand All @@ -100,22 +98,54 @@ function clustersDbscan(
// Keeps track of the clusterId for each point
var clusterIds: number[] = points.features.map((_) => -1);

// Index each point for spatial queries
tree.load(
points.features.map((point, index) => {
var [x, y] = point.geometry.coordinates;
return {
minX: x,
minY: y,
maxX: x,
maxY: y,
index: index,
} as IndexedPoint;
})
);

// Function to find neighbors of a point within a given distance
const regionQuery = (index: number): IndexedPoint[] => {
const point = points.features[index];
const [x, y] = point.geometry.coordinates;

return (
geokdbush
// @ts-expect-error 2345 until https://github.com/mourner/geokdbush/issues/20 is resolved
.around<number>(kdIndex, x, y, undefined, maxDistance)
.map((id) => ({
minX: points.features[id].geometry.coordinates[0],
minY: points.features[id].geometry.coordinates[1],
maxX: points.features[id].geometry.coordinates[0],
maxY: points.features[id].geometry.coordinates[1],
index: id,
}))
const minY = Math.max(y - latDistanceInDegrees, -90.0);
const maxY = Math.min(y + latDistanceInDegrees, 90.0);

const lonDistanceInDegrees = (function () {
// Handle the case where the bounding box crosses the poles
if (minY < 0 && maxY > 0) {
return latDistanceInDegrees;
}
if (Math.abs(minY) < Math.abs(maxY)) {
return latDistanceInDegrees / Math.cos(degreesToRadians(maxY));
} else {
return latDistanceInDegrees / Math.cos(degreesToRadians(minY));
}
})();

const minX = Math.max(x - lonDistanceInDegrees, -360.0);
const maxX = Math.min(x + lonDistanceInDegrees, 360.0);

// Calculate the bounding box for the region query
const bbox = { minX, minY, maxX, maxY };
return (tree.search(bbox) as ReadonlyArray<IndexedPoint>).filter(
(neighbor) => {
const neighborIndex = neighbor.index;
const neighborPoint = points.features[neighborIndex];
const distanceInKm = distance(point, neighborPoint, {
units: "kilometers",
});
return distanceInKm <= maxDistance;
}
);
};

Expand Down
6 changes: 3 additions & 3 deletions packages/turf-clusters-dbscan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@turf/centroid": "workspace:*",
"@turf/clusters": "workspace:*",
"@types/benchmark": "^2.1.5",
"@types/rbush": "^3.0.4",
"@types/tape": "^5.8.1",
"benchmark": "^2.1.4",
"chromatism": "^3.0.0",
Expand All @@ -75,12 +76,11 @@
},
"dependencies": {
"@turf/clone": "workspace:*",
"@turf/distance": "workspace:*",
"@turf/helpers": "workspace:*",
"@turf/meta": "workspace:*",
"@types/geojson": "^7946.0.10",
"@types/geokdbush": "^1.1.5",
"geokdbush": "^2.0.1",
"kdbush": "^4.0.2",
"rbush": "^3.0.1",
"tslib": "^2.8.1"
}
}
42 changes: 9 additions & 33 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading