Exemple #1
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)
Exemple #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')
Exemple #3
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)
Exemple #4
0
def bikin_rute(lat_input,long_input,lat_center,long_center,lat_output,long_output):
    titik_awal       = [lat_input, long_input]
    titik_tujuan     = [lat_output, long_output]
    bandung = (lat_center, long_center)
    G = ox.graph_from_point(bandung, distance=600)

    nodes, _ = ox.graph_to_gdfs(G)
    #ambil attribut edges pada G 
    edges = ox.graph_to_gdfs(G, nodes=False, edges=True, node_geometry=False, fill_edge_geometry=False)

    #kasi nilai random antara 1-10 pada G['weight']
    edges['weight'] = np.random.randint(1,10, size=len(edges))
    #campur ke G 
    G = ox.gdfs_to_graph(nodes,edges)
    #deklarasi tree untuk mendeteksi closest nodes pada titik awal dan tujuan
    tree = KDTree(nodes[['y', 'x']], metric='euclidean')

    awal    = tree.query([titik_awal], k=1, return_distance=False)[0]
    tujuan  = tree.query([titik_tujuan], k=1, return_distance=False)[0]

    #dapat nodes terdekat
    closest_node_to_awal   = nodes.iloc[awal].index.values[0]
    closest_node_to_tujuan = nodes.iloc[tujuan].index.values[0]
    route = nx.dijkstra_path(G, closest_node_to_awal,closest_node_to_tujuan,weight='weight')
    basemap = ox.plot_route_folium(G, route, route_color='green')
    folium.Marker(location=titik_awal,icon=folium.Icon(color='red')).add_to(basemap)
    folium.Marker(location=titik_tujuan,icon=folium.Icon(color='green')).add_to(basemap)
    basemap.save(outfile= "C:/xampp/htdocs/js/flask2/templates/djikstra.html")
Exemple #5
0
def test_network_saving_loading():

    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)
    G2 = ox.load_graphml('graph.graphml')

    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)
Exemple #6
0
def test_network_saving_loading():

    with httmock.HTTMock(get_mock_response_content('overpass-response-7.json.gz')):
        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)
    G2 = ox.load_graphml('graph.graphml')

    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)
Exemple #7
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
 def __init__(self, road_map=None):
     self.map = road_map
     if road_map != None:
         edges = ox.graph_to_gdfs(self.map, nodes=False, fill_edge_geometry=True)
         self.bbox = edges.total_bounds #west, south, east, north
     else:
          self.bbox = None
Exemple #9
0
def test_network_saving_loading():

    # save graph as shapefile and geopackage
    G = ox.graph_from_place(place1, network_type="drive")
    ox.save_graph_shapefile(G)
    ox.save_graph_geopackage(G)

    # save/load graph as graphml file
    ox.save_graphml(G, gephi=True)
    ox.save_graphml(G, gephi=False)
    filepath = os.path.join(ox.settings.data_folder, "graph.graphml")
    G = ox.load_graphml(filepath, node_type=str)

    # test osm xml output
    default_all_oneway = ox.settings.all_oneway
    ox.settings.all_oneway = True
    G = ox.graph_from_point(location_point, dist=500, network_type="drive")
    ox.save_graph_xml(G, merge_edges=False)

    # test osm xml output merge edges
    ox.save_graph_xml(G, merge_edges=True, edge_tag_aggs=[("length", "sum")])

    # test osm xml output from gdfs
    nodes, edges = ox.graph_to_gdfs(G)
    ox.save_graph_xml([nodes, edges])

    # test ordered nodes from way
    df = pd.DataFrame({
        "u": [54, 2, 5, 3, 10, 19, 20],
        "v": [76, 3, 8, 10, 5, 20, 15]
    })
    ordered_nodes = ox.io._get_unique_nodes_ordered_from_way(df)
    assert ordered_nodes == [2, 3, 10, 5, 8]

    ox.settings.all_oneway = default_all_oneway
