Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f2f91ce

Browse files
committed
Fix error with writing empty z/m shapefile
Also added tests to verify this for all shapetypes. Fixes #188
1 parent dcc77fd commit f2f91ce

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

shapefile.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,13 +1537,19 @@ def __shapefileHeader(self, fileObj, headerType='shp'):
15371537
if self.shapeType in (11,13,15,18):
15381538
# Z values are present in Z type
15391539
zbox = self.zbox()
1540+
if zbox is None:
1541+
# means we have empty shapefile/only null geoms (see commentary on bbox above)
1542+
zbox = [0,0]
15401543
else:
15411544
# As per the ESRI shapefile spec, the zbox for non-Z type shapefiles are set to 0s
15421545
zbox = [0,0]
15431546
# Measure
15441547
if self.shapeType in (11,13,15,18,21,23,25,28,31):
15451548
# M values are present in M or Z type
15461549
mbox = self.mbox()
1550+
if mbox is None:
1551+
# means we have empty shapefile/only null geoms (see commentary on bbox above)
1552+
mbox = [0,0]
15471553
else:
15481554
# As per the ESRI shapefile spec, the mbox for non-M type shapefiles are set to 0s
15491555
mbox = [0,0]

test_shapefile.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,17 @@ def test_write_geojson(tmpdir):
492492
assert json.dumps(feat.__geo_interface__)
493493
assert json.dumps(r.shapeRecords().__geo_interface__)
494494
assert json.dumps(r.__geo_interface__)
495+
496+
shape_types = [k for k in shapefile.SHAPETYPE_LOOKUP.keys() if k != 31] # exclude multipatch
497+
498+
@pytest.mark.parametrize("shape_type", shape_types)
499+
def test_write_empty_shapefile(tmpdir, shape_type):
500+
"""
501+
Assert that can write an empty shapefile
502+
"""
503+
filename = tmpdir.join("test").strpath
504+
with shapefile.Writer(filename, shapeType=shape_type) as w:
505+
w.field('field1', 'C') # required to create a valid dbf file
506+
507+
with shapefile.Reader(filename) as r:
508+
assert r.shapeType == shape_type

0 commit comments

Comments
 (0)