Changeset 5684 in josm for trunk/src/org/openstreetmap/josm/io/GpxWriter.java
- Timestamp:
- 2013-01-28T14:06:52+01:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/GpxWriter.java
r5681 r5684 11 11 import java.util.Collection; 12 12 import java.util.Map; 13 import java.util.Map.Entry; 13 14 14 15 import org.openstreetmap.josm.data.Bounds; 15 16 import org.openstreetmap.josm.data.coor.LatLon; 17 import org.openstreetmap.josm.data.gpx.Extensions; 16 18 import org.openstreetmap.josm.data.gpx.GpxConstants; 17 19 import org.openstreetmap.josm.data.gpx.GpxData; … … 20 22 import org.openstreetmap.josm.data.gpx.GpxTrack; 21 23 import org.openstreetmap.josm.data.gpx.GpxTrackSegment; 24 import org.openstreetmap.josm.data.gpx.IWithAttributes; 22 25 import org.openstreetmap.josm.data.gpx.WayPoint; 23 26 … … 42 45 private GpxData data; 43 46 private String indent = ""; 47 public String creator = "JOSM GPX export"; 44 48 45 49 private final static int WAY_POINT = 0; … … 49 53 public void write(GpxData data) { 50 54 this.data = data; 55 // We write JOSM specific meta information into gpx 'extensions' elements. 56 // In particular it is noted whether the gpx data is from the OSM server 57 // (so the rendering of clouds of anonymous TrackPoints can be improved) 58 // and some extra synchronization info for export of AudioMarkers. 59 // It is checked in advance, if any extensions are used, so we know whether 60 // a namespace declaration is necessary. 61 boolean hasExtensions = data.fromServer; 62 if (!hasExtensions) { 63 for (WayPoint wpt : data.waypoints) { 64 Extensions extensions = (Extensions) wpt.get(META_EXTENSIONS); 65 if (extensions != null && !extensions.isEmpty()) { 66 hasExtensions = true; 67 break; 68 } 69 } 70 } 71 51 72 out.println("<?xml version='1.0' encoding='UTF-8'?>"); 52 73 out.println("<gpx version=\"1.1\" creator=\"JOSM GPX export\" xmlns=\"http://www.topografix.com/GPX/1/1\"\n" + 53 ( data.fromServer? String.format(" xmlns:josm=\"%s\"\n", JOSM_EXTENSIONS_NAMESPACE_URI) : "") +74 (hasExtensions ? String.format(" xmlns:josm=\"%s\"\n", JOSM_EXTENSIONS_NAMESPACE_URI) : "") + 54 75 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + 55 76 " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">"); … … 63 84 } 64 85 65 @SuppressWarnings("unchecked") 66 private void writeAttr(Map<String, Object> attr) { 86 private void writeAttr(IWithAttributes obj) { 67 87 for (String key : WPT_KEYS) { 68 Object value = attr.get(key); 69 if (value != null) { 70 if (key.equals(META_LINKS)) { 71 for (GpxLink link : (Collection<GpxLink>) value) { 88 if (key.equals(META_LINKS)) { 89 @SuppressWarnings("unchecked") 90 Collection<GpxLink> lValue = obj.getCollection(key); 91 if (lValue != null) { 92 for (GpxLink link : lValue) { 72 93 gpxLink(link); 73 94 } 74 } else { 75 simpleTag(key, value.toString()); 95 } 96 } else if (key.equals(META_EXTENSIONS)) { 97 Extensions extensions = (Extensions) obj.get(key); 98 if (extensions != null) { 99 gpxExtensions(extensions); 100 } 101 } else { 102 String value = obj.getString(key); 103 if (value != null) { 104 simpleTag(key, value); 76 105 } 77 106 } … … 157 186 for (GpxRoute rte : data.routes) { 158 187 openln("rte"); 159 writeAttr(rte .attr);188 writeAttr(rte); 160 189 for (WayPoint pnt : rte.routePoints) { 161 190 wayPoint(pnt, ROUTE_POINT); … … 168 197 for (GpxTrack trk : data.tracks) { 169 198 openln("trk"); 170 writeAttr(trk .getAttributes());199 writeAttr(trk); 171 200 for (GpxTrackSegment seg : trk.getSegments()) { 172 201 openln("trkseg"); … … 259 288 } else { 260 289 openAtt(type, coordAttr); 261 writeAttr(pnt .attr);290 writeAttr(pnt); 262 291 closeln(type); 263 292 } 264 293 } 265 294 } 295 296 private void gpxExtensions(Extensions extensions) { 297 if (extensions != null && !extensions.isEmpty()) { 298 openln("extensions"); 299 for (Entry<String, String> e : extensions.entrySet()) { 300 simpleTag("josm:" + e.getKey(), e.getValue()); 301 } 302 closeln("extensions"); 303 } 304 } 266 305 }
Note:
See TracChangeset
for help on using the changeset viewer.