Ejemplo n.º 1
0
def build_auxiliary_structures(nodes_filename, ways_filename):
    max_speed_limit = float('-inf')

    adjacency = {}
    node_locs = {}
    for way in read_osm_data(ways_filename):
        if way['tags'].get('highway', None) in ALLOWED_HIGHWAY_TYPES:
            twoway = way['tags'].get('oneway', 'no') != 'yes'
            if way['nodes'][0] not in adjacency:
                adjacency[way['nodes'][0]] = set()

            if 'maxspeed_mph' in way['tags']:
                speedlimit = way['tags']['maxspeed_mph']
            else:
                speedlimit = DEFAULT_SPEED_LIMIT_MPH[way['tags']['highway']]
            if speedlimit > max_speed_limit:
                max_speed_limit = speedlimit
            for first, second in zip(way['nodes'], way['nodes'][1:]):
                adjacency.setdefault(first, set()).add((second, speedlimit))
                if second not in adjacency:
                    adjacency[second] = set()
                if twoway:
                    adjacency[second].add((first, speedlimit))

    for node in read_osm_data(nodes_filename):
        if node['id'] in adjacency:
            node_locs[node['id']] = (node['lat'], node['lon'])

    return node_locs, adjacency, max_speed_limit
Ejemplo n.º 2
0
def build_auxiliary_structures(nodes_filename, ways_filename):
    """
    Create any auxiliary structures you are interested in, by reading the data
    from the given filenames (using read_osm_data)
    """
    #The datatype I will be a dict with keys of eligible node ids
    #with values containing a set of tuples of adjacent eligible nodes:
    #{node: {(id,lat,lon,distance,speed_limit)}}
    answer_dict={}
    big_nodes={}
    #for way in read_osm_data(ways_filename):
    #    print (way)
    for y in read_osm_data(nodes_filename):
        big_nodes.update({y['id']:y})
    for x in read_osm_data(ways_filename):
        if 'highway' in x['tags'].keys():    
            if x['tags']['highway'] in ALLOWED_HIGHWAY_TYPES:
                for w in range(len(x['nodes'])):
                    #find speed limit:
                    if 'maxspeed_mph' in x['tags'].keys():
                        speed=x['tags']['maxspeed_mph']
                    else:
                        speed=DEFAULT_SPEED_LIMIT_MPH[x['tags']['highway']]
                    #adds a new key if id is not in answer_dict
                    if x['nodes'][w] not in answer_dict.keys():
                        answer_dict.update({x['nodes'][w]:set()})
                    if w!=len(x['nodes'])-1:
                        #calculate distance between x['nodes'][w] and x['nodes'][w+1]
                        lat=big_nodes[x['nodes'][w+1]]['lat']
                        lon=big_nodes[x['nodes'][w+1]]['lon']
                        oth_lat=big_nodes[x['nodes'][w]]['lat']
                        oth_lon=big_nodes[x['nodes'][w]]['lon']
                        dist=great_circle_distance((lat,lon), (oth_lat,oth_lon))
                        answer_dict[x['nodes'][w]].add((x['nodes'][w+1],lat,lon,dist,speed))
                    if 'oneway' in x['tags'].keys():
                        if x['tags']['oneway']!='yes':
                            if w!=0:
                                #calculate distance between x['nodes'][w] and x['nodes'][w-1]
                                lat=big_nodes[x['nodes'][w-1]]['lat']
                                lon=big_nodes[x['nodes'][w-1]]['lon']
                                oth_lat=big_nodes[x['nodes'][w]]['lat']
                                oth_lon=big_nodes[x['nodes'][w]]['lon']
                                dist=great_circle_distance((lat,lon), (oth_lat,oth_lon))
                                answer_dict[x['nodes'][w]].add((x['nodes'][w-1],lat,lon,dist,speed))
                    else:
                        if w!=0:
                            #calculate distance between x['nodes'][w] and x['nodes'][w-1]
                            lat=big_nodes[x['nodes'][w-1]]['lat']
                            lon=big_nodes[x['nodes'][w-1]]['lon']
                            oth_lat=big_nodes[x['nodes'][w]]['lat']
                            oth_lon=big_nodes[x['nodes'][w]]['lon']
                            dist=great_circle_distance((lat,lon), (oth_lat,oth_lon))
                            answer_dict[x['nodes'][w]].add((x['nodes'][w-1],lat,lon,dist,speed))
    #removing any empty keys
    actual_dict={}
    for f in answer_dict.keys():
        if answer_dict[f]!=set():
            actual_dict.update({f:answer_dict[f]})
    return actual_dict
