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