Beispiel #1
0
def get_valid_edges_from_point(point,radius):
    print("Finding valid edges for point",point,"and radius",radius)
    x,y = point
    
    
    # Find the nodes that are within the radius
    nod_xml = purr.mrf(env.options.map_dir,r'*.nod.xml')
    nodes_all = purr.readXMLtag(nod_xml,'node')
    nodes_valid = []
    for node in nodes_all:        
        if abs(x - float(node['x'])) <= radius and abs(y - float(node['y'])) <= radius:
            nodes_valid.append(node)
        continue
        
    nodes_valid_ids = [node['id'] for node in nodes_valid]
    
    # Now that we know the nodes, we can pick out valid edges that the node begins at
    edg_xml = purr.mrf(env.options.map_dir,r'*.edg.xml')
    edges_all = purr.readXMLtag(edg_xml,'edge')
    edges_filtered = purr.batchfilterdicts(edges_all,'from',nodes_valid_ids,show_progress=True)
    edges_filtered = purr.flattenlist(edges_filtered)
    edges_valid = []
    for edge in edges_filtered:
        if not ':' in edge['id']:
            edges_valid.append(edge)
    
    print("Found %d edge candidates." % (len(edges_valid)))
    
    return edges_valid
def main():
    global OPTIONS
    OPTIONS = get_options()

    # Nodes
    nod_xml = purr.mrf(OPTIONS.map_dir, r'*.nod.xml')
    nodes = purr.readXMLtag(nod_xml, 'node')
    print("nodes: ", len(nodes))

    # Edges
    edg_xml = purr.mrf(OPTIONS.map_dir, r'*.edg.xml')
    edges = purr.readXMLtag(edg_xml, 'edge')
    print("edges: ", len(edges))

    # Load in Uber speed data
    original_edges = speedhist.load_edges()
    connections = speedhist.load_connections()

    with open(OPTIONS.output, 'w') as f:
        f.write("% from to weight id\n")

        # Each each is a connection
        n = 0
        total = len(edges)
        for edge in edges:
            # Edges with shape
            try:
                length = length_from_shape(edge['shape'])

            # Edges w/o shape
            except KeyError:
                node_from = purr.filterdicts(nodes, 'id', edge['from'])[0]
                node_to = purr.filterdicts(nodes, 'id', edge['to'])[0]
                length = length_from_nodes(node_from, node_to)

            # Determine Speed
            # ~ speed = float(edge['speed'])
            conn = purr.filterdicts(connections, 'ID', edge['id'])[0]
            child_edges = speedhist.get_edges(conn, original_edges)
            _sum = 0
            for ce in child_edges:
                _sum += ce['mean']
            speed = _sum / len(child_edges)

            f.write("%s %s %.3f %s\n" %
                    (edge['from'], edge['to'], length / speed, edge['id']))

            n += 1
            purr.update(n, total, msg="Calculating edge lengths ")
            continue
        pass
    print()

    return
Beispiel #3
0
    def __init__(self, DIR):
        self.DIR = DIR
        self.edges = purr.readXMLtag(purr.mrf(DIR, r"*.edg.xml"), "edge")
        self.nodes = purr.readXMLtag(purr.mrf(DIR, r"*.nod.xml"), "node")

        x = [float(node['x']) for node in self.nodes]
        y = [float(node['y']) for node in self.nodes]
        self.centroid = (stats.fmean(x), stats.fmean(y))
        self.xMax = max(x)
        self.xMin = min(x)
        self.yMax = max(y)
        self.yMin = min(y)
        return
def main():
    global OPTIONS
    OPTIONS = get_options()
    edg_xml = purr.mrf(OPTIONS.map_dir, r'*.edg.xml')
    edges = purr.readXMLtag(edg_xml, 'edge')

    original_edges = speedhist.load_edges()
    connections = speedhist.load_connections()

    fn = 'hhh.edg.xml'

    with open(fn, 'w') as f:
        f.write('<edges>')

        n = 0
        total = len(edges)
        for edge in edges:
            conn = purr.filterdicts(connections, 'ID', edge['id'])[0]
            child_edges = speedhist.get_edges(conn, original_edges)
            _sum = 0
            for ce in child_edges:
                _sum += ce['mean']
            speed = _sum / len(child_edges)

            f.write(
                '\n\t<edge id="%s" from="%s" to="%s" priority="%s" numLanes="%s" speed="%.3f"/>'
                % (edge['id'], edge['from'], edge['to'], edge['priority'],
                   edge['numLanes'], speed))

            n += 1
            purr.update(n, total)
            continue

        f.write('\n</edges>')
    return
