I am using this script to import OSM data into a GIS based model.

This has to run independent from any software package such as QGIS, PostGIS, or ArcGIS.

I then create a shortest path between two nodes as follow:

shortest_path = nx.shortest_path(G, source=u'879835512', target=u'879912131')
result_graph = G.subgraph(shortest_path)

I would like to visualize (kml?) the results of the shortest path and store them (shp?).

I have a feeling that I need to use the write_shp but I think that only works if you have already used read_shp which I haven't since I'm importing directly from OSM.


Based on @gene 's comment, I added the following code:

for e in result_graph.edges():
    p0x = result_graph.node[e[0]]['lat']
    p0y = result_graph.node[e[0]]['lon']
    p1x = result_graph.node[e[1]]['lat']
    p1y = result_graph.node[e[1]]['lon']
    edges.append(LineString([(p0x, p0y), (p1x, p1y)]))

nodes = []
for n in result_graph.nodes():
    px = result_graph.node[e[0]]['lat']
    py = result_graph.node[e[0]]['lon']

G2 = nx.DiGraph()


I don't think I'm on the right track yet. as it seems I need to maintain the coordinates somehow with my nodes/links


I tried this:

highway_cat = 'motorway|trunk|primary|secondary|tertiary|road|residential|service|motorway_link|trunk_link|primary_link|secondary_link|teriary_link'
shortest_path = networkx.shortest_path(G, source=u'1810752839', target=u'3393331431')
for i in shortest_path:
w = shapefile.Writer(shapefile.POLYLINE)
w.record(FIRST_FLD='First', SECOND_FLD='Line')


enter image description here

  • Answer works! Curious, do I need to import pyshp to do so? is pyshp part of Geopandas or fiona? – dassouki Oct 19 '15 at 18:59
  • I found stand-alone module called shapefile.py somewhere on Internet, Trying to find it again and it's not working. But any of above will do. I can upload the one I've used – FelixIP Oct 19 '15 at 19:25
  • Shapefile is part of the pyshp library so it worked for me as well but perhaps if the solution could be geared towards Fiona that'll be perfect :D – dassouki Oct 19 '15 at 19:26
  • 1
    I need efficiency with GIS this is why 99% of the time I am using ArcGIS, I decide to give it a go because I love nx. I guess there are endless options to export results, i.e. modify the code below line w = shapefile.Writer(shapefile.POLYLINE). However I think important part of my solution sits above this line  – FelixIP Oct 19 '15 at 21:26

Simply look at NetworkX: write_shp

Writes a networkx.DiGraph to two shapefiles, edges and nodes. Nodes and edges are expected to have a Well Known Binary (Wkb) or Well Known Text (Wkt) key in order to generate geometries. Also acceptable are nodes with a numeric tuple key (x,y).

You don't need to use read_shp before but you must convert the nodes and edges of the digraph to WKT format.

For that, you can use Shapely

  • Thanks for providing the info about shapely. I edited my question based on your answer if you'd like :D – dassouki Oct 14 '15 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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