Skip to content

Commit

Permalink
MNT: Add a cache to the Transformer creation in transform_points
Browse files Browse the repository at this point in the history
This adds a cache mechanism to creating Transformer objects, which
can be slow when called frequently. Additionally, this requires
adding attributes to the Mercator creation as the x/y limits are
created in the initializer which uses the cache/hash function.
  • Loading branch information
greglucas committed Nov 13, 2021
1 parent 5cfb76b commit 457f4b9
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/cartopy/crs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from abc import ABCMeta
from collections import OrderedDict
from functools import lru_cache
import io
import json
import math
Expand Down Expand Up @@ -39,8 +40,14 @@
WGS84_SEMIMINOR_AXIS = 6356752.3142


# Cache the transformer creation method
@lru_cache()
def _get_transformer_from_crs(src_crs, tgt_crs):
return Transformer.from_crs(src_crs, tgt_crs, always_xy=True)


def _safe_pj_transform(src_crs, tgt_crs, x, y, z=None, trap=True):
transformer = Transformer.from_crs(src_crs, tgt_crs, always_xy=True)
transformer = _get_transformer_from_crs(src_crs, tgt_crs)
transformed_coords = transformer.transform(x, y, z, errcheck=trap)
if z is None:
xx, yy = transformed_coords
Expand Down Expand Up @@ -1639,6 +1646,9 @@ def __init__(self, central_longitude=0.0,

super().__init__(proj4_params, globe=globe)

# Need to have x/y limits defined for the initial hash which
# gets used within transform_points for caching
self._x_limits = self._y_limits = None
# Calculate limits.
minlon, maxlon = self._determine_longitude_bounds(central_longitude)
limits = self.transform_points(self.as_geodetic(),
Expand Down

0 comments on commit 457f4b9

Please sign in to comment.