Esempio n. 1
0
def test_network_saving_loading():

    # save/load graph as shapefile and graphml file
    G = ox.graph_from_place('Piedmont, California, USA')
    G_projected = ox.project_graph(G)
    ox.save_graph_shapefile(G_projected)
    ox.save_graphml(G_projected)
    ox.save_graphml(G_projected, filename='gephi.graphml', gephi=True)
    G2 = ox.load_graphml('graph.graphml')
    G3 = ox.load_graphml('graph.graphml', node_type=str)

    # convert graph to node/edge GeoDataFrames and back again
    gdf_edges = ox.graph_to_gdfs(G, nodes=False, edges=True, fill_edge_geometry=False)
    gdf_nodes, gdf_edges = ox.graph_to_gdfs(G, nodes=True, edges=True, node_geometry=True, fill_edge_geometry=True)
    G4 = ox.gdfs_to_graph(gdf_nodes, gdf_edges)

    # find graph nodes nearest to some set of points
    X = gdf_nodes['x'].head()
    Y = gdf_nodes['y'].head()
    nn1 = ox.get_nearest_nodes(G, X, Y)
    nn2 = ox.get_nearest_nodes(G, X, Y, method='kdtree')
    nn3 = ox.get_nearest_nodes(G, X, Y, method='balltree')

    # find graph edges nearest to some set of points
    ne1 = ox.get_nearest_edges(G, X, Y)
    ne2 = ox.get_nearest_edges(G, X, Y, method='kdtree')
    ne3 = ox.get_nearest_edges(G, X, Y, method='kdtree', dist=50)
Esempio n. 2
0
def test_network_saving_loading():

    # save/load graph as shapefile and graphml file
    G = ox.graph_from_place('Piedmont, California, USA')
    G_projected = ox.project_graph(G)
    ox.save_graph_shapefile(G_projected)
    ox.save_graphml(G_projected)
    ox.save_graphml(G_projected, filename='gephi.graphml', gephi=True)
    G2 = ox.load_graphml('graph.graphml')

    # convert graph to node/edge GeoDataFrames and back again
    gdf_edges = ox.graph_to_gdfs(G,
                                 nodes=False,
                                 edges=True,
                                 fill_edge_geometry=False)
    gdf_nodes, gdf_edges = ox.graph_to_gdfs(G,
                                            nodes=True,
                                            edges=True,
                                            node_geometry=True,
                                            fill_edge_geometry=True)
    G3 = ox.gdfs_to_graph(gdf_nodes, gdf_edges)

    # find graph nodes nearest to some set of points
    X = gdf_nodes['x'].head()
    Y = gdf_nodes['y'].head()
    nn1 = ox.get_nearest_nodes(G, X, Y)
    nn2 = ox.get_nearest_nodes(G, X, Y, method='kdtree')
    nn3 = ox.get_nearest_nodes(G, X, Y, method='balltree')
Esempio n. 3
0
def test_find_nearest():

    # get graph
    G = ox.graph_from_point(location_point, dist=500, network_type="drive")

    # convert graph to node/edge GeoDataFrames and back again
    gdf_nodes, gdf_edges = ox.graph_to_gdfs(
        G, nodes=True, edges=True, node_geometry=True, fill_edge_geometry=True
    )
    assert len(gdf_nodes) == len(G)
    assert len(gdf_edges) == len(G.edges(keys=True))
    G = ox.graph_from_gdfs(gdf_nodes, gdf_edges)
    assert len(gdf_nodes) == len(G)
    assert len(gdf_edges) == len(G.edges(keys=True))

    # get nearest node
    nn, d = ox.get_nearest_node(G, location_point, method="euclidean", return_dist=True)

    # get nearest nodes: haversine, kdtree, balltree
    X = gdf_nodes["x"].head()
    Y = gdf_nodes["y"].head()
    nn1 = ox.get_nearest_nodes(G, X, Y)
    nn2 = ox.get_nearest_nodes(G, X, Y, method="kdtree")
    nn3 = ox.get_nearest_nodes(G, X, Y, method="balltree")

    # get nearest edge
    u, v, k, g, d = ox.get_nearest_edge(G, location_point, return_geom=True, return_dist=True)

    # get nearest edges: haversine, kdtree, balltree
    ne1 = ox.get_nearest_edges(G, X, Y)
    ne2 = ox.get_nearest_edges(G, X, Y, method="kdtree")
    ne3 = ox.get_nearest_edges(G, X, Y, method="balltree", dist=0.0001)
Esempio n. 4
0
def import_networks(networks, topology, tesselation):
    """
    Import the transportation networks into an empty topology.

    :param networks: a dictionary of transport networks as returned by
        :py:func:`fetch_osm_data`.
    :param topology: a topology instance.
    :param tesselation: plane tesselation.
    """

    # We process the networks one by one. Pedestrian paths, bike paths
    # and roads need only a simple downsampling.
    walk_network = None
    for transport_type in TRANSPORT_TO_NETWORK:
        network = downsample_network(tesselation, networks[transport_type])
        copy_network_to_topology(transport_type, network, topology)

        if transport_type == TransportType.WALK:
            walk_network = network

    # Public transport routes are a bit different. While they go
    # through the same points on the map as the previously processed
    # networks, they should not go through the same graph nodes. They
    # exist in a parallel plane in some sense, and you can enter that
    # plain only at specific points -- the public transportation
    # platforms. In those points we will create a zero-length
    # connection between the node on the route and the node on the
    # path.
    locations = []

    for layer, transport_type in enumerate(TRANSPORT_TO_ROUTE, start=1):
        if transport_type not in networks:
            continue

        transport_network = combine_network_lines(networks[transport_type])
        offset = layer * tesselation.ncells

        platforms = extract_platforms(transport_network)
        transport_network = downsample_network(tesselation,
                                               transport_network,
                                               offset=offset)

        copy_network_to_topology(transport_type, transport_network, topology)

        px = [p["x"] for p in platforms]
        py = [p["y"] for p in platforms]

        transport_nodes = osmnx.get_nearest_nodes(transport_network, px, py)
        walk_nodes = osmnx.get_nearest_nodes(walk_network, px, py)

        for tn, wn, platform in zip(transport_nodes, walk_nodes, platforms):
            with contextlib.suppress(RuntimeError):
                topology.add_edge(tn, wn, TransportType.WALK, distance=0)
            location = extract_route_location(platform)
            location["node"] = tn
            locations.append(location)

    return locations
Esempio n. 5
0
def assign_osm(dataframe, mode='all'):
    """Assigns an OSM node by taking the average location of the two datasets and finding the nearest node present in the
    mode layer.

    Parameters
    ----------
    dataframe : pandas DataFrame
        The data records to be used to find the nearest OSM node

    mode : str
        Mode choice of  {‘walk’, ‘bike’, ‘drive’, ‘drive_service’, ‘all’, ‘all_private’, ‘none’}

    Returns
    -------
    df_osm_location : pandas DataFrame
        Data records with the OSM ID of the nearest node and its respective lat/lon
    """

    max_lat = dataframe[['lat1', 'lat2']].max().max()
    min_lat = dataframe[['lat1', 'lat2']].min().min()
    max_lon = dataframe[['lon1', 'lon2']].max().max()
    min_lon = dataframe[['lon1', 'lon2']].min().min()

    osm_layer = osm_download.download_osm_layer([max_lat, min_lat, max_lon, min_lon], mode)
    osm_nodes = pd.DataFrame([[node[0], node[1]['y'], node[1]['x']] for node in osm_layer.nodes(data=True)],
                             columns=['osm_id', 'lat', 'lon']).set_index('osm_id')

    dataframe['avg_lat'] = dataframe[['lat1', 'lat2']].mean(axis=1)
    dataframe['avg_lon'] = dataframe[['lon1', 'lon2']].mean(axis=1)

    nearest_node = ox.get_nearest_nodes(osm_layer, dataframe['avg_lon'], dataframe['avg_lat'], method='balltree')
    dataframe['osm_id'] = nearest_node
    df_osm_location = dataframe.join(osm_nodes, on=['osm_id']).drop(columns=['lat1', 'lon1', 'lat2', 'lon2', 'avg_lat', 'avg_lon'])

    return df_osm_location
Esempio n. 6
0
def get_nearest_streetnames(location_tuple, distFromPoint=50):
    """This function retrieves the nearest streets of a coordinate tuple (lat, lon). Currently set to drive network. Edit the parameters in line 40 to fit your preferences."""
    try:
        G = ox.graph_from_point(location_tuple,
                                network_type='drive',
                                distance=distFromPoint)
        G = ox.project_graph(G)
        ints = ox.clean_intersections(G)

        gdf = gpd.GeoDataFrame(ints, columns=['geometry'], crs=G.graph['crs'])
        X = gdf['geometry'].map(lambda pt: pt.coords[0][0])
        Y = gdf['geometry'].map(lambda pt: pt.coords[0][1])

        nodes = ox.get_nearest_nodes(G, X, Y, method='kdtree')
        streetnames = []

        for n in nodes:
            for nbr in nx.neighbors(G, n):
                for d in G.get_edge_data(n, nbr).values():
                    if 'name' in d:
                        if type(d['name']) == str:
                            streetnames.append(d['name'])
                        elif type(d['name']) == list:
                            for name in d['name']:
                                streetnames.append(name)
        streetnames = list(set(streetnames))
    except:
        streetnames = []
    return streetnames
Esempio n. 7
0
    def localisations_nearest_nodes(self, x_coordinates, y_coordinates, method="balltree"):

        # convert coordinate lists to np.array
        x_array = np.array(x_coordinates, dtype=np.float32)
        y_array = np.array(y_coordinates, dtype=np.float32)

        # we use osmnx get_nearest_nodes function, with method='balltree' [uses scikit_learn]
        return ox.get_nearest_nodes(self.graph, x_array, y_array, method=method)
Esempio n. 8
0
def map_matching_node(
    move_data: Union[PandasMoveDataFrame, DaskMoveDataFrame, PandasDiscreteMoveDataFrame],
    inplace: Optional[bool] = True,
    bbox: Optional[Tuple[float, float, float, float]] = None,
    place: Optional[Text] = None,
    G: Optional[MultiDiGraph] = None
) -> Optional[DataFrame]:
    """
    Generate Map matching using the graph nodes

    Parameters
    ----------
    move_data : MoveDataFrame
       The input trajectories data
    inplace: bool, optional
        if set to true the original dataframe will be altered,
        otherwise the alteration will be made in a copy, that will be returned,
        by default True
    bbox : tuple, optional
        The bounding box as (north, east, south, west), by default None
    place : string, optional
        The query to geocode to get place boundary polygon, by default None
    G : MultiDiGraph, optional
        The input graph, by default None

    Returns
    -------
    move_data : MoveDataFrame
        A copy of the original dataframe or None

    """
    if(G is None):
        if(bbox is None):
            bbox = move_data.get_bbox()
        G = ox.graph_from_bbox(
            bbox[0], bbox[2], bbox[1], bbox[3], network_type='all_private'
        )
    elif(place is not None):
        G = ox.footprints_from_place(query=place, tags={'network_type': 'all_private'})

    if not inplace:
        move_data = move_data[:]

    nodes = ox.get_nearest_nodes(
        G, X=move_data['lon'], Y=move_data['lat'], method='kdtree'
    )

    gdf_nodes = ox.graph_to_gdfs(G, edges=False)
    df_nodes = gdf_nodes.loc[nodes]

    move_data['lat'] = list(df_nodes.y)
    move_data['lon'] = list(df_nodes.x)
    move_data['geometry'] = list(df_nodes.geometry)

    if not inplace:
        return move_data
Esempio n. 9
0
def generate_distances(move_data: DataFrame,
                       inplace: Optional[bool] = False) -> Optional[DataFrame]:
    """Use generate columns distFromTrajStartToCurrPoint and edgeDistance.
     Parameters
    ----------
    move_data : dataframe
       The input trajectories data
    inplace: boolean, optional
        if set to true the original dataframe will be altered,
        otherwise the alteration will be made in a copy, that will be returned,
        by default True

    Returns
    -------
    DataFrame
        A copy of the original dataframe or None
    """
    if not inplace:
        move_data = move_data.copy()
    bbox = move_data.get_bbox()

    G = ox.graph_from_bbox(bbox[0], bbox[2], bbox[1], bbox[3])
    nodes = ox.get_nearest_nodes(G,
                                 X=move_data['lon'],
                                 Y=move_data['lat'],
                                 method='kdtree')

    distances = []
    edgeDistance = []
    dist = 0.0
    node_ant = nodes[0]
    distances.append(dist)
    edgeDistance.append(dist)

    gdf_edges = ox.graph_to_gdfs(G, nodes=False)

    for node in nodes[1:]:
        df_u = gdf_edges[gdf_edges.index.get_level_values('u') == node_ant]
        df_edge = df_u[df_u.index.get_level_values('v') == node]

        if (len(df_edge) == 0):
            dist += 0
            edgeDistance.append(dist)
        else:
            dist += df_edge['length'].values[0]
            edgeDistance.append(df_edge['length'].values[0])
        distances.append(dist)
        node_ant = node

    move_data['edgeDistance'] = edgeDistance
    move_data['distFromTrajStartToCurrPoint'] = distances

    if not inplace:
        return move_data
Esempio n. 10
0
def connections(place):
    '''
    inputs - textual query specifying spatial boundaries of some geocodeable place(s)
    returns - tuples of street intersections using OSM data
    '''

    # Create a networkx graph from OSM data within the spatial boundaries of geocodable place(s).
    G = ox.graph_from_place(place, network_type='drive', timeout=3600)

    # Project a graph from lat-long to the UTM zone appropriate for its geographic location.
    # https://en.wikipedia.org/wiki/Universal_Transverse_Mercator_coordinate_system
    Gp = ox.project_graph(G)

    # Clean-up intersections comprising clusters of nodes by merging them and returning their centroids
    ints = ox.clean_intersections(Gp)

    # create a dataframe of intersections
    gdf = gpd.GeoDataFrame(ints, columns=['geometry'], crs=Gp.graph['crs'])

    # generate lists of X, Y locations (UTM zone)
    X = gdf['geometry'].map(lambda pt: pt.coords[0][0])
    Y = gdf['geometry'].map(lambda pt: pt.coords[0][1])

    nodes = ox.get_nearest_nodes(Gp, X, Y, method='kdtree')
    connections = {}

    intersections = []
    for n in nodes:
        dict = {
            'osmid': G.nodes()[n]['osmid'],
            'latitude': G.nodes()[n]['y'],
            'longitude': G.nodes()[n]['x']
        }
        # print(dict)
        connections[n] = set([])
        for nbr in nx.neighbors(G, n):
            for d in G.get_edge_data(n, nbr).values():
                if 'name' in d:
                    if type(d['name']) == str:
                        connections[n].add(d['name'])
                    elif type(d['name']) == list:
                        for name in d['name']:
                            connections[n].add(name)
                    else:
                        connections[n].add(None)
                else:
                    connections[n].add(None)

        if len(connections) > 0:
            dict['streets'] = list(connections[n])
            print(dict)
            intersections.append(dict)

    return intersections
Esempio n. 11
0
def pt_network(G, G_s):
    '''
    Create a public transportation network matching the bus stops to the clossest intersection to be used in the analysis of multilayer urban networks.

    Parameters
    ------
    G: networkx MultiDiGraph
       Public transport graph generated from a GTF with peartree

    G_s: networkx MultiDiGraph
         Streets network, generated using OSMnx

    Returns
    ------
    G_pt: networkx MultiDigraph
          Graph where the nodes are the clossest street intersections to bus stops and the edges the public transportation routes
    '''

    G_pt = copy.deepcopy(G_s)
    G = pt.reproject(G, to_epsg=4326)
    G_s = pt.reproject(G_s, to_epsg=4326)

    # 1.- Get a list of nodes, x's and y's
    nodes = []
    x_s = []
    y_s = []
    for n, data in G.nodes(data=True):
        nodes.append(n)
        x_s.append(data['x'])
        y_s.append(data['y'])

    # Get the closest intersections as a numpy array of ID's
    match_nodes = ox.get_nearest_nodes(G_s, x_s, y_s, method='kdtree')

    # Pair the bus stop with its closest intersection
    pairs = {}
    for i, i_s in zip(nodes, match_nodes):
        pairs[i] = i_s

    # Remove all the edges from the public transport graph
    G_pt.remove_edges_from(list(G_pt.edges()))

    # Remove the unmatched intersections
    remove = [i for i in G_pt.nodes() if i not in match_nodes]
    G_pt.remove_nodes_from(remove)

    # Add the bus routes (edges)
    edges = [(pairs[i], pairs[j], d) for i, j, d in G.edges(data=True)
             if pairs[i] != pairs[j]]
    G_pt.add_edges_from(edges)
    for i, j, data in G_pt.edges(data=True):
        data['oneway'] = False
    return G_pt
Esempio n. 12
0
def test_find_nearest():

    # get graph
    G = ox.graph_from_point(location_point, dist=500, network_type="drive")

    # get nearest node
    nn, d = ox.get_nearest_node(G, location_point, method="euclidean", return_dist=True)

    # get nearest nodes: haversine, kdtree, balltree
    gdf_nodes = ox.graph_to_gdfs(G, edges=False)
    X = gdf_nodes["x"].head()
    Y = gdf_nodes["y"].head()

    nn1, dist1 = ox.get_nearest_nodes(G, X, Y, return_dist=True)
    nn2, dist2 = ox.get_nearest_nodes(G, X, Y, method="kdtree", return_dist=True)
    nn3, dist3 = ox.get_nearest_nodes(G, X, Y, method="balltree", return_dist=True)
    nn4 = ox.get_nearest_nodes(G, X, Y)
    nn5 = ox.get_nearest_nodes(G, X, Y, method="kdtree")
    nn6 = ox.get_nearest_nodes(G, X, Y, method="balltree")

    # get nearest edge
    u, v, k, g, d = ox.get_nearest_edge(G, location_point, return_geom=True, return_dist=True)

    # get nearest edges: haversine, kdtree, balltree
    ne1 = ox.get_nearest_edges(G, X, Y)
    ne2 = ox.get_nearest_edges(G, X, Y, method="kdtree")
    ne3 = ox.get_nearest_edges(G, X, Y, method="balltree", dist=0.0001)
