Esempio n. 1
0
def main():
    from optparse import OptionParser

    usage = """Usage: python gst_process <configuration file>
               See the documentation for layout of the config file."""

    parser = OptionParser(usage=usage)

    parser.add_option("-b", "--import-base", action="store_true", help="imports GTFS and OSM data into the database", dest="import_base", default=False)
    parser.add_option("-r", "--import-routes", action="store_true", help="imports routing data into the database", dest="import_routes", default=False)
    parser.add_option("-i", "--import-all", action="store_true", help="imports GTFS, OSM and routing data into the database", dest="import_all", default=False)
    parser.add_option("-c", "--calculate", action="store_true", help="calculates shortest paths", dest="calculate", default=False)
    parser.add_option("-e", "--export", action="store_true", help="exports the calculted paths as CSV-files", dest="export", default=False)

    (options, args) = parser.parse_args()

    if DEBUG: print(options)

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

    try:
        configuration, psql_connect_string = read_config(args[0])
    except:
        print(colored('ERROR: failed reading the configuration file', 'red'))
        if DEBUG: raise
        parser.print_help()
        exit(-1)

    valide = validate_input(configuration, psql_connect_string, options)

    if not valide:
        parser.print_help()
        exit(-1)



    graph = None

    if options.import_base or options.import_all:
        print('Importing base data...')
        build_base_data(psql_connect_string, configuration['osm-data'], configuration['transit-feed'])


    if options.import_routes or options.import_all:
        print('Importing routing data...')

        graph = GraphDatabase(psql_connect_string).incarnate()

        build_route_data(graph, psql_connect_string, configuration['times'], configuration['points'], configuration['routes'])


    if options.calculate:
        print('Calculating shortest paths...')

        # only create tables if some importing was done
        create_tables = options.import_all or options.import_base or options.import_routes

        if not graph: graph = GraphDatabase(psql_connect_string).incarnate()

        start = time.time()
        calculate_routes(graph, psql_connect_string, configuration, num_processes=configuration['parallel-calculations'])
        cprint('total calculation time: %s' % utils.seconds_time_string(time.time() - start), attrs=['bold'])

    try:
        graph.destroy()
    except:
        pass


    if options.export:
        print('Exporting paths...')
        export_results(psql_connect_string, configuration['results'], configuration['result-details'])


    print('DONE')
Esempio n. 2
0
def print_status(connection, logfile=None):

    def calc_calculation_time(routes_previously_wating, routes_waiting, all_routes, time_finished):
        if routes_previously_wating != routes_waiting:

            if (not routes_previously_wating) or (all_routes - routes_previously_wating == 0):
                return None, routes_waiting

            routes_processed = all_routes - routes_previously_wating
            routes_previously_wating = routes_waiting

            routes_per_second = (time.time() - time_started) / routes_processed
            time_finished = (all_routes - routes_processed) * routes_per_second

        return time_finished, routes_previously_wating


    time_started = time.time()
    time_finished = None
    routes_previously_wating = None
    routes_waiting = None

    cursor = connection.cursor()
    cursor.execute('SELECT count(*) FROM cal_routes')
    all_routes = cursor.fetchone()[0]

    finished = False
    while not finished:
        time.sleep(1.0)
        cursor.execute('SELECT count(*) FROM cal_routes WHERE NOT done')
        routes_waiting = cursor.fetchone()[0]
        if not all_routes:
            finished = True

        else:
            if time_finished:
                text = '\r%s routes waiting to be processed. Finished in about %s              ' % (routes_waiting,
                                                                                                    utils.seconds_time_string(time_finished))
                sys.stdout.write(text)
                sys.stdout.flush()
##                if logfile:
##                    logfile.write(text)
##                    logfile.flush()

            else:
                text = '\r%s routes waiting to be processed. Please wait ...              ' % routes_waiting
                sys.stdout.write(text)
                sys.stdout.flush()
##                if logfile:
##                    logfile.write(text)
##                    logfile.flush()

        time_finished, routes_previously_wating = calc_calculation_time(routes_previously_wating, routes_waiting, all_routes, time_finished)

    connection.close()

    sys.stdout.write('\rThe last routes getting processed. Please wait ...                                                                      \n')
    sys.stdout.flush()