public
Created

Geospatial issues

  • Download Gist
geospatial.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
<?php
namespace Application;
 
/**
* @Document(collection="events")
* @Index(keys={"coordinates"="2d"})
* @HasLifecycleCallbacks
*/
class Event extends Base
{
 
const STATUS_LIVE = 'live';
const STATUS_CANCELLED = 'cancelled';
 
/**
* @Id
* @var string
*/
protected $id;
 
/**
* @var string
* @EmbedOne(targetDocument="Application\Coordinates")
*/
protected $coordinates;
 
/**
* @Distance
*/
public $distance;
 
 
}
?>
 
 
 
<?php
namespace Application;
 
/**
* @EmbeddedDocument
*/
class Coordinates extends Base
{
/**
* @Float
*/
protected $latitude;
 
/**
* @Float
*/
protected $longitude;
 
}
?>
 
<?php
 
 
/**************** Client Code to get results *****************/
 
 
// All these give zero results
$places = $this->dm->createQueryBuilder('\Application\Event')->field('coordinates')->near('[50,60]')->getQuery()->execute();
$places = $this->dm->createQueryBuilder('\Application\Event')->field('coordinates')->near('50,60')->getQuery()->execute();
$places = $this->dm->createQueryBuilder('\Application\Event')->field('coordinates')->near(50,60)->getQuery()->execute();
$places = $this->dm->createQueryBuilder('\Application\Event')->field('coordinates')->near(array(50,60))->getQuery()->execute();
 
/**
 
//The Index is in place
> db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "pfcd_dev.events", "name" : "_id_" }
{ "v" : 1, "key" : { "coordinates" : "2d" }, "ns" : "pfcd_dev.events", "name" : "coordinates_" }
 
// When I run this command
>db.events.find( { coordinates: { $near : [50,50] }} )
 
{ "_id" : ObjectId("4f14f363b2a87a7981000000"), "created_on" : ISODate("2012-01-17T04:04:50Z"), "updated_on" : ISODate("2012-01-17T04:04:50Z"), "coordinates" : { "latitude" : 50.700201, "longitude" : 60.470551 }, "status" : "live" }
 
 
/*********************************************************************************************
THE SOLUTION
Using this more specific query produced the results I was looking for
********************************************************************************************************/
 
$places = $this->dm->createQueryBuilder('\Application\Event')
->field('coordinates.latitude')->near(50)
->field('coordinates.longitude')->near(60)
->getQuery()->execute();

Is there a typo?

db.events.find( { coordinates: { $near : [50,50] }} )

should be

db.events.find( { coordinates: { $near : [50,60] }} )

No, because either way [50,60] or [50,50] the result is still the same. Doctrine returns no results... MongoShell returns numerous results.

I should also note that the following command

db.runCommand( { geoNear : "events" , near : [50,60], num : 1 } )

Returns

    {
        "ns" : "pfcd_dev.events",
        "near" : "1100110001001110110001001110110001001110110001001110",
        "results" : [
            {
                "dis" : 0.8436229513248203,
                "obj" : {
                    "_id" : ObjectId("4f14f363b2a87a7981000000"),
                    "created_on" : ISODate("2012-01-17T04:04:50Z"),
                    "updated_on" : ISODate("2012-01-17T04:04:50Z"),
                    "coordinates" : {
                        "latitude" : 50.700201,
                        "longitude" : 60.470551
                    },
                    "status" : "live"
                }
            }
        ],
        "stats" : {
            "time" : 3,
            "btreelocs" : 0,
            "nscanned" : 3,
            "objectsLoaded" : 2,
            "avgDistance" : 0.8436229513248203,
            "maxDistance" : 0.8436337898378685
        },
        "ok" : 1
    }

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.