Esempio n. 13
0
def test_find_nearest():

    # get graph and x/y coords to search
    G = ox.graph_from_point(location_point, dist=500, network_type="drive")
    Gp = ox.project_graph(G)
    points = ox.utils_geo.sample_points(ox.get_undirected(Gp), 5)
    X = points.x
    Y = points.y

    # get nearest node
    nn, d = ox.get_nearest_node(Gp, location_point, method="euclidean", return_dist=True)
    nn = ox.get_nearest_node(Gp, location_point, method="euclidean", return_dist=False)

    # get nearest nodes
    nn1, dist1 = ox.get_nearest_nodes(G, X, Y, return_dist=True)
    nn2, dist2 = ox.get_nearest_nodes(Gp, X, Y, method="kdtree", return_dist=True)
    nn3, dist3 = ox.get_nearest_nodes(G, X, Y, method="balltree", return_dist=True)
    nn4 = ox.get_nearest_nodes(G, X, Y)
    nn5 = ox.get_nearest_nodes(Gp, X, Y, method="kdtree")
    nn6 = ox.get_nearest_nodes(G, X, Y, method="balltree")

    # get nearest edge
    u, v, k, g, d = ox.get_nearest_edge(Gp, location_point, return_geom=True, return_dist=True)
    u, v, k, g = ox.get_nearest_edge(Gp, location_point, return_geom=True)
    u, v, k, d = ox.get_nearest_edge(Gp, location_point, return_dist=True)
    u, v, k = ox.get_nearest_edge(Gp, location_point)

    # get nearest edges
    ne0 = ox.distance.nearest_edges(Gp, X, Y, interpolate=50)
    ne1 = ox.get_nearest_edges(Gp, X, Y)
    ne2 = ox.get_nearest_edges(Gp, X, Y, method="kdtree")
    ne3 = ox.get_nearest_edges(G, X, Y, method="balltree", dist=0.0001)
Esempio n. 14
0
def find_nearest_nodes_in_network(road_network,
                                  tdf,
                                  return_tdf_with_new_col=False):
    """Map-matching.

	For each point in a TrajDataFrame, it finds the nearest node in a road network.

	Parameters:
	----------
	road_network : networkx MultiDiGraph
		the road network on which to map the points.

	tdf : TrajDataFrame
		the trajectories of the individuals.

	return_tdf_with_new_col : boolean
		if False (default), returns the list of the nearest nodes (as OSM IDs);
		if True, returns a copy of the original TrajDataFrame with one more column called 'node_id'.

	Returns
	-------
	list (if return_tdf_with_new_col = True)
		list of the nearest nodes.

	TrajDataFrame (if return_tdf_with_new_col = False)
		the TrajDataFrame with 1 more column collecting the nearest node's ID for each point.
	"""

    tdf.sort_by_uid_and_datetime()

    # extracting arrays of lat and lng for the vehicle:
    vec_of_longitudes = np.array(tdf['lng'])
    vec_of_latitudes = np.array(tdf['lat'])

    # for each (lat,lon), find the nearest node in the road network:
    list_of_nearest_nodes = ox.get_nearest_nodes(road_network,
                                                 X=vec_of_longitudes,
                                                 Y=vec_of_latitudes,
                                                 method='balltree')
    ###
    # method (str {None, 'kdtree', 'balltree'}) – Which method to use for finding nearest node to each point.
    # If None, we manually find each node one at a time using osmnx.utils.get_nearest_node and haversine.
    # If ‘kdtree’ we use scipy.spatial.cKDTree for very fast euclidean search.
    # If ‘balltree’, we use sklearn.neighbors.BallTree for fast haversine search.
    ###

    if return_tdf_with_new_col:
        tdf_with_nearest_nodes = tdf.copy()  #.drop(columns=['lat', 'lng'])
        tdf_with_nearest_nodes['node_id'] = list_of_nearest_nodes
        return tdf_with_nearest_nodes

    return list_of_nearest_nodes
Esempio n. 15
0
def load_trips(G, path_to_trips, polygon=None):
    """
    Loads the trips and maps lat/long of start and end station to node in
    graph G.
    :param G: graph used vor lat/long to node mapping

    :param path_to_trips: path to the compacted trips csv.
    :type path_to_trips: str
    :param polygon: If only trips inside a polygon should be considered,
     pass it here.
    :type polygon: Shapely Polygon
    :return: dict with trip info and set of stations used.
    trip_nbrs structure: key=(origin node, end node), value=# of cyclists
    """
    nn_method = 'kdtree'

    nbr_of_trips, start_lat, start_long, end_lat, end_long = \
        get_lat_long_trips(path_to_trips, polygon)

    start_nodes = list(
        ox.get_nearest_nodes(G, start_long, start_lat, method=nn_method))
    end_nodes = list(
        ox.get_nearest_nodes(G, end_long, end_lat, method=nn_method))

    trip_nbrs = {}
    for trip in range(len(nbr_of_trips)):
        trip_nbrs[(int(start_nodes[trip]), int(end_nodes[trip]))] = \
            int(nbr_of_trips[trip])

    stations = set()
    for k, v in trip_nbrs.items():
        stations.add(k[0])
        stations.add(k[1])

    print('Number of trips: {}'.format(sum(trip_nbrs.values())))
    return trip_nbrs, stations
