Search code examples
pythonshapefileshapely

TypeError: 'LineString' object is not iterable


I create a dictionary with shapes from a shapefile like that

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

Then when I try to save the shape to a new shapefile like below, I get the TypeError at w.line()

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

The error is:

  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

Solution

  • The issue was that the line method requires list of points which I overlooked in the documentation and instead tried to provide the ready LineString object