1 | //License: GPLv2 or later. Copyright 2007 by Raphael Mack and others
|
---|
2 |
|
---|
3 | package org.openstreetmap.josm.data.gpx;
|
---|
4 |
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.LinkedList;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import org.openstreetmap.josm.data.Bounds;
|
---|
10 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
11 | import java.lang.Math;
|
---|
12 | import java.io.File;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * objects of this class represent a gpx file with tracks, waypoints and routes
|
---|
16 | * it uses GPX1.1 see http://www.topografix.com/GPX/1/1/ for details
|
---|
17 | *
|
---|
18 | * @author Raphael Mack <ramack@raphael-mack.de>
|
---|
19 | */
|
---|
20 | public class GpxData extends WithAttributes {
|
---|
21 | public File storageFile;
|
---|
22 | public boolean fromServer;
|
---|
23 |
|
---|
24 | public Collection<GpxTrack> tracks = new LinkedList<GpxTrack>();
|
---|
25 | public Collection<GpxRoute> routes = new LinkedList<GpxRoute>();
|
---|
26 | public Collection<WayPoint> waypoints = new LinkedList<WayPoint>();
|
---|
27 |
|
---|
28 | public Bounds bounds;
|
---|
29 |
|
---|
30 | public void mergeFrom(GpxData other) {
|
---|
31 | if (storageFile == null && other.storageFile != null) {
|
---|
32 | storageFile = other.storageFile;
|
---|
33 | }
|
---|
34 | fromServer = fromServer && other.fromServer;
|
---|
35 |
|
---|
36 | for (Map.Entry<String, Object> ent : other.attr.entrySet()) {
|
---|
37 | // TODO: Detect conflicts.
|
---|
38 | String k = ent.getKey();
|
---|
39 | if (k.equals("link") && attr.containsKey("link")) {
|
---|
40 | ((Collection<GpxLink>) attr.get("link")).addAll(
|
---|
41 | (Collection<GpxLink>) ent.getValue());
|
---|
42 | } else {
|
---|
43 | attr.put(k, ent.getValue());
|
---|
44 | }
|
---|
45 | }
|
---|
46 | tracks.addAll(other.tracks);
|
---|
47 | routes.addAll(other.routes);
|
---|
48 | waypoints.addAll(other.waypoints);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public boolean hasTrackPoints() {
|
---|
52 | for (GpxTrack trk : tracks) {
|
---|
53 | for (Collection<WayPoint> trkseg : trk.trackSegs) {
|
---|
54 | if (!trkseg.isEmpty())
|
---|
55 | return true;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public boolean hasRoutePoints() {
|
---|
62 | for (GpxRoute rte : routes) {
|
---|
63 | if (!rte.routePoints.isEmpty())
|
---|
64 | return true;
|
---|
65 | }
|
---|
66 | return false;
|
---|
67 | }
|
---|
68 |
|
---|
69 | // FIXME might perhaps use visitor pattern?
|
---|
70 | public void recalculateBounds() {
|
---|
71 | bounds = null;
|
---|
72 | for (WayPoint wpt : waypoints) {
|
---|
73 | if (bounds == null) {
|
---|
74 | bounds = new Bounds(wpt.latlon, wpt.latlon);
|
---|
75 | } else {
|
---|
76 | bounds.extend(wpt.latlon);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | for (GpxRoute rte : routes) {
|
---|
80 | for (WayPoint wpt : rte.routePoints) {
|
---|
81 | if (bounds == null) {
|
---|
82 | bounds = new Bounds(wpt.latlon, wpt.latlon);
|
---|
83 | } else {
|
---|
84 | bounds.extend(wpt.latlon);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | for (GpxTrack trk : tracks) {
|
---|
89 | for (Collection<WayPoint> trkseg : trk.trackSegs) {
|
---|
90 | for (WayPoint wpt : trkseg) {
|
---|
91 | if (bounds == null) {
|
---|
92 | bounds = new Bounds(wpt.latlon, wpt.latlon);
|
---|
93 | } else {
|
---|
94 | bounds.extend(wpt.latlon);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | if (bounds == null) {
|
---|
100 | bounds = new Bounds();
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|