| 1 | package MichiganLeft;
|
|---|
| 2 |
|
|---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.event.ActionEvent;
|
|---|
| 7 | import java.awt.event.KeyEvent;
|
|---|
| 8 | import java.util.Collection;
|
|---|
| 9 | import java.util.Enumeration;
|
|---|
| 10 | import java.util.Hashtable;
|
|---|
| 11 | import java.util.LinkedList;
|
|---|
| 12 | import java.util.ArrayList;
|
|---|
| 13 |
|
|---|
| 14 | import javax.swing.JOptionPane;
|
|---|
| 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.tools.Shortcut;
|
|---|
| 27 |
|
|---|
| 28 | public class MichiganLeftAction extends JosmAction {
|
|---|
| 29 | private LinkedList<Command> cmds = new LinkedList<Command>();
|
|---|
| 30 |
|
|---|
| 31 | public MichiganLeftAction() {
|
|---|
| 32 | super(tr("Michigan Left"), "michigan_left", tr("Adds no left turn for sets of 4 or 5 ways."), Shortcut.registerShortcut("tools:michigan_left", tr("Tool: {0}", tr("Michigan Left")),
|
|---|
| 33 | KeyEvent.VK_M, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true);
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | public void actionPerformed(ActionEvent e) {
|
|---|
| 37 | Collection<OsmPrimitive> selection = Main.main.getCurrentDataSet().getSelected();
|
|---|
| 38 |
|
|---|
| 39 | int ways = 0;
|
|---|
| 40 | for (OsmPrimitive prim : selection) {
|
|---|
| 41 | if (prim instanceof Way) ways++;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | if ((ways != 4) && (ways !=5)) {
|
|---|
| 45 | JOptionPane.showMessageDialog(Main.parent, tr("Please select 4 or 5 ways to assign no left turns."));
|
|---|
| 46 | return;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | if (ways == 5)
|
|---|
| 50 | {
|
|---|
| 51 | // Find extremities of ways
|
|---|
| 52 | Hashtable<Node, Integer> ExtremNodes=new Hashtable<Node, Integer>();
|
|---|
| 53 | for (OsmPrimitive prim : selection) {
|
|---|
| 54 | if (prim instanceof Way)
|
|---|
| 55 | {
|
|---|
| 56 | Way way = (Way) prim;
|
|---|
| 57 | incrementHashtable(ExtremNodes, way.firstNode());
|
|---|
| 58 | incrementHashtable(ExtremNodes, way.lastNode());
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 | System.out.println(tr("{0} extrem nodes.", ExtremNodes.size()));
|
|---|
| 62 |
|
|---|
| 63 | ArrayList<Node> viaNodes=new ArrayList<Node>();
|
|---|
| 64 | // find via nodes (they have 3 occurences in the list)
|
|---|
| 65 | for (Enumeration enumKey = ExtremNodes.keys() ; enumKey.hasMoreElements(); )
|
|---|
| 66 | {
|
|---|
| 67 | Node extrem=(Node)enumKey.nextElement();
|
|---|
| 68 | Integer nb=(Integer) ExtremNodes.get(extrem);
|
|---|
| 69 | System.out.println(tr("Via node {0}, {1}", extrem.getId(), nb.intValue()));
|
|---|
| 70 | if (nb.intValue() == 3)
|
|---|
| 71 | {
|
|---|
| 72 | viaNodes.add(extrem);
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 | System.out.println(tr("{0} via nodes.", viaNodes.size()));
|
|---|
| 76 |
|
|---|
| 77 | if (viaNodes.size() != 2) {
|
|---|
| 78 | JOptionPane.showMessageDialog(Main.parent, tr("Unable to find via nodes. Please check your selection"));
|
|---|
| 79 | return;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | Node viaFirst = viaNodes.get(0);
|
|---|
| 83 | Node viaLast = viaNodes.get(1); // Find middle segment
|
|---|
| 84 |
|
|---|
| 85 | Way middle = null;
|
|---|
| 86 | for (OsmPrimitive prim : selection) {
|
|---|
| 87 | if (prim instanceof Way)
|
|---|
| 88 | {
|
|---|
| 89 | Way way = (Way) prim;
|
|---|
| 90 | Node first = way.firstNode();
|
|---|
| 91 | Node last = way.lastNode();
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 | if ((first.equals(viaFirst) && last.equals(viaLast)) || (first.equals(viaLast) && last.equals(viaFirst)))
|
|---|
| 95 | middle=way;
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 | System.out.println(tr("Middle way: {0}", middle.getId()));
|
|---|
| 99 |
|
|---|
| 100 | // Build relations
|
|---|
| 101 | for (OsmPrimitive prim : selection) {
|
|---|
| 102 | if (prim instanceof Way)
|
|---|
| 103 | {
|
|---|
| 104 | Way way = (Way) prim;
|
|---|
| 105 | if (way != middle)
|
|---|
| 106 | {
|
|---|
| 107 | Node first = way.firstNode();
|
|---|
| 108 | Node last = way.lastNode();
|
|---|
| 109 |
|
|---|
| 110 | if (first==viaFirst)
|
|---|
| 111 | buildRelation(middle, way, viaNodes.get(0));
|
|---|
| 112 | else if (first==viaLast)
|
|---|
| 113 | buildRelation(middle, way, viaNodes.get(1));
|
|---|
| 114 | else if (last==viaFirst)
|
|---|
| 115 | buildRelation(way, middle, viaNodes.get(0));
|
|---|
| 116 | else if (last==viaLast)
|
|---|
| 117 | buildRelation(way, middle, viaNodes.get(1));
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 | Command c = new SequenceCommand(tr("Create Michigan left turn restriction"), cmds);
|
|---|
| 122 | Main.main.undoRedo.add(c);
|
|---|
| 123 | cmds.clear();
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | public void incrementHashtable(Hashtable<Node, Integer> hash, Node node)
|
|---|
| 128 | {
|
|---|
| 129 | System.out.println(tr("Processing {0}", node.getId()));
|
|---|
| 130 | if (hash.containsKey(node))
|
|---|
| 131 | {
|
|---|
| 132 | Integer nb=(Integer) hash.get(node);
|
|---|
| 133 | hash.put(node, new Integer (nb.intValue()+1));
|
|---|
| 134 | System.out.println(tr("Old value", nb.intValue()));
|
|---|
| 135 | }
|
|---|
| 136 | else
|
|---|
| 137 | hash.put(node, new Integer (1));
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | public void buildRelation(Way fromWay, Way toWay, Node viaNode)
|
|---|
| 141 | {
|
|---|
| 142 | System.out.println(tr("Relation: from {0} to {1} via {2}", fromWay.getId(), toWay.getId(), viaNode.getId()));
|
|---|
| 143 |
|
|---|
| 144 | Relation relation = new Relation();
|
|---|
| 145 |
|
|---|
| 146 | RelationMember from = new RelationMember("from", fromWay);
|
|---|
| 147 | relation.addMember(from);
|
|---|
| 148 |
|
|---|
| 149 | RelationMember to = new RelationMember("to", toWay);
|
|---|
| 150 | relation.addMember(to);
|
|---|
| 151 |
|
|---|
| 152 | RelationMember via = new RelationMember("via", viaNode);
|
|---|
| 153 | relation.addMember(via);
|
|---|
| 154 |
|
|---|
| 155 | relation.put("type", "restriction");
|
|---|
| 156 | relation.put("restriction", "no_left_turn");
|
|---|
| 157 |
|
|---|
| 158 | cmds.add(new AddCommand(relation));
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | }
|
|---|