这与我在GIS中的另一个question有关。
为什么TypeError在Python3中被引发,而实际上在Python2中运行的相同代码在没有错误的情况下被执行呢?
代码:
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行,在lyr中f的read_shp中: TypeError: TypeError()返回类型为“Layer”的非迭代器
发布于 2017-12-08 13:42:36
将networkx导入为nx =“Ben Reilly (benwreilly@gmail.com)”“all = 'read_shp','write_shp‘
docsdef read_shp(path,simplify=True):“从shapefile生成一个networkx.DiGraph。点几何图形被转换成节点,线被转换成边缘。坐标元组被用作键。属性被保留,行几何图形被简化为开始和结束坐标。接受一个单一的shapefile或多个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 nethttps://stackoverflow.com/questions/47715318
复制相似问题