Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Geospatial queries in php

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

like image 851
micmcg Avatar asked Mar 18 '11 03:03

micmcg


People also ask

Does MongoDB support geospatial?

MongoDB supports query operations on geospatial data.

What are geospatial queries?

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 .

How does MongoDB store location?

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.

What is 2dsphere index in MongoDB?

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.


3 Answers

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.

like image 163
Justin Dearing Avatar answered Oct 19 '22 07:10

Justin Dearing


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)))));
like image 35
Vaibhav Desai Avatar answered Oct 19 '22 09:10

Vaibhav Desai


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.

like image 39
paulscott56 Avatar answered Oct 19 '22 08:10

paulscott56