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


Ignore:
Timestamp:
2017-10-10T14:31:50+02:00 (7 days ago)
Author:
DiGro
Comment:

adjusted to revision 13

Legend:

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

    v4 v5  
    1 [[TranslatedPages(revision=11)]]
     1[[TranslatedPages(revision=13)]]
    22Nog enkele voorbeelden in Python:
    33
    4 Exporteer een verzameling routes naar een Garmin GPX-bestand:
     4Exporteer een verzameling routes naar een Garmin GPX-bestand (geen bijzonder goed voorbeeld, omdat er geen relaties voor colllections meer bestaan):
    55
    66
     
    99'''
    1010RWN2Garmin.py  - Numbered networks to Garmin GPX file converter
    11 This code is released under the GNU General
    12 Public License v2 or later.
     11This code is released under the GNU General Public License v2 or later.
    1312
    1413The GPL v3 is accessible here:
    1514http://www.gnu.org/licenses/gpl.html
    1615
    17 The GPL v2 is accessible here:
    18 http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    19 
    2016It comes with no warranty whatsoever.
    21 
    22 This code illustrates how to use Jython to:
    23 * work with selected items or how to process all the primitives of a certain kind (node, way, relation)
    2417
    2518'''
     
    3427import time
    3528
    36 def getMapView():
    37     if Main.main and Main.main.map:
    38         return Main.main.map.mapView
    39     else:
    40         return None
    41 
    42 mv = getMapView()
    4329f = open('C:/export.gpx', 'w')
    4430f.write('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n')
    4531f.write('<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="OSM Route Manager" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">\n')
    4632f.write('<!-- All data by OpenStreetMap, licensed under cc-by-sa-2.0 (http://creativecommons.org/licenses/by-sa/2.0/). -->\n')
    47 if mv and mv.editLayer and mv.editLayer.data:
    48     #selectedNodes = mv.editLayer.data.getSelectedNodes()
    49     #selectedWays = mv.editLayer.data.getSelectedWays()
     33
     34editLayer = Main.getLayerManager().getEditLayer()
     35if editLayer and editLayer.data:
    5036    selectedRelations = mv.editLayer.data.getSelectedRelations()
    5137
     
    5339        JOptionPane.showMessageDialog(Main.parent, "Please select a collection relation")
    5440    else:
    55         # nodetype = Node().getType()
    5641        print
    5742        for collection in selectedRelations:
     
    119104
    120105This code illustrates how to use Jython to:
    121 * Download all referrer for an element
     106* Download all referrers for an element
    122107
    123108'''
    124109from javax.swing import JOptionPane
    125110from org.openstreetmap.josm import Main
    126 import org.openstreetmap.josm.data.osm.Node as Node
    127 import org.openstreetmap.josm.data.osm.Way as Way
    128 import org.openstreetmap.josm.data.osm.Relation as Relation
    129 import org.openstreetmap.josm.data.osm.TagCollection as TagCollection
    130 import org.openstreetmap.josm.data.osm.DataSet as DataSet
    131 import org.openstreetmap.josm.data.osm.RelationMember as RelationMember
    132111import org.openstreetmap.josm.actions.DownloadReferrersAction as DownloadReferrersAction
    133 #import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask as DownloadReferrersTask;
    134 
    135 def getMapView():
    136     if Main.main and Main.main.map:
    137         return Main.main.map.mapView
    138     else:
    139         return None
    140 
    141 mv = getMapView()
    142 if mv and mv.editLayer and mv.editLayer.data:
    143     selectedElements = mv.editLayer.data.getSelected()
     112
     113editLayer = Main.getLayerManager().getEditLayer()
     114if editLayer and editLayer.data:
     115    selectedElements = editLayer.data.getSelected()
    144116   
    145117    if not(selectedElements):
    146         JOptionPane.showMessageDialog(Main.parent, "Please select an element")
     118        JOptionPane.showMessageDialog(Main.parent, "Selecteer een element")
    147119    else:
    148         DownloadReferrersAction.downloadReferrers(mv.editLayer, selectedElements)
     120        DownloadReferrersAction.downloadReferrers(editLayer, selectedElements)
    149121}}}
    150122
    151123
    152124Download ontbrekende leden van de relatie:
    153 
    154125
    155126{{{#!python
     
    182153import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask as DownloadRelationMemberTask
    183154
    184 def getMapView():
    185     if Main.main and Main.main.map:
    186         return Main.main.map.mapView
    187     else:
    188         return None
    189 
    190 mv = getMapView()
    191 if mv and mv.editLayer and mv.editLayer.data:
    192     selectedRelations = mv.editLayer.data.getSelectedRelations()
     155editLayer = Main.getLayerManager().getEditLayer()
     156if editLayer:
     157    selectedRelations = editLayer.data.getSelectedRelations())
    193158   
    194159    if not(selectedRelations):
    195         JOptionPane.showMessageDialog(Main.parent, "Please select a node")
     160        JOptionPane.showMessageDialog(Main.parent, "Selecteer een relatie")
    196161    else:
    197162        for relation in selectedRelations:
     
    199164                #print dir(relation)
    200165                print dir(DownloadRelationMemberTask)
    201                 DownloadRelationMemberTask.run(DownloadRelationMemberTask(relation, relation.getIncompleteMembers(), mv.editLayer ))
     166                DownloadRelationMemberTask.run(DownloadRelationMemberTask(relation, relation.getIncompleteMembers(), editLayer ))
    202167
    203168}}}
     
    420385http://www.gnu.org/licenses/gpl.html
    421386
    422 The GPL v2 is accessible here:
    423 http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    424 
    425387It comes with no warranty whatsoever.
    426388
    427389This code Loops through selected turn restriction relations, trying to remove ways split from originally the same way (with to / from roles) under turn restriction relations which should no longer remain as members of these relations, as a result of a Potlatch issue: https://trac.openstreetmap.org/ticket/3254
    428390
    429 Only work for turn restrictions with one via node
     391Only works for turn restrictions with one via node
    430392
    431393e.g. Original      : from: Way1, via: Node, to:Way2
     
    454416class RestrictionError(Exception):
    455417    pass
    456 
    457 def getMapView():
    458     if Main.main and Main.main.map:
    459         return Main.main.map.mapView
    460     else:
    461         return None
    462418
    463419def getMembers (restriction):
     
    596552   
    597553validrestrictiontypes = ('only_straight_on', 'only_right_turn', 'only_left_turn', 'no_right_turn', 'no_left_turn', 'no_straight_on', 'no_u_turn')
    598 mv = getMapView()
    599 if mv and mv.editLayer and mv.editLayer.data:
    600     selectedRelations = mv.editLayer.data.getSelectedRelations()
     554editLayer = Main.getLayerManager().getEditLayer()
     555if editLayer and editLayer.data:
     556    selectedRelations = editLayer.data.getSelectedRelations()
    601557   
    602558    if not(selectedRelations):
     
    607563            if relation.get('type') == "restriction" and relation.get('restriction') in validrestrictiontypes:
    608564                try:
    609                     memberlist = getMembers (relation)     
    610                     #print "".join(v * len(memberlist[v]) for v in memberlist.keys())           
     565                    memberlist = getMembers (relation)               
    611566                    if (len (memberlist["from"])) > 1 or (len (memberlist["to"])) > 1 :
    612567                        # Repair attempt