5 * Copyright 2018 Andy Street <andy@street.me.uk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
32 * @see https://www.topografix.com/GPX/1/1/#type_rteType
34 * @author Andy Street <andy@street.me.uk>
36 class Route extends DataType implements Geographic
47 * A list of route points.
49 * @var DoublyLinkedList (Point)
54 * Fetch the route number.
56 * @return int|null The route number or null if not set.
58 public function getNumber()
64 * Set the number of the route.
66 * @param int $number The route number or null to delete.
68 * @throws DomainException If the number is < 0
70 public function setNumber(int $number = null)
72 if ($number !== null && $number < 0)
73 throw new DomainException(
74 sprintf('Number must be >= 0 but got "%s"', $number)
76 $this->number = $number;
80 * Fetch an ordered list of route points.
82 * @param boolean $create Create the list if it does not already exist.
83 * @return TypedDoublyLinkedList|null A list of route points.
85 public function getPoints(bool $create = true)
87 if ($create && $this->points === null)
88 $this->points = new TypedDoublyLinkedList('libgpx\Point');
93 * Fetch a bounding box that covers the feature.
95 * @return Bounds|null A bounding box covering the extent of the feature or
96 * null if not applicable.
98 public function getBounds()
101 if ($this->points !== null) {
102 foreach ($this->points as $point) {
103 $bounds = $point->getBounds();
104 $result = ($result === null ? $bounds : $result->extend($bounds));
111 * Fetch the length of a route.
113 * @return float The length in meters.
115 public function getLength()
118 $points = $this->getPoints(false);
119 if ($points !== null && !$points->isEmpty()) {
121 foreach ($points as $current_point) {
122 if ($last_point !== null)
123 $length += libgpx::distance(
124 $last_point->getLatitude(),
125 $last_point->getLongitude(),
126 $current_point->getLatitude(),
127 $current_point->getLongitude()
129 $last_point = $current_point;