I am trying to compare how ordered street networks are in comparison to randomly generated street networks.
As a starting point I have created a random graph with NetworkX with each node having a random location. I wanted to export this to a shapefile so that I can use it in ArcGIS. I checked the NetworkX documentattion and was elated to see that they have a write_shp() method. Their documentation mentions:
"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)."
I decided to store the location of each node as a (x,y) tuple instead of including WKT as node's attributes.
Here is the code I am using:
g=nx.fast_gnp_random_graph(15, 0.25)
#Relabel Nodes
mapping=dict(zip(g.nodes(),"ABCDEFGHIJKLMNO"))
g=nx.relabel_nodes(g, mapping)
radius=100000 #100 Km
for d in g.nodes_iter(data=True): #I know this is a round about way to do this, but I might need node attributes later
#Generate point location
t = random.random() * (math.pi * math.pi)
r = radius * math.sqrt(random.random())
x = r * math.cos(t)
y = r * math.sin(t)
co_od=(x,y)
nx.set_node_attributes(g, 'loc', {d[0]: co_od})
nx.write_shp(g, './shp/trialAgainShp')
I am getting the following error trace:
File "D:\ProgramFiles\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "D:\ProgramFiles\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/Dipto/Desktop/CSProceedings_AuthorTools_Word_2003/createGrapg.py", line 62, in <module>
nx.write_shp(g, './shp/trialAgainShp')
File "D:\ProgramFiles\Anaconda\lib\site-packages\networkx\readwrite\nx_shp.py", line 183, in write_shp
g = netgeometry(n, data)
File "D:\ProgramFiles\Anaconda\lib\site-packages\networkx\readwrite\nx_shp.py", line 157, in netgeometry
fkey = [float(x) for x in key]
ValueError: could not convert string to float: A
However, when I open the folder 'trialAgainShp', I do see shapefiles created with the name 'node', but they are empty.
I am not sure where I am going wrong
EDITS:
I tried 2 more things:
- I thought maybe the nodes have to have numeric labels to be converted to shapefiles and changing the label to alphabets was causing the failure.
I tried adding WKT to each node with the following code as the last lines of the FOR loop:
#Create WKT wkt='POINT(' + str(x) + ' ' + str(y) + ')' print wkt nx.set_node_attributes(g, 'WKT', {d[0]: wkt})
The first option does not have any effect as the error remains the same. The second changes the error to the following:
...
nx.write_shp(g, './shp/trialAgainShp')
File "D:\ProgramFiles\Anaconda\lib\site-packages\networkx\readwrite\nx_shp.py", line 183, in write_shp
g = netgeometry(n, data)
File "D:\ProgramFiles\Anaconda\lib\site-packages\networkx\readwrite\nx_shp.py", line 139, in netgeometry
elif type(key[0]).__name__ == 'tuple': # edge keys are packed tuples
TypeError: 'int' object has no attribute '__getitem__'