Ignore:
Timestamp:
2013-01-28T14:06:52+01:00 (11 years ago)
Author:
bastiK
Message:

add session support for marker layers (see #4029)

The data is exported to a separate GPX file that contains one waypoint for each marker.
This is not very elegant, because most of the time, all the info is already contained in the original GPX File.
However, when dealing with audio markers, they can be synchronized, or additional markers are added
at certain playback positions. This info must be retained.
Another complication is, that two or more MarkerLayers can be merged to one.

All these problems are avoided by explicitly exporting the markers to a separate file (as done in this commit).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/GpxWriter.java

    r5681 r5684  
    1111import java.util.Collection;
    1212import java.util.Map;
     13import java.util.Map.Entry;
    1314
    1415import org.openstreetmap.josm.data.Bounds;
    1516import org.openstreetmap.josm.data.coor.LatLon;
     17import org.openstreetmap.josm.data.gpx.Extensions;
    1618import org.openstreetmap.josm.data.gpx.GpxConstants;
    1719import org.openstreetmap.josm.data.gpx.GpxData;
     
    2022import org.openstreetmap.josm.data.gpx.GpxTrack;
    2123import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
     24import org.openstreetmap.josm.data.gpx.IWithAttributes;
    2225import org.openstreetmap.josm.data.gpx.WayPoint;
    2326
     
    4245    private GpxData data;
    4346    private String indent = "";
     47    public String creator = "JOSM GPX export";
    4448
    4549    private final static int WAY_POINT = 0;
     
    4953    public void write(GpxData data) {
    5054        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       
    5172        out.println("<?xml version='1.0' encoding='UTF-8'?>");
    5273        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) : "") +
    5475                "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" +
    5576                "    xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">");
     
    6384    }
    6485
    65     @SuppressWarnings("unchecked")
    66     private void writeAttr(Map<String, Object> attr) {
     86    private void writeAttr(IWithAttributes obj) {
    6787        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) {
    7293                        gpxLink(link);
    7394                    }
    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);
    76105                }
    77106            }
     
    157186        for (GpxRoute rte : data.routes) {
    158187            openln("rte");
    159             writeAttr(rte.attr);
     188            writeAttr(rte);
    160189            for (WayPoint pnt : rte.routePoints) {
    161190                wayPoint(pnt, ROUTE_POINT);
     
    168197        for (GpxTrack trk : data.tracks) {
    169198            openln("trk");
    170             writeAttr(trk.getAttributes());
     199            writeAttr(trk);
    171200            for (GpxTrackSegment seg : trk.getSegments()) {
    172201                openln("trkseg");
     
    259288            } else {
    260289                openAtt(type, coordAttr);
    261                 writeAttr(pnt.attr);
     290                writeAttr(pnt);
    262291                closeln(type);
    263292            }
    264293        }
    265294    }
     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    }
    266305}
Note: See TracChangeset for help on using the changeset viewer.