Beispiel #1
0
def load_graphs(name):
    '''
    Load the graphs (bike and drive) and the CSV with the results of the algorithm into the script.
    ---
    name: str name of the city to be loaded
    layer: str layer to be loaded, it can be: drive, bike, walk, rail.

    returns: Networkx MultiDiGraph
    '''

    crs = {
        'Phoenix': {
            'init': 'epsg:2763'
        },
        'Detroit': {
            'init': 'epsg:2763'
        },
        'Manhattan': {
            'init': 'epsg:2763'
        },
        'Amsterdam': {
            'init': 'epsg:32633'
        },
        'Mexico': {
            'init': 'epsg:6372'
        },
        'London': {
            'init': 'epsg:32633'
        },
        'Singapore': {
            'init': 'epsg:3414'
        },
        'Budapest': {
            'init': 'epsg:32633'
        },
        'Copenhagen': {
            'init': 'epsg:32633'
        },
        'Barcelona': {
            'init': 'epsg:32633'
        },
        'Portland': {
            'init': 'epsg:26949'
        },
        'Bogota': {
            'init': 'epsg:3116'
        },
        'LA': {
            'init': 'epsg:2763'
        },
        'Jakarta': {
            'init': 'epsg:5331'
        }
    }

    G_bike = ox.load_graphml('{}/{}_bike.graphml'.format(name, name))
    G_bike = ox.project_graph(G_bike, to_crs=crs[name])
    G_drive = ox.load_graphml('{}/{}_drive.graphml'.format(name, name))
    G_drive = ox.project_graph(G_drive, to_crs=crs[name])
    return G_bike, G_drive
def simplify_network(network):
    """
    Simplifies a given road-network using OSMnx.
    """
    network_proj = ox.project_graph(network)
    simplified_proj = ox.consolidate_intersections(network_proj,
                                                   rebuild_graph=True,
                                                   tolerance=50,
                                                   dead_ends=False)
    simplified = ox.project_graph(simplified_proj, network.graph["crs"])
    return simplified
Beispiel #3
0
def get_streets(perimeter=None,
                point=None,
                radius=None,
                dilate=6,
                custom_filter=None):

    if perimeter is not None:
        # Boundary defined by polygon (perimeter)
        streets = ox.graph_from_polygon(union(perimeter.geometry),
                                        custom_filter=custom_filter)
        streets = ox.project_graph(streets)
        streets = ox.graph_to_gdfs(streets, nodes=False)
        #streets = ox.project_gdf(streets)
        streets = MultiLineString(list(streets.geometry)).buffer(dilate)

    elif (point is not None) and (radius is not None):
        # Boundary defined by polygon (perimeter)

        streets = ox.graph_from_point(point,
                                      dist=radius,
                                      custom_filter=custom_filter)
        crs = ox.graph_to_gdfs(streets, nodes=False).crs
        streets = ox.project_graph(streets)

        perimeter = GeoDataFrame(geometry=[Point(point[::-1])], crs=crs)
        perimeter = ox.project_gdf(perimeter).geometry[0].buffer(radius)

        streets = ox.graph_to_gdfs(streets, nodes=False)

        streets = MultiLineString(
            list(
                filter(
                    # Filter lines with at least 2 points
                    lambda line: len(line) >= 2,
                    # Iterate over lines in geometry
                    map(
                        # Filter points within perimeter
                        lambda line: list(
                            filter(lambda xy: Point(xy).within(perimeter),
                                   zip(*line.xy))),
                        streets.geometry)))).buffer(dilate)  # Dilate lines

    if not isinstance(streets, Iterable):
        streets = [streets]

    streets = list(map(pathify, streets))

    return streets, perimeter
Beispiel #4
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)
def city_statistics(city: str):

    result = {'city': city}
    G = get_graph(city)
    if G is None:
        return result

    G_proj = ox.project_graph(G)
    nodes_proj = ox.graph_to_gdfs(G_proj, edges=False)

    area = compute_area_m(nodes_proj)
    result['area_km'] = area / 1e6
    bs = compute_basic_stats(G, area)
    #basic statistics from osmnx
    result.update(bs)

    result.update(edge_length_stats(G))
    result.update(degree_stats(G))

    orig_point = compute_center(nodes_proj)
    node0 = ox.get_nearest_node(G_proj, (orig_point.y, orig_point.x),
                                method='euclidean',
                                return_dist=False)
    # print('node0 ', node0)
    central_paths = compute_paths(G_proj, node0)
    result['central_sp_mean'] = central_paths[0]
    result['central_sp_std'] = central_paths[1]
    # result.update(compute_extended_stats(G))
    # print(result)
    return result
