GeospatialPython / pyshp

This library reads and writes ESRI Shapefiles in pure Python.
MIT License
1.1k stars 259 forks source link

Crash when closing empty writer with shapeType #188

Closed yvrig closed 3 years ago

yvrig commented 5 years ago

The following program crashes:

with shapefile.Writer(target='crash.shp', shapeType=shapefile.POLYLINEZ) as writer:
    pass

If the shapeType is removed it does not crash:

with shapefile.Writer(target='fine.shp') as writer:
    pass

The error message is:

Traceback (most recent call last):
  File "C:/Python/test.py", line 6, in <module>
    pass
  File "C:/Python/pyshp\shapefile.py", line 1101, in __exit__
    self.close()
  File "C:/Python/pyshp\shapefile.py", line 1125, in close
    self.__shapefileHeader(self.shp, headerType='shp')
  File "C:/Python/pyshp\shapefile.py", line 1299, in __shapefileHeader
    f.write(pack("<4d", zbox[0], zbox[1], mbox[0], mbox[1]))
TypeError: 'NoneType' object is not subscriptable

Thanks!

karimbahgat commented 3 years ago

Fixed now, and added tests to verify that empty shapefiles can be written for all shape types. However, in order to write empty shapefiles you also need to add at least one field, since this is required by the dbf file format.

Thanks for raising the issue!