Geofencing with MongoDB
I was reading the documentation for MongoDB here and I found it r开发者_如何学JAVAeally interesting. However, there was one feature that seemed to be very-much in reach but was not explicitly documented. That is the concept of geo-fencing. If I create a radius (or a rectangle), how can I perform a query such that I can provide a point and Mongo can tell me if that point is inside of the defined area(s) instead of having to query for nearby areas and perform a Ray-Casting algorithm (or some other algorithm) to determine if my provided point lies within those areas.
Any help in the right direction or, even better, some examples of how to do this with MongoDB is greatly appreciated. Thanks!
Update
Just to let everyone know what I did. I ended up rolling my own solution built on top of Mongo (to utilize it's existing geo-spatial index/query abilities) and it works rather well for a prototype. As a solution to use in production, it could definitely use some improvements. But, I wrote a blog series on how I approached the problem along with a sample server in Ruby. You can find part one of the blog on my site here.Unfortunately mongodb's geo indexes are for storing point-data only. To store regions would require some serious reworking of the data structure we are using and is a long-term goal, but will not happen in the short term. As a work around, if your regions are roughly similarly sized you might be able to get away with storing the center point of each region. Then you could fetch the ten closest regions and do a client-side filter in your app to figure out which region the query point is in.
Alternatively, you could make an idealized grid and store the grid squares that each region contains in an array for that region. Then map the query point to it's grid square, and figure out which region (if more than one for that square) the point is in. This is actually similar to how we implement our geo index although we use a dynamic grid. You should use a normal (non-geo) index if you want to go this route.
While neither of these are ideal, hopefully they will provide a workable solution.
Why not just use collections for your regions and use polygons for your regions - here's a great presentation from Greg Studer @ 10gen that may give you some ideas: https://docs.google.com/present/edit?id=0ARXrl_iXSUmXZGhieDhqcTdfMTdjcjI4eDNkNQ&hl=en_US
精彩评论