Beispiel #6
0
def load_grid_edges(grid_x, grid_y, crs):
    x1 = grid_x - GRID_SIZE / 2
    x2 = grid_x + GRID_SIZE + GRID_SIZE / 2
    y1 = grid_y - GRID_SIZE / 2
    y2 = grid_y + GRID_SIZE + GRID_SIZE / 2
    ls = LineString([
        [x1, y1],
        [x2, y1],
        [x2, y2],
        [x1, y2],
        [x1, y1],
    ])

    t = gpd.GeoDataFrame(geometry=[ls], crs=crs)
    tll = ox.project_gdf(t, to_latlong=True)
    west, south, east, north = tll.total_bounds

    try:
        tgp = ox.project_graph(ox.graph_from_bbox(
            north, south, east, west,
            truncate_by_edge=True, simplify=True, clean_periphery=False, network_type='all', retain_all=True
        ))
        tnp, tep = ox.graph_to_gdfs(tgp, nodes=True, edges=True)
        return tep
    except ox.core.EmptyOverpassResponse:
        pass

    return gpd.GeoDataFrame(geometry=[])
Beispiel #7
0
def detect_drive_network_from_point(lat=13.14633,
                                    lon=77.514386,
                                    distance=2000,
                                    save=True,
                                    filename='icts2000'):
    G = ox.graph_from_point((lat, lon),
                            distance=distance,
                            network_type='drive',
                            simplify=False)
    hwy_types = ['primary', 'motorway', 'trunk']
    gdf = ox.graph_to_gdfs(G, nodes=False)
    mask = ~gdf['highway'].map(lambda x: isinstance(x, str) and x in hwy_types)
    edges = zip(gdf[mask]['u'], gdf[mask]['v'], gdf[mask]['key'])
    G.remove_edges_from(edges)
    G = ox.remove_isolated_nodes(G)
    G_projected = ox.project_graph(G)
    filename += '-' + str(distance)
    fig, ax = ox.plot_graph(G_projected,
                            show=False,
                            save=save,
                            filename=filename,
                            file_format='svg')
    plt.scatter(*utm.from_latlon(lat, lon)[:2])
    plt.show()
    ox.save_graphml(G, filename=filename + '.graphml')
    return G
Beispiel #8
0
    def get_map_data_with_elevation(self, city_name, key):
        # get model data from osmnx of 2d and elevation

        #city_name = 'Amherst'  # 'Springfield'
        place_query = {
            'city': city_name,
            'state': 'Massachusetts',
            'country': 'USA'
        }
        graph_orig = ox.graph_from_place(place_query, network_type='walk')

        #add Elevation data from GoogleMaps
        key = self.get_key()
        graph_orig = ox.add_node_elevations(
            graph_orig, api_key=key
        )  # 'AIzaSyDVqjj0iKq0eNNHlmslH4fjoFgRj7n-5gs')   # from elevation
        graph_orig = ox.add_edge_grades(graph_orig)
        pkl.dump(graph_orig, open("data/" + city_name + "_city_graph.pkl",
                                  "wb"))

        #project map on to 2D space
        graph_project = ox.project_graph(graph_orig)
        pkl.dump(graph_project,
                 open("data/" + city_name + "_city_graph_projected.pkl", "wb"))
        #print ("pkl: ", type(graph_orig))
        return graph_project, graph_orig
    def download_data(self):

        all_g = []

        for address in self.addresses:

            print('Address: ', address)

            if self.check_data_exists(address):
                G = pickle.load(open(self.save_path + '/addresses/{}.p'.format(address), 'rb'))
            else:
                G = ox.graph_from_place(address, network_type='drive')
                G = ox.project_graph(G)
                pickle.dump(G, open(self.save_path + '/addresses/{}.p'.format(address), 'wb'))

            all_g.append(G)

        for i, g in enumerate(all_g):

            if i == 0:
                raw_data = pd.DataFrame([data for u, v, key, data in g.edges(keys=True, data=True)])

            else:
                d = nx.to_pandas_edgelist(g)
                raw_data = pd.concat(d, raw_data)

        raw_data.to_csv(self.save_path + '/raw_data/raw.csv')

        return raw_data
Beispiel #10
0
    def GenerateIsoPoints(lon, lat, time, speed):

        '''
        Function generates points cloud of isochrone from OSM
        depending on route type.

        Returns list of points.
        '''

        distance = speed * 1000 / 60 * time * 1.5

        streets_graph = ox.graph_from_point([lat, lon], distance=distance, network_type=route, simplify=False)

        center_node = ox.get_nearest_node(streets_graph, (lat, lon), method='euclidean')

        streets_graph.add_node('dummy', osmid=999999999, x=lon, y=lat)
        dummy_length = geopy.distance.vincenty((streets_graph.node['dummy']['y'], streets_graph.node['dummy']['x']),
                                               (streets_graph.node[center_node]['y'], streets_graph.node[center_node]['x'])).m
        streets_graph.add_edge('dummy', center_node, length=dummy_length)

        projected_graph = ox.project_graph(streets_graph)

        travel_speed = speed

        meters_per_minute = travel_speed * 1000 / 60
        for u, v, k, data in projected_graph.edges(data=True, keys=True):
            data['time'] = data['length'] / meters_per_minute

        subgraph = nx.ego_graph(projected_graph, center_node, radius=time, distance='time')
        node_points = [[data['lon'], data['lat']] for node, data in subgraph.nodes(data=True)]
        points = np.array(node_points)
        return points
