(no subject)
Dec. 30th, 2008 01:44 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
использую жж как файло помойку :)
#!/usr/bin/python import sys import re filename = sys.argv[1] type = sys.argv[2] print "process [%s]" % filename tags = { 'cil': {'natural':'coastline'}, 'bdy': {'boundary':'administrative','admin_level':'8'}, 'riv': {'waterway':'river'}, }[type] reSeg = re.compile( 'segment +([0-9]+) +rank +([0-9]+) +points +([0-9]+)' ) reCrd = re.compile( '[ \t]*([0-9]+\.[0-9]+)[ \t]+([0-9]+\.[0-9]+)' ) file = open(filename) out = open("%s.osm" % filename, 'w') line = 0 id = {'cil':-1000000, 'bdy':-2000000, 'riv':-3000000}[type] ids = [] out.write( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ) out.write( "<osm version=\"0.5\" generator=\"wdb2osm.py\">\n" ) while True: src = file.readline() if src == '': break src = src.strip() matchResult = reSeg.match( src ) if matchResult: print "new seg [%s]" % src if ids: id -= 1 out.write( "<way id=\"%d\" user=\"\" timestamp=\"\">\n" % ( id ) ) for ref in ids: out.write( "<nd ref=\"%d\" />\n" % ( ref ) ) for k in tags.keys(): out.write( "<tag k=\"%s\" v=\"%s\" />\n" % ( k, tags[k] ) ) out.write( "</way>\n" ) ids = [] else: matchResult = reCrd.match( src ) if matchResult: id -= 1 ids.append( id ) lat = float( matchResult.group(1) ) lon = float( matchResult.group(2) ) out.write( "<node id=\"%d\" lat=\"%f\" lon=\"%f\" user=\"\" timestamp=\"\" />\n" % ( id, lat, lon ) ) line += 1 out.write( "</osm>\n" )