Changes between Version 13 and Version 14 of Help/Plugin/Scripting/Python


Ignore:
Timestamp:
2018-09-07T18:46:57+02:00 (8 years ago)
Author:
Polyglot
Comment:

add another 'modern' example

Legend:

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

    v13 v14  
    33Some more examples in Python:
    44
     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 atm.
     6
     7{{{#!python
     8from javax.swing import JOptionPane
     9from org.openstreetmap.josm.gui import MainApplication
     10
     11import org.openstreetmap.josm.command as Command
     12import org.openstreetmap.josm.data.osm.Way as Way
     13
     14editLayer = MainApplication.getLayerManager().getEditLayer()
     15if editLayer and editLayer.data:
     16    selected_ways = editLayer.data.getSelectedWays()
     17    print selected_ways
     18
     19    if not(selected_ways) or len(selected_ways)>1:
     20        JOptionPane.showMessageDialog(MainApplication.parent, "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 roundabout way in previous iteration
     38                                    node_after = n
     39                                    # we are done here
     40                                    break
     41                                if n.getId() == fln.getId():
     42                                    # this is the node the roundabout has in common with selected way
     43                                    common_node = n
     44                                    if not(node_before):
     45                                        # normally we encountered a node on the roundabout way before this one
     46                                        # but if the common node is the first node of an unsplit roundabout
     47                                        # we will need to take the last node of the roundabout instead
     48                                        node_before = parentway.getNodes()[-2]
     49                                        node_after = parentway.getNodes()[1]
     50                                        break
     51                                    # if not we go through the loop one more time to put the next node in node_after
     52                                    continue
     53                                node_before = n
     54                        if common_node:
     55                            # if common_node is already defined at this point, it means it was the first node of the selected way
     56                            # so it will have to be replaced with node_before in the selected way
     57                            common_node_becomes_node_before = True
     58                            adjacent_node_to_split_on = way.getNodes()[1]
     59                            break
     60                        else:
     61                            common_node_becomes_node_before = False
     62                            adjacent_node_to_split_on = way.getNodes()[-1]
     63                if not(common_node) or common_node_becomes_node_before==None:
     64                    JOptionPane.showMessageDialog(MainApplication.parent, "Please select a way that connects to a roundabout")
     65                else:
     66                    print common_node.get('name')
     67                    print node_before.get('name')
     68                    print node_after.get('name')
     69
     70                    commandsList = []
     71                    if len(way.getNodes())>2:
     72                        commandsList.append(Command.SplitWayCommand.split(way, [adjacent_node_to_split_on], []))
     73                        MainApplication.undoRedo.add(Command.SequenceCommand("Split selected way", commandsList))
     74                        commandsList = []
     75                    for waypartconnectedtoroundabout in adjacent_node_to_split_on.getParentWays():
     76                        if common_node in waypartconnectedtoroundabout.getNodes():
     77                            break
     78                    if len(way.getNodes())==2:
     79                        if common_node == waypartconnectedtoroundabout.firstNode():
     80                            adjacent_node_to_split_on = waypartconnectedtoroundabout.lastNode()
     81                        else:
     82                            adjacent_node_to_split_on = waypartconnectedtoroundabout.firstNode()
     83                    # time to actually do something
     84                    # make a copy of the way
     85                    modified_way = Way(waypartconnectedtoroundabout)
     86                    # replace its nodes, so it becomes a fork
     87                    modified_way.setNodes([node_before, adjacent_node_to_split_on, node_after])
     88                    #
     89                    modified_way.put('oneway', 'yes')
     90                    #
     91                    commandsList.append(Command.ChangeCommand(waypartconnectedtoroundabout, modified_way))
     92                    MainApplication.undoRedo.add(Command.SequenceCommand("Add oneway tag and create forked way", commandsList))
     93                    commandsList = []
     94                    commandsList.append(Command.SplitWayCommand.split(waypartconnectedtoroundabout, [adjacent_node_to_split_on], []))
     95                    MainApplication.undoRedo.add(Command.SequenceCommand("Split fork into 2 ways", commandsList))
     96                    commandsList = []
     97
     98}}}
     99
     100
    5101Export a collection of routes to Garmin GPX file (not a super example, as I don't think we do collection relations anymore):
    6102
    7 
    8103{{{#!python
     104#!/bin/jython
    9105#!/bin/jython
    10106'''