Skip to content

Refactor shapely.ops to not use lgeos#1065

Merged
jorisvandenbossche merged 7 commits intoshapely:shapely-2.0from
jorisvandenbossche:refactor-ops
Jul 7, 2021
Merged

Refactor shapely.ops to not use lgeos#1065
jorisvandenbossche merged 7 commits intoshapely:shapely-2.0from
jorisvandenbossche:refactor-ops

Conversation

@jorisvandenbossche
Copy link
Copy Markdown
Member

Replacing direct lgeos calls with pygeos python functions in the shapely.ops module.

The remaining ones to do are polygonize, polygonize_full and nearest_points. For polygonize I already have a PR in pygeos, the others still need to be added there.

@coveralls
Copy link
Copy Markdown

coveralls commented Apr 5, 2021

Coverage Status

Coverage decreased (-0.7%) to 77.151% when pulling d7e54b8 on jorisvandenbossche:refactor-ops into 62923e2 on Toblerity:shapely-2.0.

@jorisvandenbossche
Copy link
Copy Markdown
Member Author

This is ready now: it fully removed all remaining usage of ctypes/lgeos from ops.py (and then remaining usage is in strtree.py)

The points are returned in the same order as the input geometries.
"""
seq = lgeos.methods['nearest_points'](g1._geom, g2._geom)
seq = pygeos.shortest_line(g1, g2)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was about to ask about why nearest_points wasn't in PyGEOS, but then I see pygeos/pygeos#334

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we will need to decide on whether we want to expose both versions top-level in Shapely 2.0 (but that doesn't need to be done in this PR)

Copy link
Copy Markdown
Member

@mwtoews mwtoews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good! Just wait to see if to see if @sgillies wants to weigh-in on raising GEOSException vs a different exception.

shapely/ops.py Outdated
if tolerance:
errstr += " Try running again with default tolerance value."
raise ValueError(errstr)
raise pygeos.GEOSException(errstr) from err
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK with exposing GEOSException, but I thinks @sgillies has preferred raising a Shapely error instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this specific case we have easy control over it (since we are catching the original GEOSException anyway to add some context to the error message), but the general issue of raising GEOSException to the user or not is a broader discussion, and some discussion is in #991

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I very much prefer to raise an error that derives from ShapelyError (https://github.com/Toblerity/Shapely/blob/master/shapely/errors.py#L4). How about a VoronoiDiagramError ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that GEOSException is "our own exception class", just like ShapelyError is (once pygeos is incorporated into shapely).

I would prefer if we can then revive the discussion in #991, which exactly deals with this topic.
I could certainly create a custom VoronoiDiagramError for this case (to get this PR merged), but it would then be adding a special case, since most other operations raise GEOSExceptions in the shapely-2.0 branch. So personally I would rather first get to conclusion for the general discussion.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping here. Is it OK to keep this discussion for #991, and move forward with this PR?

I will keep the current ValueError instead of changing it to GEOSException, then this PR is just the status quo on that aspect.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorisvandenbossche jorisvandenbossche merged commit 3f4e2eb into shapely:shapely-2.0 Jul 7, 2021
@jorisvandenbossche jorisvandenbossche deleted the refactor-ops branch July 7, 2021 06:04
jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request Aug 20, 2021
jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request Sep 30, 2021
jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request Sep 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants