| 1 | package MichiganLeft;
|
|---|
| 2 |
|
|---|
| 3 | import java.awt.event.ActionEvent;
|
|---|
| 4 | import java.awt.event.KeyEvent;
|
|---|
| 5 | import java.util.Collection;
|
|---|
| 6 | import java.util.Enumeration;
|
|---|
| 7 | import java.util.Hashtable;
|
|---|
| 8 | import java.util.LinkedList;
|
|---|
| 9 | import java.util.ArrayList;
|
|---|
| 10 |
|
|---|
| 11 | import javax.swing.JMenuItem;
|
|---|
| 12 | import javax.swing.JOptionPane;
|
|---|
| 13 |
|
|---|
| 14 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 15 |
|
|---|
| 16 | import org.openstreetmap.josm.Main;
|
|---|
| 17 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 18 | import org.openstreetmap.josm.command.AddCommand;
|
|---|
| 19 | import org.openstreetmap.josm.command.Command;
|
|---|
| 20 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 21 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 23 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 24 | import org.openstreetmap.josm.data.osm.RelationMember;
|
|---|
| 25 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 26 | import org.openstreetmap.josm.gui.MainMenu;
|
|---|
| 27 | import org.openstreetmap.josm.plugins.Plugin;
|
|---|
| 28 | import org.openstreetmap.josm.plugins.PluginInformation;
|
|---|
| 29 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 30 |
|
|---|
| 31 | public class MichiganLeft extends Plugin {
|
|---|
| 32 | JMenuItem MichiganLeft;
|
|---|
| 33 |
|
|---|
| 34 | public MichiganLeft(PluginInformation info) {
|
|---|
| 35 | super(info);
|
|---|
| 36 | MichiganLeft = MainMenu.add(Main.main.menu.dataMenu, new MichiganLeftAction());
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | private class MichiganLeftAction extends JosmAction {
|
|---|
| 40 | /**
|
|---|
| 41 | *
|
|---|
| 42 | */
|
|---|
| 43 | private static final long serialVersionUID = 1L;
|
|---|
| 44 | private LinkedList<Command> cmds = new LinkedList<>();
|
|---|
| 45 |
|
|---|
| 46 | public MichiganLeftAction() {
|
|---|
| 47 | super(tr("Michigan Left"), "michigan_left",
|
|---|
| 48 | tr("Adds no left turn for sets of 4 or 5 ways."),
|
|---|
| 49 | Shortcut.registerShortcut("tools:michigan_left", tr("Tool: {0}",
|
|---|
| 50 | tr("Michigan Left")), KeyEvent.VK_N, Shortcut.ALT_SHIFT), true);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | @Override
|
|---|
| 54 | public void actionPerformed(ActionEvent e) {
|
|---|
| 55 | Collection<OsmPrimitive> mainSelection = Main.main.getCurrentDataSet()
|
|---|
| 56 | .getSelected();
|
|---|
| 57 |
|
|---|
| 58 | ArrayList<OsmPrimitive> selection = new ArrayList<>();
|
|---|
| 59 |
|
|---|
| 60 | for (OsmPrimitive prim: mainSelection) selection.add(prim);
|
|---|
| 61 |
|
|---|
| 62 | int ways = 0;
|
|---|
| 63 | for (OsmPrimitive prim : selection) {
|
|---|
| 64 | if (prim instanceof Way)
|
|---|
| 65 | ways++;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | if ((ways != 4) && (ways != 5)) {
|
|---|
| 69 | JOptionPane.showMessageDialog(Main.parent,
|
|---|
| 70 | tr("Please select 4 or 5 ways to assign no left turns."));
|
|---|
| 71 | return;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | if (ways == 4) {
|
|---|
| 75 | // Find extremities of ways
|
|---|
| 76 | Hashtable<Node, Integer> ExtremNodes = new Hashtable<>();
|
|---|
| 77 | for (OsmPrimitive prim : selection) {
|
|---|
| 78 | if (prim instanceof Way) {
|
|---|
| 79 | Way way = (Way) prim;
|
|---|
| 80 | incrementHashtable(ExtremNodes, way.firstNode());
|
|---|
| 81 | incrementHashtable(ExtremNodes, way.lastNode());
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | // System.out.println(tr("{0} extrem nodes.", ExtremNodes.size()));
|
|---|
| 85 | if (ExtremNodes.size() != 4) {
|
|---|
| 86 | JOptionPane.showMessageDialog(Main.parent,
|
|---|
| 87 | tr("Please select 4 ways that form a closed relation."));
|
|---|
| 88 | return;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | // order the ways
|
|---|
| 92 | ArrayList<Way> orderedWays = new ArrayList<>();
|
|---|
| 93 | Way currentWay = (Way) selection.iterator().next();
|
|---|
| 94 | orderedWays.add((Way) currentWay);
|
|---|
| 95 | selection.remove(currentWay);
|
|---|
| 96 | while (selection.size() > 0) {
|
|---|
| 97 | boolean found = false;
|
|---|
| 98 | Node nextNode = currentWay.lastNode();
|
|---|
| 99 | for (OsmPrimitive prim : selection) {
|
|---|
| 100 | Way tmpWay = (Way) prim;
|
|---|
| 101 | if (tmpWay.firstNode() == nextNode) {
|
|---|
| 102 | orderedWays.add(tmpWay);
|
|---|
| 103 | selection.remove(prim);
|
|---|
| 104 | currentWay = tmpWay;
|
|---|
| 105 | found = true;
|
|---|
| 106 | break;
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | if (!found) {
|
|---|
| 110 | JOptionPane.showMessageDialog(Main.parent,
|
|---|
| 111 | tr("Unable to order the ways. Please verify their directions"));
|
|---|
| 112 | return;
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | // Build relations
|
|---|
| 117 | for (int index = 0; index < 4; index++) {
|
|---|
| 118 | Way firstWay = orderedWays.get(index);
|
|---|
| 119 | Way lastWay = orderedWays.get((index + 1) % 4);
|
|---|
| 120 | Node lastNode = firstWay.lastNode();
|
|---|
| 121 |
|
|---|
| 122 | buildRelation(firstWay, lastWay, lastNode);
|
|---|
| 123 | }
|
|---|
| 124 | Command c = new SequenceCommand(
|
|---|
| 125 | tr("Create Michigan left turn restriction"), cmds);
|
|---|
| 126 | Main.main.undoRedo.add(c);
|
|---|
| 127 | cmds.clear();
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | if (ways == 5) {
|
|---|
| 131 | // Find extremities of ways
|
|---|
| 132 | Hashtable<Node, Integer> ExtremNodes = new Hashtable<>();
|
|---|
| 133 | for (OsmPrimitive prim : selection) {
|
|---|
| 134 | if (prim instanceof Way) {
|
|---|
| 135 | Way way = (Way) prim;
|
|---|
| 136 | incrementHashtable(ExtremNodes, way.firstNode());
|
|---|
| 137 | incrementHashtable(ExtremNodes, way.lastNode());
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | // System.out.println(tr("{0} extrem nodes.", ExtremNodes.size()));
|
|---|
| 141 |
|
|---|
| 142 | ArrayList<Node> viaNodes = new ArrayList<>();
|
|---|
| 143 | // find via nodes (they have 3 occurences in the list)
|
|---|
| 144 | for (Enumeration<Node> enumKey = ExtremNodes.keys(); enumKey
|
|---|
| 145 | .hasMoreElements();) {
|
|---|
| 146 | Node extrem = enumKey.nextElement();
|
|---|
| 147 | Integer nb = (Integer) ExtremNodes.get(extrem);
|
|---|
| 148 | // System.out.println(tr("Via node {0}, {1}", extrem.getId(),
|
|---|
| 149 | // nb.intValue()));
|
|---|
| 150 | if (nb.intValue() == 3) {
|
|---|
| 151 | viaNodes.add(extrem);
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | // System.out.println(tr("{0} via nodes.", viaNodes.size()));
|
|---|
| 155 |
|
|---|
| 156 | if (viaNodes.size() != 2) {
|
|---|
| 157 | JOptionPane.showMessageDialog(Main.parent,
|
|---|
| 158 | tr("Unable to find via nodes. Please check your selection"));
|
|---|
| 159 | return;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | Node viaFirst = viaNodes.get(0);
|
|---|
| 163 | Node viaLast = viaNodes.get(1); // Find middle segment
|
|---|
| 164 |
|
|---|
| 165 | Way middle = null;
|
|---|
| 166 | for (OsmPrimitive prim : selection) {
|
|---|
| 167 | if (prim instanceof Way) {
|
|---|
| 168 | Way way = (Way) prim;
|
|---|
| 169 | Node first = way.firstNode();
|
|---|
| 170 | Node last = way.lastNode();
|
|---|
| 171 |
|
|---|
| 172 | if ((first.equals(viaFirst) && last.equals(viaLast))
|
|---|
| 173 | || (first.equals(viaLast) && last.equals(viaFirst)))
|
|---|
| 174 | middle = way;
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 | // System.out.println(tr("Middle way: {0}", middle.getId()));
|
|---|
| 178 |
|
|---|
| 179 | // Build relations
|
|---|
| 180 | for (OsmPrimitive prim : selection) {
|
|---|
| 181 | if (prim instanceof Way) {
|
|---|
| 182 | Way way = (Way) prim;
|
|---|
| 183 | if (way != middle) {
|
|---|
| 184 | Node first = way.firstNode();
|
|---|
| 185 | Node last = way.lastNode();
|
|---|
| 186 |
|
|---|
| 187 | if (first == viaFirst)
|
|---|
| 188 | buildRelation(middle, way, viaNodes.get(0));
|
|---|
| 189 | else if (first == viaLast)
|
|---|
| 190 | buildRelation(middle, way, viaNodes.get(1));
|
|---|
| 191 | else if (last == viaFirst)
|
|---|
| 192 | buildRelation(way, middle, viaNodes.get(0));
|
|---|
| 193 | else if (last == viaLast)
|
|---|
| 194 | buildRelation(way, middle, viaNodes.get(1));
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 | }
|
|---|
| 198 | Command c = new SequenceCommand(
|
|---|
| 199 | tr("Create Michigan left turn restriction"), cmds);
|
|---|
| 200 | Main.main.undoRedo.add(c);
|
|---|
| 201 | cmds.clear();
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | public void incrementHashtable(Hashtable<Node, Integer> hash, Node node) {
|
|---|
| 206 | // System.out.println(tr("Processing {0}", node.getId()));
|
|---|
| 207 | if (hash.containsKey(node)) {
|
|---|
| 208 | Integer nb = (Integer) hash.get(node);
|
|---|
| 209 | hash.put(node, new Integer(nb.intValue() + 1));
|
|---|
| 210 | // System.out.println(tr("Old value", nb.intValue()));
|
|---|
| 211 | } else
|
|---|
| 212 | hash.put(node, new Integer(1));
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | public void buildRelation(Way fromWay, Way toWay, Node viaNode) {
|
|---|
| 216 | // System.out.println(tr("Relation: from {0} to {1} via {2}",
|
|---|
| 217 | // fromWay.getId(), toWay.getId(), viaNode.getId()));
|
|---|
| 218 |
|
|---|
| 219 | Relation relation = new Relation();
|
|---|
| 220 |
|
|---|
| 221 | RelationMember from = new RelationMember("from", fromWay);
|
|---|
| 222 | relation.addMember(from);
|
|---|
| 223 |
|
|---|
| 224 | RelationMember to = new RelationMember("to", toWay);
|
|---|
| 225 | relation.addMember(to);
|
|---|
| 226 |
|
|---|
| 227 | RelationMember via = new RelationMember("via", viaNode);
|
|---|
| 228 | relation.addMember(via);
|
|---|
| 229 |
|
|---|
| 230 | relation.put("type", "restriction");
|
|---|
| 231 | relation.put("restriction", "no_left_turn");
|
|---|
| 232 |
|
|---|
| 233 | cmds.add(new AddCommand(relation));
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | @Override
|
|---|
| 237 | protected void updateEnabledState() {
|
|---|
| 238 | setEnabled(getEditLayer() != null);
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | @Override
|
|---|
| 242 | protected void updateEnabledState(
|
|---|
| 243 | Collection<? extends OsmPrimitive> selection) {
|
|---|
| 244 | // do nothing
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | }
|
|---|
| 248 | }
|
|---|