| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.data.gpx; |
|---|
| 3 | |
|---|
| 4 | import java.util.ArrayList; |
|---|
| 5 | import java.util.Collection; |
|---|
| 6 | import java.util.Collections; |
|---|
| 7 | |
|---|
| 8 | import org.openstreetmap.josm.data.Bounds; |
|---|
| 9 | |
|---|
| 10 | public class ImmutableGpxTrackSegment implements GpxTrackSegment { |
|---|
| 11 | |
|---|
| 12 | private final Collection<WayPoint> wayPoints; |
|---|
| 13 | private final Bounds bounds; |
|---|
| 14 | private final double length; |
|---|
| 15 | |
|---|
| 16 | public ImmutableGpxTrackSegment(Collection<WayPoint> wayPoints) { |
|---|
| 17 | this.wayPoints = Collections.unmodifiableCollection(new ArrayList<WayPoint>(wayPoints)); |
|---|
| 18 | this.bounds = calculateBounds(); |
|---|
| 19 | this.length = calculateLength(); |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | private Bounds calculateBounds() { |
|---|
| 23 | Bounds result = null; |
|---|
| 24 | for (WayPoint wpt: wayPoints) { |
|---|
| 25 | if (result == null) { |
|---|
| 26 | result = new Bounds(wpt.getCoor()); |
|---|
| 27 | } else { |
|---|
| 28 | result.extend(wpt.getCoor()); |
|---|
| 29 | } |
|---|
| 30 | } |
|---|
| 31 | return result; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | private double calculateLength() { |
|---|
| 35 | double result = 0.0; // in meters |
|---|
| 36 | WayPoint last = null; |
|---|
| 37 | for (WayPoint tpt : wayPoints) { |
|---|
| 38 | if(last != null){ |
|---|
| 39 | Double d = last.getCoor().greatCircleDistance(tpt.getCoor()); |
|---|
| 40 | if(!d.isNaN() && !d.isInfinite()) { |
|---|
| 41 | result += d; |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | last = tpt; |
|---|
| 45 | } |
|---|
| 46 | return result; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | public Bounds getBounds() { |
|---|
| 50 | if (bounds == null) |
|---|
| 51 | return null; |
|---|
| 52 | else |
|---|
| 53 | return new Bounds(bounds); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | public Collection<WayPoint> getWayPoints() { |
|---|
| 57 | return wayPoints; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | public double length() { |
|---|
| 61 | return length; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | public int getUpdateCount() { |
|---|
| 65 | return 0; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | } |
|---|