Beispiel #11
0
    def __init__ (self, village, province, country, roads_type='drive'):
        """
        Initialize.

        :param village: <str> The village to download from OpenMaps
        :param province: <str> The province the villange belongs to
        :param country: <str> The country
        :param roads_type: <str> The type of roads to download. Possibilities
                            are drive, bike, and walk.

        :attr place_name: <str> The place downloaded from OpenMaps
        :attr nodes: <dict> The list of nodes
        :attr edges: <dict> The list of edges
        :attr graph: <networkx.classes.multidigraph.MultiDiGraph> The full graph object

        """
        self.place_name = ", ".join((village, province, country))
        print (f"Scraping of {self.place_name}...", end="")
        g = ox.project_graph(ox.graph_from_place (self.place_name, network_type=roads_type))
        self.nodes_gpd, self.edges_gpd = ox.graph_to_gdfs (g, nodes=True, edges=True)
        self.nodes = {i : Node(n, n.osmid, n.x, n.y, n.lat, n.lon, n.geometry) for i, n in self.nodes_gpd.iterrows()}
        self.edges = {i : Edge(e, e.osmid, e.name, e.highway, e.oneway, e.length, e.geometry, e.maxspeed, e.lanes, e.u, e.v)
                           for i, e in self.edges_gpd.iterrows()}
        self.graph = g
        print ("done")
Beispiel #12
0
def test_stats():
    # create graph, add bearings, project it
    location_point = (37.791427, -122.410018)
    G = ox.graph_from_point(location_point,
                            distance=500,
                            distance_type='network')
    G = ox.add_edge_bearings(G)
    G_proj = ox.project_graph(G)

    # calculate stats
    stats1 = ox.basic_stats(G)
    stats2 = ox.basic_stats(G, area=1000)
    stats3 = ox.basic_stats(G_proj,
                            area=1000,
                            clean_intersects=True,
                            tolerance=15,
                            circuity_dist='euclidean')

    # calculate extended stats
    stats4 = ox.extended_stats(G,
                               connectivity=True,
                               anc=False,
                               ecc=True,
                               bc=True,
                               cc=True)
Beispiel #13
0
def load_map(location='Manhattan, New York, USA',
             cache_directory='/tmp',
             force_redownload=False):
    cached_filename = hashlib.md5(location).hexdigest() + '.graphml'
    try:
        if force_redownload:
            raise IOError('Forcing re-download of graph.')
        logging.info('Trying to load map from "%s"',
                     os.path.join(cache_directory, cached_filename))
        graph = ox.load_graphml(cached_filename, folder=cache_directory)
    except IOError:
        logging.info('Downloading map from "%s"', location)
        graph = ox.graph_from_place(location, network_type='drive')
        # Keep the largest strongly connected component.
        logging.info('Finding largest component')
        graph = max(nx.strongly_connected_component_subgraphs(graph), key=len)
        graph = ox.project_graph(graph)
        logging.info('Saving map to "%s"',
                     os.path.join(cache_directory, cached_filename))
        ox.save_graphml(graph,
                        filename=cached_filename,
                        folder=cache_directory)
    # Add dummy speed and length information.
    for u, v, key, data in graph.edges(data=True, keys=True):
        if 'time' not in data:
            time = data['length'] / _DEFAULT_SPEED
            data['time'] = time
            data['speed'] = _DEFAULT_SPEED
        else:
            data['time'] = float(data['time'])
            data['speed'] = float(data['speed'])
        graph.add_edge(u, v, **data)
    return graph
Beispiel #14
0
def create_intersections_db_by_zip(data):
    """This function creates a database of the coordinates and streets of all intersections in a mile radius of a given zipcode. Currently set to drive network and a radius of 1610m (1 mile). Edit the parameters in line 81 to fit your preferences"""
    d = data[1]
    zipcode = d[1]
    city = d[2]
    state = d[3]
    county = d[4]

    tablename = "intersections_" + str(zipcode)

    try:
        cur.execute(
            "CREATE TABLE " + tablename +
            " (zip_code TEXT, city TEXT, state TEXT, county TEXT, latlon_str TEXT);"
        )

        latlon_array = []
        streetname_array = []
        latlon_array_str = ""
        streetname_array_str = ""

        try:
            G = ox.graph_from_address(zipcode,
                                      network_type='drive',
                                      distance=1610)
            G_proj = ox.project_graph(G)
            # clean up the intersections and extract their xy coords
            intersections = ox.clean_intersections(G_proj,
                                                   tolerance=15,
                                                   dead_ends=False)
            points = np.array([point.xy for point in intersections])

            gdf = gpd.GeoDataFrame(geometry=intersections)
            gdf.crs = G_proj.graph['crs']
            lonlat = ox.project_gdf(gdf, to_latlong=True)
            a = lonlat['geometry'].tolist()
            for coord in a:
                lon = coord.x
                lat = coord.y
                latlon_tuple = (lat, lon)

                # nearest_streets = get_nearest_streetnames(latlon_tuple)

                # streetname_array.append(nearest_streets)
                latlon_array.append(latlon_tuple)
        except:
            #THROW A WARNING THAT THERE IS NO LAT/LON DATA FOR THIS ZIPCODE
            pass

        latlon_array_str = str(latlon_array).strip('[]')
        # streetname_array_str = str(streetname_array).strip('[]')

        to_db = [zipcode, city, state, county, latlon_array_str]
        cmd = "INSERT INTO " + tablename + " (zip_code, city, state, county,latlon_str) VALUES (?, ?, ?, ?, ?);"
        cur.execute(cmd, to_db)

        for row in cur.execute("SELECT * from " + tablename):
            print(row)
    except:
        print(tablename + " already exists")