Esempio n. 16
0
def find_nearest(G, gdf, amenity_name):
    """
	Find the nearest graph nodes to the points in a GeoDataFrame

	Arguments:
		G {networkx.Graph} -- Graph created with OSMnx that contains geographic information (Lat,Lon, etc.)
		gdf {geopandas.GeoDataFrame} -- GeoDataFrame with the points to locate
		amenity_name {str} -- string with the name of the amenity that is used as seed (pharmacy, hospital, shop, etc.)

	Returns:
		geopandas.GeoDataFrame -- GeoDataFrame original dataframe with a new column call 'nearest' with the node id closser to the point
	"""
    gdf['x'] = gdf['geometry'].apply(lambda p: p.x)
    gdf['y'] = gdf['geometry'].apply(lambda p: p.y)
    gdf[f'nearest_{amenity_name}'] = ox.get_nearest_nodes(
        G, list(gdf['x']), list(gdf['y']))
    return gdf
Esempio n. 17
0
def ksi_data_preprocessing(G, filter_df):
  ksi_df = gpd.read_file("https://opendata.arcgis.com/datasets/cc17cc27ee5a4989b78d9a3810c6c007_0.geojson")
  ksi_df["INJ_INDEX"] = ksi_df["INJURY"].apply(lambda x: assign_injury_index(x))
  index_df = ksi_df[["ACCNUM", "INJ_INDEX"]].groupby(by="ACCNUM").sum()
  cols_to_keep = ["LATITUDE", "LONGITUDE", "ACCNUM", "YEAR", "TIME", "VISIBILITY", "LIGHT",	"RDSFCOND"]
  ksi_df = ksi_df[cols_to_keep]
  fatalities = ksi_df["ACCNUM"].value_counts()
  ksi_df["FATALITIES"] = ksi_df["ACCNUM"].apply(lambda x : fatalities[x])
  ksi_df = ksi_df.drop_duplicates()
  ksi_df.reset_index(drop=True, inplace=True)
  ksi_df["G_NODE"]= ox.get_nearest_nodes(G, ksi_df["LONGITUDE"], ksi_df["LATITUDE"], method="balltree")
  ksi_df.drop(["LATITUDE", "LONGITUDE"], axis=1, inplace=True)
  ksi_df = ksi_df.merge(index_df, on="ACCNUM")
  ksi_df = ksi_df.infer_objects()
  ksi_df["ACCIDENT"] = ksi_df.apply(lambda x: ACCIDENT(x["ACCNUM"], x["YEAR"], x["TIME"], x["VISIBILITY"], x["LIGHT"], x["RDSFCOND"], x["FATALITIES"], x["INJ_INDEX"]), axis=1)
  if filter_df == "VIS-CLEAR":
    return ksi_df.loc[ksi_df.VISIBILITY.isin(["Clear"])]
  if filter_df == "VIS-RAIN":
    return ksi_df.loc[ksi_df.VISIBILITY.isin(["Rain"])]
  if filter_df == "VIS-SNOW":
    return ksi_df.loc[ksi_df.VISIBILITY.isin(["Snow"])]
  elif filter_df == "VIS-NCLEAR":
    return ksi_df.loc[~ksi_df.VISIBILITY.isin(["Clear"])]
  elif filter_df == "RD-DRY":
    return ksi_df.loc[ksi_df.RDSFCOND.isin(["Dry"])]
  elif filter_df == "RD-WET":
    return ksi_df.loc[ksi_df.RDSFCOND.isin(["Wet"])]
  elif filter_df == "RD-OTHER":
    return ksi_df.loc[~ksi_df.RDSFCOND.isin(["Dry", "Wet"])]
  elif filter_df == "TIME-RUSH":
    ksi_df['TIME'] = ksi_df['TIME'].astype('int')
    return ksi_df.loc[(ksi_df.TIME.between(630,930, inclusive=True)) | (ksi_df.TIME.between(1500,1900, inclusive=True))]
  elif filter_df == "TIME-NRUSH":
    ksi_df['TIME'] = ksi_df['TIME'].astype('int')
    return ksi_df.loc[(ksi_df.TIME.between(0,629, inclusive=True)) | (ksi_df.TIME.between(1901,2400, inclusive=True)) | (ksi_df.TIME.between(931,1499, inclusive=True))]
  elif filter_df == "TIME-DAY":
    ksi_df['TIME'] = ksi_df['TIME'].astype('int')
    return ksi_df.loc[ksi_df.TIME.between(701,1900, inclusive=True)]
  elif filter_df == "TIME-NIGHT":
    ksi_df['TIME'] = ksi_df['TIME'].astype('int')
    return ksi_df.loc[(ksi_df.TIME.between(1901,2400, inclusive=True)) | (ksi_df.TIME.between(0,700, inclusive=True))]
  else:
    return ksi_df
Esempio n. 18
0
 def __init__(
     self, 
     G, 
     trip_times, 
     distance_buffer,
     pts = [],
     palette=Viridis,
     epsgs={
             "origin":"4326",
             "metric":"2154",
             "vis":"3857"
             },
     center_nodes=[]
     ):
     """
     
     """
     if center_nodes == [] and pts != []:
         xs, ys = map(list, zip(*pts))
         self.center_nodes = ox.get_nearest_nodes(
                 G,
                 xs, 
                 ys, 
                 method="kdtree"
                 )
     else:
         self.center_nodes = center_nodes
         
     self.colors = {
         trip_time:color for trip_time,color in zip(
                 trip_times, 
                 palette[len(trip_times)]
                 )
         }
     self.G = G
     self.trip_times = trip_times
     self.distance_buffer = distance_buffer
     self.epsgs = EPSG(
             epsgs["origin"],
             epsgs["metric"],
             epsgs["vis"]
             )
