source: josm/trunk/src/org/openstreetmap/josm/actions/relation/ExportRelationToGpxAction.java@ 15496

Last change on this file since 15496 was 15496, checked in by Don-vip, 4 years ago

fix #16796 - Rework of GPX track colors / layer preferences (patch by Bjoeni)

  • Property svn:eol-style set to native
File size: 8.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.relation;
3
4import static org.openstreetmap.josm.actions.relation.ExportRelationToGpxAction.Mode.FROM_FIRST_MEMBER;
5import static org.openstreetmap.josm.actions.relation.ExportRelationToGpxAction.Mode.TO_FILE;
6import static org.openstreetmap.josm.actions.relation.ExportRelationToGpxAction.Mode.TO_LAYER;
7import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
8import static org.openstreetmap.josm.tools.I18n.tr;
9
10import java.awt.event.ActionEvent;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Collection;
14import java.util.Collections;
15import java.util.EnumSet;
16import java.util.HashMap;
17import java.util.Iterator;
18import java.util.LinkedList;
19import java.util.List;
20import java.util.Map;
21import java.util.Set;
22import java.util.Stack;
23import java.util.concurrent.TimeUnit;
24
25import org.openstreetmap.josm.actions.GpxExportAction;
26import org.openstreetmap.josm.actions.IPrimitiveAction;
27import org.openstreetmap.josm.data.gpx.GpxData;
28import org.openstreetmap.josm.data.gpx.GpxTrack;
29import org.openstreetmap.josm.data.gpx.WayPoint;
30import org.openstreetmap.josm.data.osm.IPrimitive;
31import org.openstreetmap.josm.data.osm.Node;
32import org.openstreetmap.josm.data.osm.Relation;
33import org.openstreetmap.josm.data.osm.RelationMember;
34import org.openstreetmap.josm.gui.MainApplication;
35import org.openstreetmap.josm.gui.dialogs.relation.sort.WayConnectionType;
36import org.openstreetmap.josm.gui.dialogs.relation.sort.WayConnectionTypeCalculator;
37import org.openstreetmap.josm.gui.layer.GpxLayer;
38import org.openstreetmap.josm.gui.layer.Layer;
39import org.openstreetmap.josm.gui.layer.OsmDataLayer;
40import org.openstreetmap.josm.tools.SubclassFilteredCollection;
41
42/**
43 * Exports the current relation to a single GPX track,
44 * currently for type=route and type=superroute relations only.
45 *
46 * @since 13210
47 */
48public class ExportRelationToGpxAction extends GpxExportAction
49 implements IPrimitiveAction {
50
51 /** Enumeration of export variants */
52 public enum Mode {
53 /** concatenate members from first to last element */
54 FROM_FIRST_MEMBER,
55 /** concatenate members from last to first element */
56 FROM_LAST_MEMBER,
57 /** export to GPX layer and add to LayerManager */
58 TO_LAYER,
59 /** export to GPX file and open FileChooser */
60 TO_FILE
61 }
62
63 /** Mode of this ExportToGpxAction */
64 protected final Set<Mode> mode;
65
66 /** Primitives this action works on */
67 protected Collection<Relation> relations = Collections.<Relation>emptySet();
68
69 /** Construct a new ExportRelationToGpxAction with default mode */
70 public ExportRelationToGpxAction() {
71 this(EnumSet.of(FROM_FIRST_MEMBER, TO_FILE));
72 }
73
74 /**
75 * Constructs a new {@code ExportRelationToGpxAction}
76 *
77 * @param mode which mode to use, see {@code ExportRelationToGpxAction.Mode}
78 */
79 public ExportRelationToGpxAction(Set<Mode> mode) {
80 super(name(mode), mode.contains(TO_FILE) ? "exportgpx" : "dialogs/layerlist", tooltip(mode),
81 null, false, null, false);
82 setHelpId(ht("/Action/ExportRelationToGpx"));
83 this.mode = mode;
84 }
85
86 private static String name(Set<Mode> mode) {
87 if (mode.contains(TO_FILE)) {
88 if (mode.contains(FROM_FIRST_MEMBER)) {
89 return tr("Export GPX file starting from first member");
90 } else {
91 return tr("Export GPX file starting from last member");
92 }
93 } else {
94 if (mode.contains(FROM_FIRST_MEMBER)) {
95 return tr("Convert to GPX layer starting from first member");
96 } else {
97 return tr("Convert to GPX layer starting from last member");
98 }
99 }
100 }
101
102 private static String tooltip(Set<Mode> mode) {
103 if (mode.contains(FROM_FIRST_MEMBER)) {
104 return tr("Flatten this relation to a single gpx track recursively, " +
105 "starting with the first member, successively continuing to the last.");
106 } else {
107 return tr("Flatten this relation to a single gpx track recursively, " +
108 "starting with the last member, successively continuing to the first.");
109 }
110 }
111
112 @Override
113 protected Layer getLayer() {
114 List<RelationMember> flat = new ArrayList<>();
115
116 List<RelationMember> init = new ArrayList<>();
117 relations.forEach(t -> init.add(new RelationMember("", t)));
118
119 Stack<Iterator<RelationMember>> stack = new Stack<>();
120 stack.push(modeAwareIterator(init));
121
122 List<Relation> relsFound = new ArrayList<>();
123 do {
124 Iterator<RelationMember> i = stack.peek();
125 if (!i.hasNext())
126 stack.pop();
127 while (i.hasNext()) {
128 RelationMember m = i.next();
129 if (m.isRelation() && !m.getRelation().isIncomplete()) {
130 final List<RelationMember> members = m.getRelation().getMembers();
131 stack.push(modeAwareIterator(members));
132 relsFound.add(m.getRelation());
133 break;
134 }
135 if (m.isWay()) {
136 flat.add(m);
137 }
138 }
139 } while (!stack.isEmpty());
140
141 GpxData gpxData = new GpxData();
142 final String layerName;
143 long time = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) - 24*3600;
144
145 if (!flat.isEmpty()) {
146 Map<String, Object> trkAttr = new HashMap<>();
147 Collection<Collection<WayPoint>> trk = new ArrayList<>();
148 List<WayPoint> trkseg = new ArrayList<>();
149 trk.add(trkseg);
150
151 List<WayConnectionType> wct = new WayConnectionTypeCalculator().updateLinks(flat);
152 final HashMap<String, Integer> names = new HashMap<>();
153 for (int i = 0; i < flat.size(); i++) {
154 WayConnectionType wayConnectionType = wct.get(i);
155 if (!wayConnectionType.isOnewayLoopBackwardPart && !wayConnectionType.direction.isRoundabout()) {
156 if (!wayConnectionType.linkPrev && !trkseg.isEmpty()) {
157 gpxData.addTrack(new GpxTrack(trk, trkAttr));
158 trkAttr.clear();
159 trk.clear();
160 trkseg.clear();
161 trk.add(trkseg);
162 }
163 if (trkAttr.isEmpty()) {
164 flat.get(i).getWay().referrers(Relation.class)
165 .filter(relsFound::contains)
166 .findFirst()
167 .ifPresent(r -> {
168 trkAttr.put("name", r.getName() != null ? r.getName() : Long.toString(r.getId()));
169 trkAttr.put("desc", tr("based on osm route relation data, timestamps are synthetic"));
170 });
171 GpxData.ensureUniqueName(trkAttr, names, (String) trkAttr.get("name"));
172 }
173 List<Node> ln = flat.get(i).getWay().getNodes();
174 if (wayConnectionType.direction == WayConnectionType.Direction.BACKWARD)
175 Collections.reverse(ln);
176 for (Node n: ln) {
177 trkseg.add(OsmDataLayer.nodeToWayPoint(n, TimeUnit.SECONDS.toMillis(time)));
178 time += 1;
179 }
180 }
181 }
182 gpxData.addTrack(new GpxTrack(trk, trkAttr));
183
184 String lprefix = relations.iterator().next().getName();
185 if (lprefix == null || relations.size() > 1)
186 lprefix = tr("Selected Relations");
187 layerName = tr("{0} (GPX export)", lprefix);
188 } else {
189 layerName = "";
190 }
191
192 return new GpxLayer(gpxData, layerName, true);
193 }
194
195 private <T> Iterator<T> modeAwareIterator(List<T> list) {
196 return mode.contains(FROM_FIRST_MEMBER)
197 ? list.iterator()
198 : new LinkedList<>(list).descendingIterator();
199 }
200
201 /**
202 *
203 * @param e the ActionEvent
204 */
205 @Override
206 public void actionPerformed(ActionEvent e) {
207 if (mode.contains(TO_LAYER))
208 MainApplication.getLayerManager().addLayer(getLayer());
209 if (mode.contains(TO_FILE))
210 super.actionPerformed(e);
211 }
212
213 @Override
214 public void setPrimitives(Collection<? extends IPrimitive> primitives) {
215 relations = Collections.<Relation>emptySet();
216 if (primitives != null && !primitives.isEmpty()) {
217 relations = new SubclassFilteredCollection<>(primitives,
218 r -> r instanceof Relation && r.hasTag("type", Arrays.asList("route", "superroute")));
219 }
220 updateEnabledState();
221 }
222
223 @Override
224 protected void updateEnabledState() {
225 setEnabled(!relations.isEmpty());
226 }
227}
Note: See TracBrowser for help on using the repository browser.