Beispiel #15
0
def test_plots():

    G = ox.graph_from_place('Piedmont, California, USA', network_type='drive', simplify=False)
    G2 = ox.simplify_graph(G, strict=False)

    # test getting colors
    co = ox.get_colors(n=5, return_hex=True)
    nc = ox.get_node_colors_by_attr(G2, 'osmid')
    ec = ox.get_edge_colors_by_attr(G2, 'length')

    # save a plot to disk as png
    fig, ax = ox.plot_graph(G, save=True, file_format='png')

    # save a plot to disk as svg
    G_simplified = ox.simplify_graph(G)
    fig, ax = ox.plot_graph(G_simplified, show=False, save=True, close=True, file_format='svg')

    G_projected = ox.project_graph(G_simplified)
    fig, ax = ox.plot_graph(G_projected)

    fig, ax = ox.plot_graph(G_projected, fig_height=5, fig_width=5, margin=0.05, axis_off=False, bgcolor='y',
                            file_format='png', filename='x', dpi=180, annotate=True, node_color='k', node_size=5,
                            node_alpha=0.1, node_edgecolor='b', node_zorder=5, edge_color='r', edge_linewidth=2,
                            edge_alpha=0.1, use_geom=False, show=False, save=True, close=True)

    fig, ax = ox.plot_figure_ground(G=G_simplified, file_format='png')
    fig, ax = ox.plot_figure_ground(point=(33.694981, -117.841375), file_format='png')
    fig, ax = ox.plot_figure_ground(address='Denver, Colorado, USA', file_format='png')
Beispiel #16
0
def read_proj_graphml(proj_graphml_filepath, ori_graphml_filepath, to_crs):
    """
    Read a projected graph from local disk if exist,
    otherwise, reproject origional graphml to the UTM zone appropriate for its geographic location,
    and save the projected graph to local disk

    Parameters
    ----------
    proj_graphml_filepath: string
        the projected graphml filepath
    ori_graphml_filepath: string
        the original graphml filepath
    to_crs: dict or string or pyproj.CRS
        project to this CRS

    Returns
    -------
    networkx multidigraph
    """
    # if the projected graphml file already exist in disk, then load it from the path
    if os.path.isfile(proj_graphml_filepath):
        print("Read network from disk.")
        return ox.load_graphml(proj_graphml_filepath)

    # else, read original study region graphml and reproject it
    else:
        print("Reproject network, and save the projected network to disk")

        # load and project origional graphml from disk
        G = ox.load_graphml(ori_graphml_filepath)
        G_proj = ox.project_graph(G, to_crs=to_crs)
        # save projected graphml to disk
        ox.save_graphml(G_proj, proj_graphml_filepath)

        return G_proj
Beispiel #17
0
def test_nearest_edges():
    from pyproj import Proj

    # test in closest edge section
    sheik_sayed_dubai = [25.09, 25.06, 55.16, 55.11]
    location_coordinates = (25.071764, 55.138978)
    G = ox.graph_from_bbox(*sheik_sayed_dubai,
                           simplify=False,
                           retain_all=True,
                           network_type='drive')

    # Unprojected
    ne1 = ox.get_nearest_edges(
        G,
        X=[location_coordinates[1], location_coordinates[1]],
        Y=[location_coordinates[0], location_coordinates[0]],
        method='balltree',
        dist=0.0001)

    # Projected
    G2 = ox.project_graph(G)
    crs = Proj(G2.graph['crs'])

    projected_point = crs(location_coordinates[1], location_coordinates[0])
    ne2 = ox.get_nearest_edges(G2,
                               X=[projected_point[0], projected_point[0]],
                               Y=[projected_point[1], projected_point[1]],
                               method='kdtree',
                               dist=10)
    assert (ne1 == ne2).all()