Esempio n. 19
0
    async def _construct_isochrones(self) -> None:
        """
        TODO: Add docstring
        """
        (ys, xs) = list(zip(*self._facilities))
        nodes = osmnx.get_nearest_nodes(self._graph, xs, ys)

        # Projects the graph to UTM
        self._graph = osmnx.project_graph(self._graph)

        # Adds an edge attribute for time in minutes required to traverse each
        # edge
        meters_per_minute = TRAVEL_SPEED * 1000 / 60  # km/hour to m/minute
        for (u, v, k, data) in self._graph.edges(data=True, keys=True):
            data['time'] = data['length'] / meters_per_minute

        # Makes the isochrone polygons for isochrone map
        self._isochrones = []
        for found_node in nodes:
            subgraph = networkx.ego_graph(self._graph,
                                          found_node,
                                          radius=self._trip_time,
                                          distance='time')
            node_points = [
                Point((data['x'], data['y']))
                for (node, data) in subgraph.nodes(data=True)
            ]
            bounding_poly = geopandas.GeoSeries(
                node_points).unary_union.convex_hull

            self._isochrones.append(bounding_poly)

        self._clean_isochrones_state()
        # Causes the `isochrones_constructed` event
        await self.isochrones_constructed()
        # Saves data of isochrones
        await self._save_isochrones()
Esempio n. 20
0
def plot_map_with_probs_routes(
        unique_roads_with_weather,
        origin=[43.663389, -79.461929],
        destination=[43.650854, -79.377587],
        filename=''):  # each input is a list with [lat, lon]

    lats = [origin[1], destination[1]]
    lons = [origin[0], destination[0]]
    #load the city network for plotting
    filein = '%s/data/cleaned/Toronto_large.graphml' % (local_path)
    G = ox.load_graphml(filein)
    df_roads = unique_roads_with_weather
    df_roads = df_roads[['u', 'v', 'collision_yn']]
    df_roads['colour'] = 'red'
    df_roads['weights'] = df_roads['collision_yn'] * 10000

    nearest_nodes = ox.get_nearest_nodes(G, lats, lons, method=None)
    #add colour to the edges that have a crash risk
    nodes, edges = ox.graph_to_gdfs(G)
    edges = edges.merge(df_roads, on=['u', 'v'], how='left')
    edges.colour[edges.colour.isna()] = 'grey'
    edges.weights[edges.weights.isna()] = 1
    edges.weights[edges.weights == 0] = 1

    #add the weights to the edge attributes as impedance
    i = 0
    for u, v, k, data in G.edges(keys=True, data=True):
        #    print(data)
        data['impedance'] = edges['weights'][i]
        #    print(data['length'])
        #    print(data['impedance'])
        #    print(edges['weights'][i])
        #    print(edges['weights'][i])
        i = i + 1

    #calculate the routes
    route_by_weight = nx.shortest_path(G,
                                       source=nearest_nodes[0],
                                       target=nearest_nodes[1],
                                       weight='impedance')
    route_by_length = nx.shortest_path(G,
                                       source=nearest_nodes[0],
                                       target=nearest_nodes[1],
                                       weight='length')
    routes = [route_by_weight, route_by_length]
    # create route colors
    rc1 = ['green'] * (len(route_by_weight))
    rc2 = ['blue'] * len(route_by_length)
    route_colours = rc1 + rc2
    # save the figure as a png
    filename_save = filename
    fig, ax = ox.plot_graph_routes(G,
                                   routes,
                                   node_size=0,
                                   route_color=route_colours,
                                   orig_dest_node_color='green',
                                   edge_color=edges.colour,
                                   fig_height=8.8,
                                   fig_width=12,
                                   margin=0,
                                   axis_off=True,
                                   show=False,
                                   save=True,
                                   file_format='png',
                                   filename=filename_save)
    return
#origin = [43.681641, -79.423906]
origin = [43.663389, -79.461929]
destination = [43.650854, -79.377587]
#origin = [43.6949, -79.453]
#destination = [43.6966, -79.4453]
lats = [origin[1], destination[1]]
lons = [origin[0], destination[0]]

G = ox.load_graphml('/Users/niall/saferoute/data/cleaned/Toronto.graphml')
df = pd.read_csv('/Users/niall/insight_project/data/processed/unique_roads_collision_yn_RF.csv')
df = df[['u', 'v', 'collision_yn']]
df['colour'] = 'red'
df['weights'] = df['collision_yn'] * 100000

nearest_nodes = ox.get_nearest_nodes(G, lats, lons, method=None)

#add colour to the edges that have a crash risk
nodes, edges = ox.graph_to_gdfs(G)
edges = edges.merge(df, on=['u', 'v'], how='left')
edges.colour[edges.colour.isna()] = 'grey'
edges.weights[edges.weights.isna()] = 1
edges.weights[edges.weights == 0] = 1
#edges.weights = edges.weights.values * edges.length.values


i = 0
for u, v, k, data in G.edges(keys=True, data=True):
#    print(data)
    data['impedance'] = edges['weights'][i]
#    print(data['length'])
Esempio n. 22
0
                       south,
                       east,
                       west,
                       network_type='drive',
                       retain_all=True)
G = nx.convert_node_labels_to_integers(G)
nodes, edges = ox.graph_to_gdfs(G)

# Concatenate lats and longs to save time
lats = queries.start_lat
lats = lats.append(queries.end_lat, ignore_index=True)
longs = queries.start_long
longs = longs.append(queries.end_long, ignore_index=True)

# Find nearest nodes
nearest_node_ids = ox.get_nearest_nodes(G, X=longs, Y=lats, method='balltree')

# Extract nearest node ids
start_nn = nearest_node_ids[0:nRecords]
end_nn = nearest_node_ids[nRecords:2 * nRecords]

queries['start_nn'] = start_nn
queries['end_nn'] = end_nn

queries.to_csv(path_or_buf=fnameQueries, index=False)

