* null if not applicable.
*/
public function getBounds();
+
+ /**
+ * Fetch the length of a geographic element.
+ *
+ * @return float The length in meters.
+ */
+ public function getLength();
+
}
return $result;
}
+ /**
+ * Fetch the length of all features in this GPX file.
+ *
+ * @return float The length in meters.
+ */
+ public function getLength()
+ {
+ $length = 0;
+ $lists = [
+ $this->routes,
+ $this->tracks
+ ];
+ foreach ($lists as $list) {
+ if ($list === null) continue;
+ foreach ($list as $geo) {
+ $length += $geo->getLength();
+ }
+ }
+ return $length;
+ }
+
}
self::$initialized = true;
}
+ /**
+ * Measure the distance between two points.
+ *
+ * Haversine formula, great circle distance.
+ *
+ * @param float $lat1 The latitude of the first point.
+ * @param float $lon1 The longitude of the first point.
+ * @param float $lat2 The latitude of the second point.
+ * @param float $lon2 The longitude of the second point.
+ * @return float The distance in meters.
+ */
+ public static function distance(
+ float $lat1,
+ float $lon1,
+ float $lat2,
+ float $lon2
+ ) {
+ $deg2rad = pi() / 180;
+ $r = 6371000; // Radius of the Earth in meters
+ $dlat = ($lat2 - $lat1) * $deg2rad;
+ $dlon = ($lon2 - $lon1) * $deg2rad;
+ $a =
+ sin($dlat / 2) * sin($dlat / 2) +
+ cos($lat1 * $deg2rad) * cos($lat2 * $deg2rad) *
+ sin($dlon / 2) * sin($dlon / 2);
+ $c = 2 * atan2(sqrt($a), sqrt(1-$a));
+ return $r * $c;
+ }
+
+ /**
+ * Convert meters in to miles.
+ *
+ * @see https://en.wikipedia.org/wiki/Mile
+ *
+ * @param float $meters A distance in meters.
+ * @return float A distance in miles.
+ */
+ public static function meters2miles(float $meters)
+ {
+ return $meters / 1609.344;
+ }
+
}
{
return new Bounds($this->lat, $this->lon, $this->lat, $this->lon);
}
+
+ /**
+ * Fetch the length of a point.
+ *
+ * Note: Length is the distance between two points so calling this function
+ * on a single point makes little sense. It is implemented to complete
+ * the `Geographic` interface and will always return a value of 0.
+ *
+ * @return float The length in meters.
+ */
+ public function getLength()
+ {
+ return 0.0;
+ }
+
+ /**
+ * Find the distance to another point.
+ *
+ * @param Point $point The point to find the distance to.
+ * @return float The distance in meters.
+ */
+ public function distanceTo(Point $point)
+ {
+ return libgpx::distance(
+ $this->lat,
+ $this->lon,
+ $point->getLatitude(),
+ $point->getLongitude()
+ );
+ }
+
}
return $result;
}
+ /**
+ * Fetch the length of a route.
+ *
+ * @return float The length in meters.
+ */
+ public function getLength()
+ {
+ $length = 0;
+ $points = $this->getPoints(false);
+ if ($points !== null && !$points->isEmpty()) {
+ $last_point = null;
+ foreach ($points as $current_point) {
+ if ($last_point !== null)
+ $length += libgpx::distance(
+ $last_point->getLatitude(),
+ $last_point->getLongitude(),
+ $current_point->getLatitude(),
+ $current_point->getLongitude()
+ );
+ $last_point = $current_point;
+ }
+ }
+ return $length;
+ }
+
}
return $result;
}
+ /**
+ * Fetch the length of a track.
+ *
+ * @return float The length in meters.
+ */
+ public function getLength()
+ {
+ $length = 0;
+ $segments = $this->getSegments(false);
+ if ($segments !== null) {
+ foreach ($segments as $segment) {
+ $length += $segment->getLength();
+ }
+ }
+ return $length;
+ }
+
}
return $result;
}
+ /**
+ * Fetch the length of a track segment.
+ *
+ * @return float The length in meters.
+ */
+ public function getLength()
+ {
+ $length = 0;
+ $points = $this->getPoints(false);
+ if ($points !== null && !$points->isEmpty()) {
+ $last_point = null;
+ foreach ($points as $current_point) {
+ if ($last_point !== null)
+ $length += libgpx::distance(
+ $last_point->getLatitude(),
+ $last_point->getLongitude(),
+ $current_point->getLatitude(),
+ $current_point->getLongitude()
+ );
+ $last_point = $current_point;
+ }
+ }
+ return $length;
+ }
+
}