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