Exemple #10
0
def create_route(coords, dist_goal, name):
    """
    Creates list of routes
    Parameters:
        coords (tuple): (latitude,longitude)
        dist_goal (int): desired length in km
    Returns:
        paths (list): list of lists, each sublist is a path (list of osmid's)
    """

    graph = ox.core.graph_from_point(coords,
                                     distance=dist_goal * 700,
                                     simplify=False)
    start = ox.get_nearest_node(graph, coords)
    nodes, _ = ox.graph_to_gdfs(graph)
    pivots = get_pivots(graph, start, dist_goal)

    #print(start)

    paths = []
    for piv in pivots:
        try:
            test = make_loop(graph, piv, start, dist_goal)
            paths.append(test)
            ox.plot_graph_route(graph, test)
        except Exception as exception:
            print("Error in loop generation")
            print(exception)

    return paths, nodes
Exemple #11
0
def add_traffic_zones_to_nodes(G, gdf, zone_column):
    """
    Adds traffic zones to nodes of a graph.

    Parameters
    ----------
    G : NetworkX Graph or DiGraph
        Graph for info to be added.
    gdf: GeoDataFrame 
        GeoDataFrame of the traffic zones
    zone_column: string
        Name of the column.
    zone_column: bool
        The column of GeoDataFrame with the names of the zones.

    Returns
    -------
    NetworkX graph
    """
    gdf_g = ox.graph_to_gdfs(G)[0]
    gdf_g = gdf_g.to_crs(epsg=4326)
    join = gpd.sjoin(gdf, gdf_g, op='contains')

    for zone, osmid in zip(join[zone_column], join.osmid):
        G.nodes[osmid]['zone'] = zone
    for node in G.nodes:
        try:
            G.nodes[node]['zone']
        except:
            G.nodes[node]['zone'] = None
    return G
    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)
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
Exemple #14
0
def load_roadnet():
    """
    Loads road network graph edges of Singapore as a GeoDataFrame.
    The data is downloaded from OpenStreetMap through osmnx package.
    run `!pip install osmnx` if not yet installed
    documentation: https://github.com/gboeing/osmnx

    returns
    -------
    roadnet (GeoDataFrame): road network graph edges of Singapore
    """
    import osmnx as ox

    # download from OpenStreetMap if local datasets is not available
    print("Trying to download the network of Singapore through OSM Nominatim.")

    # try different query results
    n = 1
    while n <= 5:
        try:
            G = ox.graph_from_place(query='Singapore', network_type='drive',
                                    which_result=n)
            break
        except:
            n += 1

    roadnet = ox.graph_to_gdfs(G, nodes=False, edges=True)
    print("Singapore roadnet is downloaded and loaded as a GeoDataFrame.")

    return roadnet
Exemple #15
0
def load_OSM_basic_stats(G_filename, folder=OSM_folder):
    """
    retains all the basic stats for pedestrain network within study regions graphml from a local folder
    
    Parameters
    ----------
    G_filename : string
        the name of the graphml file (including file extension)
    OSM_folder : string
        the folder containing the OSM file, if None, use default data folder
    
    
    Returns
    -------
    DataFrame
    """
    df = pd.DataFrame()
    #load street network data from local directory
    G = ox.load_graphml(G_filename, folder=folder)
        
    gdf_nodes = ox.graph_to_gdfs(G, edges=False)
    graph_area_m = gdf_nodes.unary_union.convex_hull.area
        
    stats = ox.basic_stats(G, area=graph_area_m, clean_intersects=True, circuity_dist='euclidean', tolerance=15)
    df1 = pd.DataFrame.from_dict(stats, orient='index', columns=['OSM_pedestrain_network'])
    df = pd.concat([df, df1], axis=1)
    return df
Exemple #16
0
def Save_OSM_gdf_proj(placename=placename, network_type= 'walk', suffix=suffix, folder=OSM_folder):    
    """
    save a projected geodataframe from a projected 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_proj = ox.load_graphml(filename='{studyregion}_proj_{network_type}{suffix}.graphml'.format(
        studyregion = placename, network_type=network_type, suffix = suffix), folder=folder)
    
    #save projected network geodataframe as shapefile (project to UTM so we can use metres as a unit when buffering)
    edges_proj_gdfs = ox.graph_to_gdfs(G_proj, nodes=False, edges=True, fill_edge_geometry=True)
    ox.save_gdf_shapefile(edges_proj_gdfs, filename='{studyregion}_proj_{network_type}{suffix}'.format(
        studyregion = placename, network_type=network_type, suffix = suffix), folder=folder)
    
    #show network figure
    fig, ax = plt.subplots(figsize=(5, 5))
    ax = edges_proj_gdfs.plot(ax=ax)
    ax.set_axis_off()
    fig.suptitle('{} OSM {} street network'.format(placename, network_type), fontsize=14, fontweight='bold')
    plt.show()
Exemple #17
0
def redraw_axes(G, axes, num_iters):
    # This function will redraw the axes when a new location has been fetched. Similar
    # in behavior to later "update_axes".
    # This function replots the view after the road network has been updated.
    # params: G:              road network graph
    #         axes:           matplotlib axes for plot
    #         num_iters:      number of iterations simulated since last update, to update count at top
    #         congestion:     whether or not to graph in congestion mode (alternative is volume)
    axes.collections[0].remove()
    lines = []
    edges = ox.graph_to_gdfs(G, nodes=False, fill_edge_geometry=True)
    west, south, east, north = edges.total_bounds
    axes.set_ylim((south, north))
    axes.set_xlim((west, east))
    for u, v, data in G.edges(keys=False, data=True):
        if 'geometry' in data and True:
            # if it has a geometry attribute (a list of line segments), add them
            # to the list of lines to plot
            xs, ys = data['geometry'].xy
            lines.append(list(zip(xs, ys)))
        else:
            # if it doesn't have a geometry attribute, the edge is a straight
            # line from node to node
            x1 = G.nodes[u]['x']
            y1 = G.nodes[u]['y']
            x2 = G.nodes[v]['x']
            y2 = G.nodes[v]['y']
            line = [(x1, y1), (x2, y2)]
            lines.append(line)

    eColors = get_edge_colors_by_attribute(G, 'traversals', num_bins=5)
    widths = calc_width_from_colors(eColors, 3, calc_size_weight(north, south))
    lc = LineCollection(lines, colors=eColors, linewidths=widths, zorder=2)
    axes.set_title(num_iters)
    axes.add_collection(lc)
def node_tags(g, tag='highway'):
    n = [n for n in g.nodes(data=True) if tag in n[1].keys()]
    nid, val = zip(*n)
    ndf = pd.DataFrame(val, index=nid)
    ndf = ndf.rename(columns={'x': 'lon', 'y': 'lat'})
    x, y = zip(*project_point(list(zip(ndf.lon, ndf.lat))))
    ndf = ndf.assign(x=x, y=y, junction=list(ndf.street_count > 2))
    g = ox.add_edge_bearings(g, precision=1)
    edf = ox.graph_to_gdfs(g, nodes=False, edges=True)
    edf.reset_index(inplace=True)
    edf = edf[['u', 'v', 'osmid', 'bearing']]
    edf = edf.explode('osmid', ignore_index=True)
    edf.set_index('u', inplace=True)
    ndf = pd.merge(ndf,
                   edf[['osmid', 'bearing']],
                   how='left',
                   left_index=True,
                   right_index=True)
    # check for na values
    ndf_na = ndf[ndf.osmid.isna()].copy()
    ndf.drop(ndf_na.index, inplace=True)
    ndf_na.drop(['osmid', 'bearing'], axis=1, inplace=True)
    edf_na = edf.reset_index()
    edf_na.set_index('v', inplace=True)
    ndf_na = pd.merge(ndf_na,
                      edf_na[['osmid', 'bearing']],
                      how='left',
                      left_index=True,
                      right_index=True)
    ndf = pd.concat([ndf, ndf_na], axis=0)
    ndf = ndf.astype({'osmid': 'int64'})
    ndf = gpd.GeoDataFrame(ndf, geometry=gpd.points_from_xy(ndf.lon, ndf.lat))
    return ndf
    def convert_route_to_gdf(self, route):
        """
        Convert a route to a geopandas.GeoDataFrame

        Parameters
        ----------
        G : networkx.MultiDiGraph
            input graph
        route : list
            List of nodes in a route.

        Returns
        -------
        geopandas.GeoDataFrame
            List of the edges defined in the route
        """
        # Extracted from ox.plot_route_folium()
        node_pairs = zip(route[:-1], route[1:])
        uvk = ((
            u,
            v,
            min(self.G_risk[u][v],
                key=lambda k: self.G_risk[u][v][k]["length"]),
        ) for u, v in node_pairs)
        gdf_edges = ox.graph_to_gdfs(self.G_risk.subgraph(route),
                                     nodes=False).loc[uvk]

        return gdf_edges
    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)
Exemple #21
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)
Exemple #22
0
def load_drive_graph(path='db/shenzhen20170701-all.osm', ):
    """
    load original open street map data of a city and extract drivable road network
    :param path: original osm xml format data
    :return: drivable graph
    """
    shenzhen_all = ox.graph_from_xml(path)
    sz_nodes, sz_edges = ox.graph_to_gdfs(shenzhen_all,
                                          fill_edge_geometry=True)

    not_valid_highway = 'cycleway|footway|path|pedestrian|steps|track|corridor|elevator' \
                        '|escalator|proposed|construction|bridleway|abandoned|platform' \
                        '|raceway|service'.split('|')
    not_valid_service = 'parking|parking_aisle|driveway|private|emergency_access'.split(
        '|')
    print('original_edges', sz_edges.shape)

    sz_gdfs = sz_edges.loc[((sz_edges['highway'].notna())
                            & (sz_edges['area'] != 'yes')
                            & (sz_edges['access'] != 'private')
                            & (~sz_edges['service'].isin(not_valid_service)))]

    for tag in not_valid_highway:
        sz_gdfs = sz_gdfs.loc[sz_gdfs['highway'] != tag].copy(deep=True)
    print('drivable_edges:', sz_gdfs.shape)

    shenzhen_drive = ox.graph_from_gdfs(sz_nodes, sz_gdfs)

    return shenzhen_drive
Exemple #23
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=[])
Exemple #24
0
def simple_G():
    '''
        删除长度为0 的边
    '''
    current_dir = os.path.dirname(os.path.abspath(__file__))
    G_proj = ox.load_graphml(current_dir + '\\data\\chengdu_proj.graphml')
    nodes, edges = ox.graph_to_gdfs(G_proj, fill_edge_geometry=True)
    edges = edges[edges['u'] != edges['v']]
    edges = edges[edges['key'] == 0]
    edges = edges[edges['length'] > 0]
    nodes['osmid'] = [int(i) for i in nodes['osmid']]
    nodes['node_id'] = range(nodes.shape[0])
    edges['road_id'] = range(edges.shape[0])
    '''
        nodes_replace_id_dict 已经保存
    '''
    # 读取
    f = open(current_dir + '\\data\\nodes_replace_id_dict.txt', 'r')
    a = f.read()
    nodes_replace_id_dict = eval(a)
    f.close()
    edges['u_replace'] = [nodes_replace_id_dict[i] for i in edges['u']]
    edges['v_replace'] = [nodes_replace_id_dict[i] for i in edges['v']]
    G = ox.gdfs_to_graph(nodes, edges)
    return G, edges
Exemple #25
0
def graph_to_shp(G, edge_shp, node_shp):
    """Takes in a networkx graph object and outputs shapefiles at the paths indicated by edge_shp and node_shp

    Arguments:

        G []: networkx graph object to be converted

        edge_shp [str]: output path including extension for edges shapefile

        node_shp [str]: output path including extension for nodes shapefile

    Returns:

        None

    """
    # now only multidigraphs and graphs are used
    if type(G) == nx.classes.graph.Graph:
        G = nx.MultiGraph(G)

    # The nodes should have a geometry attribute (perhaps on top of the x and y attributes)
    nodes, edges = osmnx.graph_to_gdfs(G)

    dfs = [edges, nodes]
    for df in dfs:
        for col in df.columns:
            if df[col].dtype == np_object and col != df.geometry.name:
                df[col] = df[col].astype(str)

    print('\nSaving nodes as shapefile: {}'.format(node_shp))
    print('\nSaving edges as shapefile: {}'.format(edge_shp))

    nodes.to_file(node_shp, driver='ESRI Shapefile', encoding='utf-8')
    edges.to_file(edge_shp, driver='ESRI Shapefile', encoding='utf-8')
Exemple #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
Exemple #27
0
def get_node_gdf(graph):
    node_gdf = ox.graph_to_gdfs(graph,
                                nodes=True,
                                edges=False,
                                node_geometry=True,
                                fill_edge_geometry=False)
    return node_gdf[['geometry']]
Exemple #28
0
def nodes_from_osm(gdf, ras_path):
    """
    Function to count the number of intersections in each polygon
    
    Input:
    gdf : A geodataframe (Ideally a grid file for the city)
    ras_path : raster path of base data
    
    Returns:
    Geodataframe with 'node_count' column added to it.
    """
    
    print("Populating nodes from OSM!!")
    
    rio = rasterio.open(ras_path)
    pol = shapely.geometry.box(rio.bounds[0], rio.bounds[1], rio.bounds[2], rio.bounds[3])
    
    G = ox.graph_from_polygon(pol)
    nodes = ox.graph_to_gdfs(G, nodes=True, edges=False)

    
    gdf.crs = {'init':'epsg:4326'}
    gdf_copy = gdf.copy()
    gdf_copy.to_crs(epsg=out_proj, inplace=True) 
    nodes.to_crs(epsg=out_proj, inplace=True)
    
    nodes['geomType'] = nodes.geom_type
    nodes = nodes[nodes['geomType'] != 'GeometryCollection']
    merged = gpd.sjoin( gdf_copy, nodes, how='left', op='intersects')
    grp = merged.groupby('ID').count()
    
    gdf['node_count'] = grp.Lon.tolist()
    
    return gdf
Exemple #29
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)
Exemple #30
0
def make_road_segmentation_target(src,
                                  all_touched=False,
                                  drop_tunnels=True,
                                  **kwargs):
    """Create a binary segmentation target for the detection of roads using OpenStreetMap.
    
    There are a number of limitations here:
    * OpenStreetMap is incomplete and may have inaccuracies.
    * Clouds are not accounted for
    """

    target_img = np.zeros((src.height, src.width), dtype=np.uint8)

    try:
        feats = features.dataset_features(src, band=False, as_mask=True)
        bound = gpd.GeoDataFrame.from_features(feats, crs=4326)["geometry"][0]
        G = ox.graph_from_polygon(bound)
        nodes, edges = ox.graph_to_gdfs(G)

        if drop_tunnels:
            edges = edges.loc[edges["tunnel"].isna()
                              | edges["tunnel"].astype(str).str.contains("no")]

        shapes = [(geom, 1) for geom in edges.to_crs(src.crs)["geometry"]]

        return features.rasterize(shapes=shapes,
                                  fill=0,
                                  out=target_img,
                                  transform=src.transform,
                                  all_touched=all_touched).astype(np.uint8)

    except:
        return target_img
Exemple #31
0
 def __get_from_osmnx(self, tile: Tile) -> gpd.GeoDataFrame:
     bbox: LngLatBbox = bounds(tile)
     west, south, east, north = bbox
     west -= LAT_LNG_BUFFER
     east += LAT_LNG_BUFFER
     north += LAT_LNG_BUFFER
     south -= LAT_LNG_BUFFER
     try:
         graph = ox.graph_from_bbox(north,
                                    south,
                                    east,
                                    west,
                                    simplify=False,
                                    retain_all=True)
         gdfs = ox.graph_to_gdfs(graph)
         for gdf in gdfs:
             self.cache = self.cache.append(gdf,
                                            ignore_index=True,
                                            sort=False)
     except ox.core.EmptyOverpassResponse:
         pass
     except ValueError as e:
         if "not enough values" in str(e):
             print(
                 f"[WARNING] Could not load tile {tile}! Assuming it is empty..."
             )
         else:
             raise e
     self.cached_tiles |= {tile}
     self.cached_tiles = set(simplify(*self.cached_tiles))
     return self.__get_from_cache(tile)