Skip to content

Commit 6c40931

Browse files
mrocklinjorisvandenbossche
authored andcommitted
Prepare geometries ourselves
1 parent cb5f53c commit 6c40931

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

geopandas/_geos.pxi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ cdef extern from "geos_c.h":
5656
char GEOSPreparedTouches_r(GEOSContextHandle_t, const GEOSPreparedGeometry*, const GEOSGeometry*) nogil
5757
char GEOSPreparedWithin_r(GEOSContextHandle_t, const GEOSPreparedGeometry*, const GEOSGeometry*) nogil
5858

59-
GEOSPreparedGeometry *GEOSPrepare_r(GEOSContextHandle_t handle, const GEOSGeometry* g) nogil
59+
GEOSPreparedGeometry *GEOSPrepare_r(GEOSContextHandle_t, const GEOSGeometry*) nogil
60+
GEOSPreparedGeometry *GEOSPrepare(const GEOSGeometry*)
61+
void GEOSPreparedGeom_destroy_r(GEOSContextHandle_t, const GEOSPreparedGeometry* ) nogil
62+
void GEOSPreparedGeom_destroy(const GEOSPreparedGeometry* )
6063

6164
char GEOSHasZ(const GEOSGeometry*) nogil
6265
char GEOSHasZ_r(GEOSContextHandle_t, GEOSGeometry *) nogil

geopandas/vectorized.pyx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ cpdef points_from_xy(np.ndarray[double, ndim=1, cast=True] x,
163163
@cython.boundscheck(False)
164164
@cython.wraparound(False)
165165
cpdef prepared_binary_predicate(str op,
166-
np.ndarray[np.uintp_t, ndim=1, cast=True] geoms,
167-
object other):
166+
np.ndarray[np.uintp_t, ndim=1, cast=True] geoms,
167+
object other):
168168
"""
169169
Apply predicate to a GeometryArray and an individual shapely object
170170
@@ -185,6 +185,7 @@ cpdef prepared_binary_predicate(str op,
185185
cdef Py_ssize_t idx
186186
cdef GEOSContextHandle_t handle
187187
cdef GEOSGeometry *geom
188+
cdef uintptr_t other_pointer
188189
cdef GEOSGeometry *other_geom
189190
cdef GEOSPreparedGeometry *prepared_geom
190191
cdef GEOSPreparedPredicate predicate
@@ -193,16 +194,10 @@ cpdef prepared_binary_predicate(str op,
193194
cdef np.ndarray[np.uint8_t, ndim=1, cast=True] out = np.empty(n, dtype=np.bool_)
194195

195196
handle = get_geos_context_handle()
196-
other_geom = <GEOSGeometry *> other.__geom__
197-
198-
# Prepare the geometry if it hasn't already been prepared.
199-
# TODO: why can't we do the following instead?
200-
# prepared_geom = GEOSPrepare_r(handle, other_geom)
201-
if not isinstance(other, shapely.prepared.PreparedGeometry):
202-
other = shapely.prepared.prep(other)
197+
other_pointer = <np.uintp_t> other.__geom__
198+
other_geom = <GEOSGeometry *> other_pointer
203199

204-
geos_handle = get_geos_context_handle()
205-
prepared_geom = geos_from_prepared(other)
200+
prepared_geom = GEOSPrepare_r(handle, other_geom)
206201

207202
predicate = get_prepared_predicate(op)
208203

@@ -214,8 +209,10 @@ cpdef prepared_binary_predicate(str op,
214209
else:
215210
out[idx] = 0
216211

212+
GEOSPreparedGeom_destroy_r(handle, prepared_geom)
217213
return out
218214

215+
219216
cdef GEOSPreparedPredicate get_prepared_predicate(str op) except NULL:
220217
if op == 'contains':
221218
func = GEOSPreparedContains_r
@@ -413,8 +410,8 @@ cpdef unary_predicate(str op, np.ndarray[np.uintp_t, ndim=1, cast=True] geoms):
413410
@cython.boundscheck(False)
414411
@cython.wraparound(False)
415412
cpdef binary_predicate(str op,
416-
np.ndarray[np.uintp_t, ndim=1, cast=True] geoms,
417-
object other):
413+
np.ndarray[np.uintp_t, ndim=1, cast=True] geoms,
414+
object other):
418415
"""
419416
Apply predicate to an array of GEOSGeometry pointers and a shapely object
420417

0 commit comments

Comments
 (0)