12. Geospatial Queries

You can execute some special queries when using geospatial indexes like checking for documents within a rectangle or circle.

12.1. Mapping

First, setup some documents like the following:

  • PHP
    <?php
    
    /**
     * @Document
     * @Index(keys={"coordinates"="2d"})
     */
    class City
    {
        /** @Id */
        public $id;
    
        /** @String */
        public $name;
    
        /** @EmbedOne(targetDocument="Coordinates") */
        public $coordinates;
    
        /** @Distance */
        public $distance;
    }
    
    /** @EmbeddedDocument */
    class Coordinates
    {
        /** @Float */
        public $latitude;
    
        /** @Float */
        public $longitude;
    }
    
  • XML
    <indexes>
        <index>
            <key name="coordinates" value="2d" />
        </index>
    </indexes>
    
  • YAML
    indexes:
      coordinates:
        keys:
          coordinates: 2d
    

12.2. Near Query

Now you can execute queries against these documents like the following. Check for the 10 nearest cities to a given latitude and longitude with the near($latitude, $longitude) method:

<?php

$cities = $this->dm->createQuery('City')
    ->field('coordinates')->near(50, 60)
    ->execute();

12.3. Distance

When you use the near() functionality a distance will be calculated and placed in the property annotated with @Distance:

<?php

foreach ($cities as $city) {
    echo $city->name.': '.$city->distance."\n";
}

12.4. Within Box

You can also query for cities within a given rectangle using the withinBox($x1, $y1, $x2, $y2) method:

<?php

$cities = $this->dm->createQuery('City')
    ->field('coordinates')->withinBox(41, 41, 72, 72)
    ->execute();

12.5. Within Center

In addition to boxes you can check for cities within a circle using the withinCenter($x, $y, $radius) method:

<?php

$cities = $this->dm->createQuery('City')
    ->field('coordinates')->withinCenter(50, 50, 20)
    ->execute();

Table Of Contents

Previous topic

11. Change Tracking Policies

Next topic

13. Events

This Page