Skip to content

Commit

Permalink
PERF: get source geometry coords as numpy array
Browse files Browse the repository at this point in the history
This avoids __getitem__ calls from the shapely geometry and converts
to numpy array and memory views within cython.
  • Loading branch information
greglucas committed Sep 14, 2022
1 parent 781b972 commit 098421f
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/cartopy/trace.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cdef bool DEBUG = False
import re
import warnings

import numpy as np
import shapely.geometry as sgeom
import shapely.prepared as sprep
from pyproj import Geod, Transformer, proj_version_str
Expand Down Expand Up @@ -383,7 +384,7 @@ cdef void bisect(double t_start, const Point &p_start, const Point &p_end,
p_current = interpolator.interpolate(t_current)


cdef void _project_segment(tuple src_from, tuple src_to,
cdef void _project_segment(double[:] src_from, double[:] src_to,
Interpolator interpolator,
object gp_domain,
double threshold, LineAccumulator lines) except *:
Expand Down Expand Up @@ -504,7 +505,7 @@ def project_linear(geometry not None, src_crs not None,
double threshold = dest_projection.threshold
Interpolator interpolator
object g_domain
object src_coords
double[:, :] src_coords
unsigned int src_size, src_idx
object gp_domain
LineAccumulator lines
Expand All @@ -513,14 +514,14 @@ def project_linear(geometry not None, src_crs not None,

interpolator = _interpolator(src_crs, dest_projection)

src_coords = geometry.coords
src_coords = np.asarray(geometry.coords)
gp_domain = sprep.prep(g_domain)

src_size = len(src_coords) # check exceptions

lines = LineAccumulator()
for src_idx in range(1, src_size):
_project_segment(src_coords[src_idx - 1], src_coords[src_idx],
_project_segment(src_coords[src_idx - 1, :], src_coords[src_idx, :],
interpolator, gp_domain, threshold, lines);

del gp_domain
Expand Down

0 comments on commit 098421f

Please sign in to comment.