# Pickle graph and save as shapefile
file = open(fnamePickle, 'wb')
pickle.dump(G, file)
file.close()
ox.save_graph_shapefile(G, filename=fnameShape)
Esempio n. 23
0
def main(N=100,M=10,k=6,X=5000,choice=0):
    global G, G_o
    global healthcare, fire, shops
    global objects
    global objects_nodes
    global house_nodes
    global NearestObjectsTo, NearestObjectsFrom, ThereAndBack, IsReachableObjects, IsReachableHouses, IsReachableThereAndBack, dist2objects, dist2houses
    global A, B, C, a, b, c, km
    global D
    global E
    global Xm
    global srcs, dstns, srcs1, dstns1, srcs2, dstns2
    global new_object, tree, tree_weight, sum_dist
    global tree, tree_weight, sum_dis, Clustering, Cl, Z, Z1
    global  Clusters, Clusters1, TreeFromObject, Trees, center_nd, tree_weight1, sum_dist1, treedist

    if (G is None):
        load_graph(0)

    #fig, ax = ox.plot_graph(G_simple)

    t0 = tm.time()

    largest = max(nx.strongly_connected_components(G), key=len)

    G = G.subgraph(largest) # to work with only strongly connected graph
    if G_o is None:
        G_o = G
    G = G.to_directed()
    G = nx.DiGraph(G) # to work with DiGraphs only

    # search
    #healthcare = ox.pois_from_place('Ижевск, Россия', amenities=healthcare_list, which_result=1)
    
##    healthcare = ox.pois_from_place('Ижевск, Россия', {'amenity':healthcare_list}, which_result=1)
##    fire = ox.pois_from_place('Ижевск, Россия', {'amenity':fire_list}, which_result=1)
##    shops = ox.pois_from_place('Ижевск, Россия', {'shop':shop_list}, which_result=1)

    if (healthcare is None or fire is None or shops is None):
        load_objects(0)

    if (choice == 0):
        objects = healthcare
    elif (choice == 1):
        objects = fire
    else:
        objects = shops
    
    # get objects coordinates
    objects = objects.centroid
    objects_coord = (np.array([objects.x,objects.y]))
    # get subset of objects
    objects_coord = objects_coord[:,np.random.choice(len(objects_coord[0]),size = M, replace = False)]

    #find nodes near coordinates
   
    objects_nodes = ox.get_nearest_nodes(G, objects_coord[0],objects_coord[1])
    # ? maybe merge close nodes ?

    # get houses
    ##houses = ox.footprints.footprints_from_place(place='Ижевск, Россия')
    ###gdf_proj = ox.project_gdf(houses)
    ####fig, ax = ox.footprints.plot_footprints(gdf_proj, bgcolor='#333333', color='w', 
    ###                            save=True, show=False, close=True,  filename='piedmont_bldgs', dpi=40)
    ##houses = houses.centroid
    ##houses_coord = (np.array([houses.x,houses.y]))
    ### get subset of houses
    ##houses_coord = houses_coord[:,np.random.choice(len(houses_coord[0]),size = 100, replace = False)]
    ##
    ##house_nodes = ox.get_nearest_nodes(G, houses_coord[0],houses_coord[1])
    
    house_nodes = np.random.choice(list(G.nodes), size = N, replace = False) # get houses as random nodes

    adjust_weights(house_nodes,objects_nodes) # adjust weights

    t1 = tm.time()
    Xm = X
    km=k
    NearestObjectsTo, NearestObjectsFrom, ThereAndBack, IsReachableObjects, IsReachableHouses, IsReachableThereAndBack, dist2objects, dist2houses = Part1_1(house_nodes,objects_nodes,X=Xm)
    t2 = tm.time()
    print(t2-t0,' ',t2-t1)

    t1 = tm.time()
    
    A, B, C, a, b, c = Part1_2(house_nodes,objects_nodes, dist2objects, dist2houses)
    t2 = tm.time()
    print(t2-t0,' ',t2-t1)

    t1 = tm.time()
   
    D = Part1_3(house_nodes,objects_nodes, dist2houses)
    t2 = tm.time()
    print(t2-t0,' ',t2-t1)

    t1 = tm.time()
    E = Part1_4(house_nodes,objects_nodes)
    t2 = tm.time()
    print(t2-t0,' ',t2-t1)

    
    srcs, dstns = boolMat2Pairs(IsReachableObjects,house_nodes,objects_nodes)
    srcs1, dstns1 = boolMat2Pairs(IsReachableHouses,objects_nodes,house_nodes,)
    srcs2, dstns2 = boolMat2Pairs(IsReachableThereAndBack,house_nodes,objects_nodes)

    #------------------------------------------------------------part 2
    
    new_object = np.random.choice(list(G.nodes), size = 1, replace = False)[0]
    t1 = tm.time()
    tree, tree_weight, sum_dist = Part2_1(house_nodes,new_object)
    t2 = tm.time()
    print(t2-t0,' ',t2-t1)

    t1 = tm.time()
    
    dist_hous2house = getDistHouse2House(house_nodes)
    Z1 = Part2_2(house_nodes,dist_hous2house) # this one is used
    Z = Part2_2_scipy(house_nodes,dist_hous2house) #is not used
    t2 = tm.time()

    print(t2-t0,' ',t2-t1)
    t1 = tm.time()
    
    Clusters = getClusters(k, house_nodes, Z = Z1)
    Clusters1 = getClusters(k, house_nodes, Z = Z)
    TreeFromObject, Trees, center_nd, tree_weight1, sum_dist1, treedist = Part2_3(Clusters,house_nodes,new_object)
    t2 = tm.time()
    print(t2-t0,' ',t2-t1)

    print ('calculations done')
    #G = G_o
    return
Esempio n. 24
0
#G = ox.graph_from_address('350 5th Ave, New York, New York', network_type='drive')
#ox.plot_graph(G)

#north, east, south, west = 33.798, -84.378, 33.763, -84.422
# Downloading the map as a graph object
#G =ox.graph_from_address('Plaça de Sants, Barcelona', network_type='walk')
location_point = (41.372925, 2.121151)
G = ox.graph_from_point(location_point,
                        network_type='walk',
                        dist=500,
                        simplify=True)

areaX = [location_point[0], location_point[0] + 1]
areaY = [location_point[1], location_point[1] + 1]
nearestnodes = ox.get_nearest_nodes(
    G, areaX, areaY)  #ox.get_nearest_nodes(G, (location_point))
