Shapefile NetworkX Tutorial

1,685 views
Skip to first unread message

Bjorn Nyberg

unread,
Jul 29, 2013, 5:27:06 AM7/29/13
to networkx...@googlegroups.com
Hej,
Can anyone lead me to a good tutorial on working with shapefiles with Networkx? Namely I want to perform some shortest path algorithms on some polyline shapefiles and want to use the networks read_shp function but really need some extra help to get started! Specificially if I try the following:

import network as nx
shapefile = r'path_to_shapefile'
data = nx.read_shp(shapefile)

data.edges(data=True)[0]

((4132812.5071952357, 5195243.407249198), (4132702.507578748, 5195314.87196819), {'ShpName': 'Polygon', 'Json': '{ "type": "LineString", "coordinates": [ [ 4132812.50719523569569, 5195243.407249198295176 ], [ 4132702.507578747812659, 5195314.871968190185726 ] ] }', 'Wkb': '\x00\x00\x00\x00\x02\x00\x00\x00\x02AO\x87\xe6@\xeb\xc6\x03AS\xd1z\xda\x10^\xf1AO\x87\xaf@\xf8W%AS\xd1\x8c\xb7\xceS\xab', 'id':0, 'weight': 131, 'Wkt': 'LINESTRING (4132812.50719523569569 5195243.407249198295176,4132702.507578747812659 5195314.871968190185726)'})

In this shapefile I have a field called weight with a value of 131 on this given id of 0... How can I make a weighted edge equal to that weight or equal to the id field in a custom weight as discussed here! There is alot of additional information provided in the data=True field but all I want is essentially a [(source,target,weight)] to apply to a shortest_path algorithm in Networkx.

Finally when I try to write a shapefile from the DiGraph I get a RuntimeError: Failed to open Shapefile `r/Path_To_Shapefile.shp'. Can I somehow reuse the extra parameters in the edges (i.e. wkb or wkt) and only retrain those of the shortest path when rewriting the shapefile?

Cheers,
Bjorn


Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/networkx/readwrite/nx_shp.py", line 169, in write_shp
    nodes = shpdir.CreateLayer("nodes", None, ogr.wkbPoint)
  File "/Library/Frameworks/GDAL.framework/Versions/1.10/Python/2.7/site-packages/osgeo/ogr.py", line 557, in CreateLayer
    return _ogr.DataSource_CreateLayer(self, *args, **kwargs)
RuntimeError: Failed to open Shapefile `r/Users/bjornnyberg/Documents/QGIS/Polygon.shp'.

Moritz Emanuel Beber

unread,
Jul 29, 2013, 9:24:24 AM7/29/13
to networkx...@googlegroups.com
Hi Bjorn,


On 07/29/2013 11:27 AM, Bjorn Nyberg wrote:
Hej,
Can anyone lead me to a good tutorial on working with shapefiles with Networkx? Namely I want to perform some shortest path algorithms on some polyline shapefiles and want to use the networks read_shp function but really need some extra help to get started! Specificially if I try the following:

import network as nx
shapefile = r'path_to_shapefile'
data = nx.read_shp(shapefile)

data.edges(data=True)[0]

((4132812.5071952357, 5195243.407249198), (4132702.507578748, 5195314.87196819), {'ShpName': 'Polygon', 'Json': '{ "type": "LineString", "coordinates": [ [ 4132812.50719523569569, 5195243.407249198295176 ], [ 4132702.507578747812659, 5195314.871968190185726 ] ] }', 'Wkb': '\x00\x00\x00\x00\x02\x00\x00\x00\x02AO\x87\xe6@\xeb\xc6\x03AS\xd1z\xda\x10^\xf1AO\x87\xaf@\xf8W%AS\xd1\x8c\xb7\xceS\xab', 'id':0, 'weight': 131, 'Wkt': 'LINESTRING (4132812.50719523569569 5195243.407249198295176,4132702.507578747812659 5195314.871968190185726)'})

