1

I'm trying to convert a shapefile of only line geometry (a street network) to a NetworkX graph to run some connectivity algorithms on it. I'm running into this issue:

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-    
packages/networkx/readwrite/nx_shp.py", line 69, in read_shp
for lyr in shp:
TypeError: 'NoneType' object is not iterable

It seems to be an issue with the ogr module the fcn requires, but I believe (?) I downloaded it, and it doesn't raise the exception error written in the source code. Any ideas?

EDIT: here's my code snippet: import networkx as nx

print 'nx imported'
G=nx.DiGraph()
G=nx.read_shp('shpfile.shp', \
              simplify = False)
print "running fine"

It gets stuck at the read_shp() and doesn't run the print command at the end to confirm it's running correctly.

2
  • 1
    Can you provide a code example of how you are getting that error? – Evil Genius Feb 11 '16 at 15:05
  • it's just a short little piece of code to see if it's working, I just edited it into the main post. – jdg Feb 11 '16 at 17:10
3

It seem to me that you don't understand the principles of Nextwokx (see nx_spatial does not read all shapefile features)

Networkx generate a networkx.DiGraph from the shapefile, with nodes and edges and only these nodes and edges are iterables, not the Graph

import networkx as nx
G = nx.read_shp('mylines.shp')
print(G.nodes())
[(1.0, 2.0), (3.0, 2.0), (0.0, 0.0), (3.0, 1.0), (4.0, 4.0), (2.0, 1.0), (2.0, 4.0), (1.0, 3.0), (2.0, 3.0), (1.0, 4.0), (4.0, 3.0), (4.0, 2.0), (3.0, 4.0), (1.0, 1.0)]
print(G.edges())
[((1.0, 2.0), (1.0, 1.0)), ((3.0, 2.0), (2.0, 1.0)), ((3.0, 1.0), (2.0, 1.0)), ((4.0, 4.0), (3.0, 4.0)), ((2.0, 1.0), (1.0, 1.0)), ((2.0, 4.0), (2.0, 3.0)), ((1.0, 3.0), (1.0, 2.0)), ((2.0, 3.0), (1.0, 2.0)), ((1.0, 4.0), (1.0, 3.0)), ((4.0, 3.0), (4.0, 2.0)), ((4.0, 2.0), (3.0, 2.0)), ((3.0, 4.0), (2.0, 3.0)), ((1.0, 1.0), (0.0, 0.0))]

Therefore, the error for lyr in shp TypeError: 'NoneType' object is not iterable is normal.

But you don't need to browse anything, use directly the resulting DiGraph

enter image description here

1
  • I am trying to use Networkx for analyzing some networks that I created in QGIS represented by one lines shapefile and one points shapefile. I am a relative newbie so I apologize if my question is trivial, but I don't fully understand what you mean by "only nodes and edges are iterables, not the Graph" and "use directly the resulting DiGraph". How can I use the resulting Graph if I get the same TypeError presented in the original question? – M Taglia May 18 '18 at 15:01

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.