namespace libgpx;
+use \ArrayAccess;
+use \Countable;
+use \IteratorAggregate;
+
/**
* A GPX trkseg type.
*
* @author Andy Street <andy@street.me.uk>
*/
-class TrackSegment implements Geographic
+class TrackSegment
+ implements Geographic, ArrayAccess, Countable, IteratorAggregate
{
/**
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;
+ }
+
+ /**
+ * Check if an offset holds a value.
+ *
+ * @see ArrayAccess::offsetExists
+ *
+ * @param mixed $offset The offset to search
+ * @return boolean
+ */
+ public function offsetExists($offset)
+ {
+ return $this->getPoints()->offsetExists($offset);
+ }
+
+ /**
+ * Fetch a point from this segment.
+ *
+ * @see ArrayAccess::offsetGet
+ *
+ * @param mixed $offset The offset to retrieve
+ * @return Point
+ */
+ public function offsetGet($offset)
+ {
+ return $this->getPoints()->offsetGet($offset);
+ }
+
+ /**
+ * Set the point at an offset.
+ *
+ * @see ArrayAccess::offsetSet
+ *
+ * @param mixed $offset Where to store the point.
+ * @param Point $value The point to store.
+ * @return void
+ */
+ public function offsetSet($offset, $value)
+ {
+ return $this->getPoints()->offsetSet($offset, $value);
+ }
+
+ /**
+ * Remove a point.
+ *
+ * @see ArrayAccess::offsetUnset
+ *
+ * @param mixed $offset Which point to remove
+ * @return void
+ */
+ public function offsetUnset($offset)
+ {
+ return $this->getPoints()->offsetUnset($offset);
+ }
+
+ /**
+ * Count the number of points in this track segment.
+ *
+ * @return int
+ */
+ public function count()
+ {
+ return ($this->points === null ? 0 : $this->points->count());
+ }
+
+ /**
+ * Fetch an iterator for the points contained in this segment.
+ *
+ * @return \Iterator
+ */
+ public function getIterator() {
+ return $this->getPoints();
+ }
+
}