From 64ce5be14afd33d05d9d5ef42af2e376f936e1a2 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Wed, 8 Jan 2025 17:59:43 +0100 Subject: [PATCH] Set winding order depending on first polygon --- geojsoncontour/utilities/multipoly.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/geojsoncontour/utilities/multipoly.py b/geojsoncontour/utilities/multipoly.py index 961a2ab..11c4dd8 100644 --- a/geojsoncontour/utilities/multipoly.py +++ b/geojsoncontour/utilities/multipoly.py @@ -40,6 +40,12 @@ def orientation(vertices) -> Orientation: def multi_polygon(path, min_angle_deg, ndigits): + # It seems matplotlib emits polygons in either CW or CCW order. + # We detect which order the first polygon has, and uses this + # as the ring, with polygons of the other winding order as + # holes. If order is reversed compared to the conventions, + # we reverse the order of the polygon so rings have CCW order. + orientation_for_keep = None polygons = [] for linestring in path.to_polygons(): if min_angle_deg: @@ -48,7 +54,12 @@ def multi_polygon(path, min_angle_deg, ndigits): linestring = np.around(linestring, ndigits) handedness = orientation(linestring) - if handedness == Orientation.CCW: + if len(polygons) == 0: + orientation_for_keep = handedness + if orientation_for_keep != Orientation.CCW: + linestring = linestring[::-1, :] + + if handedness == orientation_for_keep: polygons.append([linestring.tolist()]) else: # This is a hole, which we assume belong