Beispiel #18
0
def save_graph(target, graph_name, distance=1000, is_address=True):

    # Load graph from OSM
    if is_address:
        graph = ox.graph_from_address(address=target,
                                      distance=distance,
                                      network_type='drive')
    else:
        graph = ox.graph_from_place(query=target, network_type='drive')

    # Project graph
    graph_proj = ox.project_graph(graph)

    folder_path = path.join('graphs', graph_name)
    if not path.exists(folder_path):
        mkdir(folder_path)

    graph_data = graph_proj.graph
    serialize_dict(dictionary=graph_data,
                   file_path=path.join(folder_path, 'graph_data.pkl.gz'))

    # We make the graph strongly connected to ensure that any combination of source / sink
    # constitutes a valid problem
    graph_component = ox.get_largest_component(graph_proj,
                                               strongly=True).to_directed()

    # Save pictures of the graph
    plot_road_graph(graph_component,
                    graph_name=graph_name,
                    file_path=path.join(folder_path, 'graph'))

    # Save graph
    ox.save_graphml(graph_component,
                    filename='graph.graphml',
                    folder=folder_path,
                    gephi=True)

    # Save a selection of graph-wide stats.
    stats_file = path.join(folder_path, 'stats.csv')
    delete_if_exists(stats_file)

    n_nodes = graph_component.number_of_nodes()
    n_edges = graph_component.number_of_edges()
    avg_in_deg = np.average([d for _, d in graph_component.in_degree()])
    avg_out_deg = np.average([d for _, d in graph_component.out_degree()])
    diam = nx.diameter(graph_component)

    append_row_to_log(['Number of Nodes', n_nodes], stats_file)
    append_row_to_log(['Number of Edges', n_edges], stats_file)
    append_row_to_log(['Average In Degree', avg_in_deg], stats_file)
    append_row_to_log(['Average Out Degree', avg_out_deg], stats_file)
    append_row_to_log(['Diameter', diam], stats_file)

    cleaned_target = target.replace('\"', '').replace('\'',
                                                      '').replace(',', '')
    query = '{0}; Dist: {1}'.format(cleaned_target,
                                    distance) if is_address else cleaned_target
    append_row_to_log(['Query', query], stats_file)

    return graph_component
Beispiel #19
0
def test_stats():
    # create graph, add bearings, project it
    location_point = (37.791427, -122.410018)
    G = ox.graph_from_point(location_point,
                            distance=500,
                            distance_type='network')
    G = ox.add_edge_bearings(G)
    G_proj = ox.project_graph(G)

    # calculate stats
    stats1 = ox.basic_stats(G)
    stats2 = ox.basic_stats(G, area=1000)
    stats3 = ox.basic_stats(G_proj,
                            area=1000,
                            clean_intersects=True,
                            tolerance=15,
                            circuity_dist='euclidean')

    try:
        stats4 = ox.extended_stats(G,
                                   connectivity=True,
                                   anc=True,
                                   ecc=True,
                                   bc=True,
                                   cc=True)
    except NetworkXNotImplemented as e:
        warnings.warn(
            "Testing coordinates results in a MultiDigraph, and extended stats are not available for it"
        )
        warnings.warn(e.args)
Beispiel #20
0
def use_osmnx() -> None:
    graph = ox.graph_from_place('Leuven, Belgium', network_type='drive')
    graph_proj = ox.project_graph(graph)

    # Create GeoDataFrames
    # Approach 1
    nodes_proj, edges_proj = ox.graph_to_gdfs(graph_proj,
                                              nodes=True,
                                              edges=True)
    for nid, row in nodes_proj[['x', 'y']].iterrows():
        map_con.add_node(nid, (row['x'], row['y']))
    for nid, row in edges_proj[['u', 'v']].iterrows():
        map_con.add_edge(row['u'], row['v'])

    # Approach 2
    nodes, edges = ox.graph_to_gdfs(graph_proj, nodes=True, edges=True)

    nodes_proj = nodes.to_crs("EPSG:3395")
    edges_proj = edges.to_crs("EPSG:3395")

    for nid, row in nodes_proj.iterrows():
        map_con.add_node(nid, (row['lat'], row['lon']))

    # adding edges using networkx graph
    for nid1, nid2, _ in graph.edges:
        map_con.add_edge(nid1, nid2)
