Ejemplo n.º 1
0
wo.transfer_slack = 60 * 5
wo_foot = wo

# TRANSIT
wo = WalkOptions() 
wo.max_walk = 2000 
wo.walking_overage = 0.0
wo.walking_speed = 1.0 # trimet uses 0.03 miles / 1 minute - but it uses straight line distance as well
wo.transfer_penalty = 60 * 10
wo.walking_reluctance = 1.5
wo.max_transfers = 5
wo.transfer_slack = 60 * 4
wo_transit = wo

print "Fetching grid from OSMDB..."
grid = list(osmdb.execute("SELECT x, y, vertex FROM grid"))
max_x, max_y = osmdb.execute("SELECT max(x), max(y) FROM grid").next()

print "Finding unique GTFS station linking points..."
stop_vertices = [e[0] for e in gtfsdb.execute("SELECT DISTINCT osm_vertex FROM osm_links")]

#test
#stop_vertices = stop_vertices[:10]

def save_image(spt, fname) :
    print "saving grid image..."
    im = Image.new("L", (max_x, max_y))
    for (x, y, vertex) in grid :
        v = spt.get_vertex('osm-%s'%vertex)
        if v != None : 
            c = ((v.best_state.time - t0) / (120. * 60)) * 255
Ejemplo n.º 2
0
def FindDisjunctGraphs (dbname):
        db = OSMDB(dbname)
        
        #should really be done before simplifying and splitting
        #fuse_nodes(db)
        
        c = db.cursor()
        c.execute("DROP table if exists graph_nodes")
        c.execute("DROP table if exists graph_edges")
        c.execute("CREATE table graph_nodes (graph_num INTEGER, node_id TEXT, WKT_GEOMETRY TEXT)")
        c.execute("CREATE table graph_edges (graph_num INTEGER, edge_id TEXT, WKT_GEOMETRY TEXT)")
        c.execute("CREATE index graph_nodes_id_indx ON graph_nodes(node_id)")
        c.execute("CREATE index graph_edges_id_indx ON graph_edges(edge_id)")
        c.close()
      
        g = Graph()
        t0 = time.time()
        
        vertices = {}
        print "load vertices into memory"
        for row in db.execute("SELECT DISTINCT start_nd from edges"):
            g.add_vertex(str(row[0]))
            vertices[str(row[0])] = 0
            #print str(row[0])

        for row in db.execute("SELECT DISTINCT end_nd from edges"):
            g.add_vertex(str(row[0]))
            vertices[str(row[0])] = 0

        #k = vertices.keys()
        #k.sort()
        #print k, len(k)
        
        print "load edges into memory"
        for start_nd, end_nd in db.execute("SELECT start_nd, end_nd from edges"):
            g.add_edge(start_nd, end_nd, Link())
            g.add_edge(end_nd, start_nd, Link())
            #print start_nd, end_nd
            
        db.conn.commit()
        
        t1 = time.time()
        print "populating graph took: %f"%(t1-t0)
        t0 = t1
        
        print len(vertices)
        iteration = 1
        c = db.cursor()
        while True:
            #c.execute("SELECT id from nodes where id not in (SELECT node_id from graph_nodes) LIMIT 1")
            try:
                vertex, dummy = vertices.popitem()
                #print vertex
            except:
                break
            spt = g.shortest_path_tree(vertex, None, State(1,0))
            print spt.size
            for v in spt.vertices:
                lat, lon = c.execute("SELECT lat, lon from nodes where id=?", (v.label, )).next()
                c.execute("INSERT into graph_nodes VALUES (?, ?, ?)", (iteration, v.label, "POINT(%f %f)" % (lon, lat)))
                for e in v.outgoing: # this gives a wierd maze graph, should do for all edges outside loop.
                    lat1, lon1 = c.execute("SELECT lat, lon from nodes where id=?", (e.from_v.label, )).next()
                    lat2, lon2 = c.execute("SELECT lat, lon from nodes where id=?", (e.to_v.label, )).next()
                    c.execute("INSERT into graph_edges VALUES (?, ?, ?)", 
                        (iteration, e.from_v.label + '->' + e.to_v.label, "LINESTRING(%f %f, %f %f)" % (lon1, lat1, lon2, lat2)))
                #print v.label
                vertices.pop(v.label, None)
                g.remove_vertex(v.label, True, True)
                #print v.label
            spt.destroy()
            
            t1 = time.time()
            print "pass %s took: %f nvertices %d"%(iteration, t1-t0, len(vertices))
            t0 = t1
            iteration += 1
        c.close()
        
        db.conn.commit()
        g.destroy()
        # audit
        for gnum, count in db.execute("SELECT graph_num, count(*) FROM graph_nodes GROUP BY graph_num"):
            print "FOUND: %s=%s" % (gnum, count)
Ejemplo n.º 3
0
#!/usr/bin/env python

from graphserver.ext.osm.osmdb   import OSMDB
from PIL import Image
from math import log, sqrt

osmdb  = OSMDB ('/home/andrew/data/pdx/testgrid.osmdb'  )

print "Fetching grid from OSMDB..."
grid = list(osmdb.execute("SELECT g.x, g.y, gp.surf, gp.pop FROM grid AS g, grid_pop AS gp WHERE g.rowid = gp.rowid"))
max_x, max_y = osmdb.execute("SELECT max(x), max(y) FROM grid").next()

max_pop,  = osmdb.execute("SELECT max(pop)  FROM grid_pop").next()
max_surf, = osmdb.execute("SELECT max(surf) FROM grid_pop").next()

max_x += 1
max_y += 1
print max_surf, max_pop

print "saving image..."
im_surf = Image.new("L", (max_x, max_y))
im_pop  = Image.new("L", (max_x, max_y))
for (x, y, surf, pop) in grid :
    # s = int((sqrt(surf)/sqrt(max_surf)) * 255)
    s = int((surf/max_surf) * 255)
    im_surf.putpixel((x, max_y - y - 1), s)
    # p = int((sqrt(pop)/sqrt(max_pop)) * 255)
    p = int((pop / max_pop) * 255)
    im_pop.putpixel((x, max_y - y - 1), p)

im_pop.save('population.png')