Skip to content

Commit

Permalink
PERF: Avoid create/destroy shapely geometry
Browse files Browse the repository at this point in the history
With Shapely 2.0 we can avoid the create/destroy point for checking
whether a point is within a geometry.
  • Loading branch information
greglucas committed Dec 8, 2022
1 parent 428d90b commit bed3576
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/cartopy/trace.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import re
import warnings

import numpy as np
import shapely
import shapely.geometry as sgeom
import shapely.prepared as sprep
from pyproj import Geod, Transformer, proj_version_str
Expand Down Expand Up @@ -241,10 +242,13 @@ cdef State get_state(const Point &point, object gp_domain):
cdef State state

if isfinite(point.x) and isfinite(point.y):
# TODO: Avoid create-destroy
g_point = sgeom.Point((point.x, point.y))
state = POINT_IN if gp_domain.covers(g_point) else POINT_OUT
del g_point
if shapely.__version__ >= "2":
# Shapely 2.0 doesn't need to create/destroy a point
state = POINT_IN if shapely.intersects_xy(gp_domain.context, point.x, point.y) else POINT_OUT
else:
g_point = sgeom.Point((point.x, point.y))
state = POINT_IN if gp_domain.covers(g_point) else POINT_OUT
del g_point
else:
state = POINT_NAN
return state
Expand Down

0 comments on commit bed3576

Please sign in to comment.