You are here

public function LineString::greatCircleLength in geoPHP 7

Same name and namespace in other branches
  1. 8 geoPHP/lib/geometry/LineString.class.php \LineString::greatCircleLength()

Overrides Collection::greatCircleLength

File

geoPHP/lib/geometry/LineString.class.php, line 78

Class

LineString
LineString. A collection of Points representing a line. A line can have more than one segment.

Code

public function greatCircleLength($radius = 6378137) {
  $length = 0;
  $points = $this
    ->getPoints();
  for ($i = 0; $i < $this
    ->numPoints() - 1; $i++) {
    $point = $points[$i];
    $next_point = $points[$i + 1];
    if (!is_object($next_point)) {
      continue;
    }

    // Great circle method
    $lat1 = deg2rad($point
      ->getY());
    $lat2 = deg2rad($next_point
      ->getY());
    $lon1 = deg2rad($point
      ->getX());
    $lon2 = deg2rad($next_point
      ->getX());
    $dlon = $lon2 - $lon1;
    $length += $radius * atan2(sqrt(pow(cos($lat2) * sin($dlon), 2) + pow(cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($dlon), 2)), sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($dlon));
  }

  // Returns length in meters.
  return $length;
}