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 | import java.util.HashMap;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Map;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.Bounds;
|
---|
12 |
|
---|
13 | public class ImmutableGpxTrack extends WithAttributes implements GpxTrack {
|
---|
14 |
|
---|
15 | private final Collection<GpxTrackSegment> segments;
|
---|
16 | private final double length;
|
---|
17 | private final Bounds bounds;
|
---|
18 |
|
---|
19 | public ImmutableGpxTrack(Collection<Collection<WayPoint>> trackSegs, Map<String, Object> attributes) {
|
---|
20 | List<GpxTrackSegment> newSegments = new ArrayList<GpxTrackSegment>();
|
---|
21 | for (Collection<WayPoint> trackSeg: trackSegs) {
|
---|
22 | if (trackSeg != null && !trackSeg.isEmpty()) {
|
---|
23 | newSegments.add(new ImmutableGpxTrackSegment(trackSeg));
|
---|
24 | }
|
---|
25 | }
|
---|
26 | this.attr = Collections.unmodifiableMap(new HashMap<String, Object>(attributes));
|
---|
27 | this.segments = Collections.unmodifiableCollection(newSegments);
|
---|
28 | this.length = calculateLength();
|
---|
29 | this.bounds = calculateBounds();
|
---|
30 | }
|
---|
31 |
|
---|
32 | private double calculateLength(){
|
---|
33 | double result = 0.0; // in meters
|
---|
34 |
|
---|
35 | for (GpxTrackSegment trkseg : segments) {
|
---|
36 | result += trkseg.length();
|
---|
37 | }
|
---|
38 | return result;
|
---|
39 | }
|
---|
40 |
|
---|
41 | private Bounds calculateBounds() {
|
---|
42 | Bounds result = null;
|
---|
43 | for (GpxTrackSegment segment: segments) {
|
---|
44 | Bounds segBounds = segment.getBounds();
|
---|
45 | if (segBounds != null) {
|
---|
46 | if (result == null) {
|
---|
47 | result = new Bounds(segBounds);
|
---|
48 | } else {
|
---|
49 | result.extend(segBounds);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 | return result;
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public Map<String, Object> getAttributes() {
|
---|
58 | return attr;
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public Bounds getBounds() {
|
---|
63 | if (bounds == null)
|
---|
64 | return null;
|
---|
65 | else
|
---|
66 | return new Bounds(bounds);
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public double length() {
|
---|
71 | return length;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public Collection<GpxTrackSegment> getSegments() {
|
---|
76 | return segments;
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public int getUpdateCount() {
|
---|
81 | return 0;
|
---|
82 | }
|
---|
83 | }
|
---|