Ejemplo n.º 3
0
def build_auxiliary_structures(nodes_filename, ways_filename):
    """
    Create any auxiliary structures you are interested in, by reading the data
    from the given filenames (using read_osm_data)
    """
    nodes = {}
    for way in read_osm_data(ways_filename):
        highway_type = way['tags'].get('highway', '( ͡° ͜ʖ ͡°)')
        if highway_type in ALLOWED_HIGHWAY_TYPES:
            nodes_along_way = way['nodes']  # List of nodes along this way
            for i in range(len(nodes_along_way) - 1):
                # A pair of adjacent nodes along this way
                left = nodes_along_way[i]
                right = nodes_along_way[i + 1]
                default_speed_limit = DEFAULT_SPEED_LIMIT_MPH[highway_type]
                # If this way doesn't have a speed limit tag, we use the default value based on highway type
                speed_limit = way['tags'].get('maxspeed_mph',
                                              default_speed_limit)

                def build_data(root, adjacent):
                    """
                        root: ID of some node along way
                        adjacent: ID of some node adjacent to root node along way
                    """
                    new_node_data_struct = {
                        'adjacent': {
                            adjacent: speed_limit
                        }
                    }  # Init dict for node data structure
                    root_data = nodes.get(root, new_node_data_struct)
                    # There might be another way where root and adjacent are directly adjacent, so our
                    # speed limit is the max of the speed limits of those two ways:
                    root_data['adjacent'][adjacent] = max(
                        root_data['adjacent'].get(adjacent, 0), speed_limit)
                    nodes[
                        root] = root_data  # Add the data on root to our dictionary of node data

                build_data(left, right)
                if not way['tags'].get('oneway', '( ͡° ͜ʖ ͡°)') == 'yes':
                    # If this isn't a oneway way, we can build the data structure for the next node as well
                    build_data(right, left)
                elif right == nodes_along_way[-1]:
                    # In non-oneway ways, the above build_data(right, left) call creates the data structure
                    # for the final node at the same time as the penultimate one. However, in the case of a
                    # oneway path, we have to do it manually:
                    nodes[right] = nodes.get(right, {'adjacent': {}})

    for node in read_osm_data(nodes_filename):
        id = node['id']
        if id in nodes:
            # If the id of this node in the generator was on a valid way, we add the data about that node
            # to its dictionary in nodes.
            # Add lat/lon data
            nodes[id]['lat'] = node['lat']
            nodes[id]['lon'] = node['lon']

    return nodes
Ejemplo n.º 4
0
def build_auxiliary_structures(nodes_filename, ways_filename):
    """
    Create any auxiliary structures you are interested in, by reading the data
    from the given filenames (using read_osm_data)
    
    Returns a tuple consisting of a dictionary of nodes and a dictionary of ways
    nodes_db has keys of ids and values of another dictionary containing {coordinates,neighbors,cost,heuristic}
    """

    ways = read_osm_data(ways_filename)
    nodes = read_osm_data(nodes_filename)
    nodes_db = {}

    for way in ways:
        if 'highway' in way['tags']:
            highway_type = way['tags']['highway']
            if highway_type in ALLOWED_HIGHWAY_TYPES:
                """ Adds neighbors and creates the key for each relevant id (eliminates disconected nodes) """
                num_nodes_in_way = len(way['nodes'])
                if 'maxspeed_mph' in way['tags']:
                    speed = way['tags']['maxspeed_mph']
                else:
                    speed = DEFAULT_SPEED_LIMIT_MPH[highway_type]
                for i in range(num_nodes_in_way - 1):  #Forward neighbors case
                    node = way['nodes'][i]
                    neighbor_node = way['nodes'][i + 1]
                    update_node_neighbors(nodes_db, node, neighbor_node, speed)
                    if i == num_nodes_in_way - 2:  #Adds the last neighbor as a relevant node
                        if neighbor_node not in nodes_db:
                            node_dict = {'neighbors': set()}
                            nodes_db[neighbor_node] = node_dict

                if 'oneway' in way['tags']:  #Backwards neighbors case
                    if way['tags']['oneway'] == 'no':
                        for i in range(num_nodes_in_way - 1, 0, -1):
                            node = way['nodes'][i]
                            neighbor_node = way['nodes'][i - 1]
                            update_node_neighbors(nodes_db, node,
                                                  neighbor_node, speed)

                else:  #In the case the tags does not include oneway, we assume it is bidirectional
                    for i in range(num_nodes_in_way - 1, 0, -1):
                        node = way['nodes'][i]
                        neighbor_node = way['nodes'][i - 1]
                        update_node_neighbors(nodes_db, node, neighbor_node,
                                              speed)

    for node in nodes:  #Initializes dictionary of nodes with empty neighbor set
        """ Gets the coordinates, don't need node tags, adds a cost and heuristic key"""
        node_id = node['id']
        if node_id in nodes_db:
            nodes_db[node_id]['coordinates'] = (node['lat'], node['lon'])
            nodes_db[node_id]['cost'] = 0.0
            nodes_db[node_id]['heuristic'] = 0.0
    return nodes_db
