Mark
Mark

Reputation: 1569

Mongodb GeoSpatial querying within shapes in collection

I have a collection that has a 2d geospatial index on a field (center) which is an array of long/lat, the collection also has a radius field. So each item can represent a circle. I know that mongodb has a operator $within, and I want to get a list of all items that contain a specific point [long,lat], but it seems that I can only check which points are within a specific shape.

Upvotes: 0

Views: 465

Answers (2)

The Nail
The Nail

Reputation: 8500

This is how I solved it (i.e. get a shape that covers a given point) in my situation, using a basic grid. It has a limited accuracy, depending on the grid resolution:

  1. create a collection "grid" with points that covers the common bounding box of all your shapes (use a nested for loop in javascript)
  2. create a 2d index on the grid
  3. for each shape, search for all the grid points that lie within the shape; label each grid point with the id of the shape (use an array attribute on the grid point)

To see in what shape(s) a point lies, search for the closest grid point, and return its assigned shape(s) property. First check for the common bounding box, because points outside it should always return 'not in any shape', and not use the closest grid point.

Depending on the precision you need, this might or might not be a usable solution. The accuracy depends on how many points you put in your grid, and you may be able to do something smart with local densities of your grid.

Upvotes: 0

Derick
Derick

Reputation: 36784

You are correct, right now, you can't do what you want. Please file a feature request at http://jira.mongodb.org as I can't find one already existing for this.

Upvotes: 2

Related Questions