Beispiel #5
0
def main():
    global OPTIONS
    OPTIONS = get_options()

    # Nodes
    nod_xml = purr.mrf(OPTIONS.map_dir, r'*.nod.xml')
    nodes = purr.readXMLtag(nod_xml, 'node')
    print("nodes: ", len(nodes))

    # Edges
    edg_xml = purr.mrf(OPTIONS.map_dir, r'*.edg.xml')
    edges = purr.readXMLtag(edg_xml, 'edge')
    print("edges: ", len(edges))

    with open(OPTIONS.output, 'w') as f:
        f.write("% from to weight id\n")

        # Each each is a connection
        n = 0
        total = len(edges)
        for edge in edges:
            if edge['from'] == edge['to']:
                n += 1
                continue

            # Edges with shape
            try:
                length = length_from_shape(edge['shape'])

            # Edges w/o shape
            except KeyError:
                node_from = purr.filterdicts(nodes, 'id', edge['from'])[0]
                node_to = purr.filterdicts(nodes, 'id', edge['to'])[0]
                length = length_from_nodes(node_from, node_to)

            speed = float(edge['speed'])

            f.write("%s %s %.3f %s\n" %
                    (edge['from'], edge['to'], length / speed, edge['id']))

            n += 1
            purr.update(n, total, msg="Calculating edge lengths ")
            continue
        pass
    print()

    return
def main():
    map_dir = "../london-seg4/data/100"
    edges = purr.readXMLtag(purr.mrf(map_dir, r'*.edg.xml'), 'edge')
    connections = purr.readCSV(purr.mrf(map_dir, r'*.conn.csv'))
    stats = purr.readCSV('edges.stats.csv')
    purr.head(edges)
    purr.head(connections)
    purr.head(stats)

    # ~ return

    eids = [edge['id'] for edge in edges]
    connections = purr.flattenlist(
        purr.batchfilterdicts(connections,
                              'CONNECTION ID',
                              eids,
                              show_progress=True))
    print()

    total = len(connections) - 1
    for i, conn in enumerate(connections):
        try:
            speeds = purr.flattenlist(
                purr.batchfilterdicts(stats, 'ID', conn['EDGE IDS']))
        except TypeError:
            speeds = purr.flattenlist(
                purr.batchfilterdicts(stats, 'ID', [conn['EDGE IDS']]))
        mean = [float(speed['mean']) for speed in speeds]
        std = [float(speed['std']) for speed in speeds]
        p50 = [float(speed['p50']) for speed in speeds]
        p85 = [float(speed['p85']) for speed in speeds]
        edges[i]['mean'] = "%.3f" % (statistics.mean(mean))
        edges[i]['std'] = "%.3f" % (statistics.mean(std))
        edges[i]['p50'] = "%.3f" % (statistics.mean(p50))
        edges[i]['p85'] = "%.3f" % (statistics.mean(p85))
        purr.update(i, total, "Correlating speeds with edges ")
        continue

    with open("edg.xml", 'w') as f:
        f.write("<edges>")
        for edge in edges:
            f.write("\n\t<edge")
            for key, val in edge.items():
                f.write(' %s="%s"' % (key, val))
            f.write("/>")
            continue
        f.write("\n</edges>")

    print("COMPLETE!")

    return
Beispiel #7
0
def main():
    map_dir = "../TAPASCologne-0.32.0"
    print("map dir=%s" % (map_dir))
    
    # Retrieve the rou.xml file
    trips_xml = purr.mrf(map_dir,r'*trips.xml')
    print("trips.xml=%s" % (trips_xml))
    
    # Load the trips into a dictionary type
    trips = purr.readXMLtag(trips_xml,'trip')
    print("First five trips.")
    purr.head(trips,5)
    
    # The data will be read into a dictionary as a string type
    # YOu can acess them like this.
    print('id=%s' % (trips[0]['id']))
    print('depart time=%.2f' % (float(trips[0]['depart'])))
    return
Beispiel #8
0
def initialize(traci):
    os.system("cls")
    print("Initializing...")
    
    trips = purr.readXMLtag(purr.mrf(env.map_dir,r"*.trips.xml"),"trip")
    
    total = len(trips) - 1
    print('Number of trips:',len(trips))
    for i,trip in enumerate(trips):
        veh = vehicle.Vehicle("veh%d" % (i))
        try:
            veh.add(traci,trip['from'],trip['to'],trip['depart'])
        except:
            pass
        env.vehicles.append(veh)
        purr.update(i,total)

    print("Initialization complete!")
    return
    def __init__(self, map_dir, N, M, tau, R):
        self.map_dir = map_dir
        self.N = N
        self.M = M
        self.tau = tau
        self.R = R

        self.nodes = purr.readXMLtag(purr.mrf(self.map_dir, r'*.nod.xml'),
                                     'node')
        self.normalizeNodes(0.1)

        self.points_pybin = "temp/points.pybin"

        self.dst2src_point_validation()

        if not os.path.exists(self.out_dir_sumo):
            os.mkdir(self.out_dir_sumo)
        if not os.path.exists(self.out_dir_geom):
            os.mkdir(self.out_dir_geom)
        return
