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

时间:2021-04-08 11:19:53

标签: python shapefile shapely

我用这样的 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

1 个答案:

答案 0 :(得分:0)

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