Changes between Version 15 and Version 16 of Help/Plugin/Scripting/Python


Ignore:
Timestamp:
2018-09-08T18:43:41+02:00 (7 years ago)
Author:
Polyglot
Comment:

extended the fork adder so it now works on split roundabouts as well. It still breaks the relations atm.

Legend:

Unmodified
Added
Removed
Modified
  • Help/Plugin/Scripting/Python

    v15 v16  
    33Some more examples in Python:
    44
    5 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 atm.
     5Convert 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.
    66
    77{{{#!python
     
    114114
    115115
     116This 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.
     117
     118{{{#!python
     119from javax.swing import JOptionPane
     120from org.openstreetmap.josm.gui import MainApplication
     121
     122import org.openstreetmap.josm.command as Command
     123import org.openstreetmap.josm.data.osm.Way as Way
     124
     125editLayer = MainApplication.getLayerManager().getEditLayer()
     126print '==== Fresh run ===='
     127if editLayer and editLayer.data:
     128    selected_ways = editLayer.data.getSelectedWays()
     129    print selected_ways
     130
     131    if not(selected_ways) or len(selected_ways)>1:
     132        JOptionPane.showMessageDialog(MainApplication.parent, "Please select a single way that connects to a roundabout")
     133    else:
     134        for way in selected_ways:
     135            if way.get('oneway') in ['yes', '-1']:
     136                JOptionPane.showMessageDialog(MainApplication.parent, "This way has oneway tag set")
     137            elif way.get('junction') in ['roundabout']:
     138                JOptionPane.showMessageDialog(MainApplication.parent, "This way is part of a roundabout")
     139            else:
     140                node_before = None
     141                node_after = None
     142                common_node = None
     143                common_node_becomes_node_before=None
     144                roundabout_way_before = None
     145                roundabout_way_after = None
     146                for fln in [way.firstNode(),  way.lastNode()]:
     147                    print 'fln',  fln
     148                    for parentway in fln.getParentWays():
     149                        if parentway.get("junction")=='roundabout':
     150                            print parentway.get('name')
     151                            if parentway.isClosed():
     152                                # unsplit roundabout
     153                                for n in parentway.getNodes():
     154                                    if common_node:
     155                                        # found common node between selected way
     156                                        # and roundabout way in previous iteration
     157                                        node_after = n
     158                                        print node_before.get('name')
     159                                        print node_after.get('name')
     160                                        # we are done here
     161                                        break
     162                                    if n.getId() == fln.getId():
     163                                        # this is the node the roundabout has in common with selected way
     164                                        common_node = n
     165                                        print common_node.get('name')
     166                                        if not(node_before):
     167                                            # normally we encountered a node on the roundabout way
     168                                            # before this one, but if the common node is the first node
     169                                            # of an unsplit roundabout, we will need to take the last
     170                                            # node of the roundabout instead
     171                                            node_before = parentway.getNodes()[-2]
     172                                            node_after = parentway.getNodes()[1]
     173                                            print node_before.get('name')
     174                                            print node_after.get('name')
     175                                            break
     176                                        # if not we go through the loop one more time to put the next
     177                                        # node in node_after
     178                                        continue
     179                                    node_before = n
     180                            else:
     181                                # this is a split roundabout
     182                                if parentway.firstNode().getId() == fln.getId():
     183                                    node_after = parentway.getNodes()[1]
     184                                    roundabout_way_after = parentway
     185                                    print 'node after', node_after.get('name')
     186                                else:
     187                                    node_before = parentway.getNodes()[-2]
     188                                    roundabout_way_before = parentway
     189                                if node_before and node_after:
     190                                    common_node = fln
     191                                    break
     192
     193                            if common_node:
     194                                # if common_node is already defined at this point, it means it was
     195                                # the first node of the selected way,
     196                                # so it will have to be replaced with node_before in the selected way
     197                                common_node_becomes_node_before = True
     198                                break
     199                            else:
     200                                common_node_becomes_node_before = False
     201                       
     202                if common_node.getId() == way.firstNode().getId():
     203                    adjacent_node_to_split_on = way.getNodes()[1]
     204                else:
     205                    adjacent_node_to_split_on = way.getNodes()[-1]
     206
     207                if not(common_node) or ((parentway.isClosed() and common_node_becomes_node_before==None)):
     208                    JOptionPane.showMessageDialog(MainApplication.parent, "Please select a way that connects to a roundabout")
     209                else:
     210                    commandsList = []
     211                    if len(way.getNodes())>2:
     212                        # split off last segment before roundabout if needed
     213                        commandsList.append(Command.SplitWayCommand.split(
     214                                    way, [adjacent_node_to_split_on], []))
     215                        MainApplication.undoRedo.add(Command.SequenceCommand(
     216                                    "Split selected way", commandsList))
     217                        commandsList = []
     218                    # After splitting find the segment that connects to the roundabout again
     219                    for waypartconnectedtoroundabout in adjacent_node_to_split_on.getParentWays():
     220                        if waypartconnectedtoroundabout.get('junction') == 'roundabout':
     221                            continue
     222                        if common_node in waypartconnectedtoroundabout.getNodes():
     223                            break
     224                    if len(way.getNodes())==2:
     225                        if common_node == waypartconnectedtoroundabout.firstNode():
     226                            adjacent_node_to_split_on = waypartconnectedtoroundabout.lastNode()
     227                        else:
     228                            adjacent_node_to_split_on = waypartconnectedtoroundabout.firstNode()
     229                    # time to actually do something
     230                    # make a copy of the way
     231                    modified_way = Way(waypartconnectedtoroundabout)
     232                    # replace its nodes, so it becomes a fork
     233                    modified_way.setNodes([node_before, adjacent_node_to_split_on, node_after])
     234                    # add oneway tag
     235                    modified_way.put('oneway', 'yes')
     236                    # apply the changes
     237                    commandsList.append(Command.ChangeCommand(
     238                                    waypartconnectedtoroundabout, modified_way))
     239                    MainApplication.undoRedo.add(Command.SequenceCommand(
     240                                    "Add oneway tag and create forked way", commandsList))
     241                    commandsList = []
     242                    # split this way where it forks
     243                    commandsList.append(Command.SplitWayCommand.split(
     244                                    waypartconnectedtoroundabout, [adjacent_node_to_split_on], []))
     245                    MainApplication.undoRedo.add(Command.SequenceCommand(
     246                                    "Split fork into 2 ways", commandsList))
     247                    commandsList = []
     248                   
     249                    if roundabout_way_before and roundabout_way_after:
     250                        origway = roundabout_way_before
     251                        roundabout_way_before.addNode(node_after)
     252                        commandsList.append(Command.ChangeCommand(
     253                                        origway,  roundabout_way_before))
     254                        origway = roundabout_way_after
     255                        roundabout_way_after.removeNode(common_node)
     256                        commandsList.append(Command.ChangeCommand(
     257                                        origway,roundabout_way_after))
     258#                        middleway = Way(roundabout_way_after).setNodes(
     259#                                        [node_before, common_node, node_after])
     260#                        commandsList.append(Command.AddCommand(editLayer.data, middleway))
     261#                        MainApplication.undoRedo.add(Command.SequenceCommand(
     262#                                        "Add way on roundabout where fork attaches", commandsList))
     263                        commandsList.append(Command.SplitWayCommand.split(
     264                                        roundabout_way_before, [node_before], []))
     265                        MainApplication.undoRedo.add(Command.SequenceCommand(
     266                                        "Split roundabout way", commandsList))
     267                        commandsList = []
     268
     269}}}
     270
     271
    116272Export a collection of routes to Garmin GPX file (not a super example, as I don't think we do collection relations anymore):
    117273
    118274{{{#!python
    119 #!/bin/jython
    120275#!/bin/jython
    121276'''