Ejemplo n.º 5
0
def build_auxiliary_structures(nodes_filename, ways_filename):
    """
    Create any auxiliary structures you are interested in, by reading the data
    from the given filenames (using read_osm_data)
    """
    all_nodes = {}
    for node in read_osm_data(nodes_filename):
        id = node['id']
        node['loc'] = (node['lat'], node['lon'])
        all_nodes[id] = node

    nodes = {}
    ways = {}
    digraph = {}
    distances = {}
    speeds = {}
    locations = {}
    for way in read_osm_data(ways_filename):
        id = way['id']
        highway = way['tags'].get('highway', None)
        if highway in ALLOWED_HIGHWAY_TYPES:
            ways[id] = way
            path = way['nodes']
            is_one_way = way['tags'].get('oneway', 'no') == 'yes'
            speed = way['tags'].get('maxspeed_mph',
                                    DEFAULT_SPEED_LIMIT_MPH[highway])
            for i in range(len(path) - 1):
                all_ids = (path[i], path[i + 1])

                for id in all_ids:
                    nodes[id] = all_nodes[id]
                    locations[id] = nodes[id]['loc']
                distance = distance_between_node_ids(nodes, all_ids[0],
                                                     all_ids[1])

                if is_one_way:
                    all_ids = [all_ids]
                else:
                    all_ids = [all_ids, (all_ids[1], all_ids[0])]

                for ids in all_ids:
                    if speed > speeds.get(ids, 0):
                        speeds[ids] = speed
                    distances[ids] = distance
                    digraph.setdefault(ids[0], set()).add(ids[1])

    return {
        'nodes': nodes,
        'ways': ways,
        'digraph': digraph,
        'distances': distances,
        'speeds': speeds,
        'locations': locations
    }
Ejemplo n.º 6
0
def build_auxiliary_structures(nodes_filename, ways_filename):
    """
    Creates a set of ways and a set of nodes from the files; returns these sets in a tupe (nodes, ways)
    """
    relevant_nodes = set()

    node_web = {}  # node_id mapped to children of node
    node_coord = {}  # node_id: (lat, lon)

    for way in read_osm_data(
            ways_filename):  #loops thru ways in the given dataset

        if way['tags'].get(
                'highway'
        ) in ALLOWED_HIGHWAY_TYPES:  # finds speed limit of current way
            if 'maxspeed_mph' in way['tags']:
                speed_limit = way['tags']['maxspeed_mph']
            else:
                speed_limit = DEFAULT_SPEED_LIMIT_MPH[way['tags']['highway']]

            for i in range(
                    len(way['nodes'])
            ):  # organizes a nodes_web (node_id's mapped to their neighbors). NOTE: only considers relevant nodes
                relevant_nodes.add(way['nodes'][i])
                if way['nodes'][i] not in node_web:
                    node_web[way['nodes'][i]] = {}

            for i in range(
                    len(way['nodes']) - 1
            ):  # assumes MAX speed limit if a node is in 2 different ways
                node_web[way['nodes'][i]][way['nodes'][i + 1]] = max(
                    speed_limit,
                    node_web[way['nodes'][i]].get(way['nodes'][i + 1], 0))

            if not way['tags'].get('oneway') == 'yes':  # 2 directional highway
                for i in range(len(way['nodes']) - 1, 0, -1):
                    node_web[way['nodes'][i]][way['nodes'][i - 1]] = max(
                        speed_limit,
                        node_web[way['nodes'][i]].get(way['nodes'][i - 1], 0))

    for node in read_osm_data(
            nodes_filename):  #creates cood_coordinate dictionary
        if node['id'] in relevant_nodes:
            node_coord[node['id']] = node['lat'], node['lon']

    return (node_web, node_coord)