Beispiel #21
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
Beispiel #22
0
def load_graph(graph_name):
    folder_path = path.join('graphs', graph_name)

    graph_data = deserialize_dict(
        file_path=path.join(folder_path, 'graph_data.pkl.gz'))
    graph = ox.load_graphml(filename='graph.graphml', folder=folder_path)
    # graph = nx.MultiDiGraph(nx.read_graphml(path.join(folder_path, 'graph.graphml'), node_type=int))

    if not nx.is_strongly_connected(graph):
        graph = ox.get_largest_component(graph, strongly=True)

    node_mapping = {node: i for i, node in enumerate(sorted(graph.nodes()))}
    graph = nx.relabel_nodes(graph, node_mapping)

    # Ensure nodes and edges are ordered consistently
    G = nx.MultiDiGraph()
    for node, data in sorted(graph.nodes(data=True), key=lambda t: t[0]):
        data['demand'] = 0
        G.add_node(node, **data)

    for src, dst, data in sorted(graph.edges(data=True),
                                 key=lambda t: (t[0], t[1])):
        # Remove parallel edges and self-loops
        if src == dst or (src in G and dst in G[src]):
            continue

        # Dummy data for compatibility with plotter
        data['zero'] = 0
        G.add_edge(src, dst, key=0, **data)

    G.graph['crs'] = graph_data['crs']
    G.graph['name'] = graph_data['name']
    G = ox.project_graph(G, to_crs=graph_data['crs'])

    return G.to_directed()
Beispiel #23
0
def plot_edge_grades():
    files_map = '../data/maps/m43.96267779776494_m19.944747838679202_m43.929659815391865_m19.905049264605925.graphml'
    G = ox.load_graphml(files_map)

    max_lat = -12.934333867695516
    min_lat = -12.961083555974895
    max_lon = -38.473331269107426
    min_lon = -38.49996781691653

    name_geotiff = '../data/maps/19S45_ZN.tif'
    G = ox.graph_from_bbox(max_lat,
                           min_lat,
                           max_lon,
                           min_lon,
                           network_type='all')
    G = Graph.set_node_elevation(G, name_geotiff)
    # deadends = [(u, v) for u, v, k, data in G.edges(keys=True, data=True) if data['highway'] == '']
    # print(deadends)
    # G2.remove_nodes_from(deadends)
    G = Graph.edge_grades(G)
    G_proj = ox.project_graph(G)
    ec = ox.plot.get_edge_colors_by_attr(G_proj,
                                         "grade",
                                         cmap="plasma",
                                         num_bins=5,
                                         equal_size=True)
    fig, ax = ox.plot_graph(G_proj,
                            edge_color=ec,
                            edge_linewidth=0.5,
                            node_size=0,
                            bgcolor="w")
def get_graph(place):
    string = place.split(',')[0]

    print('Fetching graph data for {}'.format(place))

    poly = get_polygon(string)

    if poly == -1:
        gdf = ox.gdf_from_place('{}'.format(string))
        G = ox.graph_from_bbox(gdf.bbox_north, gdf.bbox_south, gdf.bbox_east,
                               gdf.bbox_west)
        val = 0
    else:
        try:
            G = nx.read_gpickle('data/{a}/{a}'.format(a=string))
            val = 1
        except FileNotFoundError:
            G = ox.graph_from_polygon(poly, network_type='drive')
            val = 0

    G = ox.project_graph(G)

    print('Writing graph file')

    try:
        os.mkdir('data/{}'.format(string))
    except FileExistsError:
        pass

    if val != 1:
        nx.write_gpickle(G, path='data/{a}/{a}'.format(a=string))

    return G
Beispiel #25
0
def test_plots():

    G = ox.graph_from_place('Piedmont, California, USA', network_type='drive', simplify=False)
    G2 = ox.simplify_graph(G, strict=False)

    # test getting colors
    co = ox.get_colors(n=5, return_hex=True)
    nc = ox.get_node_colors_by_attr(G2, 'osmid')
    ec = ox.get_edge_colors_by_attr(G2, 'length')

    # save a plot to disk as png
    fig, ax = ox.plot_graph(G, save=True, file_format='png')

    # save a plot to disk as svg
    G_simplified = ox.simplify_graph(G)
    fig, ax = ox.plot_graph(G_simplified, show=False, save=True, close=True, file_format='svg')

    G_projected = ox.project_graph(G_simplified)
    fig, ax = ox.plot_graph(G_projected)

    fig, ax = ox.plot_graph(G_projected, fig_height=5, fig_width=5, margin=0.05, axis_off=False, bgcolor='y',
                            file_format='png', filename='x', dpi=180, annotate=True, node_color='k', node_size=5,
                            node_alpha=0.1, node_edgecolor='b', node_zorder=5, edge_color='r', edge_linewidth=2,
                            edge_alpha=0.1, use_geom=False, show=False, save=True, close=True)

    fig, ax = ox.plot_figure_ground(G=G_simplified, file_format='png')
    fig, ax = ox.plot_figure_ground(point=(33.694981, -117.841375), file_format='png')
    fig, ax = ox.plot_figure_ground(address='Denver, Colorado, USA', file_format='png')
