2

I have two shapefiles that represent a railway network.

The first shapefile (tracks.shp) contains LineStrings representing tracks The second shapefile (crossings.shp) contains Points representing rail crossings.

My intention is to create a Graph network where the nodes are these crossing points and edge the tracks between these crossings.

Here is a visual of both shapefiles (using QGIS): The green points are the crossings (vertex) rail_network

I saw this answer which is similar but in this case, the vertex for the Networkx graph has to come from another shapefile.

This was the code I wrote:

def generate_graph():
    G_nodes = nx.read_shp("crossings.shp")
    positions_nodes = {k: v for k,v in enumerate(G_nodes.nodes())}

    G_edges = nx.read_shp("tracks.shp")
    positions_edges = G_edges.edges()

    X = nx.Graph()
    X.add_nodes_from(positions_nodes.keys())
    X.add_edges_from(positions_edges)

The issue is I do not know how to sync both graphs. For example, If I try to display the network, I get a KeyError due to discrepancies between both data sources.

X = nx.Graph()
X.add_nodes_from(positions_nodes.keys())
X.add_edges_from(positions_edges)
nx.draw_networkx_edges(X,positions_nodes, positions_edges)

Any pointers? Both on this error and my attempt to sync both shapefiles into one NetworkX graph.

0 Answers0