print(nearestnodes)
"""
origin_point = (33.787201, -84.405076)
origin_node = ox.get_nearest_node(G, origin_point)
print("origin_node",origin_node)

ox.plot_graph(G)
for n in list(G.nodes(data=True)):
    print ("node",n)
"""

fig, ax = ox.plot_graph(G, show=False, close=False)
ax.scatter(location_point[1], location_point[0], c='red')
for n in nearestnodes:
    print(G.nodes[n]['x'])
def dist_mat(tim_loc_data,main_lat,main_long):
	
	print('\nCreating Distance Matrix......')
	
	# Get the pair of customer locations latitude and longitude
	service_pos_arr = tim_loc_data[:,1:3]
	
	# Loac the graph
	fp = 'VKI.graphml' 
	graph = ox.load_graphml(fp)

	# Get nodes closes to the required locations of depot and customers
	main_node = ox.get_nearest_node(graph,(main_lat,main_long),method='haversine')
	ids = ox.get_nearest_nodes(graph,service_pos_arr[:,1],service_pos_arr[:,0],method='balltree')
	ids = np.asarray(ids)
	
	# Create an empty distance matrix
	matr = np.zeros((np.size(tim_loc_data,0)+1,np.size(tim_loc_data,0)+1))

	# Looping over the matrix rowwise
	for x in range(np.size(matr,0)):

		# Looping over matrix columnwise
		for y in range(x+1):

			# If the column equal row, distance is zero
			if y == x:
				matr[x][y] = 0				
				continue

			else:

				# If the column for depot i.e. first column
				if y == 0:

					# Get the minimum distance using the graph
					try:
						matr[x][y] = nx.shortest_path_length(graph,source=main_node,target=ids[x-1],weight='length')#cost_calc(main_lat,main_long,service_pos_arr[x-1,0],service_pos_arr[x-1,1])
					
					# If path is not found, use default value
					except nx.exception.NetworkXNoPath:
						avgValue = 3500
						print('No Direct Path was found. Taking distance as average value of 3500 units')
						matr[x][y] = 3500					
				
				# If the column is not of depot
				else:

					# Get the minimum distance using the graph
					try:
						matr[x][y] = nx.shortest_path_length(graph,source=ids[x-1],target=ids[y-1],weight='length')#cost_calc(service_pos_arr[x-1,0],service_pos_arr[x-1,1],service_pos_arr[y-1,0],service_pos_arr[y-1,1])
					
					# If path is not found, use default value
					except nx.exception.NetworkXNoPath:
						avgValue = 3500
						print('No Direct Path was found. Taking distance as average value of 3500 units')
						matr[x][y] = 3500	

	# Get the indices of a upper triangular traingular matrix
	ul = np.triu_indices(np.size(matr,0))

	# Store the distance values in those indices, thus making the distance matrix symmetric
	matr[ul] = matr.T[ul]

	# Save the distance matrix and return it
	np.savetxt('Dist_Matrix.csv',matr,delimiter=",",header='Distance Matrix')
	print('Distance Matrix Created and Saved......')
	return matr
Esempio n. 26
0
def origindestination(originPath, destinationPath, networkType="all"):
    """
    Create a bounding box from user entered origin and destination points, \
get the corresponding road network
    and form the origin destination cost matrix.

    Parameters
    ----------
    originPath: string
        absolute path of the origin shapefile to be used
    destinationPath: string
        absolute path of the destination shapefile to be used
    networkType: string
        what type of street network to get

    Returns
    -------
    route_gdf: GeoDataFrame
        geopandas dataframe of routes
    nodes: GeoDataFrame
        geopandas dataframe of nodes
    G_proj: networkx multidigraph
        road network of closed region that is reprojected in corresponding \
UTM zone
    """

    origins = gpd.read_file(originPath)
    destinations = gpd.read_file(destinationPath)

    origins["geometry"] = origins["geometry"].to_crs(epsg=4326)
    destinations["geometry"] = destinations["geometry"].to_crs(epsg=4326)
    origins.crs = from_epsg(4326)
    destinations.crs = from_epsg(4326)

    points_concat = gpd.GeoDataFrame(
        pd.concat([origins, destinations], ignore_index=True, sort=True))
    minx, miny, maxx, maxy = points_concat.geometry.total_bounds

    G = ox.graph_from_bbox(maxy + 0.005,
                           miny - 0.005,
                           maxx + 0.005,
                           minx - 0.005,
                           clean_periphery=False,
                           network_type=str(networkType))

    G_proj = nx.MultiGraph(ox.project_graph(G))
    nodes, edges = ox.graph_to_gdfs(G_proj)
    nodes["x"] = nodes["x"].astype(float)

    origins = origins.to_crs(nodes.crs)
    destinations = destinations.to_crs(nodes.crs)
    origins["x"] = origins.geometry.x
    origins["y"] = origins.geometry.y
    destinations["x"] = destinations.geometry.x
    destinations["y"] = destinations.geometry.y

    orignodes = list(
        ox.get_nearest_nodes(G_proj,
                             origins["x"],
                             origins["y"],
                             method="kdtree"))
    destnodes = list(
        ox.get_nearest_nodes(G_proj,
                             destinations["x"],
                             destinations["y"],
                             method="kdtree"))

    routelinelist = []
    fromlist = []
    tolist = []
    for dest in destnodes:
        for org in orignodes:
            try:
                route = nx.shortest_path(G_proj, org, dest, weight="length")
                route_nodes = nodes.loc[route]
                fromlist.append(route[0])
                tolist.append(route[-1])
                route_line = LineString(list(route_nodes.geometry.values))
                routelinelist.append(route_line)
            except:
                pass

    route_gdf = gpd.GeoDataFrame(crs=edges.crs)
    route_gdf["geometry"] = routelinelist
    route_gdf["length"] = route_gdf.length
    route_gdf["from_id"] = fromlist
    route_gdf["to_id"] = tolist

    return route_gdf, nodes, G_proj, origins, destinations