From d02b6fae88bdef48f071a034edd5cb1a2351aef2 Mon Sep 17 00:00:00 2001 From: Oliver Tonnhofer Date: Tue, 17 Dec 2019 11:57:25 +0100 Subject: [PATCH] handle empty geometries when creating multipolygon/linestring/points --- admin_clipper.py | 11 ++++++++--- setup.py | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/admin_clipper.py b/admin_clipper.py index 20afe14..4ad256d 100644 --- a/admin_clipper.py +++ b/admin_clipper.py @@ -1,4 +1,3 @@ - # coding: utf-8 import sys import json @@ -26,16 +25,22 @@ def transform_bbox(transf, bbox): return min(xs), min(ys), max(xs), max(ys) def as_multipolygon(p): + if not p: + return [] if p.type == 'MultiPolygon': return p return MultiPolygon([p]) def as_multilinestring(l): + if not l: + return [] if l.type == 'MultiLineString': return l return MultiLineString([l]) def as_multipoint(l): + if not l: + return [] if l.type == 'MultiPoint': return l return MultiPoint([l]) @@ -137,9 +142,8 @@ def main(): clip_features = clipsrc.items(bbox=bbox_clip) clip_geom = as_multipolygon(cascaded_union([asShape(f['geometry']) for i, f in clip_features])) - clip_geom = transform(proj_clip_to_src, clip_geom) - if clip_geom.is_empty: + if not clip_geom or clip_geom.is_empty: result_features.append({ 'type': 'Feature', 'properties': src_feature['properties'], @@ -147,6 +151,7 @@ def main(): }) continue + clip_geom = transform(proj_clip_to_src, clip_geom) # Buffer the clipping geometry clip_geom_buffered = clip_geom.buffer(clip_buffer) diff --git a/setup.py b/setup.py index 599e4c1..8dfab70 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name='admin_clipper', - version='0.1.1', + version='0.1.2', description='Clip administrative boundaries at coastlines.', long_description='', author='Omniscale',