I'm trying to use MongDBs 2d indexing to run geospatial queries, as outlined here - http://www.mongodb.org/display/DOCS/Geospatial+Indexing
It's working fine, and I'm able run queries such as
db.places.find( { point : { $near : [151.1955562233925,-33.87107475181752] , $maxDistance : 0.1/111} } )
from the CLI tool, but when I try and run the query in PHP (using the PECL mongo driver) I'm not able to get any results.
Any help with how to write the above query for PHP? Does anyone know if the PHP driver supports geospatial queries?
Thanks
MongoDB supports query operations on geospatial data.
Geospatial queries are specialized types of SQL queries supported in Athena. They differ from non-spatial SQL queries in the following ways: Using the following specialized geometry data types: point , line , multiline , polygon , and multipolygon .
MongoDB provides the functionality to store locations under the object type geoJSON and their coordinates against the coordinate field in [longitude, latitude] form where longitude must lie between [-180, 180] and latitude must lie between [-90,90], both inclusive.
A 2dsphere index supports queries that calculate geometries on an earth-like sphere. 2dsphere index supports all MongoDB geospatial queries: queries for inclusion, intersection and proximity. For more information on geospatial queries, see Geospatial Queries.
I have geo lookup with the PHP driver working in production. It looks something like this:
$latLong = $geo->lookupLatLong($address);
$cursor = $coats->find(Array('latLong' => Array('$near' => $latLong)))->limit(10);
Also, you might want to consider using $nearSphere as opposed to $near to get more accurate coordinates. See the mongo docs for more information.
According to documentation in MongoDB website
{
$near: {
$geometry: {
type: "Point" ,
coordinates: [ <longitude> , <latitude> ]
},
$maxDistance: <distance in meters>,
$minDistance: <distance in meters>
}
}
The php code for the above query would be:
$query = array(
'loc' => array(
'$near' => array(
'$geometry' =>array(
'type' => 'Point',
'coordinates'=>array(floatval($lon),floatval($lat)),
'$maxDistance' => intval($max_distance),
'$mixDistance' => intval($min_distance)))));
An example of using the $nearSphere command in PHP would look something like:
$lonlat = array($lon, $lat);
$cursor = $this->collection->find(Array('loc' => Array('$nearSphere' => $lonlat)))->limit($limit);
Where 'loc' is the 2d geospatial index and limit is optional.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With