Exemple #1
0
def main():
    usage = """usage: python gdb_import_gtfs.py [options] <graphdb_filename> <gtfsdb_filename> [<agency_id>]"""
    parser = OptionParser(usage=usage)
    parser.add_option("-n", "--namespace", dest="namespace", default="0",
                      help="agency namespace")
    parser.add_option("-m", "--maxtrips", dest="maxtrips", default=None, help="maximum number of trips to load")
    parser.add_option("-d", "--date", dest="sample_date", default=None, help="only load transit running on a given day. YYYYMMDD" )
    
    (options, args) = parser.parse_args()
    
    if len(args) != 2:
        parser.print_help()
        exit(-1)
    
    graphdb_filename = args[0]
    gtfsdb_filename  = args[1]
    agency_id        = args[2] if len(args)==3 else None
    
    print "importing from gtfsdb '%s' into graphdb '%s'"%(gtfsdb_filename, graphdb_filename)
    
    gtfsdb = GTFSDatabase( gtfsdb_filename )
    gdb = GraphDatabase( graphdb_filename, overwrite=False )
    
    maxtrips = int(options.maxtrips) if options.maxtrips else None
    gdb_load_gtfsdb( gdb, options.namespace, gtfsdb, gdb.get_cursor(), agency_id, maxtrips=maxtrips, sample_date=options.sample_date)
    gdb.commit()
    
    print "done"
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() )
Exemple #3
0
def main():
    usage = """usage: python dedupe.py <graphdb_filename>"""
    parser = OptionParser(usage=usage)
    
    (options, args) = parser.parse_args()
    
    if len(args) != 1:
        parser.print_help()
        exit(-1)
        
    graphdb_filename = args[0]    
    
    gtfsdb = GTFSDatabase( graphdb_filename )

    query = """
    SELECT count(*), monday, tuesday, wednesday, thursday, friday, saturday, sunday, start_date, end_date 
    FROM calendar
    GROUP BY monday, tuesday, wednesday, thursday, friday, saturday, sunday, start_date, end_date"""

    duped_periods = gtfsdb.execute( query )

    equivilants = []

    for count, m,t,w,th,f,s,su,start_date,end_date in duped_periods:
        # no need to check for dupes if there's only one
        if count==1:
            continue
        
        #print count, m, t, w, th, f, s, su, start_date, end_date
        
        # get service_ids for this dow/start_date/end_date combination
        service_ids = [x[0] for x in list(  gtfsdb.execute( "SELECT service_id FROM calendar where monday=? and tuesday=? and wednesday=? and thursday=? and friday=? and saturday=? and sunday=? and start_date=? and end_date=?", (m,t,w,th,f,s,su,start_date,end_date) ) ) ]
        
        # group by service periods with the same set of exceptions
        exception_set_grouper = {}
        for service_id in service_ids:
            exception_set = list(gtfsdb.execute( "SELECT date, exception_type FROM calendar_dates WHERE service_id=?", (service_id,) ) )
            exception_set.sort()
            exception_set = tuple(exception_set)
            
            exception_set_grouper[exception_set] = exception_set_grouper.get(exception_set,[])
            exception_set_grouper[exception_set].append( service_id )
        
        # extend list of equivilants
        for i, exception_set_group in enumerate( exception_set_grouper.values() ):
            equivilants.append( ("%d%d%d%d%d%d%d-%s-%s-%d"%(m,t,w,th,f,s,su,start_date,end_date,i), exception_set_group) )
        
    for new_name, old_names in equivilants:
        for old_name in old_names:
            print old_name, new_name
            
            c = gtfsdb.conn.cursor()
            
            c.execute( "UPDATE calendar SET service_id=? WHERE service_id=?", (new_name, old_name) )
            c.execute( "UPDATE calendar_dates SET service_id=? WHERE service_id=?", (new_name, old_name) )
            c.execute( "UPDATE trips SET service_id=? WHERE service_id=?", (new_name, old_name) )

            gtfsdb.conn.commit()
            
            c.close()
    def ccp_save_cache(self):

        time_0 = time.time()

        log.debug('ccp_save_cache: loading the transit database')
        db_transit = GTFSDatabase(conf.transitdb_filename)

        # NOTE: Cannot cache edges, since they are C-objects. See usages of
        #       compiler.gtfsdb_to_edges(maxtrips). We can, however, at least
        #       count the edges....
        self.cache_edges(db_transit)

        log.debug('ccp_save_cache: making the transit graph link cache')
        self.cache_links(db_transit)

        log.debug('ccp_save_cache: done: %s' %
                  (misc.time_format_elapsed(time_0), ))
Exemple #5
0
    def load_make_graph_add_transit(self, qb):
        # Not calling base class fcn.

        # FIXME: What happens here on update? We reload all, don't we?
        # FIXME: For p2, only do this on load, not on update.
        # BUG nnnn: For p2, start new instance of route finder and then
        #           just change routed_ports to use that one, then kill
        #           the existing one.

        time_0 = time.time()
        usage_0 = None
        if conf.debug_mem_usage:
            usage_0 = mem_usage.get_usage_mb()
        log.debug('load: adding transit...')

        loaded = False

        # Load the transit network, maybe (if we have data for it).
        if conf.transitdb_filename:
            self.cache_reg = self.links_get_cache_reg(qb)
            log.debug('load: loading the transit database')
            db_transit = GTFSDatabase(conf.transitdb_filename)
            log.debug('load: making the transit graph')
            self.load_transit(qb, db_transit)
            # Link the two graphs
            log.debug('load: linking the two graphs')
            self.link_graphs(qb, db_transit)
            loaded = True
        # else, using Graphserver, but no public transit data to load.

        if loaded:
            log.info('load: added transit: in %s' %
                     (misc.time_format_elapsed(time_0), ))
        else:
            # MAYBE: Let devs test without loading transit.
            raise GWIS_Error(
                'Unable to load route finder: no transit info found.')

        conf.debug_log_mem_usage(log, usage_0, 'tgraph.load / transit')

        return loaded
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 ""
Exemple #7
0
#!/usr/bin/python

import sys, struct

# requires graphserver to be installed
from graphserver.ext.gtfs.gtfsdb import GTFSDatabase

if len(sys.argv) != 2:
    print 'usage: datecheck.py inputfile.gtfsdb'
    exit(1)
else:
    gtfsdb_file = sys.argv[1]

try:
    with open(gtfsdb_file) as f:
        db = GTFSDatabase(gtfsdb_file)
except IOError as e:
    print 'gtfsdb file %s cannot be opened' % gtfsdb_file
    exit(1)

# check that all routes gs reports running are actually running on each day
for line in sys.stdin.readlines():
    trip_id, fromid, fromtime, toid, totime = line.split()
    if trip_id == 'walk':
        continue
    service_id = list(
        db.execute('select service_id from trips where trip_id = ?',
                   (trip_id, )))[0][0]
    print trip_id, '___', service_id
    # and date > 20130415 and date < 20130420
    for line in db.execute(
Exemple #8
0
 def __init__(self, gtfsdb_filename, timezone_name="America/Los_Angeles"):
     self.gtfsdb = GTFSDatabase(gtfsdb_filename)
     self.timezone_name = timezone_name