1

I was trying to implement the same code in Python 2.7 and Python 3.0 opening the same file from the same directory using NetworkX's read_shp function.

One, using Python 2.7 IDE and the other is using Virtualenv Python 3.0 in Django.

Python 2.7

import fiona
import networkx as nx

features = fiona.open("/home/gridlockdev/Desktop/heroku/grace/files/test/test.shp")

print len(features)
#for feat in features:
    #print feat['geometry']

G = nx.read_shp('/home/gridlockdev/Desktop/heroku/grace/files/test/test.shp')
print len(G.nodes())
print G.nodes()

print len(G.edges())
print G.edges()

Python 3.0 Django

def view_network(request):
directory  = os.path.join(settings.BASE_DIR, 'files/test/test.shp')

#Open shapefile
shp = fiona.open(directory)

if shp is None:
    print('Could not open %s' % (directory))
else:
    print('Opened %s' % (directory))

    featureCount = len(shp)
    print("Number of features in %s: %d" % (os.path.basename(directory), featureCount))

    G = nx.read_shp(directory)

    print("Number of nodes %d" % G.nodes())
    print("Number of edges %d" % G.edges())

    return render(request, 'plexus/analysis.html')

Python 2.7 would read it and output the corresponding nodes and edges. But 3.0 would not, instead this error shows up:

Internal Server Error: /network/view/

Traceback (most recent call last): File "/home/gridlockdev/Desktop/heroku/grace/env/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner response = get_response(request) File "/home/gridlockdev/Desktop/heroku/grace/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "/home/gridlockdev/Desktop/heroku/grace/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/gridlockdev/Desktop/heroku/grace/network/views.py", line 242, in view_network G = nx.read_shp(directory) File "/home/gridlockdev/Desktop/heroku/grace/env/lib/python3.5/site-packages/networkx/readwrite/nx_shp.py", line 71, in read_shp for f in lyr:

TypeError: iter() returned non-iterator of type 'Layer'

Your Answer

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

Browse other questions tagged or ask your own question.