I have an osmnx graph. I identified a specific edge that I want to remove from the graph using the G.remove_edge(u, v) function. After doing this, if I run G[u][v] to look at the edge attributes I get a key error, which makes sense and indicates that the edge was removed. However, if I plot the graph (or export it as a geopackage) I can see that the edge is clearly still there.
import osmnx as ox
import utm
# point used to create download the graph.
point = {'lat':48.1540155043, 'lon':11.5623458228}
# Converts point latitude and longitude from decimal degrees to utm meters.
point_utm = utm.from_latlon(point['lat'], point['lon'])
# Downloads a network originating from the point.
G = ox.graph_from_point((point['lat'], point['lon']), dist = 1000, network_type = 'walk')
#Projects the network into utm.
G_projected = ox.project_graph(G)
# Identifies the nearest edge to the point.
nearest_edge = ox.nearest_edges(G_projected, point_utm[0], point_utm[1])
# Nearest edge u and v values.
u = nearest_edge[0]
v = nearest_edge[1]
# Removes the nearest edge based on the u and v values.
G_projected.remove_edge(u, v)
I have tried a number of things. I plotted the graph before removing the edge and told it to highlight the edge in question. It highlighted the edge I was expecting. This tells me that I am not deleting some other edge.
I also tried converting the graph to a geopandas geodataframe, deleting the row in the geodataframe that corresponds to this edge, then converting it back into osmnx graph. I was able to convert it to geopandas geodataframe, remove the edge, and confirm that it was no longer in the geopandas geodataframe, but the edge still appears once it has been converted back into a graph.
Like @jasonharper suggests in the comments, you may be using the wrong graph after edge removal. You can check this by confirming the existence of the edge in question at the end of your code snippet:
print((u, v) in G.edges()) # True
print((u, v) in G_projected.edges()) # False
That edge no longer exists in G_projected
but it does still exist in G
, as expected.
Aside from that possibility, also note the docs:
a one-way street will be represented with a single directed edge from node u to node v, but a bidirectional street will be represented with two reciprocal directed edges (with identical geometries): one from node u to node v and another from v to u, to represent both possible directions of flow.
So if you delete edge (u, v)
and then plot the graph, you may still see edge (v, u)
if it is a bidirectional street in a directed graph. If so, you must delete both reciprocal directed edges.