edgesosmnx

Find the coordinate of center point on an edge of OSMNX


I am looking to find the coordinates (Lat, Lon) of a center point on any edge of a network of OSMNX.

For instance my network is:

G=ox.graph_from_place('San Diego County, CA', network_type='drive')

Solution

  • Use the interpolate function from shapely.

    import osmnx as ox
    G=ox.graph_from_place('San Diego', network_type='drive')
    nodes, edges = ox.graph_to_gdfs(G, nodes=True, edges=True)
    

    Derive midpoint for every edge of the graph.

    edges['midpoint'] = ''
    for i in range(0,len(edges)):
        midpoint = edges['geometry'].iloc[i].interpolate(0.5, normalized = True)
        edges['midpoint'].iloc[i] = midpoint
    

    Plot the graph. Nodes are denoted by red. Midpoints of edges are denoted by green.

    import matplotlib.pyplot as plt
    fig, ax = ox.plot_graph(G, node_size=2, figsize=(48, 48),node_color='r', show=False, close=False)    
    for i in range(0,len(edges)):
        ax.scatter(edges['midpoint'].iloc[i].x, edges['midpoint'].iloc[i].y, c='g', s = 2)    
    plt.show() 
    

    Output (cropped image shown): enter image description here