[go: up one dir, main page]

Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

aloi214
Copy link
@aloi214 aloi214 commented May 13, 2024

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.

@aloi214
Copy link
Author
aloi214 commented May 28, 2024

@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 Show resolved Hide resolved
shapely/constructive.py Outdated Show resolved Hide resolved
shapely/constructive.py Outdated Show resolved Hide resolved
@aloi214 aloi214 requested a review from martinfleis May 31, 2024 06:43
shapely/constructive.py Outdated Show resolved Hide resolved
Comment on lines 942 to 945
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
Copy link
Contributor

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.

Copy link
Author

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.

shapely/constructive.py Outdated Show resolved Hide resolved
Comment on lines +973 to +975
np.bool_(is_outer),
np.intc(parameter_mode),
np.double(parameter),
Copy link
Contributor

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.

Copy link
Author
@aloi214 aloi214 Jun 4, 2024

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''

Comment on lines 943 to 944
Larger values produce less concave results. A value of 1 produces
the convex hull; a value of 0 produces the original geometry.
Copy link
Contributor

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.

Copy link
Contributor

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).

Copy link
Author
@aloi214 aloi214 Jun 4, 2024

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.

Copy link
Author

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.😟

aloi214 and others added 3 commits June 4, 2024 14:35
Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net>
Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net>
@aloi214 aloi214 requested a review from martinfleis June 13, 2024 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants