-
Notifications
You must be signed in to change notification settings - Fork 992
Significantly improved performance of clustersDbscan #2885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
4a4ac06
862cf89
12acc52
cef9323
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { distance } from "@turf/distance"; | ||
| import { degreesToRadians, lengthToDegrees, Units } from "@turf/helpers"; | ||
| import { rbush as RBush } from "./lib/rbush-export.js"; | ||
| import { Units } from "@turf/helpers"; | ||
| import KDBush from "kdbush"; | ||
| import * as geokdbush from "geokdbush"; | ||
|
|
||
| /** | ||
| * Point classification within the cluster. | ||
|
|
@@ -66,6 +66,9 @@ function clustersDbscan( | |
| } = {} | ||
| ): FeatureCollection<Point, DbscanProps> { | ||
| // Input validation being handled by Typescript | ||
| // TODO oops! No it isn't. Typescript doesn't do runtime checking. We should | ||
| // re-enable these checks, though will have to wait for a major version bump | ||
| // as more restrictive checks could break currently working code. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah there's a philosophical argument on whether these should be static checks or runtime checks. We've gone back and forth on it over the years. |
||
| // collectionOf(points, 'Point', 'points must consist of a FeatureCollection of only Points'); | ||
| // if (maxDistance === null || maxDistance === undefined) throw new Error('maxDistance is required'); | ||
| // if (!(Math.sign(maxDistance) > 0)) throw new Error('maxDistance is invalid'); | ||
|
|
@@ -77,11 +80,13 @@ 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 | ||
| var tree = new RBush(points.features.length); | ||
| 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(); | ||
|
|
||
| // Keeps track of whether a point has been visited or not. | ||
| var visited = points.features.map((_) => false); | ||
|
|
@@ -95,54 +100,22 @@ 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; | ||
|
|
||
| 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; | ||
| } | ||
| return ( | ||
| geokdbush | ||
| // @ts-expect-error - until https://github.com/mourner/geokdbush/issues/20 is resolved | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add the error number that we're expecting just so its a little more safe? |
||
| .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, | ||
| })) | ||
| ); | ||
| }; | ||
|
|
||
|
|
||
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love kdbush, it's saved my bacon a few times now.