]> git.street.me.uk Git - andy/gpx.git/blobdiff - src/libgpx/tracksegment.php
Rewrite GPXReader class
[andy/gpx.git] / src / libgpx / tracksegment.php
index ccb30789a807858987ee2c94d5b68ae553a4b7ab..92ea5ea69f1d867fcf0ab05d2b4bd4a0a7853bfe 100644 (file)
 
 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
 {
 
   /**
@@ -90,4 +95,101 @@ class TrackSegment implements Geographic
     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();
+  }
+
 }