Skip to content

Commit

Permalink
Fix incorrect bounding box
Browse files Browse the repository at this point in the history
  • Loading branch information
TadaTeruki authored and smallsaucepan committed Oct 24, 2023
1 parent fc795a5 commit 553398d
Showing 1 changed file with 12 additions and 39 deletions.
51 changes: 12 additions & 39 deletions packages/turf-clusters-dbscan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function clustersDbscan(
units?: Units;
minPoints?: number;
mutate?: boolean;
} = {},
} = {}
): FeatureCollection<Point, DbscanProps> {
// Input validation being handled by Typescript
// collectionOf(points, 'Point', 'points must consist of a FeatureCollection of only Points');
Expand Down Expand Up @@ -84,15 +84,15 @@ function clustersDbscan(
// Index each point for spatial queries
tree.load(
points.features.map((point, index) => {
const [x, y] = point.geometry.coordinates;
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
Expand All @@ -119,42 +119,15 @@ function clustersDbscan(
const maxX = Math.min(x + lonDistanceInDegrees, 360.0);

// Calculate the bounding box for the region query
const baseBbox = { minX, minY, maxX, maxY };

const bboxes = (function () {
if (baseBbox.minX >= -180 && baseBbox.maxX <= 180) {
return [baseBbox];
}
// Handle the case where the bounding box crosses the antimeridian
var bboxes = [];
for (
var ib = baseBbox.minX < -180 ? -1 : 0;
ib <= (baseBbox.maxX > 180 ? 1 : 0);
ib++
) {
bboxes.push({
minX: baseBbox.minX + ib * 360,
minY: baseBbox.minY,
maxX: baseBbox.maxX + ib * 360,
maxY: baseBbox.maxY,
});
}
return bboxes;
})();

const neighbors = bboxes
.map((bbox) => {
return tree.search(bbox).filter((neighbor) => {
const neighborIndex = (neighbor as IndexedPoint).index;
const neighborPoint = points.features[neighborIndex];
const distanceInKm = distance(point, neighborPoint, {
units: "kilometers",
});
return distanceInKm <= maxDistance;
});
})
.flat();
return neighbors as IndexedPoint[];
const bbox = { minX, minY, maxX, maxY };
return tree.search(bbox).filter((neighbor) => {
const neighborIndex = (neighbor as IndexedPoint).index;
const neighborPoint = points.features[neighborIndex];
const distanceInKm = distance(point, neighborPoint, {
units: "kilometers",
});
return distanceInKm <= maxDistance;
}) as IndexedPoint[];
};

// Function to expand a cluster
Expand Down

0 comments on commit 553398d

Please sign in to comment.