How do I get all the circles that contain a point?

Is there a way to find out which polygons (specifically the circles) are at a certain point?

In this case, I would save documents containing circles as shown below, I would go in latitude and longitude for a point and would like to return all documents where the point is within the given circle.

{
  "_id" : ObjectId("53e3e85ce4b0c2e8227a1dad"),
  "name" : "Menlo College",
  "location" : [-122.1928, 37.45632],
  "radius" : NumberLong(215),
},
{
  "_id" : ObjectId("53e43d19e4b0aeabcb3d3f9d"),
  "name" : "West Valley College",
  "location" : [-122.01021194458008, 37.263226547586207],
  "radius" : NumberLong(604),
}

      

If this is not possible, then is it possible at least with other GeoJSON forms? Everything I've found so far shows that the opposite is possible (find all points that are like inside a circle), but nothing for this scenario.

thank

+3


source to share


1 answer


It is possible to use MongoDB $geoIntersects

Geospatial Query Operator.

So, if you have a GeoJson collection of polygons and you want to find out all the polygons that intersect with your point, you need to run the following:

db.places.find( { <locationFieldOfYourDocuments> :
                  { $geoIntersects :
                    { $geometry :
                      { type : "Point" ,
                        coordinates: [long, lat]
                } } } } )

      

In the above command loc

, this is the attribute of each document that contains the coordinates for the GeoJson polygon. Also, make sure you have an 2dsphere

index over <locationFieldOfYourDocuments>

.

Now, to solve your original problem, I'll be using a little javascript. There may be better solutions, but I don't know.

Let's say all your circles are stored in a collection Circles

. I would like to query this assembly and get each circle one at a time, and then intersect with another collection that will contain one point, which would be the one you wanted to query if intersect with the circles or not. So let the point be stored in a collection SinglePoint

.



The script will look like ...

db.Intersections.remove({}); // emptying the output collection
var circleCursor = db.Circles.find();
while (circleCursor.hasNext()) {
    var circle = circleCursor.next();
    var coord = circle.location;
    var radiusInRadians = circle.radius * conversionFactorForRadius;
    var intersect = db.SinglePoint.find({loc :
                                         { $geoWithin :
                                           {$centerSphere : [coord], radiusInRadians}
                                         }});
    if (intersect.hasNext()) {db.Intersections.add(circle)} // this will add all intersecting circles to Intersections collection
}

      

All you have to do is save this script in a file (myScript.js) and make the call:

mongo DBName pathTomyScript.js

      

This will keep all the circles that intersect with your entry point in the Intersects collection. All the listed collections must be in the DBName database.

+1


source







All Articles