In this shapefile I have a field called weight with a value of 131 on this given id of 0... How can I make a weighted edge equal to that weight or equal to the id field in a custom weight as discussed here! There is alot of additional information provided in the data=True field but all I want is essentially a [(source,target,weight)] to apply to a shortest_path algorithm in Networkx.

Don't throw away your data (unless you have limited memory) but rather look at the weight parameter in, for example, http://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.shortest_paths.generic.all_shortest_paths.html



Finally when I try to write a shapefile from the DiGraph I get a RuntimeError: Failed to open Shapefile `r/Path_To_Shapefile.shp'. Can I somehow reuse the extra parameters in the edges (i.e. wkb or wkt) and only retrain those of the shortest path when rewriting the shapefile?
It's not quite clear to me what you want to do. Do you want to limit your graph to those nodes and edges in the shortest path? Do you want to change the wkb and wkt attributes of edges in the shortest path?


Cheers,
Bjorn


Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/networkx/readwrite/nx_shp.py", line 169, in write_shp
    nodes = shpdir.CreateLayer("nodes", None, ogr.wkbPoint)
  File "/Library/Frameworks/GDAL.framework/Versions/1.10/Python/2.7/site-packages/osgeo/ogr.py", line 557, in CreateLayer
    return _ogr.DataSource_CreateLayer(self, *args, **kwargs)
RuntimeError: Failed to open Shapefile `r/Users/bjornnyberg/Documents/QGIS/Polygon.shp'.
Your RuntimeError seems to be due to a typo: `r/Users/bjornnyberg/Documents/QGIS/Polygon.shp' is probably meant to be r`/Users/bjornnyberg/Documents/QGIS/Polygon.shp'.

--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To post to this group, send email to networkx...@googlegroups.com.
Visit this group at http://groups.google.com/group/networkx-discuss.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Best,
Moritz

Daniel Schult

unread,
Jul 29, 2013, 10:21:28 AM7/29/13
to networkx...@googlegroups.com
Looks like you are mostly there.  The edgedata you show includes an attribute 'weight' (=131) which can be used by the short_path routines.  For those routines you just specify which attribute to use as an edge weight.  So yours would be 'weight'.  You can just ignore the other edge attributes.

nx.shortest_path(weight='weight')  # will find all shortest paths

For writing the file, it looks like you put the r inside the quotes.  There isn't probably a folder named "r" in your current location.


Bjorn Nyberg

unread,
Jul 30, 2013, 2:58:19 AM7/30/13
to networkx...@googlegroups.com
Ah thanks guys yes I didn't realize that the data attributes could be utilized as weights! And in my haste yesterday I did place the r inside the quotation mark by accident! 

Another question that perhaps deserves another thread - is is possible to find the shortest paths between source and target regardless of weight? i.e. if I use the example here

G=nx.Graph()
>>> G.add_path([0,1,2])
>>> G.add_path([0,10,2])
>>> print(list(nx.all_shortest_paths(G,source=0,target=2)))
[[0, 1, 2], [0, 10, 2]]

both G have a weight of 2 and thereby are printed as the shortest path... however how do i find all paths between source=0 and target=2 regardless of weight (I presume). In other words....

G=nx.Graph()
>>> G.add_path([0,1,2])
>>> G.add_path([0,10,15,2])
>>> G.add_path([0,11,8])                                                                                                         print(list(nx.all_shortest_paths(G,source=0,target=2)))

This should return the list [[0, 1, 2], [0, 10, 15,2]], unfortunately this gives me the shortest number of path segments. What is the best way to go about this as by default if weights are not specified they are given a weight of 1 and I assume if I assign the weight to 0 for all segments it would yield the desired result?

Cheers,
Bjorn

Daniel Schult

unread,
Jul 30, 2013, 3:24:29 AM7/30/13
to networkx...@googlegroups.com
That would be nx.all_simple_paths(G,s,t)

If you truly want all paths, you would have an infinite number of paths because nodes could appear twice (be revisited).  Like [0,1,0,1,0,1,2] would be a path.  Simple paths rule out reaching the same node twice in the path.
Reply all
Reply to author
Forward
0 new messages