Reindenting to 2-space indents

This commit is contained in:
Komяpa 2010-05-01 17:16:16 +03:00
parent c9a19f4da2
commit 542a1da2ec
3 changed files with 361 additions and 362 deletions

View file

@ -18,23 +18,22 @@ import datetime
import sys
def debug(st):
"""
Debug write to stderr
"""
"""
Debug write to stderr
"""
sys.stderr.write(str(st)+"\n")
sys.stderr.flush()
sys.stderr.write(str(st)+"\n")
sys.stderr.flush()
class Timer:
"""
A small timer for debugging
"""
def __init__(self, comment):
self.time = datetime.datetime.now()
self.comment = comment
debug("%s started" % comment)
def stop(self):
debug("%s finished in %s" % (self.comment, str(datetime.datetime.now() - self.time)))
"""
A small timer for debugging
"""
def __init__(self, comment):
self.time = datetime.datetime.now()
self.comment = comment
debug("%s started" % comment)
def stop(self):
debug("%s finished in %s" % (self.comment, str(datetime.datetime.now() - self.time)))

510
src/kothic.py Executable file → Normal file
View file

@ -30,291 +30,291 @@ from debug import debug, Timer
try:
import psyco
psyco.full()
import psyco
psyco.full()
except ImportError:
debug("Psyco import failed. Program may run slower. Ir you run it on i386 machine, please install Psyco to get best performance.")
debug("Psyco import failed. Program may run slower. Ir you run it on i386 machine, please install Psyco to get best performance.")
class Renderer(threading.Thread):
def __init__(self, comm):
self.comm = comm
threading.Thread.__init__(self)
def run(self):
debug("Thread started")
self.tc = {}
while(True):
while(True):
request = self.comm[0].get()
if(self.comm[0].empty):
break
#debug (" got request:", request)
t = Timer("Rendering screen")
res = RasterTile(request.size[0], request.size[1], request.zoomlevel, request.data_projection)
res.update_surface(request.center_lonlat[0], request.center_lonlat[1], request.zoom, self.tc, request.style)
t.stop()
comm[1].put(res)
def __init__(self, comm):
self.comm = comm
threading.Thread.__init__(self)
def run(self):
debug("Thread started")
self.tc = {}
while(True):
while(True):
request = self.comm[0].get()
if(self.comm[0].empty):
break
#debug (" got request:", request)
t = Timer("Rendering screen")
res = RasterTile(request.size[0], request.size[1], request.zoomlevel, request.data_projection)
res.update_surface(request.center_lonlat[0], request.center_lonlat[1], request.zoom, self.tc, request.style)
t.stop()
comm[1].put(res)
class Navigator:
def __init__(self, comm):
self.comm = comm
self.lon_c = 27.6749791
self.lat_c = 53.8621394
self.width, self.height = 640, 480
self.zoomlevel = 17
self.data_projection = "EPSG:4326"
self.zoom = self.width/0.09;
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.dx = 0
self.dy = 0
self.drag = False
self.tilecache = {}
self.border = 200
self.rastertile = None
self.f = True
undef = None
self.style = [
def __init__(self, comm):
self.comm = comm
self.lon_c = 27.6749791
self.lat_c = 53.8621394
self.width, self.height = 640, 480
self.zoomlevel = 15
self.data_projection = "EPSG:4326"
self.zoom = self.width/0.02;
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.dx = 0
self.dy = 0
self.drag = False
self.tilecache = {}
self.border = 200
self.rastertile = None
self.f = True
undef = None
self.style = [
[None, None, None, 0],
[undef, [6.0, [0,0,0]], [4.0, [1, 1, .7]], 1],
[undef, [4.5, [0,0,0]], [2.5, [1, 1, .7]], 2],
[undef, [6.0, [0,0,0]], [4.0, [1, 1, .7]], 1],
[undef, [4.5, [0,0,0]], [2.5, [1, 1, .7]], 2],
[undef, [3.5, [0,0,0]], [2.5, [1, 1, .7]], 3],
[undef, [2.8, [0,0,0]], [2.0, [1, 1, 1]], 4],
[undef, undef, [1.0, [1, 1, 1]], 5],
[undef, [0, [0.7, 0.4, 0.4]], undef, 6],
[[1, [0.30, 0.5, 0.30]], undef, undef, 7],
[undef, undef, [2, [1, 0.3, 0.3]], 8],
[[0, [0.7, 0.6, 0.6]], undef, undef, 9],
[[0, [0.4, 0.4, 1.0]], undef, undef, 10],
[[0, [0.6, 0.6, 0.6]], undef, undef, 11],
[undef, [3.5, [0.4, 0.4, 1.0]], undef, 12],
[undef, [2, [0.4, 0.4, 1.0]], undef, 13],
[[0, [0.72, 0.51, 0.32]], undef, undef, 14],
[undef, [2.8, [0,0,0]], [2.0, [1, 1, 1]], 4],
[undef, undef, [1.0, [1, 1, 1]], 5],
[undef, [0, [0.7, 0.4, 0.4]], undef, 6],
[[1, [0.30, 0.5, 0.30]], undef, undef, 7],
[undef, undef, [2, [1, 0.3, 0.3]], 8],
[[0, [0.7, 0.6, 0.6]], undef, undef, 9],
[[0, [0.4, 0.4, 1.0]], undef, undef, 10],
[[0, [0.6, 0.6, 0.6]], undef, undef, 11],
[undef, [3.5, [0.4, 0.4, 1.0]], undef, 12],
[undef, [2, [0.4, 0.4, 1.0]], undef, 13],
[[0, [0.72, 0.51, 0.32]], undef, undef, 14],
[[0, [1, 0.0, 0.0]], undef, undef, 0] #unknown landuse
]
da = gtk.DrawingArea()
da.add_events(gtk.gdk.BUTTON1_MOTION_MASK)
da.add_events(gtk.gdk.POINTER_MOTION_MASK)
da.add_events(gtk.gdk.BUTTON_PRESS_MASK)
da.add_events(gtk.gdk.BUTTON_RELEASE_MASK)
da.add_events(gtk.gdk.SCROLL)
# self.window.add_events(gtk.gdk.BUTTON1_MOTION_MASK)
da.connect("expose_event",self.expose_ev)
da.connect("motion_notify_event",self.motion_ev)
da.connect("button_press_event",self.press_ev)
da.connect("button_release_event",self.release_ev)
da.connect("scroll_event",self.scroll_ev)
self.da = da
# self.surface = cairo.ImageSurfaceicreate(gtk.RGB24, self.width, self.height)
self.window.set_size_request(self.width, self.height)
self.window.add(da)
self.window.connect("delete_event", self.delete_ev)
def motion_ev(self, widget, event):
# debug("Motion")
if self.drag:
self.dx = event.x - self.drag_x
self.dy = event.y - self.drag_y
if((abs(self.dx) > 100 or abs(self.dy) > 100) and self.f):
com = MessageContainer()
com.center_lonlat = self.rastertile.screen2lonlat(self.rastertile.w/2 - self.dx, self.rastertile.h/2 - self.dy)
com.data_projection = self.data_projection
com.zoomlevel = self.zoomlevel
com.zoom = self.zoom
com.size = (self.width + self.border*2, self.height + self.border*2)
com.style = self.style
self.comm[0].put(com)
self.request_d = (self.dx, self.dy)
self.f = False
if not self.comm[1].empty():
self.rastertile = self.comm[1].get()
self.f = True
self.drag_x += self.request_d[0]
self.drag_y += self.request_d[1]
self.dx = event.x - self.drag_x
self.dy = event.y - self.drag_y
widget.queue_draw()
def delete_ev(self, widget, event):
gtk.main_quit()
def press_ev(self, widget, event):
if event.button == 1:
debug("Start drag")
self.drag = True
self.drag_x = event.x
self.drag_y = event.y
elif event.button == 2:
debug("Button2")
elif event.button == 3:
debug("Button3")
def release_ev(self, widget, event):
if event.button == 1:
debug("Stop drag")
self.drag = False
# debug("ll:", self.latcenter, self.loncenter)
debug("LL before: %s, %s" % (self.lon_c, self.lat_c))
debug("dd: %s,%s "%(self.dx, self.dy))
self.lon_c, self.lat_c = self.rastertile.screen2lonlat(self.rastertile.w/2 - self.dx, self.rastertile.h/2 - self.dy);
self.dx = self.dy = 0
self.f = True
debug("LL after: %s, %s" % (self.lon_c, self.lat_c))
com = MessageContainer()
com.center_lonlat = (self.lon_c,self.lat_c)
com.data_projection = self.data_projection
com.zoomlevel = self.zoomlevel
com.zoom = self.zoom
com.size = (self.width + self.border*2, self.height + self.border*2)
com.style = self.style
self.comm[0].put(com)
# self.rastertile.update_surface( self.lat_c, self.lon_c, self.zoom, self.tilecache, self.style)
widget.queue_draw()
def scroll_ev(self, widget, event):
# Zoom test :3
if event.direction == gtk.gdk.SCROLL_UP:
self.zoom *= 2
debug("Zoom in")
elif event.direction == gtk.gdk.SCROLL_DOWN:
debug("Zoom out")
def expose_ev(self, widget, event):
# debug("Expose")
if(widget.allocation.width != self.width):
debug("Rrresize!")
self.width = widget.allocation.width
self.height = widget.allocation.height
self.rastertile = None
if self.rastertile is None:
self.rastertile = RasterTile(self.width + self.border*2, self.height + self.border*2, self.zoomlevel, self.data_projection)
self.rastertile.update_surface(self.lon_c, self.lat_c, self.zoom, self.tilecache, self.style)
cr = widget.window.cairo_create()
cr.set_source_surface(self.rastertile.surface, self.dx-self.border, self.dy - self.border)
cr.paint()
# cr.
]
da = gtk.DrawingArea()
da.add_events(gtk.gdk.BUTTON1_MOTION_MASK)
da.add_events(gtk.gdk.POINTER_MOTION_MASK)
da.add_events(gtk.gdk.BUTTON_PRESS_MASK)
da.add_events(gtk.gdk.BUTTON_RELEASE_MASK)
da.add_events(gtk.gdk.SCROLL)
# self.window.add_events(gtk.gdk.BUTTON1_MOTION_MASK)
da.connect("expose_event",self.expose_ev)
da.connect("motion_notify_event",self.motion_ev)
da.connect("button_press_event",self.press_ev)
da.connect("button_release_event",self.release_ev)
da.connect("scroll_event",self.scroll_ev)
self.da = da
# self.surface = cairo.ImageSurfaceicreate(gtk.RGB24, self.width, self.height)
self.window.set_size_request(self.width, self.height)
self.window.add(da)
self.window.connect("delete_event", self.delete_ev)
def motion_ev(self, widget, event):
# debug("Motion")
if self.drag:
self.dx = event.x - self.drag_x
self.dy = event.y - self.drag_y
if((abs(self.dx) > 30 or abs(self.dy) > 30) and self.f):
com = MessageContainer()
com.center_lonlat = self.rastertile.screen2lonlat(self.rastertile.w/2 - self.dx, self.rastertile.h/2 - self.dy)
com.data_projection = self.data_projection
com.zoomlevel = self.zoomlevel
com.zoom = self.zoom
com.size = (self.width + self.border*2, self.height + self.border*2)
com.style = self.style
self.comm[0].put(com)
self.request_d = (self.dx, self.dy)
self.f = False
if not self.comm[1].empty():
self.rastertile = self.comm[1].get()
self.f = True
self.drag_x += self.request_d[0]
self.drag_y += self.request_d[1]
self.dx = event.x - self.drag_x
self.dy = event.y - self.drag_y
widget.queue_draw()
def delete_ev(self, widget, event):
gtk.main_quit()
def press_ev(self, widget, event):
if event.button == 1:
debug("Start drag")
self.drag = True
self.drag_x = event.x
self.drag_y = event.y
elif event.button == 2:
debug("Button2")
elif event.button == 3:
debug("Button3")
def release_ev(self, widget, event):
if event.button == 1:
debug("Stop drag")
self.drag = False
# debug("ll:", self.latcenter, self.loncenter)
debug("LL before: %s, %s" % (self.lon_c, self.lat_c))
debug("dd: %s,%s "%(self.dx, self.dy))
self.lon_c, self.lat_c = self.rastertile.screen2lonlat(self.rastertile.w/2 - self.dx, self.rastertile.h/2 - self.dy);
self.dx = self.dy = 0
self.f = True
debug("LL after: %s, %s" % (self.lon_c, self.lat_c))
def main(self):
self.window.show_all()
gtk.main()
com = MessageContainer()
com.center_lonlat = (self.lon_c,self.lat_c)
com.data_projection = self.data_projection
com.zoomlevel = self.zoomlevel
com.zoom = self.zoom
com.size = (self.width + self.border*2, self.height + self.border*2)
com.style = self.style
self.comm[0].put(com)
# self.rastertile.update_surface( self.lat_c, self.lon_c, self.zoom, self.tilecache, self.style)
widget.queue_draw()
def scroll_ev(self, widget, event):
# Zoom test :3
if event.direction == gtk.gdk.SCROLL_UP:
self.zoom *= 2
debug("Zoom in")
elif event.direction == gtk.gdk.SCROLL_DOWN:
debug("Zoom out")
def expose_ev(self, widget, event):
# debug("Expose")
if(widget.allocation.width != self.width):
debug("Rrresize!")
self.width = widget.allocation.width
self.height = widget.allocation.height
self.rastertile = None
if self.rastertile is None:
self.rastertile = RasterTile(self.width + self.border*2, self.height + self.border*2, self.zoomlevel, self.data_projection)
self.rastertile.update_surface(self.lon_c, self.lat_c, self.zoom, self.tilecache, self.style)
cr = widget.window.cairo_create()
cr.set_source_surface(self.rastertile.surface, self.dx-self.border, self.dy - self.border)
cr.paint()
# cr.
def main(self):
self.window.show_all()
gtk.main()
class MessageContainer:
"""
A class to keep messages to render-threads in.
"""
pass
"""
A class to keep messages to render-threads in.
"""
pass
def line(cr, c):
cr.move_to(c[0], c[1])
for k in range(2, len(c), 2):
cr.line_to(c[k], c[k + 1])
cr.stroke()
cr.move_to(c[0], c[1])
for k in range(2, len(c), 2):
cr.line_to(c[k], c[k + 1])
cr.stroke()
def poly(cr, c):
cr.move_to(c[0], c[1])
for k in range(2, len(c), 2):
cr.line_to(c[k], c[k + 1])
cr.fill()
cr.move_to(c[0], c[1])
for k in range(2, len(c), 2):
cr.line_to(c[k], c[k + 1])
cr.fill()
def ways(t):
# return [y for x in t.itervalues() for y in x.itervalues()]
r = {}
for i in t.values():
r.update(i)
return r.values()
# return [y for x in t.itervalues() for y in x.itervalues()]
r = {}
for i in t.values():
r.update(i)
return r.values()
def load_tile(k):
#debug("loading tile: ", k)
try:
f = open(key_to_filename(k))
except IOError:
# debug ( "Failed open: %s" % key_to_filename(k) )
return {}
t = {}
for line in f:
a = line.split(" ")
w = Way(a[0], int(a[1]), int(a[2]), map(lambda x: float(x), a[3:]))
t[w.id] = w
f.close()
return t
#debug("loading tile: ", k)
try:
f = open(key_to_filename(k))
except IOError:
# debug ( "Failed open: %s" % key_to_filename(k) )
return {}
t = {}
for line in f:
a = line.split(" ")
w = Way(a[0], int(a[1]), int(a[2]), map(lambda x: float(x), a[3:]))
t[w.id] = w
f.close()
return t
class RasterTile:
def __init__(self, width, height, zoom, data_projection):
self.w = width
self.h = height
self.surface = cairo.ImageSurface(cairo.FORMAT_RGB24, self.w, self.h)
self.x_offset = 0
self.y_offset = 0
self.lat_c = None
self.lon_c = None
self.zoomlevel = zoom
self.zoom = None
self.data_projection = data_projection
def screen2lonlat(self, x, y):
return (x - self.w/2)/(math.cos(self.lat_c*math.pi/180)*self.zoom) + self.lon_c, -(y - self.h/2)/self.zoom + self.lat_c
def lonlat2screen(self, lon, lat, lcc):
return (lon - self.lon_c)*lcc*self.zoom + self.w/2, -(lat - self.lat_c)*self.zoom + self.h/2
def update_surface(self, lon, lat, zoom, tilecache, style):
self.zoom = zoom
self.lat_c = lat
self.lon_c = lon
cr = cairo.Context(self.surface)
cr.rectangle(0, 0, self.w, self.h)
cr.set_source_rgb(0.7, 0.7, 0.7)
cr.fill()
lonmin, latmin = self.screen2lonlat(0, self.h)
lonmax, latmax = self.screen2lonlat(self.w, 0)
a,d,c,b = [int(x) for x in projections.tile_by_bbox((lonmin, latmin, lonmax, latmax),self.zoomlevel, self.data_projection)]
def __init__(self, width, height, zoom, data_projection):
self.w = width
self.h = height
self.surface = cairo.ImageSurface(cairo.FORMAT_RGB24, self.w, self.h)
self.x_offset = 0
self.y_offset = 0
self.lat_c = None
self.lon_c = None
self.zoomlevel = zoom
self.zoom = None
self.data_projection = data_projection
def screen2lonlat(self, x, y):
return (x - self.w/2)/(math.cos(self.lat_c*math.pi/180)*self.zoom) + self.lon_c, -(y - self.h/2)/self.zoom + self.lat_c
def lonlat2screen(self, lon, lat, lcc):
return (lon - self.lon_c)*lcc*self.zoom + self.w/2, -(lat - self.lat_c)*self.zoom + self.h/2
def update_surface(self, lon, lat, zoom, tilecache, style):
self.zoom = zoom
self.lat_c = lat
self.lon_c = lon
cr = cairo.Context(self.surface)
cr.rectangle(0, 0, self.w, self.h)
cr.set_source_rgb(0.7, 0.7, 0.7)
cr.fill()
lonmin, latmin = self.screen2lonlat(0, self.h)
lonmax, latmax = self.screen2lonlat(self.w, 0)
a,d,c,b = [int(x) for x in projections.tile_by_bbox((lonmin, latmin, lonmax, latmax),self.zoomlevel, self.data_projection)]
debug((latmin, lonmin, latmax, lonmax))
debug(( a, b, c, d))
#debug((latmin, lonmin, latmax, lonmax))
debug(( a, b, c, d))
#FIXME: add time
active_tile = set([(self.zoomlevel,i,j) for i in range(a, c+1) for j in range(b, d+1)])
debug(active_tile)
for k in tilecache.keys():
if k not in active_tile:
del tilecache[k]
debug("del tile: %s" % (k,))
for k in active_tile:
if k not in tilecache:
tilecache[k] = load_tile(k)
#FIXME add time2
ww = ways(tilecache)
debug("ways: %s" % len(ww))
active_tile = set([(self.zoomlevel,i,j) for i in range(a, c+1) for j in range(b, d+1)])
debug("Active tiles in memory: %s" % len(active_tile))
for k in tilecache.keys():
if k not in active_tile:
del tilecache[k]
debug("del tile: %s" % (k,))
for k in active_tile:
if k not in tilecache:
tilecache[k] = load_tile(k)
#FIXME add time2
ww = ways(tilecache)
debug("ways: %s" % len(ww))
ww.sort(key=lambda x: style[x.style][3])
lcc = math.cos(self.lat_c*math.pi/180)
for w in ww:
cs = []
for k in range(0, len(w.coords), 2):
x, y = self.lonlat2screen(w.coords[k], w.coords[k+1], lcc);
cs.append(x)
cs.append(y)
w.cs = cs
for passn in range(1, 4):
debug("pass %s" % passn)
for w in ww:
stn = w.style
if stn < len(style) and style[stn] is not None and style[stn][passn-1] is not None:
st = style[w.style][passn-1]
cr.set_line_width(st[0])
cr.set_source_rgb(st[1][0], st[1][1], st[1][2])
if w.type == "L":
line(cr, w.cs)
elif w.type == "P":
poly(cr, w.cs)
ww.sort(key=lambda x: style[x.style][3])
lcc = math.cos(self.lat_c*math.pi/180)
for w in ww:
cs = []
for k in range(0, len(w.coords), 2):
x, y = self.lonlat2screen(w.coords[k], w.coords[k+1], lcc);
cs.append(x)
cs.append(y)
w.cs = cs
for passn in range(1, 4):
debug("pass %s" % passn)
for w in ww:
stn = w.style
if stn < len(style) and style[stn] is not None and style[stn][passn-1] is not None:
st = style[w.style][passn-1]
cr.set_line_width(st[0])
cr.set_source_rgb(st[1][0], st[1][1], st[1][2])
if w.type == "L":
line(cr, w.cs)
elif w.type == "P":
poly(cr, w.cs)
class Way:
def __init__(self, type, id, style, coords):
self.type = type
self.id = id
self.coords = coords
self.style = style
self.cs = None
def __init__(self, type, id, style, coords):
self.type = type
self.id = id
self.coords = coords
self.style = style
self.cs = None
def key_to_filename((z,x,y)):
return "tiles/z%s/%s/x%s/%s/y%s.vtile"%(z, x/1024, x, y/1024, y)
if __name__ == "__main__":
comm = (Queue.Queue(), Queue.Queue())
gtk.gdk.threads_init()
nav = Navigator(comm)
r = Renderer(comm)
r.daemon = True
r.start()
nav.main()
comm = (Queue.Queue(), Queue.Queue())
gtk.gdk.threads_init()
nav = Navigator(comm)
r = Renderer(comm)
r.daemon = True
r.start()
nav.main()

