Create Graph from street file

65 views
Skip to first unread message

Narayan Das

unread,
Jan 24, 2024, 9:17:48 AMJan 24
to networkx-discuss
Hi everyone,
I am new here. I am trying to create a graph from given street file using geopandas, momepy and networkx library. 
With my scripts I can create the Graph, but this graph does not contains any keys so I want to add keys as well in Graph G.
Please have look in my script and let me know where I am doing mistake, If anyone have other idea, I'll be happy to discuss with that as well:- 

Thank you everyone

def construct_graph_from_network_layer(network_layer, col_drop:list=[], length_column:str=None, time_column:str=None, avg_speed:int = 25):
    street = geoapndas.read_file(network_layer)
    street = street.explode().reset_index(drop=True)
    if sum(street.geom_type=='LineString')!=len(street): raise ValueError("Please provide the Proper Network layer, Network layer must have Linestring or Multilinestring type of Geometry!!!")
    if col_drop: street.drop(col_drop, axis=1, inplace=True)
    graph = momepy.gdf_to_nx(street, length='length')

    nodes, edges = nx_to_gdf(graph, points=True, lines=True,spatial_weights=False)  ##Extracting node and edges geodatframe from graph
    nodes['x']= nodes['geometry'].apply(lambda pt: pt.x)
    nodes['y']= nodes['geometry'].apply(lambda pt: pt.y)
    edges.reset_index(inplace=True)
    edges.rename(columns={'node_start':'u', 'node_end':'v','index':'k'}, inplace=True)
    cr1 = edges.estimate_utm_crs()

    if length_column is None:
        length_column= 'length'
        edges[length_column]= edges.to_crs(cr1).length

    meters_per_minute = avg_speed * 1000 / 60  # km per hour to m per minute
    if time_column is None:
        time_column= 'time'
        edges[time_column]= edges[length_column].apply(lambda x: round(x/meters_per_minute, 3))
    edges[length_column] = edges[length_column].apply(lambda x: round(x,2))
    edges= edges[[ 'u', 'v','k']+[x for x in edges.columns if x not in [ 'u', 'v','k']]]

    #Creating Graph and adding node and edges
    G = nx.Graph()
    G.graph['crs'] = 'EPSG:4326'
    for idx, row in nodes.iterrows():
        x = row['x']
        y = row['y']
        G.add_node(idx, x=x, y=y)
    for idx, row in edges.iterrows():
        u = row['u']
        v = row['v']
        k = row['k']
        G.add_edge(u, v, k =k, attr_dict=row[3:].to_dict())
    return G, nodes, edges

Dan Schult

unread,
Jan 24, 2024, 3:15:51 PMJan 24
to networkx...@googlegroups.com
It is not clear whether you want an edge attribute, or you want multiple edges between two nodes.
Edge attributes are the way to store information about the edge. 
Using a MultiGraph instead of a Graph is how you store more than one edge between a pair of nodes.
See the tutorial for more info and help with deciding how to configure your graph.

Rhyd Lewis

unread,
Jan 31, 2024, 5:51:53 AMJan 31
to networkx-discuss
Hello,

The OSMNX library should suit your purposes. A tutorial is here

Reply all
Reply to author
Forward
0 new messages