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'.