* @package WB * @subpackage geo */ WBClass::load('WBGeo'); /** * Geo Information Tools: Neighbour * * @todo this is alpha * @version 0.2.0 * @package WB * @subpackage geo */ class WBGeo_Neighbour extends WBStdClass { /** * Maxiumum Distance Of Neighbour * @var int */ private $maxDistance = 1000; /** * Center Point * @var WBGeo_Point */ private $center; /** * Position of Bounding Box Edges * * North, east, south, west * * @var array */ private $bounding = array(0,0,0,0); /** * Constructor * * @param array */ public function __construct($parameter = array()) { } public function setCenter(WBGeo_Point $point) { $this->center = $point; $this->updateBoundings(); } /** * Set Maxiumum Distance * * @param int meter */ public function setMaxDistance($distance = 1000) { $this->maxDistance = $distance; $this->updateBoundings(); } /** * Is Point in Neighbourhood? * * See if point's coordinates are within bounding box * * @param WBGeo_Point * @return bool */ public function check(WBGeo_Point $point) { $lat = $point->getLatitude(false); if ($lat > $this->bounding[0] || $lat < $this->bounding[2]) { return false; } $lon = $point->getLongitude(false); if ($lon > $this->bounding[1] || $lon < $this->bounding[3]) { return false; } return true; } /** * Get Bounding Box Coordinates * * @param bool convert to deg * @return array */ public function getBoundingBoxCoordinates($deg = true) { if (!$deg) { return $this->bounding; } $box = array(); foreach ($this->bounding as $b) { $box[] = WBGeo::rad2deg($b); } return $box; } /** * Recalculate Bounding Box * * @todo juggle with negative values? */ private function updateBoundings() { $this->bounding = array(0,0,0,0); if (empty($this->center)) { return; } $lat = $this->center->getLatitude(false); $lon = $this->center->getLongitude(false); // north / south offset $cir = WBGeo::EARTH_RADIUS * 2 * M_PI; $offset = 2 * M_PI * $this->maxDistance / $cir; $this->bounding[0] = $lat + $offset; $this->bounding[2] = $lat - $offset; $rad = cos($lat) * WBGeo::EARTH_RADIUS; $cir = $rad * 2 * M_PI; $offset = 2 * M_PI * $this->maxDistance / $cir; $this->bounding[1] = $lon + $offset; $this->bounding[3] = $lon - $offset; // swap if ($this->bounding[0] > $this->bounding[2]) { $tmp = $this->bounding[0]; $this->bounding[0] = $this->bounding[2]; $this->bounding[2] = $tmp; } if ($this->bounding[1] > $this->bounding[3]) { $tmp = $this->bounding[1]; $this->bounding[1] = $this->bounding[3]; $this->bounding[3] = $tmp; } } }