コード例 #1
0
 /**
  * Test geolib::expand().
  * This test isn't yet working for edge cases,
  * expanding to the original precision or to the minimum precision.
  * Perhaps those expansions don't make much sense.
  * 
  * @dataProvider geohashProvider
  * @param string $geohash
  */
 public function testCircle($geohash)
 {
     $circle = new geohash_circle($geohash);
     $center = geohash::decode($geohash);
     $last_radius = 0;
     // test all precisions up to 0
     do {
         $radius = $circle->max_radius() / EARTH_RADIUS;
         $circle->expand($geohash, $precision);
         // the center point should be in the set
         $this->assertTrue($circle->contains($center));
         // the following tests only work for points away from the poles where radiuses expand usefully
         if ($radius > 0) {
             $this->assertGreaterThan($last_radius, $radius);
             // all points box size x 1 distance from the center should be in the set
             $this->assertTrue($circle->contains(new geopoint(min($center->latitude + $radius, 90), self::normalize_longitude($center->longitude + $radius))));
             $this->assertTrue($circle->contains(new geopoint(min($center->latitude + $radius, 90), self::normalize_longitude($center->longitude - $radius))));
             $this->assertTrue($circle->contains(new geopoint(max($center->latitude - $radius, -90), self::normalize_longitude($center->longitude + $radius))));
             $this->assertTrue($circle->contains(new geopoint(max($center->latitude - $radius, -90), self::normalize_longitude($center->longitude - $radius))));
             // at least one point box size x 2 distance from the center should be outside the set
             /*
             $this->assertFalse($circle->contains(new geopoint(min($center->latitude + $radius*2, 90), $center->longitude + $radius*2)) &&
                                $circle->contains(new geopoint(min($center->latitude + $radius*2, 90), $center->longitude - $radius*2)) &&
                                $circle->contains(new geopoint(max($center->latitude - $radius*2, -90), $center->longitude + $radius*2)) &&
                                $circle->contains(new geopoint(max($center->latitude - $radius*2, -90), $center->longitude - $radius*2)));
             */
             $last_radius = $radius;
         }
     } while ($circle->expand());
 }
コード例 #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;
 }