Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jgriesfeller committed Jan 13, 2025
2 parents aed1e38 + b54cdb7 commit b4d1b75
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 12 additions & 1 deletion geojsoncontour/utilities/multipoly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ def create_grid_data():
Z = numpy.sqrt(X*X + Y*Y)
return latrange, lonrange, Z

def test_orientation_order_gh31(self):
# Flipping x should still result in CCW orientation
# of final polygon
x = numpy.linspace(0, 10, 14)[::-1]
y = numpy.linspace(10, 20, 15)
x, y = numpy.meshgrid(x, y)
z = numpy.sin(x) * numpy.cos(y)
contourf = plt.contourf(x, y, z)
mp = geojsoncontour.contourf_to_geojson(contourf, ndigits=3)

class ContourPlotConfig(object):
def __init__(self, level_lower=0.0, level_upper=100.0, colormap=plt.cm.jet, unit=''): # jet, jet_r, YlOrRd, gist_rainbow
Expand Down

0 comments on commit b4d1b75

Please sign in to comment.