Beispiel #26
0
def get_OSM_edges_gdf(shapefile_path, OSM_folder, polygon=None, network_type= 'walk'):
    """
    If studyregion gdf file exist:
    Load a (projected) edge shapefile from disk 
    
    Else: query OSM to get the network gdf.

    Parameters
    ----------
    shapefile_path : string
        the name of the shapefile path(including file extension)
    OSM_folder : string
        the folder containing the OSM file, if None, use default data folder
    polygon : shapely Polygon or MultiPolygon
        the shape to get network data within. coordinates should be in units of
        latitude-longitude degrees.
    network_type : string
        what type of street network to get. Default type is pedestrain network  

    Returns
    -------
    GeoDataFrame
    """
    if os.path.exists(shapefile_path):
        edges = gpd.GeoDataFrame.from_file(shapefile_path)
    else:
        G = ox.graph_from_polygon(polygon, network_type=network_type, retain_all = True)
        G_proj = ox.project_graph(G)
        edges = ox.graph_to_gdfs(G_proj, nodes=False, edges=True, fill_edge_geometry=True)
    return edges
def download_map(location_point, range):
    G = ox.graph_from_point(location_point,
                            distance=range,
                            simplify=False,
                            network_type='drive')
    G = ox.project_graph(G)
    return G
Beispiel #28
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')
Beispiel #29
0
def Save_OSM_G_proj(placename=placename, network_type= 'walk', suffix=suffix, folder=OSM_folder, to_crs=None):    
    """
    save a projected graphml from graphml file
    Parameters
    ----------
    network_type : string
        what type of street network to get. Default type is pedestrain network
    placename: string
        place name
    suffix: string
        output data date
    folder : string
        where to save the shapefile, specify local folder path for OSM resource
    Returns
    -------
    none

    """
    G = ox.load_graphml(filename='{studyregion}_{network_type}{suffix}.graphml'.format(
        studyregion = placename, network_type=network_type, suffix = suffix), folder=folder)
    #set up project projection, and save the projected graph (project to UTM so we can use metres as a unit when buffering)
    G_proj = ox.project_graph(G, to_crs=to_crs)
    ox.save_graphml(G_proj, filename='{studyregion}_proj_{network_type}{suffix}.graphml'.format(
        studyregion = placename, network_type=network_type, suffix = suffix), folder=folder)
    ox.plot_graph(G_proj)
Beispiel #30
0
def test_stats():
    # create graph, add bearings, project it
    G = ox.graph_from_point(location_point, dist=500, network_type="drive")
    G = ox.add_edge_bearings(G)
    G_proj = ox.project_graph(G)

    # calculate stats
    stats = ox.basic_stats(G)
    stats = ox.basic_stats(G, area=1000)
    stats = ox.basic_stats(G_proj,
                           area=1000,
                           clean_intersects=True,
                           tolerance=15,
                           circuity_dist="euclidean")

    # calculate extended stats
    stats = ox.extended_stats(G,
                              connectivity=True,
                              anc=False,
                              ecc=True,
                              bc=True,
                              cc=True)

    # test cleaning and rebuilding graph
    G_clean = ox.consolidate_intersections(G_proj,
                                           tolerance=10,
                                           rebuild_graph=True,
                                           dead_ends=True)
    def get_map(self, name):

        file_path = os.path.dirname(os.path.abspath(__file__))

        file_path = file_path[:len(file_path) - 7] + 'maps/'

        try:
            with open(file_path + name + '.p', 'rb') as f:
                self._graph = pickle.load(f)
        except:

            try:

                self._graph = ox.graph_from_place(name, network_type='drive')
            except:
                self._graph = ox.graph_from_address(name,
                                                    distance=250,
                                                    network_type='drive')

            with open(file_path + name + '.p', 'wb') as f:
                pickle.dump(self._graph, f)

        self._graph_proj = ox.project_graph(self._graph)

        self._nodes, self._edges = ox.graph_to_gdfs(self._graph_proj,
                                                    nodes=True,
                                                    edges=True)
    def __init__(self, node_name, place_name, publish_rate):

        #initialise class variables
        self._place_name = place_name
        self._ros_node_name = node_name
        self._road_info = pointsList()
        self._road_points = pointsList()
        self._publish_rate = publish_rate

        #create publisher to the topic "road_info"
        self._road_info_publisher = rospy.Publisher("road_info",
                                                    pointsList,
                                                    queue_size=100)
        self._road_points_publisher = rospy.Publisher("road_points",
                                                      pointsList,
                                                      queue_size=100)

        #create a node
        rospy.init_node(self._ros_node_name, anonymous=False)
        self._publish_rate = rospy.Rate(self._publish_rate)  # 1hz

        #convert place in a map to a graph
        self._graph = ox.graph_from_place(self._place_name,
                                          network_type='drive')
        self._graph_proj = ox.project_graph(self._graph)

        #extract edges and nodes
        #edges define the geometry of the road
        #nodes define the start and end points of each road
        self._nodes, self._edges = ox.graph_to_gdfs(self._graph_proj,
                                                    nodes=True,
                                                    edges=True)
