Reputation: 846
Following postgis query creates buffer zones for all points and then gets the points which are intersected by the user's location (which is also buffered by a constant value):
SELECT id, created_at, ST_AsGeoJSON(the_geom) FROM table WHERE ST_INTERSECTS(ST_Buffer(ST_GeomFromText('POINT(x y)',4326)::geography, 150)::geometry,
ST_Buffer(the_geom::geography, 100*(seenCount+1))::geometry) ORDER BY created_at DESC
I want to migrate our database to mongodb but i couldn't find how can i achieve the above query in mongodb.
Upvotes: 0
Views: 431
Reputation: 12571
There is no equivalent of ST_Buffer in MongoDB, which is still quite limited in the type of spatial queries that can be performed, essentially limited to within, intersects and near operators, see: MongoDB geospatial query operators
One approach would be to save your field, the_geom, as a pre-buffered GeoJSON Polygon and then you will be able to make use of a 2dsphere index to find anything that intersects some buffer of your point, eg,
db.points.find(
{geom:
{$geoIntersects:
{$geometry:
{type:"Polygon",
coordinates:[[[49, 50],[50,51],[51,50],[50,49],[49,50]]]
}
}
}
}
)
where the coordinates represents some buffer of a point. Over small distances, in lat/lon, you can imitate a buffer of a point with a circle, though the distortion will increase with buffer size and latitude. If you need more precision, you will have to calculate the buffer properly on the WGS 84 spheroid, before inserting into MongoDB -- hard to recommend any library without knowing what client you will be using to interact with Mongo.
You could, of course, do this with map-reduce too, just by calculating the distance between each point and each polygon. For exact distance measures on a sphere, look at the Vincenty Distance, see OpenLayers distVincenty Javascript function. for one implementation. You would be able to simulate the intersection of two buffers, if the Vincenty Distance between the two geometries was less than the value of the two buffers added together.
I suspect that the map-reduce approach would be quite slow, as it wouldn't utilize any spatial index, but if time is not an issue, and if you don't want the additional overhead of buffering points and saving them as GeoJSON polygons, then it would work.
As as aside, there have been discussions on mailing lists of changing the Java Topology Suite library's license to BSD, which means that MongoDB could theoretically include its functionality, if there were a push towards adding more spatial functionality. Java Topology Suite, in its C++ port, GEOS, is the library that provides many of the spatial relation operators in Postgis.
Upvotes: 1