source: josm/trunk/src/org/openstreetmap/josm/data/gpx/ImmutableGpxTrackSegment.java @ 5241

Revision 5170, 1.7 KB checked in by Don-vip, 6 weeks ago (diff)

cleanup svn:mime-type properties preventing Java sources from being viewed as such on Trac

  • Property svn:eol-style set to native
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.gpx;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.Collections;
7
8import org.openstreetmap.josm.data.Bounds;
9
10public 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}
Note: See TracBrowser for help on using the repository browser.