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