184
src/python-osm-converter/osm2tiles.py Executable file → Normal file
View file

@ -21,10 +21,10 @@ from lxml import etree
from twms import projections
try:
import psyco
psyco.full()
import psyco
psyco.full()
except ImportError:
pass
pass
MAXZOOM = 18
proj = "EPSG:4326"
@ -53,114 +53,114 @@ style["P"] = {
# elsif($k eq 'highway' and $v eq 'footway' or $v eq 'path' or $v eq 'track'){
def tilelist_by_geometry(way, start_zoom = 0, ispoly = False):
"""
Gives a number of (z,x,y) tile numbers that geometry crosses.
"""
ret = set([])
tiles_by_zooms = {} # zoom: set(tile,tile,tile...)
for t in xrange(0,MAXZOOM+1):
tiles_by_zooms[t] = set([])
for point in way:
tile = projections.tile_by_coords(point, MAXZOOM, proj)
tile = (MAXZOOM, int(tile[0]),int(tile[1]))
tiles_by_zooms[MAXZOOM].add(tile)
for t in xrange(MAXZOOM-1,start_zoom-1,-1):
for tt in tiles_by_zooms[t+1]:
tiles_by_zooms[t].add((t, int(tt[1]/2), int(tt[2]/2)))
for z in tiles_by_zooms.values():
ret.update(z)
return ret
"""
Gives a number of (z,x,y) tile numbers that geometry crosses.
"""
ret = set([])
tiles_by_zooms = {} # zoom: set(tile,tile,tile...)
for t in xrange(0,MAXZOOM+1):
tiles_by_zooms[t] = set([])
for point in way:
tile = projections.tile_by_coords(point, MAXZOOM, proj)
tile = (MAXZOOM, int(tile[0]),int(tile[1]))
tiles_by_zooms[MAXZOOM].add(tile)
for t in xrange(MAXZOOM-1,start_zoom-1,-1):
for tt in tiles_by_zooms[t+1]:
tiles_by_zooms[t].add((t, int(tt[1]/2), int(tt[2]/2)))
for z in tiles_by_zooms.values():
ret.update(z)
return ret
def pix_distance(a,b,z):
"""
Calculates onscreen disatnce between 2 points on given zoom.
"""
return 2**z*256*(((a[0]-b[0])/360.)**2+((a[1]-b[1])/180.)**2)**0.5
"""
Calculates onscreen disatnce between 2 points on given zoom.
"""
return 2**z*256*(((a[0]-b[0])/360.)**2+((a[1]-b[1])/180.)**2)**0.5
needed_ways_tags = set(['highway','building','landuse'])
def way_interesting(tags):
res = {}
for k,v in tags.iteritems():
if k in needed_ways_tags:
res[k] = v
return res
res = {}
for k,v in tags.iteritems():
if k in needed_ways_tags:
res[k] = v
return res
def main ():
DROPPED_POINTS = 0
tilefiles = {}
osm_infile = open("minsk.osm", "rb")
nodes = {}
curway = []
tags = {}
context = etree.iterparse(osm_infile)
for action, elem in context:
DROPPED_POINTS = 0
tilefiles = {}
osm_infile = open("minsk.osm", "rb")
nodes = {}
curway = []
tags = {}
context = etree.iterparse(osm_infile)
for action, elem in context:
items = dict(elem.items())
if elem.tag == "node":
nodes[int(items["id"])] = (float(items["lon"]), float(items["lat"]))
nodes[int(items["id"])] = (float(items["lon"]), float(items["lat"]))
elif elem.tag == "nd":
curway.append(nodes[int(items["ref"])])
curway.append(nodes[int(items["ref"])])
elif elem.tag == "tag":
tags[items["k"]] = items["v"]
tags[items["k"]] = items["v"]
elif elem.tag == "way":
mzoom = 1
mzoom = 1
way_simplified = {}
for zoom in xrange(MAXZOOM,-1,-1): ######## generalize a bit
# TODO: Douglas-Peucker
prev_point = curway[0]
way = [prev_point]
for point in curway:
if pix_distance(point, prev_point, zoom) > 2.:
way.append(point)
else:
DROPPED_POINTS += 1
prev_point = point
if len(way) == 1:
mzoom = zoom
print zoom
break
if len(way) > 1:
way_simplified[zoom] = way
#print way
way_simplified = {}
for zoom in xrange(MAXZOOM,-1,-1): ######## generalize a bit
# TODO: Douglas-Peucker
prev_point = curway[0]
way = [prev_point]
for point in curway:
if pix_distance(point, prev_point, zoom) > 2.:
way.append(point)
else:
DROPPED_POINTS += 1
prev_point = point
if len(way) == 1:
mzoom = zoom
#print zoom
break
if len(way) > 1:
way_simplified[zoom] = way
#print way
waytype, waynum = 0, 0
for objtype, tagset in style.iteritems():
waytype, waynum = 0, 0
for objtype, tagset in style.iteritems():
for tid, tagz in tagset.iteritems():
for k, v in tagz:
if k in tags:
if v:
if tags[k] not in v:
continue
#print k, v
waytype = objtype
waynum = tid
for tid, tagz in tagset.iteritems():
for k, v in tagz:
if k in tags:
if v:
if tags[k] not in v:
continue
#print k, v
waytype = objtype
waynum = tid
if waytype is not 0:
if waytype is not 0:
for tile in tilelist_by_geometry(curway, mzoom+1):
z, x, y = tile
path = "../tiles/z%s/%s/x%s/%s/"%(z, x/1024, x, y/1024)
if tile not in tilefiles:
z, x, y = tile
path = "../tiles/z%s/%s/x%s/%s/"%(z, x/1024, x, y/1024)
if tile not in tilefiles:
if not os.path.exists(path):
os.makedirs(path)
tilefiles[tile] = "aaa"
tilefile = open(path+"y"+str(y)+".vtile","wb")
tilefile = open(path+"y"+str(y)+".vtile","a")
print >>tilefile, "%s %s %s" % (waytype, items["id"], waynum), " ".join([str(x[0])+" "+str(x[1]) for x in way_simplified[tile[0]]])
tilefile.flush()
tilefile.close()
print len(tilefiles)
#print >>corr, "%s %s %s %s %s %s"% (curway[0][0],curway[0][1],curway[1][0],curway[1][1], user, ts )
curway = []
tags = {}
#user = default_user
#ts = ""
print DROPPED_POINTS
print len(nodes)
if not os.path.exists(path):
os.makedirs(path)
tilefiles[tile] = "aaa"
tilefile = open(path+"y"+str(y)+".vtile","wb")
tilefile = open(path+"y"+str(y)+".vtile","a")
print >>tilefile, "%s %s %s" % (waytype, items["id"], waynum), " ".join([str(x[0])+" "+str(x[1]) for x in way_simplified[tile[0]]])
tilefile.flush()
tilefile.close()
#print >>corr, "%s %s %s %s %s %s"% (curway[0][0],curway[0][1],curway[1][0],curway[1][1], user, ts )
curway = []
tags = {}
#user = default_user
#ts = ""
print "Tiles generated:",len(tilefiles)
print "Nodes dropped when generalizing:", DROPPED_POINTS
print "Nodes in memory:", len(nodes)
main()