Ejemplo n.º 7
0
def build_auxiliary_structures(nodes_filename, ways_filename):
    """
    Create any auxiliary structures you are interested in, by reading the data
    from the given filenames (using read_osm_data)
    """
    Nodes = {}
    Edges = {}
    Dic = {}
    # Loop over each node in ways file
    # Look for the node in node file
    for ways in read_osm_data(ways_filename):
        # if it has an allowed highway types
        if 'highway' in ways['tags'] and ways['tags'][
                'highway'] in ALLOWED_HIGHWAY_TYPES:
            for node in ways['nodes']:
                if not (node in Nodes):
                    Nodes[node] = (0.0)
                if not (node in Edges):
                    Edges[node] = []

            Speed = 0
            # Get speed for ways
            if 'maxspeed_mph' in ways['tags']:
                Speed = ways['tags']['maxspeed_mph']
            else:
                Speed = DEFAULT_SPEED_LIMIT_MPH[ways['tags']['highway']]
            # Get Neighbor nodes for ways
            Neighbor = ways['nodes']

            # always append right neighbor
            for i in range(len(Neighbor) - 1):
                Edges[Neighbor[i]].append((Neighbor[i + 1], Speed))

            # if not oneway, append left neighbor to the node
            if not ('oneway'
                    in ways['tags']) or ways['tags']['oneway'] != 'yes':
                Neighbor = Neighbor[::-1]
                for i in range(len(Neighbor) - 1):
                    Edges[Neighbor[i]].append((Neighbor[i + 1], Speed))

    # Loop over nodes_file
    for node in read_osm_data(nodes_filename):
        if node['id'] in Nodes:
            Nodes[node['id']] = (node['lat'], node['lon'])
            Dic[(node['lat'], node['lon'])] = node['id']
    return (Nodes, Edges, Dic)
Ejemplo n.º 8
0
def is_highway(filename):
    highways = []

    for node in read_osm_data(filename):
        if 'highway' in node['tags']:
            if node['tags']['highway'] in ALLOWED_HIGHWAY_TYPES:
                highways.append(node)

    return highways
Ejemplo n.º 9
0
def build_auxiliary_structures(nodes_filename, ways_filename):
    """
    read data from nodes_filename, ways_filename using read_osm_data
    return a graph of adjacency nodes, as a dictionary
    {
        node1: {node2: speed-x, node3: speed-y, node9: speed-z,...}
        node2: {node1: speed-a, node4: speed-b,...}
        ,...
    }
    """
    graph = {}
    for raw_way in read_osm_data(ways_filename):
        if 'highway' in raw_way['tags'] and raw_way['tags'][
                'highway'] in ALLOWED_HIGHWAY_TYPES:  # relevant ways
            # speed limit property
            if 'maxspeed_mph' in raw_way['tags']:
                speed = raw_way['tags']['maxspeed_mph']
            else:
                speed = DEFAULT_SPEED_LIMIT_MPH[raw_way['tags']['highway']]
            for node1, node2 in zip(raw_way['nodes'], raw_way['nodes'][1:]):
                if node1 in graph and node2 in graph[
                        node1]:  # found higher-speed way for node1 -> node2
                    graph[node1][node2] = min(graph[node1][node2], speed)
                else:
                    graph.setdefault(node1, {})[node2] = speed
                if not ('oneway' in raw_way['tags']
                        and raw_way['tags']['oneway'] == 'yes'):  # 2 way
                    if node2 in graph and node1 in graph[
                            node2]:  # found higher-speed way for node2 -> node1
                        graph[node2][node1] = min(graph[node2][node1], speed)
                    else:
                        graph.setdefault(node2, {})[node1] = speed
            if ('oneway' in raw_way['tags']
                    and raw_way['tags']['oneway'] == 'yes'):
                # NOTE: dont forget, the zip(raw_way['nodes'], raw_way['nodes'][1:]) statement
                # might skip adding the last node in way['nodes'] if it's a one-way way
                graph.setdefault(node2, {})
    for node in read_osm_data(nodes_filename):
        if node['id'] in graph:  # only nodes that appeared on some ways are considered
            graph[node['id']]['loc'] = (node['lat'], node['lon'])
    return graph
Ejemplo n.º 10
0
def ids_to_location(valid_ids, node_filename):
    '''
    Will replace location values in our dictionary.
    Key will be 'location'.
    Maps to the coordinates.
    '''
    data = read_osm_data(node_filename)

    for node in data:  #loops through all nodes in dataset
        if node['id'] in valid_ids:  #if it finds an id which is in our dictionary, adjust the value in its 'location' key and make it the coordinates.
            valid_ids[node['id']]['location'] = (node.get('lat', 0),
                                                 node.get('lon', 0))
    return valid_ids
