Changes between Version 5 and Version 6 of Nl:Help/Plugin/Scripting/Python


Ignore:
Timestamp:
2018-09-10T16:57:10+02:00 (7 years ago)
Author:
DiGro
Comment:

adjusted to revision 16

Legend:

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

    v5 v6  
    1 [[TranslatedPages(revision=13)]]
     1[[TranslatedPages(revision=16)]]
    22Nog enkele voorbeelden in Python:
     3
     4Converteren van een weg die een rotonde verbindt met een vork die bestaat uit twee wegen met éénrichtingverkeer. Splitst de weg op zijn laatste knoop en verbindt die met de 2 naastgelegen knopen van de algemene knoop van de weg van de rotonde. Deze versie werkt alleen met niet gesplitste rotondes. Bekijk het volgende voorbeeld voor een bijgewerkte versie.
     5
     6{{{#!python
     7from javax.swing import JOptionPane
     8from org.openstreetmap.josm.gui import MainApplication
     9
     10import org.openstreetmap.josm.command as Command
     11import org.openstreetmap.josm.data.osm.Way as Way
     12
     13editLayer = MainApplication.getLayerManager().getEditLayer()
     14if editLayer and editLayer.data:
     15    selected_ways = editLayer.data.getSelectedWays()
     16    print selected_ways
     17
     18    if not(selected_ways) or len(selected_ways)>1:
     19        JOptionPane.showMessageDialog(MainApplication.parent,
     20                                         "Please select a single way that connects to a roundabout")
     21    else:
     22        for way in selected_ways:
     23            if way.get('oneway') in ['yes', '-1']:
     24                JOptionPane.showMessageDialog(MainApplication.parent, "This way has oneway tag set")
     25            else:
     26                node_before = None
     27                node_after = None
     28                common_node = None
     29                common_node_becomes_node_before=None
     30                #print dir(way)
     31                for fln in [way.firstNode(),  way.lastNode()]:
     32                    print 'fln',  fln
     33                    for parentway in fln.getParentWays():
     34                        if parentway.get("junction")=='roundabout':
     35                            for n in parentway.getNodes():
     36                                if common_node:
     37                                    # found common node between selected way and
     38                                    # roundabout way in previous iteration
     39                                    node_after = n
     40                                    # we are done here
     41                                    break
     42                                if n.getId() == fln.getId():
     43                                    # this is the node the roundabout has in common with selected way
     44                                    common_node = n
     45                                    if not(node_before):
     46                                        # normally we encountered a node on the roundabout way
     47                                        # before this one, but if the common node is the first node
     48                                        # of an unsplit roundabout, we will need to take the last
     49                                        # node of the roundabout instead
     50                                        node_before = parentway.getNodes()[-2]
     51                                        node_after = parentway.getNodes()[1]
     52                                        break
     53                                    # if not we go through the loop one more time to put the next
     54                                    # node in node_after
     55                                    continue
     56                                node_before = n
     57                        if common_node:
     58                            # if common_node is already defined at this point, it means it was
     59                            # the first node of the selected way,
     60                            # so it will have to be replaced with node_before in the selected way
     61                            common_node_becomes_node_before = True
     62                            adjacent_node_to_split_on = way.getNodes()[1]
     63                            break
     64                        else:
     65                            common_node_becomes_node_before = False
     66                            adjacent_node_to_split_on = way.getNodes()[-1]
     67                if not(common_node) or common_node_becomes_node_before==None:
     68                    JOptionPane.showMessageDialog(MainApplication.parent,
     69                                               "Please select a way that connects to a roundabout")
     70                else:
     71                    print common_node.get('name')
     72                    print node_before.get('name')
     73                    print node_after.get('name')
     74
     75                    commandsList = []
     76                    if len(way.getNodes())>2:
     77                        # split off last segment before roundabout if needed
     78                        commandsList.append(Command.SplitWayCommand.split(
     79                                    way, [adjacent_node_to_split_on], []))
     80                        MainApplication.undoRedo.add(Command.SequenceCommand(
     81                                    "Split selected way", commandsList))
     82                        commandsList = []
     83                    # After splitting find the segment that connects to the roundabout again
     84                    for waypartconnectedtoroundabout in adjacent_node_to_split_on.getParentWays():
     85                        if common_node in waypartconnectedtoroundabout.getNodes():
     86                            break
     87                    if len(way.getNodes())==2:
     88                        if common_node == waypartconnectedtoroundabout.firstNode():
     89                            adjacent_node_to_split_on = waypartconnectedtoroundabout.lastNode()
     90                        else:
     91                            adjacent_node_to_split_on = waypartconnectedtoroundabout.firstNode()
     92                    # time to actually do something
     93                    # make a copy of the way
     94                    modified_way = Way(waypartconnectedtoroundabout)
     95                    # replace its nodes, so it becomes a fork
     96                    modified_way.setNodes([node_before, adjacent_node_to_split_on, node_after])
     97                    # add oneway tag
     98                    modified_way.put('oneway', 'yes')
     99                    # apply the changes
     100                    commandsList.append(Command.ChangeCommand(
     101                                    waypartconnectedtoroundabout, modified_way))
     102                    MainApplication.undoRedo.add(Command.SequenceCommand(
     103                                    "Add oneway tag and create forked way", commandsList))
     104                    commandsList = []
     105                    # split this way where it forks
     106                    commandsList.append(Command.SplitWayCommand.split(
     107                                    waypartconnectedtoroundabout, [adjacent_node_to_split_on], []))
     108                    MainApplication.undoRedo.add(Command.SequenceCommand(
     109                                    "Split fork into 2 ways", commandsList))
     110                    commandsList = []
     111
     112}}}
     113
     114
     115Dit script doet hetzelfde als dat hiervoor, maar werkt ook voor rotondes waarvan de wegen zijn gesplitst. Het bemoeit zich niet met de relaties.
     116
     117{{{#!python
     118from javax.swing import JOptionPane
     119from org.openstreetmap.josm.gui import MainApplication
     120
     121import org.openstreetmap.josm.command as Command
     122import org.openstreetmap.josm.data.osm.Way as Way
     123
     124editLayer = MainApplication.getLayerManager().getEditLayer()
     125print '==== Fresh run ===='
     126if editLayer and editLayer.data:
     127    selected_ways = editLayer.data.getSelectedWays()
     128    print selected_ways
     129
     130    if not(selected_ways) or len(selected_ways)>1:
     131        JOptionPane.showMessageDialog(MainApplication.parent, "Please select a single way that connects to a roundabout")
     132    else:
     133        for way in selected_ways:
     134            if way.get('oneway') in ['yes', '-1']:
     135                JOptionPane.showMessageDialog(MainApplication.parent, "This way has oneway tag set")
     136            elif way.get('junction') in ['roundabout']:
     137                JOptionPane.showMessageDialog(MainApplication.parent, "This way is part of a roundabout")
     138            else:
     139                node_before = None
     140                node_after = None
     141                common_node = None
     142                common_node_becomes_node_before=None
     143                roundabout_way_before = None
     144                roundabout_way_after = None
     145                for fln in [way.firstNode(),  way.lastNode()]:
     146                    print 'fln',  fln
     147                    for parentway in fln.getParentWays():
     148                        if parentway.get("junction")=='roundabout':
     149                            print parentway.get('name')
     150                            if parentway.isClosed():
     151                                # unsplit roundabout
     152                                for n in parentway.getNodes():
     153                                    if common_node:
     154                                        # found common node between selected way
     155                                        # and roundabout way in previous iteration
     156                                        node_after = n
     157                                        print node_before.get('name')
     158                                        print node_after.get('name')
     159                                        # we are done here
     160                                        break
     161                                    if n.getId() == fln.getId():
     162                                        # this is the node the roundabout has in common with selected way
     163                                        common_node = n
     164                                        print common_node.get('name')
     165                                        if not(node_before):
     166                                            # normally we encountered a node on the roundabout way
     167                                            # before this one, but if the common node is the first node
     168                                            # of an unsplit roundabout, we will need to take the last
     169                                            # node of the roundabout instead
     170                                            node_before = parentway.getNodes()[-2]
     171                                            node_after = parentway.getNodes()[1]
     172                                            print node_before.get('name')
     173                                            print node_after.get('name')
     174                                            break
     175                                        # if not we go through the loop one more time to put the next
     176                                        # node in node_after
     177                                        continue
     178                                    node_before = n
     179                            else:
     180                                # this is a split roundabout
     181                                if parentway.firstNode().getId() == fln.getId():
     182                                    node_after = parentway.getNodes()[1]
     183                                    roundabout_way_after = parentway
     184                                    print 'node after', node_after.get('name')
     185                                else:
     186                                    node_before = parentway.getNodes()[-2]
     187                                    roundabout_way_before = parentway
     188                                if node_before and node_after:
     189                                    common_node = fln
     190                                    break
     191
     192                            if common_node:
     193                                # if common_node is already defined at this point, it means it was
     194                                # the first node of the selected way,
     195                                # so it will have to be replaced with node_before in the selected way
     196                                common_node_becomes_node_before = True
     197                                break
     198                            else:
     199                                common_node_becomes_node_before = False
     200                       
     201                if common_node.getId() == way.firstNode().getId():
     202                    adjacent_node_to_split_on = way.getNodes()[1]
     203                else:
     204                    adjacent_node_to_split_on = way.getNodes()[-1]
     205
     206                if not(common_node) or ((parentway.isClosed() and common_node_becomes_node_before==None)):
     207                    JOptionPane.showMessageDialog(MainApplication.parent, "Please select a way that connects to a roundabout")
     208                else:
     209                    commandsList = []
     210                    if len(way.getNodes())>2:
     211                        # split off last segment before roundabout if needed
     212                        commandsList.append(Command.SplitWayCommand.split(
     213                                    way, [adjacent_node_to_split_on], []))
     214                        MainApplication.undoRedo.add(Command.SequenceCommand(
     215                                    "Split selected way", commandsList))
     216                        commandsList = []
     217                    # After splitting find the segment that connects to the roundabout again
     218                    for waypartconnectedtoroundabout in adjacent_node_to_split_on.getParentWays():
     219                        if waypartconnectedtoroundabout.get('junction') == 'roundabout':
     220                            continue
     221                        if common_node in waypartconnectedtoroundabout.getNodes():
     222                            break
     223                    if len(way.getNodes())==2:
     224                        if common_node == waypartconnectedtoroundabout.firstNode():
     225                            adjacent_node_to_split_on = waypartconnectedtoroundabout.lastNode()
     226                        else:
     227                            adjacent_node_to_split_on = waypartconnectedtoroundabout.firstNode()
     228                    # time to actually do something
     229                    # make a copy of the way
     230                    modified_way = Way(waypartconnectedtoroundabout)
     231                    # replace its nodes, so it becomes a fork
     232                    modified_way.setNodes([node_before, adjacent_node_to_split_on, node_after])
     233                    # add oneway tag
     234                    modified_way.put('oneway', 'yes')
     235                    # apply the changes
     236                    commandsList.append(Command.ChangeCommand(
     237                                    waypartconnectedtoroundabout, modified_way))
     238                    MainApplication.undoRedo.add(Command.SequenceCommand(
     239                                    "Add oneway tag and create forked way", commandsList))
     240                    commandsList = []
     241                    # split this way where it forks
     242                    commandsList.append(Command.SplitWayCommand.split(
     243                                    waypartconnectedtoroundabout, [adjacent_node_to_split_on], []))
     244                    MainApplication.undoRedo.add(Command.SequenceCommand(
     245                                    "Split fork into 2 ways", commandsList))
     246                    commandsList = []
     247                   
     248                    if roundabout_way_before and roundabout_way_after:
     249                        origway = roundabout_way_before
     250                        roundabout_way_before.addNode(node_after)
     251                        commandsList.append(Command.ChangeCommand(
     252                                        origway,  roundabout_way_before))
     253                        origway = roundabout_way_after
     254                        roundabout_way_after.removeNode(common_node)
     255                        commandsList.append(Command.ChangeCommand(
     256                                        origway,roundabout_way_after))
     257#                        middleway = Way(roundabout_way_after).setNodes(
     258#                                        [node_before, common_node, node_after])
     259#                        commandsList.append(Command.AddCommand(editLayer.data, middleway))
     260#                        MainApplication.undoRedo.add(Command.SequenceCommand(
     261#                                        "Add way on roundabout where fork attaches", commandsList))
     262                        commandsList.append(Command.SplitWayCommand.split(
     263                                        roundabout_way_before, [node_before], []))
     264                        MainApplication.undoRedo.add(Command.SequenceCommand(
     265                                        "Split roundabout way", commandsList))
     266                        commandsList = []
     267
     268}}}
    3269
    4270Exporteer een verzameling routes naar een Garmin GPX-bestand (geen bijzonder goed voorbeeld, omdat er geen relaties voor colllections meer bestaan):