1 | //License: GPLv2 or later. Copyright 2007 by Raphael Mack and others
|
---|
2 |
|
---|
3 | package org.openstreetmap.josm.data.gpx;
|
---|
4 |
|
---|
5 | import java.io.File;
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.LinkedList;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.Bounds;
|
---|
11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Objects of this class represent a gpx file with tracks, waypoints and routes.
|
---|
15 | * It uses GPX v1.1, see {@link <a href="http://www.topografix.com/GPX/1/1/">the spec</a>}
|
---|
16 | * for details.
|
---|
17 | *
|
---|
18 | * @author Raphael Mack <ramack@raphael-mack.de>
|
---|
19 | */
|
---|
20 | public class GpxData extends WithAttributes {
|
---|
21 |
|
---|
22 | public static final String META_PREFIX = "meta.";
|
---|
23 | public static final String META_AUTHOR_NAME = META_PREFIX + "author.name";
|
---|
24 | public static final String META_AUTHOR_EMAIL = META_PREFIX + "author.email";
|
---|
25 | public static final String META_AUTHOR_LINK = META_PREFIX + "author.link";
|
---|
26 | public static final String META_COPYRIGHT_AUTHOR = META_PREFIX + "copyright.author";
|
---|
27 | public static final String META_COPYRIGHT_LICENSE = META_PREFIX + "copyright.license";
|
---|
28 | public static final String META_COPYRIGHT_YEAR = META_PREFIX + "copyright.year";
|
---|
29 | public static final String META_DESC = META_PREFIX + "desc";
|
---|
30 | public static final String META_KEYWORDS = META_PREFIX + "keywords";
|
---|
31 | public static final String META_LINKS = META_PREFIX + "links";
|
---|
32 | public static final String META_NAME = META_PREFIX + "name";
|
---|
33 | public static final String META_TIME = META_PREFIX + "time";
|
---|
34 |
|
---|
35 | public File storageFile;
|
---|
36 | public boolean fromServer;
|
---|
37 |
|
---|
38 | public Collection<GpxTrack> tracks = new LinkedList<GpxTrack>();
|
---|
39 | public Collection<GpxRoute> routes = new LinkedList<GpxRoute>();
|
---|
40 | public Collection<WayPoint> waypoints = new LinkedList<WayPoint>();
|
---|
41 |
|
---|
42 | @SuppressWarnings("unchecked")
|
---|
43 | public void mergeFrom(GpxData other) {
|
---|
44 | if (storageFile == null && other.storageFile != null) {
|
---|
45 | storageFile = other.storageFile;
|
---|
46 | }
|
---|
47 | fromServer = fromServer && other.fromServer;
|
---|
48 |
|
---|
49 | for (Map.Entry<String, Object> ent : other.attr.entrySet()) {
|
---|
50 | // TODO: Detect conflicts.
|
---|
51 | String k = ent.getKey();
|
---|
52 | if (k.equals(META_LINKS) && attr.containsKey(META_LINKS)) {
|
---|
53 | ((Collection<GpxLink>) attr.get(META_LINKS)).addAll(
|
---|
54 | (Collection<GpxLink>) ent.getValue());
|
---|
55 | } else {
|
---|
56 | attr.put(k, ent.getValue());
|
---|
57 | }
|
---|
58 | }
|
---|
59 | tracks.addAll(other.tracks);
|
---|
60 | routes.addAll(other.routes);
|
---|
61 | waypoints.addAll(other.waypoints);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public boolean hasTrackPoints() {
|
---|
65 | for (GpxTrack trk : tracks) {
|
---|
66 | for (Collection<WayPoint> trkseg : trk.trackSegs) {
|
---|
67 | if (!trkseg.isEmpty())
|
---|
68 | return true;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public boolean hasRoutePoints() {
|
---|
75 | for (GpxRoute rte : routes) {
|
---|
76 | if (!rte.routePoints.isEmpty())
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 | return false;
|
---|
80 | }
|
---|
81 |
|
---|
82 | // FIXME might perhaps use visitor pattern?
|
---|
83 | public Bounds recalculateBounds() {
|
---|
84 | Bounds bounds = null;
|
---|
85 | for (WayPoint wpt : waypoints) {
|
---|
86 | if (bounds == null) {
|
---|
87 | bounds = new Bounds(wpt.getCoor());
|
---|
88 | } else {
|
---|
89 | bounds.extend(wpt.getCoor());
|
---|
90 | }
|
---|
91 | }
|
---|
92 | for (GpxRoute rte : routes) {
|
---|
93 | for (WayPoint wpt : rte.routePoints) {
|
---|
94 | if (bounds == null) {
|
---|
95 | bounds = new Bounds(wpt.getCoor());
|
---|
96 | } else {
|
---|
97 | bounds.extend(wpt.getCoor());
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 | for (GpxTrack trk : tracks) {
|
---|
102 | for (Collection<WayPoint> trkseg : trk.trackSegs) {
|
---|
103 | for (WayPoint wpt : trkseg) {
|
---|
104 | if (bounds == null) {
|
---|
105 | bounds = new Bounds(wpt.getCoor());
|
---|
106 | } else {
|
---|
107 | bounds.extend(wpt.getCoor());
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | return bounds;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * calculates the sum of the lengths of all track segments
|
---|
117 | */
|
---|
118 | public double length(){
|
---|
119 | double result = 0.0; // in meters
|
---|
120 | WayPoint last = null;
|
---|
121 |
|
---|
122 | for (GpxTrack trk : tracks) {
|
---|
123 | for (Collection<WayPoint> trkseg : trk.trackSegs) {
|
---|
124 | for (WayPoint tpt : trkseg) {
|
---|
125 | if(last != null){
|
---|
126 | result += last.getCoor().greatCircleDistance(tpt.getCoor());
|
---|
127 | }
|
---|
128 | last = tpt;
|
---|
129 | }
|
---|
130 | last = null; // restart for each track segment
|
---|
131 | }
|
---|
132 | }
|
---|
133 | return result;
|
---|
134 | }
|
---|
135 | }
|
---|