Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache transformer crs for v0.20.x #1931

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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