import MySQLdb import osm c = MySQLdb.connect("localhost", "user", "pass", "osm") cur = c.cursor() cont = True i = 0 delta = 1 startlat = lowlat = -30 startlon = lowlon = -45 endlat = 30 endlon = 45 def microrange(start, finish, step): i = start while i <= finish: yield i i += step for lat in microrange(startlat+delta, endlat, delta): for lon in microrange(startlon+delta, endlon, delta): ids = [] cur.execute("""select id, latitude, longitude, visible, tags from (select * from ( select nodes.id, nodes.latitude, nodes.longitude, nodes.visible, nodes.tags from nodes where latitude > %s and latitude < %s and longitude > %s and longitude < %s order by nodes.timestamp desc) as b group by id) as c where visible = true and latitude > %s and latitude < %s and longitude > %s and longitude < %s """ % (lowlat, lat, lowlon, lon, lowlat, lat, lowlon, lon)) data = cur.fetchall() for j in data: ids.append(str(j[0])) node = {'id':j[0], 'lat':j[1], 'lon': j[2], 'tags':j[4]} osm.add_node(node) idstring = "(%s)" % ",".join(ids) if len(ids): cur.execute("""SELECT segment.id, segment.node_a, segment.node_b, segment.tags FROM ( select * from (SELECT * FROM segments where (node_a IN %s OR node_b IN %s) ORDER BY timestamp DESC) as a group by id) as segment where visible = true""" % (idstring, idstring)) data = cur.fetchall() newids = [] segids = [] for j in data: osm.add_segment({'id':j[0], 'from': j[1], 'to': j[2], 'tags': j[3]}) if str(j[1]) not in ids: newids.append(str(j[1])) if str(j[2]) not in ids: newids.append(str(j[2])) segids.append(str(j[0])) newidstring = "(%s)" % ",".join(newids) if len(newids): cur.execute("""select id, latitude, longitude, visible, tags from (select * from ( select nodes.id, nodes.latitude, nodes.longitude, nodes.visible, nodes.tags from nodes where id IN %s order by nodes.timestamp desc) as b group by id) as c where visible = true AND id IN %s """ % (newids, newids)) data = cur.fetchall() for j in data: node = {'id':j[0], 'lat':j[1], 'lon': j[2], 'tags':j[4]} osm.add_node(node) # if len(segids): # type = "way" # segstring = "(%s)" % ",".join(newids) # cur.execute("""select g.id from %ss as g, # (select id, max(version) as version from %ss where id in # (select distinct a.id from # (select id, max(version) as version from %s_segments where segment_id in %s group by id) as a # ) group by id # ) as b where g.id = b.id and g.version = b.version and g.visible = 1 """ % (type, type, type, segstring) ) # data = cur.fetchall() # for j in data: # osm.add_multi({'id':) lowlon = lon lowlat = lat