Skip to content

Refactor shapely.prepared to use prepared base geometries#1039

Merged
jorisvandenbossche merged 1 commit intoshapely:shapely-2.0from
jorisvandenbossche:refactor-prepared-geometries
Dec 16, 2020
Merged

Refactor shapely.prepared to use prepared base geometries#1039
jorisvandenbossche merged 1 commit intoshapely:shapely-2.0from
jorisvandenbossche:refactor-prepared-geometries

Conversation

@jorisvandenbossche
Copy link
Copy Markdown
Member

In PyGEOS, the GEOSPreparedGeometry is now stored on the same python extension type (C struct) as used for the base Geometry class (and there are functions to (un)prepare a geometry object "in place"). If an object is prepared, the predicate functions will automatically use it.

So in the future, we can make this interface in Shapely easier, but on the short term, I refactored the existing shapely PreparedGeometry to use the prepared functionality of PyGEOS.
(since there is not yet an alternative for this in Shapely 1.8, so we can't deprecate this yet (if we would want that long term), and keep a working version initially in Shapely 2.0)

@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.5%) to 76.45% when pulling 8ff1f59 on jorisvandenbossche:refactor-prepared-geometries into d52edde on Toblerity:shapely-2.0.

from pygeos import STRtree
tree = STRtree([other])
idx = tree.query(self.context, predicate="contains_properly")
return bool(len(idx))
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.

I opened pygeos/pygeos#267 to add this to pygeos properly

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, and I appreciate _geom and __geom__ have been cleaned up too, hopefully no downstream issues may rise from this.

I did a quick "timeit" comparison using:

from shapely.geometry import Point, Polygon
from shapely.prepared import prep
triangle = Polygon(((0.0, 0.0), (1.0, 1.0), (1.0, -1.0)))
pt = Point(0.5, 0.5)
%timeit prep(triangle).intersects(pt)

and I see a minor slowdown from ~ 5 µs to 9 µs, which is acceptable. The first of these loops takes several times longer, which confirms that PyGEOS does some in-place caching of the prepared geometry.

This is also the last use of delegated, so it could be considered to be cleaned up soon. And I suppose the whole impl module could be cleaned up too.

@sgillies
Copy link
Copy Markdown
Contributor

Yes, we're all in on GEOS now, so impl.py can be deleted.

@jorisvandenbossche jorisvandenbossche merged commit 5b4d3a1 into shapely:shapely-2.0 Dec 16, 2020
@jorisvandenbossche jorisvandenbossche deleted the refactor-prepared-geometries branch December 16, 2020 12:10
@jorisvandenbossche
Copy link
Copy Markdown
Member Author

OK, next PR to remove impl !

jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request Apr 5, 2021
jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request Apr 5, 2021
jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request May 27, 2021
jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request May 27, 2021
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
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.

4 participants