Beispiel #33
0
def test_stats():

    # create graph, add bearings, project it
    location_point = (37.791427, -122.410018)
    G = ox.graph_from_point(location_point, distance=500, distance_type='network')
    G = ox.add_edge_bearings(G)
    G_proj = ox.project_graph(G)

    # calculate stats
    stats1 = ox.basic_stats(G)
    stats2 = ox.basic_stats(G, area=1000)
    stats3 = ox.basic_stats(G_proj, area=1000, clean_intersects=True, tolerance=15, circuity_dist='euclidean')
    stats4 = ox.extended_stats(G, connectivity=True, anc=True, ecc=True, bc=True, cc=True)
Beispiel #34
0
def test_nearest_edges():

    from pyproj import Proj

    # test in closest edge section
    sheik_sayed_dubai = [25.09, 25.06, 55.16, 55.11]
    location_coordinates = (25.071764, 55.138978)
    G = ox.graph_from_bbox(*sheik_sayed_dubai, simplify=False, retain_all=True, network_type='drive')

    # Unprojected
    ne1 = ox.get_nearest_edges(G, X=[location_coordinates[1], location_coordinates[1]],
                                  Y=[location_coordinates[0], location_coordinates[0]], method='balltree', dist=0.0001)

    # Projected
    G2 = ox.project_graph(G)
    crs = Proj(G2.graph['crs'])

    projected_point = crs(location_coordinates[1], location_coordinates[0])
    ne2 = ox.get_nearest_edges(G2, X=[projected_point[0], projected_point[0]],
                                   Y=[projected_point[1], projected_point[1]], method='kdtree', dist=10)
    assert (ne1 == ne2).all()
place_names = ['Ho Chi Minh City, Vietnam',
               #'Beijing, China', 
               #'Jakarta, Indonesia',
               'London, UK',
               'Los Angeles, California, USA',
               'Manila, Philippines',
               #'Mexico City, Mexico',
               'New Delhi, India',
               'Sao Paulo, Brazil',
               'New York, New York, USA',
               'Seoul',
               'Singapore',
               #'Tokyo, Japan',
               #'Nairobi, Kenya',
               #'Bangalore, India'
              ]
              
# In this for-loop, we save all the shapefiles for the valid cities.
for city in place_names:  
    city_admin_20kmbuff = ox.gdf_from_place(city, gdf_name = 'global_cities', buffer_dist = 20000)
    fig, ax = ox.plot_shape(city_admin_20kmbuff)
    ox.save_gdf_shapefile(city_admin_20kmbuff, filename = city)
    
# In this for-loop, we save all the street networks for the valid cities.
for city in place_names:
    grid = ox.graph_from_place(city, network_type = 'drive', retain_all = True)
    grid_projected = ox.project_graph(grid)
    ox.save_graph_shapefile(grid_projected, filename = city + '_grid')
    ox.plot_graph(grid_projected)
ox.config(log_console=True, use_cache=True, data_folder='data/vector/city_networks/')

scriptpath = os.path.dirname(__file__)
cities = glob.glob("data/vector/city_boundaries/*.shp")


#indir = "data/vector/city_boundaries/"
for city in cities:
    name = os.path.basename(city).split('.')[0]
    
    place = gpd.read_file(city)
    place_simple = place.unary_union # disolving boundaries based on attributes

    # Use retain_all if you want to keep all disconnected subgraphs (e.g. when your places aren't adjacent)
    G = ox.graph_from_polygon(place_simple, network_type='drive', retain_all=True)
    G_projected = ox.project_graph(G)

    # save the shapefile to disk
    #name = os.path.basename("beijing.shp")).split(".")[0]  # make better place_names
    ox.save_graph_shapefile(G_projected, filename=name)

    area = ox.project_gdf(place).unary_union.area
    stats = ox.basic_stats(G, area=area)
    # save to file:
    def ensure_dir(file_path):
        directory = os.path.dirname(file_path)
        if not os.path.exists(directory):
            os.makedirs(directory)

    path = os.path.join('data/vector/city_networks/', name)
    ensure_dir(path)
def ensure_dir(file_path):
    directory = os.path.dirname(file_path)
    if not os.path.exists(directory):
        os.makedirs(directory)
        

for place in places:
    
    name = (place.replace(",","").replace(" ","")) # make better place_names
    print('working on: ', name)

    #make a geodataframe of the street network (outline) from openstreetmap place names
    # use retain_all if you want to keep all disconnected subgraphs (e.g. when your places aren't adjacent)
    G = ox.graph_from_place(place, network_type='drive', retain_all=True)
    G = ox.project_graph(G)
    
    #make a geodataframe of the shape (outline) from openstreetmap place names
    gdf = ox.gdf_from_place(place)
    gdf = ox.project_gdf(gdf)
    ox.save_graph_shapefile(G, filename=name)
    
    
    print(name, ' has crs:' ) 
    gdf.to_crs({'init': 'epsg:3395'})
    # Confirm big step of projection change
    
    # calculate basic stats for the shape
    # TODO adjust this to calculate stats based on neighborhoods
    stats = ox.basic_stats(G, area=gdf['geometry'].area[0])
    print('area', gdf['area'][0] / 10**6, 'sqkm')