Beispiel #10
0
def initialize_edges_and_nodes():
    edg_xml = purr.mrf(env.options.map_dir,r'*.edg.xml')
    env.edges = purr.readXMLtag(edg_xml,'edge')
    nod_xml = purr.mrf(env.options.map_dir,r'*.nod.xml')
    env.nodes = purr.readXMLtag(nod_xml,'node')
    return
def main():
    map_dir = "../orlando-seg1/"

    # Load Nodes
    nod_xml = purr.mrf(map_dir, r'*.nod.xml')
    nodes = purr.readXMLtag(nod_xml, 'node')

    # Load Edges
    edg_xml = purr.mrf(map_dir, r'*edg.xml')
    edges = purr.readXMLtag(edg_xml, 'edge')

    # Correlate From/To Nodes with edges
    print('Finding Nodes From')
    nids_from = [edge['from'] for edge in edges]
    nodes_from = purr.flattenlist(
        purr.batchfilterdicts(nodes, 'id', nids_from, show_progress=True))

    print('\nFinding Nodes To')
    nids_to = [edge['to'] for edge in edges]
    nodes_to = purr.flattenlist(
        purr.batchfilterdicts(nodes, 'id', nids_to, show_progress=True))
    print()

    # Create the CSV for Nodes
    node_attr = [
        'id', 'x', 'y', 'z', 'type', 'tlType', 'tl', 'radius', 'keepClear',
        'rightOfWay'
    ]
    node_csv = 'node.csv'
    with open(node_csv, 'w') as csv:
        # Column Names
        csv.write(node_attr[0])
        for attr in node_attr[1:]:
            csv.write(',%s' % (attr))
        csv.write('\n')

        # Fill in rows
        n = 0
        total = len(nodes)
        for node in nodes:
            csv.write(node[node_attr[0]])
            for attr in node_attr[1:]:
                csv.write(',')
                try:
                    csv.write(node[attr])
                except KeyError:
                    pass
                continue
            csv.write('\n')
            n += 1
            purr.update(n, total, msg="Writing node.csv ")
            continue
        pass

    print()

    # Create the CSV for Edges
    edge_attr = [
        'id', 'from', 'to', 'type', 'numLanes', 'speed', 'priority',
        'spreadType', 'width', 'name', 'endOffset', 'sidewalkWidth'
    ]
    edge_allow = [
        'emergency', 'authority', 'passenger', 'hov', 'taxi', 'bus',
        'delivery', 'truck', 'trailer', 'motorcyle', 'moped'
    ]
    edge_csv = 'edge.csv'
    with open(edge_csv, 'w') as csv:
        # Column Names
        csv.write(edge_attr[0])
        for attr in edge_attr[1:]:
            csv.write(',%s' % (attr))
        csv.write(',length')
        for attr in edge_allow:
            csv.write(',%s' % (attr))
        csv.write('\n')

        # Fill in Rows
        n = 0
        total = len(edges)
        for i, edge in enumerate(edges):
            # id
            csv.write(edge[edge_attr[0]])

            # Attributes
            for attr in edge_attr[1:]:
                csv.write(',')
                try:
                    csv.write(edge[attr])
                except KeyError:
                    pass
                continue

            # Length
            try:
                csv.write(',%.3f' % (shape_len(edge['shape'])))
            except KeyError:
                x0 = float(nodes_from[i]['x'])
                y0 = float(nodes_from[i]['y'])
                x1 = float(nodes_to[i]['x'])
                y1 = float(nodes_to[i]['y'])
                csv.write(',%.3f' % (dist(x0, y0, x1, y1)))

            # Vehicle Types
            vtype_allowed = [0 for item in edge_allow]

            # Has neither tag. It is assumed that all vehicles are allowed.
            if (not purr.xml_has_atrribute(edge, 'allow')) and (
                    not purr.xml_has_atrribute(edge, 'disallow')):
                vtype_allowed = [1 for item in edge_allow]

            # Has the allow tag. Those specified are allowed
            elif (purr.xml_has_atrribute(edge, 'allow')):
                vtypes = edge['allow'].split(' ')
                for j, vt in enumerate(edge_allow):
                    if vt in vtypes:
                        edge_allow[j] = 1
                    continue

            # Lastly it has the dissalow tag, and what is not speficied is allowed
            elif (purr.xml_has_atrribute(edge, 'disallow')):
                vtype_allowed = [1 for item in edge_allow]
                vtypes = edge['allow'].split(' ')
                for j, vt in enumerate(edge_allow):
                    if vt in vtypes:
                        edge_allow[j] = 0
                    continue

            # ~ for j,vt in enumerate(edge_allow):
            # ~ print(edge_allow[j],vtype_allowed[j])

            for vt in vtype_allowed:
                csv.write(',%d' % (vt))
            csv.write('\n')

            n += 1
            purr.update(n, total, 'Writing edges.csv ')
            continue
        pass
        print('\nComplete.')
    return