-
Notifications
You must be signed in to change notification settings - Fork 565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Add polygonhull_simplify exposing PolygonHullSimplifier #2050
base: main
Are you sure you want to change the base?
Conversation
@jorisvandenbossche I hope I’m not interrupting you at this time. It's my first PR and I want to know what do I need to do with this PR. 😄 |
shapely/constructive.py
Outdated
parameter : float or array_like | ||
Larger values produce less concave results. A value of 1 produces | ||
the convex hull; a value of 0 produces the original geometry. | ||
parameter_mode : str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A question is whether this shall be called tolerance
like in simplify
but that is more up to a discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I think parameter
can be replaced by tolerance
to be consistent with simplify
.
np.bool_(is_outer), | ||
np.intc(parameter_mode), | ||
np.double(parameter), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for casting to numpy dtypes here? None of the other functions require that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I casting numpy dtypes is that the tests in CI/CD workflow failed in ubuntu 3.11/3.12 and macos 3.11/3.12 Env as below. So I cast parameters to numpy dtypes.
E TypeError: ufunc 'simplify_polygon_hull' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
shapely/constructive.py
Outdated
Larger values produce less concave results. A value of 1 produces | ||
the convex hull; a value of 0 produces the original geometry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually correct only for area mode. In the vertex mode, it is the vice versa. 0 is a convex_hull, while 1 is original geometry (as in PostGIS). That can be a bit confusing. I would recommend keeping both the same, probably following the vertex mode and PostGIS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it is convex hull only if is_outer=True
, otherwise it is the largest triangle within the geom (I think).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's easy to misunderstand. The explanation should be:
Two parameters are supported:
- Vertext Mode: the tolerance means Vertex Number fraction. The fraction of the input vertices retained in the result. Value 1 produces the original geometry. Smaller values produce less concave results. For outer hulls, value 0 produces the convex hull (with triangles for any holes). For inner hulls, value 0 produces a triangle (if no holes are present). The value should be in the range [0,1].
- Area Mode: the tolerance means Area Delta ratio. The ratio of the change in area to the input area. Value 0 produces the original geometry. Larger values produce less concave results. The value must be 0 or greater.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parameter is in range [0, 1] for vertex mode and in range [0, ∞) for area mode.
a = shapely.Polygon([[0,0], [100,0], [200, 500], [100, 10], [0, 10]])
b = shapely.simplify_polygon_hull(a, 1, "area", True)
c =shapely.simplify_polygon_hull(a, 10, "area", True)
d = shapely.simplify_polygon_hull(a, 99, "area", True)
print(a.area, b.area, c.area, d.area) # 1500.0 1500.0 1500.0 26000.0
I can keep both same by limit the tolerance in the range [0, 1] like
if parameter_mode == "area":
parameter_mode = 2
tolerance = 1-tolerance if tolerance < 1 else 0
elif parameter_mode == "vertex":
parameter_mode = 1
However, it will limit result in area mode as the area delta ratio couldn't be larger than 1 in this case. It's hard to keep both same.😟
Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net>
Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net>
xref #1570
GEOS introduces a GEOSPolygonHullSimplify and GEOSPolygonHullSimplifyMode in v3.11. GEOSPolygonHullSimplifyMode adds an extra parameter named parameterMode, when parameterMode equals 1 it works same as GEOSPolygonHullSimplify.
This PR adds a shapely binding of GEOSPolygonHullSimplifyMode.