rendered paste body#!/usr/bin/python
import sys, math, cairo
from xml.etree import ElementTree
WIDTH, HEIGHT, DOTSIZE = 1024, 768, 3
if len(sys.argv) > 1:
osm_filename = sys.argv[1]
else:
osm_filename = 'osm.xml'
tree = ElementTree.parse(osm_filename)
root = tree.getroot()
if root.tag != 'osm':
print "%s is not an osm XML file" % osm_filename
sys.exit(1)
# Parse XML
box = {}
nodes = {}
ways = {}
for elem in root:
if elem.tag == "bounds":
box['minlat'] = float(elem.attrib['minlat'])
box['minlon'] = float(elem.attrib['minlon'])
box['maxlat'] = float(elem.attrib['maxlat'])
box['maxlon'] = float(elem.attrib['maxlon'])
#
#
#
elif elem.tag == "node":
# <node id="241468971" lat="48.7557063" lon="2.2961071" ...>
node = {}
id = int(elem.attrib['id'])
node['visible'] = (True if elem.attrib['visible'] == "true" else False)
node['lat'] = float(elem.attrib['lat'])
node['lon'] = float(elem.attrib['lon'])
node['tags'] = []
for children in elem:
if children.tag == "tag":
# <tag k="created_by" v="JOSM" />
node['tags'].append((children.attrib['k'], children.attrib['v']))
else:
print "Unknown sub xml tag \"%s\" for node" % ElementTree.tostring(elem)
nodes[id] = node
#
#
#
elif elem.tag == "way":
# <way id="22493122" visible="true" ...>
way = {}
id = int(elem.attrib['id'])
if elem.attrib['visible'] != "true":
continue
way['tags'] = []
way['nodes'] = []
way['road'] = 0
for children in elem:
if children.tag == "tag":
# <tag k="created_by" v="JOSM" />
way['tags'].append((children.attrib['k'], children.attrib['v']))
if children.attrib['k'] == "highway":
way['road'] = 1
elif children.tag == "nd":
# <nd ref="233273784"/>
way['nodes'].append(int(children.attrib['ref']))
else:
print "Unknown sub xml tag \"%s\" for way" % ElementTree.tostring(elem)
ways[id] = way
#
#
#
else:
print "Unknown xml tag \"%s\"" % ElementTree.tostring(elem)
def map_longitude_to_screen_coor(longitude):
return ((longitude - box['minlon']) * (WIDTH)) / (box['maxlon'] - box['minlon'])
def map_latitude_to_screen_coor(latitude):
return ((latitude - box['minlat']) * (HEIGHT)) / (box['maxlat'] - box['minlat'])
# Use cairo to make the graphic
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
#surface = cairo.PSSurface("example.ps", WIDTH, HEIGHT)
#surface = cairo.PSSurface("example.pdf", WIDTH, HEIGHT)
#surface = cairo.SVGSurface("example.svg", WIDTH, HEIGHT)
cr = cairo.Context(surface)
# Plot dot
cr.set_source_rgba(1.0, 0.1, 0.1, 0.8)
for way_id in ways:
for node_id in ways[way_id]['nodes']:
lat = map_latitude_to_screen_coor(nodes[node_id]['lat'])
lon = map_longitude_to_screen_coor(nodes[node_id]['lon'])
cr.arc(lat, lon, 3, 0, 2 * math.pi)
cr.fill()
# Plot line
for way_id in ways:
first = 1
for node_id in ways[way_id]['nodes']:
lat = map_latitude_to_screen_coor(nodes[node_id]['lat'])
lon = map_longitude_to_screen_coor(nodes[node_id]['lon'])
if first:
first = 0
cr.move_to(lat, lon)
else:
cr.line_to(lat, lon)
if ways[way_id]['road'] == 1:
cr.set_source_rgba(0.1, 0.4, 1.0, 1.0)
cr.stroke ()
else:
cr.set_source_rgba(0.1, 0.6, 0.0, 0.5)
cr.fill()
cr.stroke ()
surface.write_to_png ("example.png")