类型错误:“LineString”对象不可迭代

问题描述

我用这样的 shapefile 中的形状创建了一个字典

sfWholeStreets = shapefile.Reader(inputFilename)
shapesWholeStreets = sfWholeStreets.shapes()
recordsWholeStreets = sfWholeStreets.records()

recordindex = 0
for record in recordsWholeStreets:
    streetName = record[1]
    featureWholeStreet = sfWholeStreets.shapeRecords()[recordindex].shape.__geo_interface__
    linestringShapeWholeStreets = shapely.geometry.shape(feature)
    if streetName in streetDictionary:
        streetDictionary[streetName].append(record)
        streetShapeDictionaryWholeStreets[streetName].append(linestringShapeWholeStreets)
    else:
        streetDictionary[streetName] = [record]
        streetShapeDictionaryWholeStreets[streetName] = [linestringShapeWholeStreets]
    recordindex = recordindex + 1

然后,当我尝试将形状保存到如下所示的新 shapefile 时,我在 w.line() 处收到 TypeError

for record in recordsWholeStreets:
    w.line(streetShapeDictionaryWholeStreets[record['Name']])

错误是:

  File "C:/dev/PycharmProjects/myProjectt/main.py",line 429,in <module>
    w.line(streetShapeDictionaryWholeStreets[record['Name']])
  File "C:\OSGeo4W64\apps\python37\lib\site-packages\shapefile.py",line 1977,in line
    self._shapeparts(parts=lines,shapeType=shapeType)
  File "C:\OSGeo4W64\apps\python37\lib\site-packages\shapefile.py",line 2066,in _shapeparts
    for point in part:
TypeError: 'Linestring' object is not iterable

解决方法

问题是 line 方法需要我在文档中忽略的点列表,而是尝试提供准备好的 LineString 对象