How to store a circle and see if it overlaps with a box/ Geojson point in mongodb

435 views Asked by At

I am not sure if there is a way to do this. I need to have a circle in mongodb and run a query against that with a box using $box to see if these two shapes overlap or not. However, Geojson does not support circles. What would be the best way to get this done? The circle is stored like this:

places = { 
...
"location": {
    "type": "Point",
    "coordinates": [
        -79.390756,
        43.706685
    ]
},
"radius": 100
}

I have two specific problems:

  1. The first issue is that maxDistance is stored in the same object as the Geojson object and cannot be used in a $near query with $maxDistance; it only takes a number.
  2. I do a partial postal code/ zip code search on Google Geocoding Api which returns a box with two corner coordinates like this:

    "geometry": { "bounds": { "northeast": { "lat": 43.710565, "lng": -79.37363479999999 }, "southwest": { "lat": 43.690848, "lng": -79.40025399999999 } } As far as I know,I cannot use $box as it only works with $geoWithin.

Edit 1: My initial plan with the circle and the box changed mainly because I did not find a suitable and efficient solution to this problem. Instead of checking if the circle overlaps with the box, now I check if a Geojson point is inside the circle as follows:

db.places.aggregate([
{"$geoNear": {near: { type: "Point", coordinates: [  -80.459293, 40.713640] }, 
distanceField: "dist.calculated", maxDistance: 100000, 
key: 'myLocation', query: { 'SomeField': "..." }, spherical: true}},
{ "$match" :  {$expr:{ $lte:['$dist.calculated', 'radius']}}}])

The problem here is that I d have to run a query within 100 KM first and then in another stage of the aggregation check the distance. Is there a more efficient way to implement this? Thanks.

1

There are 1 answers

6
Stan Wiechers On

You can store a circle as point and radius. And you can use a $near query with a point and $maxDistance in meters which is the radius of the circle. See MongoDB Documentation.

https://en.wikipedia.org/wiki/Radius

Query to find all location, geometry field of the collection, at a certain distance from a point.

db.places.find(
   {
     location:
       { $near :
          {
            $geometry: { type: "Point",  coordinates: [ -73.9667, 40.78 ] },
            $maxDistance: 5000
          }
       }
   }
)

Query to find if a given geometry (point, polygon(rect too)) in a query intersects with a geometry of a field in the collection.

//find quests bots that matches the users location
await Collection.find({     geometry:   
  { $geoIntersects: 
   { 
     {
      type: "Point",
      coordinates: [
        -73.99460599999999,
        40.7347229
        ]
      }
    } 
  }                         
});