Ejemplo n.º 11
0
def build_auxiliary_structures(nodes_filename, ways_filename):
    """
    Create any auxiliary structures you are interested in, by reading the data
    from the given filenames (using read_osm_data)
    """
    #The datatype I will be a dict with keys of eligible node ids
    #with values containing a set of tuples of adjacent eligible nodes:
    #{node: {(id,lat,lon,distance,speed_limit)}}
    answer_dict = {}
    big_nodes = {}
    list_of_nodes = set()
    for z in read_osm_data(ways_filename):
        if 'highway' in z['tags'].keys():
            if z['tags']['highway'] in ALLOWED_HIGHWAY_TYPES:
                for f in z['nodes']:
                    list_of_nodes.add(f)
    for y in read_osm_data(nodes_filename):
        if y['id'] in list_of_nodes:
            big_nodes.update({y['id']: y})
            big_tuple = (y['id'], y['lat'], y['lon'])
            node_list.add(big_tuple)
    list_of_nodes.clear()
    for x in read_osm_data(ways_filename):
        if 'highway' in x['tags'].keys():
            if x['tags']['highway'] in ALLOWED_HIGHWAY_TYPES:
                for w in range(len(x['nodes'])):
                    #find speed limit:
                    if 'maxspeed_mph' in x['tags'].keys():
                        speed = x['tags']['maxspeed_mph']
                    else:
                        speed = DEFAULT_SPEED_LIMIT_MPH[x['tags']['highway']]
                    #adds a new key if id is not in answer_dict
                    if x['nodes'][w] not in answer_dict.keys():
                        answer_dict.update({x['nodes'][w]: set()})
                    if w != len(x['nodes']) - 1:
                        #calculate distance between x['nodes'][w] and x['nodes'][w+1]
                        lat = big_nodes[x['nodes'][w + 1]]['lat']
                        lon = big_nodes[x['nodes'][w + 1]]['lon']
                        oth_lat = big_nodes[x['nodes'][w]]['lat']
                        oth_lon = big_nodes[x['nodes'][w]]['lon']
                        dist = great_circle_distance((lat, lon),
                                                     (oth_lat, oth_lon))
                        answer_dict[x['nodes'][w]].add(
                            (x['nodes'][w + 1], lat, lon, dist, speed))
                        '''
                        #to avoid duplicate reference nodes.
                        length=None
                        sonic=None
                        for f in answer_dict[x['nodes'][w]]:
                            if f[0]==x['nodes'][w+1]:
                                length=f[3]
                                sonic=f[4]
                        if length==None:
                            answer_dict[x['nodes'][w]].add((x['nodes'][w+1],lat,lon,dist,speed))
                        elif (length/sonic)>(dist/speed):
                            answer_dict[x['nodes'][w]].remove((x['nodes'][w+1],lat,lon,length,sonic))
                            answer_dict[x['nodes'][w]].add((x['nodes'][w+1],lat,lon,dist,speed))
                         '''
                    if 'oneway' in x['tags'].keys():
                        if x['tags']['oneway'] != 'yes':
                            if w != 0:
                                #calculate distance between x['nodes'][w] and x['nodes'][w-1]
                                lat = big_nodes[x['nodes'][w - 1]]['lat']
                                lon = big_nodes[x['nodes'][w - 1]]['lon']
                                oth_lat = big_nodes[x['nodes'][w]]['lat']
                                oth_lon = big_nodes[x['nodes'][w]]['lon']
                                dist = great_circle_distance(
                                    (lat, lon), (oth_lat, oth_lon))
                                answer_dict[x['nodes'][w]].add(
                                    (x['nodes'][w - 1], lat, lon, dist, speed))
                                '''
                                #to avoid duplicate reference nodes.
                                length1=None
                                sonic1=None
                                for f in answer_dict[x['nodes'][w]]:
                                    if f[0]==x['nodes'][w-1]:
                                        length1=f[3]
                                        sonic1=f[4]
                                if length1==None:
                                    answer_dict[x['nodes'][w]].add((x['nodes'][w-1],lat,lon,dist,speed))
                                elif (length1/sonic1)>(dist/speed):
                                    answer_dict[x['nodes'][w]].remove((x['nodes'][w-1],lat,lon,length1,sonic1))
                                    answer_dict[x['nodes'][w]].add((x['nodes'][w-1],lat,lon,dist,speed))
                                '''
                    else:
                        if w != 0:
                            #calculate distance between x['nodes'][w] and x['nodes'][w-1]
                            lat = big_nodes[x['nodes'][w - 1]]['lat']
                            lon = big_nodes[x['nodes'][w - 1]]['lon']
                            oth_lat = big_nodes[x['nodes'][w]]['lat']
                            oth_lon = big_nodes[x['nodes'][w]]['lon']
                            dist = great_circle_distance((lat, lon),
                                                         (oth_lat, oth_lon))
                            answer_dict[x['nodes'][w]].add(
                                (x['nodes'][w - 1], lat, lon, dist, speed))
                            '''
                            #to avoid duplicate reference nodes.
                            length2=None
                            sonic2=None
                            for f in answer_dict[x['nodes'][w]]:
                                if f[0]==x['nodes'][w-1]:
                                    length2=f[3]
                                    sonic2=f[4]
                            if length2==None:
                                answer_dict[x['nodes'][w]].add((x['nodes'][w-1],lat,lon,dist,speed))
                            elif (length2/sonic2)>(dist/speed):
                                answer_dict[x['nodes'][w]].remove((x['nodes'][w-1],lat,lon,length2,sonic2))
                                answer_dict[x['nodes'][w]].add((x['nodes'][w-1],lat,lon,dist,speed))
                            '''
    big_nodes.clear()
    #removing any empty keys
    actual_dict = {}
    print(node_list)
    for f in answer_dict.keys():
        if answer_dict[f] != set():
            actual_dict.update({f: answer_dict[f]})
    answer_dict.clear()
    return actual_dict
