You are here

public function LineString::length in geoPHP 7

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

Overrides Collection::length

File

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

Class

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

Code

public function length() {
  if ($this
    ->geos()) {
    return $this
      ->geos()
      ->length();
  }
  $length = 0;
  foreach ($this
    ->getPoints() as $delta => $point) {
    $previous_point = $this
      ->geometryN($delta);
    if ($previous_point) {
      $length += sqrt(pow($previous_point
        ->getX() - $point
        ->getX(), 2) + pow($previous_point
        ->getY() - $point
        ->getY(), 2));
    }
  }
  return $length;
}