From bed35761107fe83066bd1abb5cc44b75bae4ab5b Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Thu, 8 Dec 2022 11:03:29 -0700 Subject: [PATCH] PERF: Avoid create/destroy shapely geometry With Shapely 2.0 we can avoid the create/destroy point for checking whether a point is within a geometry. --- lib/cartopy/trace.pyx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/cartopy/trace.pyx b/lib/cartopy/trace.pyx index 1a80c4381c..6281d45235 100644 --- a/lib/cartopy/trace.pyx +++ b/lib/cartopy/trace.pyx @@ -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 @@ -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