コード例 #1
0
ファイル: readMap.py プロジェクト: ebaustria/the-one
proj = Projector(precision=2)
width, height = proj.init_dimensions(points)

print(width, height)

# Transform coordinates from lat, lon to x, y
for route_name in routes_latlon.keys():
    routes_xy[route_name], stops_xy[route_name] = {}, {}
    routes_xy[route_name]['nodes'] = proj.transform_coords(
        routes_latlon[route_name]['nodes'])
    routes_xy[route_name]['stops'] = proj.transform_coords(
        routes_latlon[route_name]['stops'])
    nodes_file = route_name + "_nodes.wkt"
    stops_file = route_name + "_stops.csv"

    #stops_xy[route_name] = [routes_xy[route_name][0],routes_xy[route_name][-1]]
    stops_xy[route_name]['stops'] = [x for x in routes_xy[route_name]['stops']]

    nodes[route_name] = routes_latlon[route_name]['nodes']
    vis_stops[route_name] = routes_latlon[route_name]['stops']

    writer.write_wkt_linestring(coords=routes_xy[route_name]['nodes'],
                                file=nodes_file)
    writer.write_csv_stops(
        coords=stops_xy[route_name]['stops'],
        durations=[30 for _ in range(len(stops_xy[route_name]['stops']))],
        file=stops_file)
#print(routes_xy)

write_local_and_gps(p=proj, routes=routes_latlon)
コード例 #2
0
def main(args):
    scenario = args.name or basename_without_ext(args.gtfs_file)
    route_type = args.type
    with_shapes = not args.osm

    logging.info('reading gtfs feed in '+args.gtfs_file)
    gtfs = GTFSReader()
    gtfs.load_feed(args.gtfs_file, with_shapes=with_shapes)

    gtfs.print_feed_info()

    chosen_day = gtfs.get_first_day_from_feed('Monday')
    gtfs.set_trips_of_interest(type=route_type, day=chosen_day)
    gtfs.set_stop_times()
    gtfs.build_ref_trips()

    routes = shape_routes(gtfs)

    logging.info("building schedule and trip durations")
    schedule = gtfs.schedule()
    durations = gtfs.trip_durations()

    points = set()
    for r in routes:
        points.update(r.nodes)

    # initialize projection pane (width, height in m)
    # from coordinate bounds of all nodes
    proj = Projector(precision=2)
    width, height = proj.init_dimensions(points)

    # switch to ONE project root and make dir
    # for the new scenario in /data
    one_dir = Path.cwd().parent.parent
    out_dir = path.join(one_dir, DATA_DIR, scenario)
    if not path.isdir(out_dir):
        os.mkdir(out_dir)

    logging.info('creating ONE scenario "'+scenario+'" and writing files')

    nodes_file = path.join(out_dir, NODES_FILE)
    stops_file = path.join(out_dir, STOPS_FILE)
    schedule_file = path.join(out_dir, SCHEDULE_FILE)
    stations_file = path.join(out_dir, STATIONS_FILE)

    # begin contents of a ONE settings file for this scenario
    s = ScenarioSettings(scenario)
    stations = set()

    nhosts = None
    min_hosts = None
    if args.nhosts.isdigit():
        nhosts = int(args.nhosts)
    elif args.nhosts == 'auto':
        min_hosts = determine_min_hosts(schedule, durations)
    else:
        logging.error('invalid value of parameter nhosts. must be either positive int or "auto"')
        exit(1)

    hosts_total = 0
    for r in routes:
        name = r.name
        if not schedule.get(name) or not durations.get(name):
            continue

        # transform the coordinates from lat,long
        # to x,y tuples on projection pane
        nodes = proj.transform_coords(r.nodes)
        stops = proj.transform_coords(r.stops)

        hosts = nhosts if nhosts is not None else min_hosts.get(name)
        hosts_total += hosts
        stations.update(stops)

        # for each route, create a host group.
        # this group will contain the moving hosts along the route.
        # nodes_file is the map file this group is ok to move on

        g = HostGroup(name, HOST_ID_DELIM)
        g.set('movementModel', 'TransitMapMovement')
        g.set('routeFile', stops_file.format(name))
        g.set('scheduleFile', schedule_file.format(name))
        g.set('routeType', 2)
        # if there are less trips for this route than hosts specified,
        # use the amount of trips as host count
        g.set('nrofHosts', min(hosts, len(schedule.get(r.name))))
        g.set_okmap(nodes_file.format(name))
        s.add_group(g)

        # write a wkt LINESTRING with all nodes in this route
        # (includes stops and way nodes)
        writer.write_wkt_linestring(
            coords=nodes,
            file=nodes_file.format(name)
        )

        # write only stop nodes in csv with
        # 1st col: stop coords
        # 2nd col: durations between each stop to the next one
        # (will be used to generate path speeds)
        writer.write_csv_stops(
            coords=stops,
            durations=durations.get(r.name),
            file=stops_file.format(name)
        )


        # write the schedule of this line
        # (start times, start and stop ids and direction)
        writer.write_csv_schedule(
            schedule=[('1:06:'+s[0],s[1],s[2]) for s in schedule.get(name)],
            file=schedule_file.format(name)
        )

    main_local_to_gps = proj.local_to_gps

    with open(scenario + "_gps_coordinates.csv", 'w') as f:
        writer2 = csv.writer(f)
        for key, value in sorted(main_local_to_gps.items()):
            writer2.writerow([key, value])

    # add another group for all stations.
    # in this group, for each station one stationary host
    # will be created to act as a fixed relay node at the platform
    writer.write_wkt_points(stations, stations_file)
    g = HostGroup('S')
    g.set('movementModel', 'StationaryMultiPointMovement')
    g.set('stationarySystemNr', 1)
    g.set('pointFile', stations_file)
    g.set('nrofHosts', len(stations))
    s.add_group(g)

    # write group options and map files to settings contents
    s.complete_groups()
    s.spacer()

    # set world size to ceiled projection pane bounds
    # and adjust host address range to total number of hosts
    s.set('MovementModel.worldSize', '{w}, {h}'.format(
        w=math.ceil(width),
        h=math.ceil(height)
    ))
    s.set('Events1.hosts', '{min},{max}'.format(
        min=0,
        max=hosts_total + len(stations) - 1
    ))

    # write settings contents to file in ONE project root
    s.write(path.join(one_dir, '{scenario}_settings.txt'.format(
        scenario=scenario
    )))