/** * @param Point $center * @param Distance $distance * * @return BoundingBox * @author Maximilian Ruta <*****@*****.**> */ public static function createFromCenterAndDistance(Point $center, Distance $distance) { $distanceInDegrees = $distance->getInGeographicDegrees() / 2; $sw = new Point($center->getLatitude() - $distanceInDegrees, $center->getLongitude() - $distanceInDegrees); $ne = new Point($center->getLatitude() + $distanceInDegrees, $center->getLongitude() + $distanceInDegrees); return new BoundingBox($sw, $ne); }
/** * @param Point $place1 * @param Point $place2 * * @return Distance * @author Maximilian Ruta <*****@*****.**> */ public static function distance(Point $place1, Point $place2) { $theta = $place1->getLongitude() - $place2->getLongitude(); $dist = sin(deg2rad($place1->getLatitude())) * sin(deg2rad($place2->getLatitude())) + cos(deg2rad($place1->getLatitude())) * cos(deg2rad($place2->getLatitude())) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); return new Distance($dist); }
/** * @param Point $point * @return float * * thanks to http://stackoverflow.com/questions/7672759/how-to-calculate-distance-from-lat-long-in-php */ public function getCartographicDistance(Point $point) { $earthRadius = 3958.75; $dLat = deg2rad($point->getLatitude() - $this->latitude); $dLng = deg2rad($point->getLongitude() - $this->longitude); $a = sin($dLat / 2) * sin($dLat / 2) + cos(deg2rad($this->latitude)) * cos(deg2rad($point->getLatitude())) * sin($dLng / 2) * sin($dLng / 2); $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); $dist = $earthRadius * $c; // from miles to km $meterConversion = 1.609344; $geopointDistance = $dist * $meterConversion; return round($geopointDistance, 0); }
/** * Calculate distance to another point * * @param Point $point * @param string $unit * @return float */ public function distanceTo(Point $point, $unit = Gps::KILOMETER) { $latitudeFrom = deg2rad($this->latitude); $longitudeFrom = deg2rad($this->longitude); $latitudeTo = deg2rad(floatval($point->getLatitude())); $longitudeTo = deg2rad(floatval($this->getLongitude())); $longitudeDelta = $longitudeTo - $longitudeFrom; $longitudeDelta = $longitudeTo - $longitudeFrom; $magnitudeA = pow(cos($latitudeTo) * sin($longitudeDelta), 2) + pow(cos($latitudeFrom) * sin($latitudeTo) - sin($latitudeFrom) * cos($latitudeTo) * cos($longitudeDelta), 2); $magnitudeB = sin($latitudeFrom) * sin($latitudeTo) + cos($latitudeFrom) * cos($latitudeTo) * cos($longitudeDelta); $distance = atan2(sqrt($magnitudeA), $magnitudeB) * Gps::EARTH_RADIUS; switch ($unit) { case Gps::KILOMETER: return round($distance, 2); break; case Gps::METER: return round($distance * 1000, 2); break; default: throw new \InvalidArgumentException(sprintf('Unit "%s" is not valid', $unit)); break; } }
public function getAzimuthToPoint(Point $point2) { // перевести координаты в радианы $lat1 = $this->getLatitude() * M_PI / 180; $lat2 = $point2->getLatitude() * M_PI / 180; $long1 = $this->getLongitude() * M_PI / 180; $long2 = $point2->getLongitude() * M_PI / 180; // косинусы и синусы широт и разницы долгот $cl1 = cos($lat1); $cl2 = cos($lat2); $sl1 = sin($lat1); $sl2 = sin($lat2); $delta = $long2 - $long1; $cdelta = cos($delta); $sdelta = sin($delta); //вычисление начального азимута $x = (double) ($cl1 * $sl2) - $sl1 * $cl2 * $cdelta; $y = (double) $sdelta * $cl2; $z = atan2($y, $x) * 180 / M_PI; if ($z < 0) { $z += 360; } return $z; }
public function distanceTo(Point $point) : float { return Tools::distance($this->getLatitude(), $this->getLongitude(), $point->getLatitude(), $point->getLongitude()); }