From 71d535e84532fe7bcd40e2e09dd6aaafa990d3f5 Mon Sep 17 00:00:00 2001 From: Al Date: Thu, 17 Nov 2016 17:38:54 -0500 Subject: [PATCH] [polygons] using try/except in polygons --- scripts/geodata/polygons/index.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/scripts/geodata/polygons/index.py b/scripts/geodata/polygons/index.py index 89fac217..3129fc8e 100644 --- a/scripts/geodata/polygons/index.py +++ b/scripts/geodata/polygons/index.py @@ -168,10 +168,18 @@ class PolygonIndex(object): holes = [(lon + 360.0 if lon < 0 else lon, lat) for lon, lat in holes] poly = Polygon(coords, holes) - if cls.fix_invalid_polygons and not poly.is_valid and not poly.contains(poly.centroid): - poly_fix = cls.fix_polygon(poly) - if poly_fix is not None and poly_fix.bounds and len(poly_fix.bounds) == 4 and poly_fix.is_valid and (poly_fix.contains(poly.centroid) or poly_fix.contains(poly_fix.representative_point())): - poly = poly_fix + try: + invalid = cls.fix_invalid_polygons and not poly.is_valid and not poly.contains(poly.centroid) + except Exception: + invalid = True + + if invalid: + try: + poly_fix = cls.fix_polygon(poly) + if poly_fix is not None and poly_fix.bounds and len(poly_fix.bounds) == 4 and poly_fix.is_valid and (poly_fix.contains(poly.centroid) or poly_fix.contains(poly_fix.representative_point())): + poly = poly_fix + except Exception: + poly = None return poly