Ejemplo n.º 1
0
 /**
  * Test for the geohash::quadrant() method.
  * 
  * @dataProvider geohashProvider
  * @param string $geohash
  */
 public function testQuadrant($geohash)
 {
     $len = strlen($geohash);
     $this->assertGreaterThan(0, $len);
     $point = geohash::decode($geohash);
     // test every precision from invalid to global
     for ($precision = $len + 1; $precision >= 0; $precision--) {
         $quadrant = geohash::quadrant($geohash, $precision);
         if ($precision >= $len) {
             // invalid precision, returns null
             $this->assertNull($quadrant);
         } else {
             $box = $precision > 0 ? geohash::decode_box(substr($geohash, 0, $precision)) : new geobox(new geopoint(90, 180), new geopoint(-90, -180));
             $center = $box->center();
             switch ($quadrant) {
                 case geohash::northwest:
                     $this->assertGreaterThanOrEqual($center->latitude, $point->latitude);
                     $this->assertLessThanOrEqual($center->longitude, $point->longitude);
                     break;
                 case geohash::northeast:
                     $this->assertGreaterThanOrEqual($center->latitude, $point->latitude);
                     $this->assertGreaterThanOrEqual($center->longitude, $point->longitude);
                     break;
                 case geohash::southwest:
                     $this->assertLessThanOrEqual($center->latitude, $point->latitude);
                     $this->assertLessThanOrEqual($center->longitude, $point->longitude);
                     break;
                 case geohash::southeast:
                     $this->assertLessThanOrEqual($center->latitude, $point->latitude);
                     $this->assertGreaterThanOrEqual($center->longitude, $point->longitude);
                     break;
             }
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Increase the size the of the circle.
  * @param int $amount
  * @return boolean success|failure
  */
 public function expand($amount = 1)
 {
     if ($this->precision == 1) {
         // currently unwilling to expand to cover the entire globe
         return false;
     }
     $this->precision = max($this->precision - $amount, 1);
     // shrink the geohash, grow the box
     $center = substr($this->center_geohash, 0, $this->precision);
     $box = geohash::decode_box($center);
     $dlat = $box->north - $box->south;
     $dlon = $box->east - $box->west;
     $set = new geohash_set();
     $set->add($center);
     $quadrant = geohash::quadrant($this->center_geohash, $this->precision);
     if ($quadrant == geohash::northeast || $quadrant == geohash::northwest) {
         // north side
         $north = geohash::neighbor($center, geohash::north);
         if ($north) {
             $set->add_set(geohash::halve($north, geohash::south));
             if ($quadrant == geohash::northeast) {
                 $set->add_set(geohash::quarter(geohash::neighbor($north, geohash::east), geohash::southwest));
             } else {
                 // northwest
                 $set->add_set(geohash::quarter(geohash::neighbor($north, geohash::west), geohash::southeast));
             }
             $box->north += $dlat / 2;
         }
     } else {
         // south side
         $south = geohash::neighbor($center, geohash::south);
         if ($south) {
             $set->add_set(geohash::halve($south, geohash::north));
             if ($quadrant == geohash::southeast) {
                 $set->add_set(geohash::quarter(geohash::neighbor($south, geohash::east), geohash::northwest));
             } else {
                 // southwest
                 $set->add_set(geohash::quarter(geohash::neighbor($south, geohash::west), geohash::northeast));
             }
             $box->south -= $dlat / 2;
         }
     }
     if ($quadrant == geohash::northeast || $quadrant == geohash::southeast) {
         // east side
         $set->add_set(geohash::halve(geohash::neighbor($center, geohash::east), geohash::west));
         $box->east += $dlon / 2;
     } else {
         // west side
         $set->add_set(geohash::halve(geohash::neighbor($center, geohash::west), geohash::east));
         $box->west -= $dlon / 2;
     }
     $this->geohash_set = $set;
     $this->max_radius = min($this->center->distance_to_longitude($box->east), $this->center->distance_to_longitude($box->west));
     if ($box->north < 90) {
         $this->max_radius = min($this->center->distance_to_latitude(min($box->north, 90)), $this->max_radius);
     }
     if ($box->south > -90) {
         $this->max_radius = min($this->center->distance_to_latitude(max($box->south, -90)), $this->max_radius);
     }
     return true;
 }