Skip to content

Commit

Permalink
Fix #1373 -- Ignore empty geometries for SHP export (#1439)
Browse files Browse the repository at this point in the history
* Fix #1373 -- Ignore empty geometries for SHP export

* Add test for `geometry=None`
  • Loading branch information
oliverroick authored and amplifi committed May 17, 2017
1 parent e1e2179 commit 195f0d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cadasta/organization/download/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ def write_features(self, ds, filename):
layers = {}

for su in spatial_units:
# Excluding empty geometries from export
if not su.geometry:
continue

geom = ogr.CreateGeometryFromWkt(su.geometry.wkt)
layer_type = geom.GetGeometryName().lower()
layer = layers.get(layer_type, None)
Expand Down
15 changes: 15 additions & 0 deletions cadasta/organization/tests/test_downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,14 @@ def test_write_features(self):
'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),'
'((15 5, 40 10, 10 20, 5 10, 15 5)))',
attributes={'geom_type': 'multipolygon'})
su7 = SpatialUnitFactory.create(
project=project,
geometry='SRID=4326;POLYGON EMPTY',
attributes={'geom_type': 'empty'})
su8 = SpatialUnitFactory.create(
project=project,
geometry=None,
attributes={'geom_type': 'none'})

dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file4')
ds = exporter.create_datasource(dst_dir)
Expand All @@ -430,6 +438,9 @@ def test_write_features(self):
feature = layer.GetNextFeature()
assert geom.equals(GEOSGeometry(feature.geometry().ExportToWkt()))
assert feature.GetFieldAsString('id') == su.id

# Ensuring empty polygons are not added to shape
assert su.id not in [su7.id, su8.id]
ds.Destroy()

with open(filename) as csvfile:
Expand All @@ -449,6 +460,10 @@ def test_write_features(self):
assert row == [su5.id, su5.type, 'multilinestring']
if i == 6:
assert row == [su6.id, su6.type, 'multipolygon']
if i == 7:
assert row == [su7.id, su7.type, 'empty']
if i == 8:
assert row == [su8.id, su8.type, 'none']

# remove this so other tests pass
os.remove(filename)
Expand Down

0 comments on commit 195f0d8

Please sign in to comment.