Ejemplo n.º 12
0
        a list of (latitude, longitude) tuples representing the shortest path
        (in terms of time) from loc1 to loc2.
    """
    return find_short_path(aux_structures, loc1, loc2, 1)


if __name__ == '__main__':
    # additional code here will be run only when lab.py is invoked directly
    # (not when imported from test.py), so this is a good place to put code
    # used, for example, to generate the results for the online questions.
    #Section 2 Questions:
    #   count=0
    #for node in read_osm_data('resources/cambridge.nodes'):
    #    count+=1

    for way in read_osm_data('resources/mit.ways'):
        print(way)
    '''for node in read_osm_data('resources/mit.nodes'):
        print(node)'''

    #for node in read_osm_data('resources/cambridge.nodes'):
    #    if len(node['tags'])!=0:
    #        if 'name' in node['tags'].keys():
    #            count+=1
    #print(count)

    #for node in read_osm_data('resources/cambridge.nodes'):
    #    if len(node['tags'])!=0:
    #        if 'name' in node['tags'].keys():
    #            if node['tags']['name']=='77 Massachusetts Ave':
    #                print(node['id'])
Ejemplo n.º 13
0
def build_auxiliary_structures(nodes_filename, ways_filename):
    """
    Create any auxiliary structures you are interested in, by reading the data
    from the given filenames (using read_osm_data)
    ways = {way_id: {'nodes': [id, id], 'tags':{'maxspeed_mph':x, 'oneway':y, 'highway': z}}}
    nodes = {node_id: {'neighbors': {id, id}, 'lat': #, 'lon': #}}
    """

    ways = {}  # ways = {way_id: {'nodes': [id, id], 'tags':{x:y, u:z}}}
    nodes = {
    }  # nodes = {node_id: {'neighbors': {id, id}, 'lat': #, 'lon': #}}
    max_speed_dic = {}

    # Make data structure for allowed highways
    for w in read_osm_data(ways_filename):
        if 'highway' in w['tags'] and w['tags'][
                'highway'] in ALLOWED_HIGHWAY_TYPES:

            ways[w['id']] = {
                'nodes': w['nodes'],
                'tags': {
                    'maxspeed_mph':
                    w['tags'].get(
                        'maxspeed_mph',
                        DEFAULT_SPEED_LIMIT_MPH[w['tags']['highway']]),
                    'oneway':
                    w['tags'].get('oneway', None),
                    'highway':
                    w['tags'].get('highway', None)
                }
            }

            i = w['id']

            for j in range(len(
                    ways[i]
                ['nodes'])):  # go through the nodes connected by that way
                # Initialize all the nodes
                if ways[i]['nodes'][j] not in nodes:
                    nodes[ways[i]['nodes'][j]] = {
                        'neighbors': set(),
                        'lat': None,
                        'lon': None
                    }

            for j in range(len(ways[i]['nodes']) -
                           1):  # go through the nodes connected by that way
                # One-way roads
                # If one-way key exists and is yes
                if 'oneway' in ways[i]['tags'] and ways[i]['tags'][
                        'oneway'] == 'yes':
                    nodes[ways[i]['nodes'][j]]['neighbors'].add(
                        ways[i]['nodes'][j + 1])

                    max_speed_dic[(
                        w['nodes'][j],
                        w['nodes'][j + 1])] = ways[i]['tags']['maxspeed_mph']

                # Two-way roads
                else:
                    nodes[ways[i]['nodes'][j]]['neighbors'].add(
                        ways[i]['nodes'][j + 1])
                    nodes[ways[i]['nodes'][j + 1]]['neighbors'].add(
                        ways[i]['nodes'][j])

                    max_speed_dic[(
                        w['nodes'][j],
                        w['nodes'][j + 1])] = ways[i]['tags']['maxspeed_mph']
                    max_speed_dic[(
                        w['nodes'][j + 1],
                        w['nodes'][j])] = ways[i]['tags']['maxspeed_mph']

    # Get nodes' lat and lon
    for n in read_osm_data(nodes_filename):
        if n['id'] in nodes:
            nodes[n['id']]['lat'] = n['lat']
            nodes[n['id']]['lon'] = n['lon']

    return nodes, ways, max_speed_dic
Ejemplo n.º 14
0
def build_auxiliary_structures(nodes_filename, ways_filename):
    """
    Create any auxiliary structures you are interested in, by reading the data
    from the given filenames (using read_osm_data)
    """
    # initialize empty dictionaries
    nodes_dict = {}
    dist_dict = {}
    speed_dict = {}
    speed = 0

    # read each way from file
    for way in read_osm_data(ways_filename):
        # check for 'highway' and allowed highway type
        if 'highway' in way['tags'] and way['tags']['highway'] in ALLOWED_HIGHWAY_TYPES:
            # find speed along specific way
            if 'maxspeed_mph' in way['tags']:
                speed = way['tags']['maxspeed_mph']
            else:
                speed = DEFAULT_SPEED_LIMIT_MPH[way['tags']['highway']]

            # cycle through each node for this way
            for i in range(len(way['nodes'])):
                n = way['nodes'][i]

                if n not in nodes_dict:
                    nodes_dict[n] = set()
                if n not in dist_dict:
                    dist_dict[n] = ()
                # add node adjacent to the right as a child
                if i != len(way['nodes'])-1:
                    n_plus = way['nodes'][i+1]
                    nodes_dict[n].add(n_plus)

                    # create tuple of n and n+1 as key
                    # always sort pair in ascending order - consistent throughout search
                    pair = sorted_tuple(n, n_plus)

                    # set value of tuple nodes equal to speed along way
                    # reset value if a higher speed is found
                    if pair not in speed_dict:
                        speed_dict[pair] = speed
                    else:
                        if speed_dict[pair] < speed:
                            speed_dict[pair] = speed

                # check for oneway before adding nodes adjacent to the left
                if i != 0 and not ('oneway' in way['tags'] and way['tags']['oneway'] == 'yes'):
                    n_minus = way['nodes'][i-1]
                    nodes_dict[n].add(n_minus)

                    # repeat process above for speed dict and pair of n and n-1
                    pair = sorted_tuple(n, n_minus)

                    if pair not in speed_dict:
                        speed_dict[pair] = speed
                    else:
                        if speed_dict[pair] < speed:
                            speed_dict[pair] = speed

    # populate dist_dict with lat and lon of all valid nodes
    for node in read_osm_data(nodes_filename):
        if node['id'] in nodes_dict:
            id = node['id']
            lat = node['lat']
            lon = node['lon']
            dist_dict[id] = (lat, lon)

    return nodes_dict, dist_dict, speed_dict
Ejemplo n.º 15
0
def build_auxiliary_structures(nodes_filename, ways_filename):
    """
    Create any auxiliary structures you are interested in, by reading the data
    from the given filenames (using read_osm_data), and returns three structures,
    the first of which is a map of the nodes to other nodes it is connected to by
    a way. The second is a dictionary map of nodes to their individual locations,
    and the third is a map of the nodes to the connected nodes by way as well as
    the speed limit of the associate way connecting them
    """
    final_data_structure_1 = {}
    final_data_structure_2 = {}
    final_data_structure_3 = {}
    for way in read_osm_data(ways_filename):
        if 'highway' in way['tags'].keys():
            if way['tags']['highway'] in ALLOWED_HIGHWAY_TYPES:
                if 'maxspeed_mph' in way['tags']:
                    limit = way['tags']['maxspeed_mph']
                else:
                    limit = DEFAULT_SPEED_LIMIT_MPH[way['tags']['highway']]
                if 'oneway' in way['tags'].keys(
                ) and way['tags']['oneway'] == 'yes':
                    index = 0
                    while index < len(way['nodes']) - 1:
                        id1, id2 = way['nodes'][index], way['nodes'][index + 1]
                        if id1 not in final_data_structure_1:
                            final_data_structure_1[id1] = {id2}
                            final_data_structure_3[id1] = {id2: limit}
                        else:
                            final_data_structure_1[id1].add(id2)
                            final_data_structure_3[id1][id2] = limit
                        index = index + 1
                    if way['nodes'][len(way['nodes']) -
                                    1] not in final_data_structure_1.keys():
                        final_data_structure_1[way['nodes'][len(way['nodes']) -
                                                            1]] = set()
                        final_data_structure_3[way['nodes'][len(way['nodes']) -
                                                            1]] = dict()
                else:
                    index = 0
                    while index < len(way['nodes']) - 1:
                        id1, id2 = way['nodes'][index], way['nodes'][index + 1]
                        if id1 not in final_data_structure_1:
                            final_data_structure_1[id1] = {id2}
                            final_data_structure_3[id1] = {id2: limit}
                        else:
                            final_data_structure_1[id1].add(id2)
                            final_data_structure_3[id1][id2] = limit
                        index = index + 1
                    index2 = len(way['nodes']) - 1
                    while index2 > 0:
                        id1, id2 = way['nodes'][index2], way['nodes'][index2 -
                                                                      1]
                        if id1 not in final_data_structure_1:
                            final_data_structure_1[id1] = {id2}
                            final_data_structure_3[id1] = {id2: limit}
                        else:
                            final_data_structure_1[id1].add(id2)
                            final_data_structure_3[id1][id2] = limit
                        index2 = index2 - 1
    for node in read_osm_data(nodes_filename):
        location = (node['lat'], node['lon'])
        if node['id'] in final_data_structure_1.keys():
            final_data_structure_2[node['id']] = location
    for node in final_data_structure_1:
        for neighbor in final_data_structure_1[node]:
            loc1 = final_data_structure_2[node]
            loc2 = final_data_structure_2[neighbor]
            first_cost_distance = great_circle_distance(loc1, loc2)
            time = first_cost_distance / final_data_structure_3[node][neighbor]
            final_data_structure_3[node][neighbor] = time
    return final_data_structure_1, final_data_structure_2, final_data_structure_3
Ejemplo n.º 16
0
MIDWEST_NODES = root_folder + '/resources/midwest.nodes'
MIDWEST_WAYS = root_folder + '/resources/midwest.ways'

MIT_NODES = root_folder + '/resources/mit.nodes'
MIT_WAYS = root_folder + '/resources/mit.ways'

if __name__ == '__main__':
    # additional code here will be run only when lab.py is invoked directly
    # (not when imported from test.py), so this is a good place to put code
    # used, for example, to generate the results for the online questions.
    nodes_count = 0
    name_count = 0
    id = None
    name = '77 Massachusetts Ave'
    for node in read_osm_data(CAMBRIDGE_NODES):
        nodes_count += 1
        node_name = node['tags'].get('name', None)
        if node_name is not None:
            name_count += 1
            if node_name == name:
                id = node['id']
    print('%s nodes' % nodes_count)
    print('%s names nodes' % name_count)
    print('%s - %s' % (id, name))

    ways_count = 0
    one_way_count = 0
    for way in read_osm_data(CAMBRIDGE_WAYS):
        ways_count += 1
        one_way = way['tags'].get('oneway', 'no')
Ejemplo n.º 17
0
    # loc2 = (42.3573, -71.0928)
    #
    # path = find_short_path(aux_structures, loc1, loc2)
    # print(path)
    #
    # midwest_nodes = [item for item in read_osm_data('resources/midwest.nodes')]
    # print(midwest_nodes)
    # lat1 = [node['lat'] for node in midwest_nodes if node['id'] == 233941454]
    # lon1 = [node['lon'] for node in midwest_nodes if node['id'] == 233941454]
    #
    # lat2 = [node['lat'] for node in midwest_nodes if node['id'] == 233947199]
    # lon2 = [node['lon'] for node in midwest_nodes if node['id'] == 233947199]
    #
    # print(lat1, lat2)
    # print(great_circle_distance((lat1[0], lon1[0]), (lat2[0], lon2[0])))
    midwest_ways = [item for item in read_osm_data('resources/midwest.ways')]
    # print(midwest_ways)

    # cum_dist = 0
    # our_path = None
    # our_path = [path for path in midwest_ways if path['id'] == 21705939]
    # nodes = get_path_nodes(our_path[0], 'resources/midwest.nodes', 'resources/midwest.ways')
    #
    #
    # for i in range(len(nodes)-1):
    #     print("nodes: ", nodes)
    #     print("\n\n\nnodes[i]", nodes[i])
    #     print(nodes[i]['lat'])
    #     cum_dist+=great_circle_distance((nodes[i]['lat'], nodes[i]['lon']), (nodes[i+1]['lat'], nodes[i+1]['lon']))
    # print(cum_dist)
Ejemplo n.º 18
0
    #a = {'id': 0, 'children': (1,2,3,4), 'loc': (20,15)}
    #b = {'id': 1, 'children': (3,4),'loc': (10,2)}

    #nodes = (a,b)
    #for x in nodes:
        #if x['id'] == 0:
            #print(x['children'])
    # construct a dictionary of allowed nodes and each of each children, take note of one ways
    # second dictionary of allowed


    #aux = build_auxiliary_structures('resources/cambridge.nodes','resources/cambridge.ways')

    #print(find_nearest_loc(aux, (42.3592, -71.0932), (42.3575,  -71.0956)))

    # Find distance between (42.3858, -71.0783),(42.5465, -71.1787)
    # Checked size w/o heuristics = 372625
    # Checked size = 45928
    # print(find_short_path(aux, (42.3858, -71.0783),(42.5465, -71.1787)))

    for way in read_osm_data('resources/mit.ways'):
        print(way)

    for node in read_osm_data('resources/mit.nodes'):
        print(node)
    print(build_auxiliary_structures('resources/mit.nodes','resources/mit.ways'))


    pass