| 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 |
|
|---|
| 32 | public class MichiganLeft extends Plugin {
|
|---|
| 33 | JMenuItem MichiganLeft;
|
|---|
| 34 |
|
|---|
| 35 | public MichiganLeft(PluginInformation info) {
|
|---|
| 36 | super(info);
|
|---|
| 37 | MichiganLeft = MainMenu.add(Main.main.menu.toolsMenu, new MichiganLeftAction());
|
|---|
| 38 |
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | private class MichiganLeftAction extends JosmAction {
|
|---|
| 43 | private LinkedList<Command> cmds = new LinkedList<Command>();
|
|---|
| 44 |
|
|---|
| 45 | public MichiganLeftAction() {
|
|---|
| 46 | 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")),
|
|---|
| 47 | KeyEvent.VK_M, Shortcut.GROUP_EDIT, KeyEvent.SHIFT_DOWN_MASK|KeyEvent.ALT_DOWN_MASK), true);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | public void actionPerformed(ActionEvent e) {
|
|---|
| 51 | Collection<OsmPrimitive> selection = Main.main.getCurrentDataSet().getSelected();
|
|---|
| 52 |
|
|---|
| 53 | int ways = 0;
|
|---|
| 54 | for (OsmPrimitive prim : selection) {
|
|---|
| 55 | if (prim instanceof Way) ways++;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | if ((ways != 4) && (ways !=5)) {
|
|---|
| 59 | JOptionPane.showMessageDialog(Main.parent, tr("Please select 4 or 5 ways to assign no left turns."));
|
|---|
| 60 | return;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | if (ways == 4)
|
|---|
| 64 | {
|
|---|
| 65 | // Find extremities of ways
|
|---|
| 66 | Hashtable<Node, Integer> ExtremNodes=new Hashtable<Node, Integer>();
|
|---|
| 67 | for (OsmPrimitive prim : selection)
|
|---|
| 68 | {
|
|---|
| 69 | if (prim instanceof Way)
|
|---|
| 70 | {
|
|---|
| 71 | Way way = (Way) prim;
|
|---|
| 72 | incrementHashtable(ExtremNodes, way.firstNode());
|
|---|
| 73 | incrementHashtable(ExtremNodes, way.lastNode());
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | //System.out.println(tr("{0} extrem nodes.", ExtremNodes.size()));
|
|---|
| 77 | if (ExtremNodes.size() != 4)
|
|---|
| 78 | {
|
|---|
| 79 | JOptionPane.showMessageDialog(Main.parent, tr("Please select 4 ways that form a closed relation."));
|
|---|
| 80 | return;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | // order the ways
|
|---|
| 84 | ArrayList<Way> orderedWays=new ArrayList<Way>();
|
|---|
| 85 | Way currentWay=(Way) selection.iterator().next();
|
|---|
| 86 | orderedWays.add((Way) currentWay);
|
|---|
| 87 | selection.remove(currentWay);
|
|---|
| 88 | while (selection.size()>0)
|
|---|
| 89 | {
|
|---|
| 90 | boolean found=false;
|
|---|
| 91 | Node nextNode=currentWay.lastNode();
|
|---|
| 92 | for (OsmPrimitive prim : selection)
|
|---|
| 93 | {
|
|---|
| 94 | Way tmpWay=(Way) prim;
|
|---|
| 95 | if (tmpWay.firstNode() == nextNode)
|
|---|
| 96 | {
|
|---|
| 97 | orderedWays.add(tmpWay);
|
|---|
| 98 | selection.remove(prim);
|
|---|
| 99 | currentWay=tmpWay;
|
|---|
| 100 | found=true;
|
|---|
| 101 | break;
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | if (!found)
|
|---|
| 105 | {
|
|---|
| 106 | JOptionPane.showMessageDialog(Main.parent, tr("Unable to order the ways. Please verify their directions"));
|
|---|
| 107 | return;
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | // Build relations
|
|---|
| 112 | for (int index=0 ; index<4 ; index++)
|
|---|
| 113 | {
|
|---|
| 114 | Way firstWay=orderedWays.get(index);
|
|---|
| 115 | Way lastWay=orderedWays.get( (index+1) % 4);
|
|---|
| 116 | Node lastNode = firstWay.lastNode();
|
|---|
| 117 |
|
|---|
| 118 | buildRelation(firstWay, lastWay, lastNode);
|
|---|
| 119 | }
|
|---|
| 120 | Command c = new SequenceCommand(tr("Create Michigan left turn restriction"), cmds);
|
|---|
| 121 | Main.main.undoRedo.add(c);
|
|---|
| 122 | cmds.clear();
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | if (ways == 5)
|
|---|
| 126 | {
|
|---|
| 127 | // Find extremities of ways
|
|---|
| 128 | Hashtable<Node, Integer> ExtremNodes=new Hashtable<Node, Integer>();
|
|---|
| 129 | for (OsmPrimitive prim : selection) {
|
|---|
| 130 | if (prim instanceof Way)
|
|---|
| 131 | {
|
|---|
| 132 | Way way = (Way) prim;
|
|---|
| 133 | incrementHashtable(ExtremNodes, way.firstNode());
|
|---|
| 134 | incrementHashtable(ExtremNodes, way.lastNode());
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 | //System.out.println(tr("{0} extrem nodes.", ExtremNodes.size()));
|
|---|
| 138 |
|
|---|
| 139 | ArrayList<Node> viaNodes=new ArrayList<Node>();
|
|---|
| 140 | // find via nodes (they have 3 occurences in the list)
|
|---|
| 141 | for (Enumeration enumKey = ExtremNodes.keys() ; enumKey.hasMoreElements(); )
|
|---|
| 142 | {
|
|---|
| 143 | Node extrem=(Node)enumKey.nextElement();
|
|---|
| 144 | Integer nb=(Integer) ExtremNodes.get(extrem);
|
|---|
| 145 | //System.out.println(tr("Via node {0}, {1}", extrem.getId(), nb.intValue()));
|
|---|
| 146 | if (nb.intValue() == 3)
|
|---|
| 147 | {
|
|---|
| 148 | viaNodes.add(extrem);
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 | //System.out.println(tr("{0} via nodes.", viaNodes.size()));
|
|---|
| 152 |
|
|---|
| 153 | if (viaNodes.size() != 2) {
|
|---|
| 154 | JOptionPane.showMessageDialog(Main.parent, tr("Unable to find via nodes. Please check your selection"));
|
|---|
| 155 | return;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | Node viaFirst = viaNodes.get(0);
|
|---|
| 159 | Node viaLast = viaNodes.get(1); // Find middle segment
|
|---|
| 160 |
|
|---|
| 161 | Way middle = null;
|
|---|
| 162 | for (OsmPrimitive prim : selection) {
|
|---|
| 163 | if (prim instanceof Way)
|
|---|
| 164 | {
|
|---|
| 165 | Way way = (Way) prim;
|
|---|
| 166 | Node first = way.firstNode();
|
|---|
| 167 | Node last = way.lastNode();
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 | if ((first.equals(viaFirst) && last.equals(viaLast)) || (first.equals(viaLast) && last.equals(viaFirst)))
|
|---|
| 171 | middle=way;
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 | //System.out.println(tr("Middle way: {0}", middle.getId()));
|
|---|
| 175 |
|
|---|
| 176 | // Build relations
|
|---|
| 177 | for (OsmPrimitive prim : selection) {
|
|---|
| 178 | if (prim instanceof Way)
|
|---|
| 179 | {
|
|---|
| 180 | Way way = (Way) prim;
|
|---|
| 181 | if (way != middle)
|
|---|
| 182 | {
|
|---|
| 183 | Node first = way.firstNode();
|
|---|
| 184 | Node last = way.lastNode();
|
|---|
| 185 |
|
|---|
| 186 | if (first==viaFirst)
|
|---|
| 187 | buildRelation(middle, way, viaNodes.get(0));
|
|---|
| 188 | else if (first==viaLast)
|
|---|
| 189 | buildRelation(middle, way, viaNodes.get(1));
|
|---|
| 190 | else if (last==viaFirst)
|
|---|
| 191 | buildRelation(way, middle, viaNodes.get(0));
|
|---|
| 192 | else if (last==viaLast)
|
|---|
| 193 | buildRelation(way, middle, viaNodes.get(1));
|
|---|
| 194 | }
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 | Command c = new SequenceCommand(tr("Create Michigan left turn restriction"), cmds);
|
|---|
| 198 | Main.main.undoRedo.add(c);
|
|---|
| 199 | cmds.clear();
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | public void incrementHashtable(Hashtable<Node, Integer> hash, Node node)
|
|---|
| 204 | {
|
|---|
| 205 | //System.out.println(tr("Processing {0}", node.getId()));
|
|---|
| 206 | if (hash.containsKey(node))
|
|---|
| 207 | {
|
|---|
| 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 | }
|
|---|
| 212 | else
|
|---|
| 213 | hash.put(node, new Integer (1));
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | public void buildRelation(Way fromWay, Way toWay, Node viaNode)
|
|---|
| 217 | {
|
|---|
| 218 | //System.out.println(tr("Relation: from {0} to {1} via {2}", fromWay.getId(), toWay.getId(), viaNode.getId()));
|
|---|
| 219 |
|
|---|
| 220 | Relation relation = new Relation();
|
|---|
| 221 |
|
|---|
| 222 | RelationMember from = new RelationMember("from", fromWay);
|
|---|
| 223 | relation.addMember(from);
|
|---|
| 224 |
|
|---|
| 225 | RelationMember to = new RelationMember("to", toWay);
|
|---|
| 226 | relation.addMember(to);
|
|---|
| 227 |
|
|---|
| 228 | RelationMember via = new RelationMember("via", viaNode);
|
|---|
| 229 | relation.addMember(via);
|
|---|
| 230 |
|
|---|
| 231 | relation.put("type", "restriction");
|
|---|
| 232 | relation.put("restriction", "no_left_turn");
|
|---|
| 233 |
|
|---|
| 234 | cmds.add(new AddCommand(relation));
|
|---|
| 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 | }
|
|---|