| | 1 | Some more examples in Python: |
| | 2 | |
| | 3 | Export a collection of routes to Garmin GPX file: |
| | 4 | |
| | 5 | #!/bin/jython |
| | 6 | ''' |
| | 7 | RWN2Garmin.py - Numbered networks to Garmin GPX file converter |
| | 8 | This code is released under the GNU General |
| | 9 | Public License v2 or later. |
| | 10 | |
| | 11 | The GPL v3 is accessible here: |
| | 12 | http://www.gnu.org/licenses/gpl.html |
| | 13 | |
| | 14 | The GPL v2 is accessible here: |
| | 15 | http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| | 16 | |
| | 17 | It comes with no warranty whatsoever. |
| | 18 | |
| | 19 | This code illustrates how to use Jython to: |
| | 20 | * work with selected items or how to process all the primitives of a certain kind (node, way, relation) |
| | 21 | |
| | 22 | ''' |
| | 23 | from javax.swing import JOptionPane, JDialog |
| | 24 | from java.awt.event import ActionListener, ActionEvent |
| | 25 | from org.openstreetmap.josm import Main |
| | 26 | import org.openstreetmap.josm.command as Command |
| | 27 | import org.openstreetmap.josm.data.osm.Node as Node |
| | 28 | import org.openstreetmap.josm.data.osm.Way as Way |
| | 29 | import org.openstreetmap.josm.data.osm.TagCollection as TagCollection |
| | 30 | import org.openstreetmap.josm.data.osm.DataSet as DataSet |
| | 31 | import time |
| | 32 | |
| | 33 | def getMapView(): |
| | 34 | if Main.main and Main.main.map: |
| | 35 | return Main.main.map.mapView |
| | 36 | else: |
| | 37 | return None |
| | 38 | |
| | 39 | mv = getMapView() |
| | 40 | f = open('C:/export.gpx', 'w') |
| | 41 | f.write('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n') |
| | 42 | f.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') |
| | 43 | f.write('<!-- All data by OpenStreetMap, licensed under cc-by-sa-2.0 (http://creativecommons.org/licenses/by-sa/2.0/). -->\n') |
| | 44 | if mv and mv.editLayer and mv.editLayer.data: |
| | 45 | #selectedNodes = mv.editLayer.data.getSelectedNodes() |
| | 46 | #selectedWays = mv.editLayer.data.getSelectedWays() |
| | 47 | selectedRelations = mv.editLayer.data.getSelectedRelations() |
| | 48 | |
| | 49 | if not(selectedRelations): |
| | 50 | JOptionPane.showMessageDialog(Main.parent, "Please select a collection relation") |
| | 51 | else: |
| | 52 | # nodetype = Node().getType() |
| | 53 | print |
| | 54 | for collection in selectedRelations: |
| | 55 | print 'COLLECTION:', collection |
| | 56 | for member in collection.getMembers(): |
| | 57 | print 'MEMBER:',member |
| | 58 | if member.isNode(): |
| | 59 | node = member.getNode() |
| | 60 | coords = node.getCoor() |
| | 61 | lon = coords.getX() |
| | 62 | lat = coords.getY() |
| | 63 | rwn_ref = node.get('rwn_ref') |
| | 64 | f.write('\t<wpt lat="' + str(lat) + '" lon="' + str(lon) + '">\n') |
| | 65 | if rwn_ref: |
| | 66 | f.write('\t\t<name>' + rwn_ref + '</name>\n') |
| | 67 | f.write('\t</wpt>\n') |
| | 68 | for member in collection.getMembers(): |
| | 69 | if member.isRelation(): |
| | 70 | routerelation = member.getRelation() |
| | 71 | f.write('\t<trk>\n') |
| | 72 | networkname = routerelation.get('network:name') |
| | 73 | if not(networkname): |
| | 74 | networkname = '' |
| | 75 | else: |
| | 76 | networkname += ' ' |
| | 77 | note = routerelation.get('note') |
| | 78 | if not(note): note = '' |
| | 79 | f.write('\t\t<name>' + networkname + note + '</name>\n') |
| | 80 | f.write('\t\t<src>OpenStreetMap.org</src>\n') |
| | 81 | f.write('\t\t<type>foot</type>\n') |
| | 82 | for routerelmember in routerelation.getMembers(): |
| | 83 | if routerelmember.isWay(): |
| | 84 | f.write('\t\t<trkseg>\n') |
| | 85 | way=routerelmember.getWay() |
| | 86 | for waynode in way.getNodes(): |
| | 87 | coords = waynode.getCoor() |
| | 88 | lon = coords.getX() |
| | 89 | lat = coords.getY() |
| | 90 | f.write('\t\t\t<trkpt lat=' + str(lat) + '" lon="' + str(lon) + '"> </trkpt>\n') |
| | 91 | |
| | 92 | f.write('\t\t</trkseg>\n') |
| | 93 | f.write('\t</trk>\n') |
| | 94 | f.write('</gpx>\n') |
| | 95 | f.close() |
| | 96 | |
| | 97 | |
| | 98 | |