Example #1
0
def main():
    usage = """usage: python gdb_link_gtfs_gtfs.py <graphdb_filename> <gtfsdb_filename> <range>"""
    parser = OptionParser(usage=usage)
    
    (options, args) = parser.parse_args()
    
    if len(args) != 3:
        parser.print_help()
        exit(-1)
        
    graphdb_filename = args[0]
    gtfsdb_filename  = args[1]
    range = float(args[2])
    
    gtfsdb = GTFSDatabase( gtfsdb_filename )
    gdb = GraphDatabase( graphdb_filename )

    n_stops = gtfsdb.count_stops()

    for i, (stop_id, stop_name, stop_lat, stop_lon) in enumerate( gtfsdb.stops() ):
        print "%d/%d %s"%(i,n_stops,stop_id),
        
        station_vertex_id = "sta-%s"%stop_id
        
        for link_stop_id, link_stop_name, link_stop_lat, link_stop_lon in gtfsdb.nearby_stops( stop_lat, stop_lon, range ):
            if link_stop_id == stop_id:
                continue
            
            print ".",
            
            link_length = vincenty( stop_lat, stop_lon, link_stop_lat, link_stop_lon)
            link_station_vertex_id = "sta-%s"%link_stop_id
            gdb.add_edge( station_vertex_id, link_station_vertex_id, Street("link", link_length) )
            
        print ""
def main():
    usage = """usage: python gdb_link_osm_gtfs.py <graphdb_filename> <osmdb_filename> <gtfsdb_filename>"""
    parser = OptionParser(usage=usage)
    
    (options, args) = parser.parse_args()
    
    if len(args) != 3:
        parser.print_help()
        exit(-1)
        
    graphdb_filename = args[0]
    osmdb_filename   = args[1]
    gtfsdb_filename  = args[2]
    
    gtfsdb = GTFSDatabase( gtfsdb_filename )
    osmdb = OSMDB( osmdb_filename )
    gdb = GraphDatabase( graphdb_filename )

    n_stops = gtfsdb.count_stops()

    for i, (stop_id, stop_name, stop_lat, stop_lon) in enumerate( gtfsdb.stops() ):
        print "%d/%d"%(i,n_stops)
        
        nd_id, nd_lat, nd_lon, nd_dist = osmdb.nearest_node( stop_lat, stop_lon )
        station_vertex_id = "sta-%s"%stop_id
        osm_vertex_id = "osm-%s"%nd_id
        
        print station_vertex_id, osm_vertex_id
        
        gdb.add_edge( station_vertex_id, osm_vertex_id, Link() )
        gdb.add_edge( osm_vertex_id, station_vertex_id, Link() )
Example #3
0
def link_osm_gtfs(db_conn_string, max_link_dist=150):

    conn = psycopg2.connect(db_conn_string)
    cursor = conn.cursor()

    gdb = GraphDatabase(db_conn_string)

    cursor.execute('SELECT stop_id, stop_lat, stop_lon FROM gtfs_stops')
    for i, (s_label, s_lat, s_lon) in enumerate(cursor.fetchall()):
        j = False

        range = 0.05 # might not be the best number
        cursor.execute('''SELECT id, lat, lon FROM osm_nodes WHERE endnode_refs > 1 AND lat > %s AND lat < %s AND lon > %s AND lon < %s''', ( s_lat-range, s_lat+range, s_lon-range, s_lon+range ))
        nodes = cursor.fetchall()
        dists = []

        for n_label, n_lat, n_lon in nodes:
            dists.append( distance(s_lat, s_lon, n_lat, n_lon) )

        for d in dists:
            if d < max_link_dist:
                j = True

                n_label, n_lat, n_lon = nodes[dists.index(d)]

                gdb.add_edge('sta-'+s_label, 'osm-'+n_label, Street('gtfs-osm link', d))
                gdb.add_edge('osm-'+n_label, 'sta-'+s_label, Street('gtfs-osm link', d))


        if not j and dists: # fallback mode
            d = min(dists)

            n_label, n_lat, n_lon = nodes[dists.index(d)]

            gdb.add_edge('sta-'+s_label, 'osm-'+n_label, Street('gtfs-osm link', d))
            gdb.add_edge('osm-'+n_label, 'sta-'+s_label, Street('gtfs-osm link', d))


        if not dists:
            print(colored('WARNING: failed linking %s! (%s, %s)' % (s_label, s_lat, s_lon), 'yellow'))

    gdb.commit()
    conn.commit()
    cursor.close()
def main():
    usage = """usage: python gdb_link_gtfs_gtfs.py <graphdb_filename> <gtfsdb_filename> <range>"""
    parser = OptionParser(usage=usage)

    (options, args) = parser.parse_args()

    if len(args) != 3:
        parser.print_help()
        exit(-1)

    graphdb_filename = args[0]
    gtfsdb_filename = args[1]
    range = float(args[2])

    gtfsdb = GTFSDatabase(gtfsdb_filename)
    gdb = GraphDatabase(graphdb_filename)

    n_stops = gtfsdb.count_stops()

    for i, (stop_id, stop_name, stop_lat,
            stop_lon) in enumerate(gtfsdb.stops()):
        print "%d/%d %s" % (i, n_stops, stop_id),

        station_vertex_id = "sta-%s" % stop_id

        for link_stop_id, link_stop_name, link_stop_lat, link_stop_lon in gtfsdb.nearby_stops(
                stop_lat, stop_lon, range):
            if link_stop_id == stop_id:
                continue

            print ".",

            link_length = vincenty(stop_lat, stop_lon, link_stop_lat,
                                   link_stop_lon)
            link_station_vertex_id = "sta-%s" % link_stop_id
            gdb.add_edge(station_vertex_id, link_station_vertex_id,
                         Street("link", link_length))

        print ""
Example #5
0
def main():
    if len(argv) < 2:
        print "usage: python import_ned.py graphdb_filename profiledb_filename"
        return
        
    graphdb_filename = argv[1]
    profiledb_filename = argv[2]
        
    gdb = GraphDatabase( graphdb_filename )
    profiledb = ProfileDB( profiledb_filename )
    
    n = gdb.num_edges()

    for i, (oid, vertex1, vertex2, edge) in enumerate( list(gdb.all_edges(include_oid=True)) ):
        if i%500==0: print "%s/%s"%(i,n)
        
        if isinstance( edge, Street ):
            rise, fall = get_rise_and_fall( profiledb.get( edge.name ) )
            edge.rise = rise
            edge.fall = fall
            
            gdb.remove_edge( oid )
            gdb.add_edge( vertex1, vertex2, edge )
Example #6
0
def main():
    if len(argv) < 2:
        print "usage: python import_ned.py graphdb_filename profiledb_filename"
        return

    graphdb_filename = argv[1]
    profiledb_filename = argv[2]

    gdb = GraphDatabase(graphdb_filename)
    profiledb = ProfileDB(profiledb_filename)

    n = gdb.num_edges()

    for i, (oid, vertex1, vertex2,
            edge) in enumerate(list(gdb.all_edges(include_oid=True))):
        if i % 500 == 0: print "%s/%s" % (i, n)

        if isinstance(edge, Street):
            rise, fall = get_rise_and_fall(profiledb.get(edge.name))
            edge.rise = rise
            edge.fall = fall

            gdb.remove_edge(oid)
            gdb.add_edge(vertex1, vertex2, edge)