TypeError:iter()返回类型为' Layer'的非迭代器。对于Python 3

时间:2017-12-08 13:25:44

标签: python python-3.x python-2.7

这与我在GIS stackexchange中的其他question有关。

为什么在Python 3中引发TypeError,实际上在Python 2中运行的相同代码执行时没有错误?

代码:

net = nx.DiGraph()
print(path)
shp = ogr.Open(path)
for lyr in shp:
    print (type(lyr))
    print (lyr)
    fields = [x.GetName() for x in lyr.schema]
    print (lyr.schema)
    print(fields)
    for f in lyr:
        print(f)

错误

  

文件" /home/gridlockdev/Desktop/heroku/grace/env/lib/python3.5/site-packages/networkx/readwrite/nx_shp.py",第81行,在read_shp中       对于ly中的f:   TypeError:iter()返回类型' Layer'

的非迭代器

1 个答案:

答案 0 :(得分:0)

将networkx导入为nx 作者 =“”“Ben Reilly(benwreilly@gmail.com)”“” 全部 = ['read_shp','write_shp']

[docs] def read_shp(path,simplify = True):     msgstr“”“从shapefile生成networkx.DiGraph。点几何是     翻译成节点,线条变成边缘。坐标元组用作     键。保留属性,将线几何简化为开始     和结束坐标。接受单个shapefile或许多目录     shape文件。

"The Esri Shapefile or simply a shapefile is a popular geospatial vector
data format for geographic information systems software [1]_."

Parameters
----------
path : file or string
   File, directory, or filename to read.

simplify:  bool
    If ``True``, simplify line geometries to start and end coordinates.
    If ``False``, and line feature geometry has multiple segments, the 
    non-geometric attributes for that feature will be repeated for each 
    edge comprising that feature.

Returns
-------
G : NetworkX graph

Examples
--------
>>> G=nx.read_shp('test.shp') # doctest: +SKIP

References
----------
.. [1] http://en.wikipedia.org/wiki/Shapefile
"""
try:
    from osgeo import ogr
except ImportError:
    raise ImportError("read_shp requires OGR: http://www.gdal.org/")

if not isinstance(path, str):
    return

net = nx.DiGraph()
shp = ogr.Open(path)
for lyr in shp:
    fields = [x.GetName() for x in lyr.schema]
    for f in lyr:
        flddata = [f.GetField(f.GetFieldIndex(x)) for x in fields]
        g = f.geometry()
        attributes = dict(zip(fields, flddata))
        attributes["ShpName"] = lyr.GetName()
        if g.GetGeometryType() == 1:  # point
            net.add_node((g.GetPoint_2D(0)), attributes)
        if g.GetGeometryType() == 2:  # linestring
            last = g.GetPointCount() - 1
            if simplify:
                attributes["Wkb"] = g.ExportToWkb()
                attributes["Wkt"] = g.ExportToWkt()
                attributes["Json"] = g.ExportToJson()
                net.add_edge(g.GetPoint_2D(0), g.GetPoint_2D(last), attributes)
            else:
                # separate out each segment as individual edge
                for i in range(last):
                    pt1 = g.GetPoint_2D(i)
                    pt2 = g.GetPoint_2D(i + 1)
                    segment = ogr.Geometry(ogr.wkbLineString)
                    segment.AddPoint_2D(pt1[0], pt1[1])
                    segment.AddPoint_2D(pt2[0], pt2[1])
                    attributes["Wkb"] = segment.ExportToWkb()
                    attributes["Wkt"] = segment.ExportToWkt()
                    attributes["Json"] = segment.ExportToJson()
                    net.add_edge(pt1, pt2, attributes)

return net