source: josm/trunk/src/org/openstreetmap/josm/data/gpx/GpxData.java@ 1167

Last change on this file since 1167 was 655, checked in by ramack, 16 years ago

patch by bruce89, closes #812; thanks bruce

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1//License: GPLv2 or later. Copyright 2007 by Raphael Mack and others
2
3package org.openstreetmap.josm.data.gpx;
4
5import java.io.File;
6import java.util.Collection;
7import java.util.LinkedList;
8import java.util.Map;
9
10import org.openstreetmap.josm.data.Bounds;
11import 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 */
20public 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
104 /**
105 * calculates the sum of the lengths of all track segments
106 */
107 public double length(){
108 double result = 0.0; // in meters
109 WayPoint last = null;
110
111 for (GpxTrack trk : tracks) {
112 for (Collection<WayPoint> trkseg : trk.trackSegs) {
113 for (WayPoint tpt : trkseg) {
114 if(last != null){
115 result += calcDistance(last.latlon, tpt.latlon);
116 }
117 last = tpt;
118 }
119 last = null; // restart for each track segment
120 }
121 }
122 return result;
123 }
124
125 /**
126 * returns the distance in meters between two LatLons
127 */
128 public static double calcDistance(LatLon p1, LatLon p2){
129 double lat1, lon1, lat2, lon2;
130 double dlon, dlat;
131
132 lat1 = p1.lat() * Math.PI / 180.0;
133 lon1 = p1.lon() * Math.PI / 180.0;
134 lat2 = p2.lat() * Math.PI / 180.0;
135 lon2 = p2.lon() * Math.PI / 180.0;
136
137 dlon = lon2 - lon1;
138 dlat = lat2 - lat1;
139
140 double a = (Math.pow(Math.sin(dlat/2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon/2), 2));
141 double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
142 return 6367000 * c;
143 }
144
145}
Note: See TracBrowser for help on using the repository browser.