#!/usr/bin/python # Parse an XML file produced by the Overpass API with the geometry="center" # print option. Produces a file where the way is replaced with a single # node, placed at the way center. import sys import xml.etree.ElementTree xml_file = sys.argv[1] tag_type = sys.argv[2] def escape(s): return s.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """) print u''.encode('utf-8') print u''.encode('utf-8') e = xml.etree.ElementTree.parse(xml_file).getroot() for way in e.findall(u'way'): way_id = int(way.get(u"id")) center = way.find(u"center") tag_k_type = way.find(u"tag[@k='%s']" % (tag_type,)) tag_k_name = way.find(u"tag[@k='name']") #print center.items() #print tag_k_type.items() #print tag_k_name.items() if center != None and tag_k_type != None: if tag_k_name != None: name = tag_k_name.get('v') else: name = tag_k_type.get('v') print (u'' % (way_id, float(center.get('lat')), float(center.get('lon')))).encode('utf-8') print (u' ' % (escape(tag_type), escape(tag_k_type.get('v')))).encode('utf-8') print (u' ' % (escape(name),)).encode('utf-8') print u''.encode('utf-8') print u''