Tricky $centerSphere problem

152 views
Skip to first unread message

Christopher Noyes

unread,
May 8, 2014, 4:53:01 PM5/8/14
to mongod...@googlegroups.com
I want to do something with

db.<collection>.find( { <location field> :
                         { $geoWithin :
                            { $centerSphere : [ [ <x>, <y> ] , <radius> ] }
                      } } )


The collection will have a location field (long/lat) and a radius field in addition to some other fields

I want to do something like

db.<collection>.find( { [<x>,<y>] : { $geoWithin : { $centerSphere : [ this.location , this.radius ] } } } )


to find those locations that my x,y is inside the circle


which is really the inverse of how this type of query is specified.


Andrew Ryder

unread,
May 28, 2014, 1:55:10 AM5/28/14
to mongod...@googlegroups.com
Hi Christopher!

You will need to do 2 queries to achieve what you want. This makes sense given that you want to search over the whole collection once to locate a specific document at the epicenter to retrieve it's radius property (what happens if there is none or multiple matches?) and then using that value, do a second query (geoWithin) over the whole collection for items within that range.

I hope this helps.

Kind regards,
Andrew

Christopher Noyes

unread,
May 28, 2014, 10:14:44 AM5/28/14
to mongod...@googlegroups.com

I wound up storing keys and radius separately, using the geoNear command using the known long/lat and some max radius and using the distance that comes back and comparing with the radius data as a separate step. Works pretty well, not the cleanest answer.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/0AnIgROAcEU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/cd12660b-f720-40a6-9eb0-1b8bfe63ed48%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nicolai Kilian

unread,
Mar 19, 2015, 5:05:10 PM3/19/15
to mongod...@googlegroups.com
Hi,


Instead of defining your location data as point and radius, you can simulate GeoJSON-circles using this JS

function generateGeoJSONCircle(center, radius, numSides){

  var points = [], degreeStep = 360 / numSides;

  for(var i = 0; i < numSides; i++){
    var gpos = google.maps.geometry.spherical.computeOffset(center, radius, degreeStep * i);
    points.push([gpos.lng(), gpos.lat()]);
  };

  // Duplicate the last point to close the geojson ring
  points.push(points[0]);

  return {
    type: 'Polygon',
    coordinates: [ points ]
  };
};

console.log(JSON.stringify(generateGeoJSONCircle(
    new google.maps.LatLng(47.567444, 6.9999), 5000, 30)));

Then, use this function to convert your starting point into a little triangle and use mongo geoIntersect to find which circles intersect / include your triangle.
Cheers!
Reply all
Reply to author
Forward
0 new messages