I have a shapefile of all street segments in Los Angeles County that I have made sure is clean. I am able to use the QGIS Road Graph plugin to calculate various shortest paths, see attached as my first example.

I am getting an error when trying to use the NetworkX shortest_path on my network. I am loading a shapefile (roughly 410,000 edges). My route start and end points are well known and ON the network. There are probably better data structures, but i have created a dictionary of Points with values of what i believe to be the street segment keys, though not sure (from docs: Coordinate tuples are used as keys).


.... weighted.py", line 359, in _dijkstra
  for u, e in G_succ[v].items():
KeyError: ((-118.3162743390968, 34.25880839639892), (-118.31575202245774, 34.25880723483486))

I verified that this street segment listed in the KeyError is in the edges list as I wrote all edges to file.

I am now going to start diagnosing the weighted.py code.

Core code snippets:

def InitRoadGraph():

    self.roadGraph = nx.read_shp(self.roadsrc)
    self.StreetNodeFromDict = {}
    for edge in self.roadGraph.edges(data=True):
        self.StreetNodeFromDict[edge[0]] = (edge[0], edge[1])

def GetStreetIDFromNodeSimple(self, geometry_point):

    return self.StreetNodeFromDict[geometry_point]

def GetShortestRoute(self, srcPoint, dstStreetSegmentID): 

    srcSegmentID = self.GetStreetIDFromNodeSimple(srcPoint, "FNODE_")

    shortestRoute = nx.shortest_path(self.roadGraph, srcSegmentID, dstStreetSegmentID)[![enter image description here][1]][1]

1 Answer 1


I have confirmed that in networkx 1.11, the keys of the network dictionary are the start node coordinates, not the start and end node segments as i would suspect. Though significant changes have been made to the read_shp method through 2.1, I don't see a change to this behavior. It would seem we'd want the begin/end even though that might not be unique either.

Also, attached is my network though it doesn't appear to have attached to my original post.

Finally, i would think the read_shp would calculate a weight attribute. I may try to add that and make a pull request if that makes sense.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.