【问题标题】:TypeError: iter() returned non-iterator of type 'Layer' for Python 3TypeError:iter() 为 Python 3 返回了“层”类型的非迭代器
【发布时间】:2017-12-08 13:25:43
【问题描述】:

这与我在 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 对于 lyr 中的 f: TypeError: iter() 返回了“Layer”类型的非迭代器

【问题讨论】:

  • 与NetworkX 开发人员交谈,这是他们的错误(或功能)。

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


【解决方案1】:

将networkx导入为nx 作者 = """本·赖利 (benwreilly@gmail.com)""" 全部 = ['read_shp', 'write_shp']

[文档]def read_shp(路径,简化=真): """从 shapefile 生成 networkx.DiGraph。点几何是 翻译成节点,线翻译成边。坐标元组用作 键。属性被保留,线几何被简化为开始 和结束坐标。接受单个 shapefile 或多个目录 形状文件。

"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

【讨论】:

  • 正确的格式和一些解释在这里会很有用。
猜你喜欢
  • 2017-04-16
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 2015-09-06
  • 2018-02-05
  • 1970-01-01
  • 2017-03-10
  • 1970-01-01
相关资源
最近更新 更多