From df922e9ca7bc9f5be9099d56dd0d910da5ffc6cd Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Tue, 30 May 2023 23:14:56 -0700 Subject: [PATCH] Force polygons into validity during GIS import. Not sure why, but some polygons become invalid (which usually means a self-intersecting "polygon", such as two triangles that meet at a point) during this transformation. Shapely includes a tool to reshape polygons into validity, so use that. --- resources/tools/arcgis_landmap_import.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/resources/tools/arcgis_landmap_import.py b/resources/tools/arcgis_landmap_import.py index 5c007d93..d4b20af4 100644 --- a/resources/tools/arcgis_landmap_import.py +++ b/resources/tools/arcgis_landmap_import.py @@ -8,6 +8,7 @@ from pathlib import Path from pyproj import CRS, Transformer from shapefile import Reader, Shape +from shapely import validation from shapely.geometry import LineString, MultiPolygon, Polygon, shape from shapely.ops import unary_union @@ -46,11 +47,13 @@ class CoordinateConverter: for poly in polys: for boundary, holes in self._boundary_and_holes_of(poly): new_polys.append( - Polygon( - self._convert_line_to_dcs_coords(boundary), - holes=[ - self._convert_line_to_dcs_coords(hole) for hole in holes - ], + validation.make_valid( + Polygon( + self._convert_line_to_dcs_coords(boundary), + holes=[ + self._convert_line_to_dcs_coords(hole) for hole in holes + ], + ) ) ) return new_polys