Skip to content

Commit

Permalink
radius filtering is more efficient now and works for other type of
Browse files Browse the repository at this point in the history
objects as well
  • Loading branch information
bonifaido committed Jan 11, 2012
1 parent 35a4d9d commit 870b726
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
18 changes: 7 additions & 11 deletions README.textile
Expand Up @@ -47,25 +47,21 @@ h2. Point in polygon

h2. Radius filtering

WARNING: Only works against points right now. TODO: Lines and polygons

If you retrieve a bunch of results from a bounding box query (common with R-Tree geo DBs), but you want to filter the rectangular result set by circular radius:
If you retrieve a bunch of results from a bounding box query (common with R-tree geo DBs), but you want to filter the rectangular result set by circular radius:

<pre>
<code>
// get the center of the original bounding box
var center = gju.rectangleCentroid({
"type": "Polygon",
"coordinates": [[[-122.677, 45.523],[-122.675, 45.524]]]
"coordinates": [[[-122.677, 45.523], [-122.675, 45.524]]]
}),
// draw a circular polygon from the center of
// the bounding box with a 100 meter radius
// but only in your head!
// radius (in meters)
radius = 100;
for (var point in listOfPointsReturnedFromBoundingBoxQuery) {
if (gju.pointDistance(point, center) <= radius) {
// ... do stuff with points inside the circle

for (var i in geometryObjectsWithinBBox) {
if (gju.geometryWithinRadius(geometryObjectsWithinBBox[i], center, radius)) {
// ... do stuff with objects inside the circle
}
}
</code>
Expand Down
24 changes: 24 additions & 0 deletions geojson-utils.js
Expand Up @@ -99,6 +99,30 @@
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return (6371 * c) * 1000; // returns meters
},

// checks if geometry lies entirely within a circle
// works with Point, LineString, Polygon
gju.geometryWithinRadius = function(geometry, center, radius) {
if (geometry.type == 'Point') {
return gju.pointDistance(geometry, center) <= radius;
} else if (geometry.type == 'LineString' || geometry.type == 'Polygon') {
var point = {};
var coordinates;
if (geometry.type == 'Polygon') {
// it's enough to check the exterior ring of the Polygon
coordinates = geometry.coordinates[0];
} else {
coordinates = geometry.coordinates;
}
for (var i in coordinates) {
point.coordinates = coordinates[i];
if (gju.pointDistance(point, center) > radius) {
return false;
}
}
}
return true;
}

// adapted from http://paulbourke.net/geometry/polyarea/javascript.txt
gju.area = function(polygon) {
Expand Down

0 comments on commit 870b726

Please sign in to comment.