| | 7 | [[PageOutline(2,Table of Contents)]] |
| | 8 | |
| | 9 | |
| | 10 | == Convert a way that connects to an unsplit roundabout to a fork == |
| | 11 | Convert a way that connects to a roundabout to a fork consisting of 2 oneway ways. Splitting the way on its last node and attaching to the 2 nodes adjacent to the common node of the roundabout way. This version only works on unsplit roundabouts. See the next example for an updated version. |
| | 12 | |
| | 13 | {{{#!python |
| | 14 | from javax.swing import JOptionPane |
| | 15 | from org.openstreetmap.josm.gui import MainApplication |
| | 16 | |
| | 17 | import org.openstreetmap.josm.command as Command |
| | 18 | import org.openstreetmap.josm.data.osm.Way as Way |
| | 19 | |
| | 20 | editLayer = MainApplication.getLayerManager().getEditLayer() |
| | 21 | if editLayer and editLayer.data: |
| | 22 | selected_ways = editLayer.data.getSelectedWays() |
| | 23 | print selected_ways |
| | 24 | |
| | 25 | if not(selected_ways) or len(selected_ways)>1: |
| | 26 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), |
| | 27 | "Please select a single way that connects to a roundabout") |
| | 28 | else: |
| | 29 | for way in selected_ways: |
| | 30 | if way.get('oneway') in ['yes', '-1']: |
| | 31 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), "This way has oneway tag set") |
| | 32 | else: |
| | 33 | node_before = None |
| | 34 | node_after = None |
| | 35 | common_node = None |
| | 36 | common_node_becomes_node_before=None |
| | 37 | #print dir(way) |
| | 38 | for fln in [way.firstNode(), way.lastNode()]: |
| | 39 | print 'fln', fln |
| | 40 | for parentway in fln.getParentWays(): |
| | 41 | if parentway.get("junction")=='roundabout': |
| | 42 | for n in parentway.getNodes(): |
| | 43 | if common_node: |
| | 44 | # found common node between selected way and |
| | 45 | # roundabout way in previous iteration |
| | 46 | node_after = n |
| | 47 | # we are done here |
| | 48 | break |
| | 49 | if n.getId() == fln.getId(): |
| | 50 | # this is the node the roundabout has in common with selected way |
| | 51 | common_node = n |
| | 52 | if not(node_before): |
| | 53 | # normally we encountered a node on the roundabout way |
| | 54 | # before this one, but if the common node is the first node |
| | 55 | # of an unsplit roundabout, we will need to take the last |
| | 56 | # node of the roundabout instead |
| | 57 | node_before = parentway.getNodes()[-2] |
| | 58 | node_after = parentway.getNodes()[1] |
| | 59 | break |
| | 60 | # if not we go through the loop one more time to put the next |
| | 61 | # node in node_after |
| | 62 | continue |
| | 63 | node_before = n |
| | 64 | if common_node: |
| | 65 | # if common_node is already defined at this point, it means it was |
| | 66 | # the first node of the selected way, |
| | 67 | # so it will have to be replaced with node_before in the selected way |
| | 68 | common_node_becomes_node_before = True |
| | 69 | adjacent_node_to_split_on = way.getNodes()[1] |
| | 70 | break |
| | 71 | else: |
| | 72 | common_node_becomes_node_before = False |
| | 73 | adjacent_node_to_split_on = way.getNodes()[-1] |
| | 74 | if not(common_node) or common_node_becomes_node_before==None: |
| | 75 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), |
| | 76 | "Please select a way that connects to a roundabout") |
| | 77 | else: |
| | 78 | print common_node.get('name') |
| | 79 | print node_before.get('name') |
| | 80 | print node_after.get('name') |
| | 81 | |
| | 82 | commandsList = [] |
| | 83 | if len(way.getNodes())>2: |
| | 84 | # split off last segment before roundabout if needed |
| | 85 | commandsList.append(Command.SplitWayCommand.split( |
| | 86 | way, [adjacent_node_to_split_on], [])) |
| | 87 | MainApplication.undoRedo.add(Command.SequenceCommand( |
| | 88 | "Split selected way", commandsList)) |
| | 89 | commandsList = [] |
| | 90 | # After splitting find the segment that connects to the roundabout again |
| | 91 | for waypartconnectedtoroundabout in adjacent_node_to_split_on.getParentWays(): |
| | 92 | if common_node in waypartconnectedtoroundabout.getNodes(): |
| | 93 | break |
| | 94 | if len(way.getNodes())==2: |
| | 95 | if common_node == waypartconnectedtoroundabout.firstNode(): |
| | 96 | adjacent_node_to_split_on = waypartconnectedtoroundabout.lastNode() |
| | 97 | else: |
| | 98 | adjacent_node_to_split_on = waypartconnectedtoroundabout.firstNode() |
| | 99 | # time to actually do something |
| | 100 | # make a copy of the way |
| | 101 | modified_way = Way(waypartconnectedtoroundabout) |
| | 102 | # replace its nodes, so it becomes a fork |
| | 103 | modified_way.setNodes([node_before, adjacent_node_to_split_on, node_after]) |
| | 104 | # add oneway tag |
| | 105 | modified_way.put('oneway', 'yes') |
| | 106 | # apply the changes |
| | 107 | commandsList.append(Command.ChangeCommand( |
| | 108 | waypartconnectedtoroundabout, modified_way)) |
| | 109 | MainApplication.undoRedo.add(Command.SequenceCommand( |
| | 110 | "Add oneway tag and create forked way", commandsList)) |
| | 111 | commandsList = [] |
| | 112 | # split this way where it forks |
| | 113 | commandsList.append(Command.SplitWayCommand.split( |
| | 114 | waypartconnectedtoroundabout, [adjacent_node_to_split_on], [])) |
| | 115 | MainApplication.undoRedo.add(Command.SequenceCommand( |
| | 116 | "Split fork into 2 ways", commandsList)) |
| | 117 | commandsList = [] |
| | 118 | |
| | 119 | }}} |
| | 120 | |
| | 121 | |
| | 122 | == Convert a way that connects to a split roundabout to a fork == |
| | 123 | This script does the same as the previous one, but it also works on roundabouts where the ways are split. It's not taking care of the relations. |
| | 124 | |
| | 125 | {{{#!python |
| | 126 | from javax.swing import JOptionPane |
| | 127 | from org.openstreetmap.josm.gui import MainApplication |
| | 128 | |
| | 129 | import org.openstreetmap.josm.command as Command |
| | 130 | import org.openstreetmap.josm.data.osm.Way as Way |
| | 131 | |
| | 132 | editLayer = MainApplication.getLayerManager().getEditLayer() |
| | 133 | print '==== Fresh run ====' |
| | 134 | if editLayer and editLayer.data: |
| | 135 | selected_ways = editLayer.data.getSelectedWays() |
| | 136 | print selected_ways |
| | 137 | |
| | 138 | if not(selected_ways) or len(selected_ways)>1: |
| | 139 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), "Please select a single way that connects to a roundabout") |
| | 140 | else: |
| | 141 | for way in selected_ways: |
| | 142 | if way.get('oneway') in ['yes', '-1']: |
| | 143 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), "This way has oneway tag set") |
| | 144 | elif way.get('junction') in ['roundabout']: |
| | 145 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), "This way is part of a roundabout") |
| | 146 | else: |
| | 147 | node_before = None |
| | 148 | node_after = None |
| | 149 | common_node = None |
| | 150 | common_node_becomes_node_before=None |
| | 151 | roundabout_way_before = None |
| | 152 | roundabout_way_after = None |
| | 153 | for fln in [way.firstNode(), way.lastNode()]: |
| | 154 | print 'fln', fln |
| | 155 | for parentway in fln.getParentWays(): |
| | 156 | if parentway.get("junction")=='roundabout': |
| | 157 | print parentway.get('name') |
| | 158 | if parentway.isClosed(): |
| | 159 | # unsplit roundabout |
| | 160 | for n in parentway.getNodes(): |
| | 161 | if common_node: |
| | 162 | # found common node between selected way |
| | 163 | # and roundabout way in previous iteration |
| | 164 | node_after = n |
| | 165 | print node_before.get('name') |
| | 166 | print node_after.get('name') |
| | 167 | # we are done here |
| | 168 | break |
| | 169 | if n.getId() == fln.getId(): |
| | 170 | # this is the node the roundabout has in common with selected way |
| | 171 | common_node = n |
| | 172 | print common_node.get('name') |
| | 173 | if not(node_before): |
| | 174 | # normally we encountered a node on the roundabout way |
| | 175 | # before this one, but if the common node is the first node |
| | 176 | # of an unsplit roundabout, we will need to take the last |
| | 177 | # node of the roundabout instead |
| | 178 | node_before = parentway.getNodes()[-2] |
| | 179 | node_after = parentway.getNodes()[1] |
| | 180 | print node_before.get('name') |
| | 181 | print node_after.get('name') |
| | 182 | break |
| | 183 | # if not we go through the loop one more time to put the next |
| | 184 | # node in node_after |
| | 185 | continue |
| | 186 | node_before = n |
| | 187 | else: |
| | 188 | # this is a split roundabout |
| | 189 | if parentway.firstNode().getId() == fln.getId(): |
| | 190 | node_after = parentway.getNodes()[1] |
| | 191 | roundabout_way_after = parentway |
| | 192 | print 'node after', node_after.get('name') |
| | 193 | else: |
| | 194 | node_before = parentway.getNodes()[-2] |
| | 195 | roundabout_way_before = parentway |
| | 196 | if node_before and node_after: |
| | 197 | common_node = fln |
| | 198 | break |
| | 199 | |
| | 200 | if common_node: |
| | 201 | # if common_node is already defined at this point, it means it was |
| | 202 | # the first node of the selected way, |
| | 203 | # so it will have to be replaced with node_before in the selected way |
| | 204 | common_node_becomes_node_before = True |
| | 205 | break |
| | 206 | else: |
| | 207 | common_node_becomes_node_before = False |
| | 208 | |
| | 209 | if common_node.getId() == way.firstNode().getId(): |
| | 210 | adjacent_node_to_split_on = way.getNodes()[1] |
| | 211 | else: |
| | 212 | adjacent_node_to_split_on = way.getNodes()[-1] |
| | 213 | |
| | 214 | if not(common_node) or ((parentway.isClosed() and common_node_becomes_node_before==None)): |
| | 215 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), "Please select a way that connects to a roundabout") |
| | 216 | else: |
| | 217 | commandsList = [] |
| | 218 | if len(way.getNodes())>2: |
| | 219 | # split off last segment before roundabout if needed |
| | 220 | commandsList.append(Command.SplitWayCommand.split( |
| | 221 | way, [adjacent_node_to_split_on], [])) |
| | 222 | MainApplication.undoRedo.add(Command.SequenceCommand( |
| | 223 | "Split selected way", commandsList)) |
| | 224 | commandsList = [] |
| | 225 | # After splitting find the segment that connects to the roundabout again |
| | 226 | for waypartconnectedtoroundabout in adjacent_node_to_split_on.getParentWays(): |
| | 227 | if waypartconnectedtoroundabout.get('junction') == 'roundabout': |
| | 228 | continue |
| | 229 | if common_node in waypartconnectedtoroundabout.getNodes(): |
| | 230 | break |
| | 231 | if len(way.getNodes())==2: |
| | 232 | if common_node == waypartconnectedtoroundabout.firstNode(): |
| | 233 | adjacent_node_to_split_on = waypartconnectedtoroundabout.lastNode() |
| | 234 | else: |
| | 235 | adjacent_node_to_split_on = waypartconnectedtoroundabout.firstNode() |
| | 236 | # time to actually do something |
| | 237 | # make a copy of the way |
| | 238 | modified_way = Way(waypartconnectedtoroundabout) |
| | 239 | # replace its nodes, so it becomes a fork |
| | 240 | modified_way.setNodes([node_before, adjacent_node_to_split_on, node_after]) |
| | 241 | # add oneway tag |
| | 242 | modified_way.put('oneway', 'yes') |
| | 243 | # apply the changes |
| | 244 | commandsList.append(Command.ChangeCommand( |
| | 245 | waypartconnectedtoroundabout, modified_way)) |
| | 246 | MainApplication.undoRedo.add(Command.SequenceCommand( |
| | 247 | "Add oneway tag and create forked way", commandsList)) |
| | 248 | commandsList = [] |
| | 249 | # split this way where it forks |
| | 250 | commandsList.append(Command.SplitWayCommand.split( |
| | 251 | waypartconnectedtoroundabout, [adjacent_node_to_split_on], [])) |
| | 252 | MainApplication.undoRedo.add(Command.SequenceCommand( |
| | 253 | "Split fork into 2 ways", commandsList)) |
| | 254 | commandsList = [] |
| | 255 | |
| | 256 | if roundabout_way_before and roundabout_way_after: |
| | 257 | origway = roundabout_way_before |
| | 258 | roundabout_way_before.addNode(node_after) |
| | 259 | commandsList.append(Command.ChangeCommand( |
| | 260 | origway, roundabout_way_before)) |
| | 261 | origway = roundabout_way_after |
| | 262 | roundabout_way_after.removeNode(common_node) |
| | 263 | commandsList.append(Command.ChangeCommand( |
| | 264 | origway,roundabout_way_after)) |
| | 265 | # middleway = Way(roundabout_way_after).setNodes( |
| | 266 | # [node_before, common_node, node_after]) |
| | 267 | # commandsList.append(Command.AddCommand(editLayer.data, middleway)) |
| | 268 | # MainApplication.undoRedo.add(Command.SequenceCommand( |
| | 269 | # "Add way on roundabout where fork attaches", commandsList)) |
| | 270 | commandsList.append(Command.SplitWayCommand.split( |
| | 271 | roundabout_way_before, [node_before], [])) |
| | 272 | MainApplication.undoRedo.add(Command.SequenceCommand( |
| | 273 | "Split roundabout way", commandsList)) |
| | 274 | commandsList = [] |
| | 275 | |
| | 276 | }}} |
| | 277 | |
| | 278 | |
| | 279 | == Export a collection of routes to Garmin GPX file == |