-
Notifications
You must be signed in to change notification settings - Fork 175
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
PERF: CRS - use WKT for cache and lazy load EPSG code #1322
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,25 +116,25 @@ def from_points(p1: Tuple[float, float], | |
(p1[1], p2[1])) | ||
|
||
|
||
def _make_crs_key(crs_spec: Union[str, _CRS]) -> str: | ||
def _make_crs_key(crs_spec: Union[str, int, _CRS]) -> str: | ||
if isinstance(crs_spec, str): | ||
normed_epsg = crs_spec.upper() | ||
if normed_epsg.startswith("EPSG:"): | ||
return normed_epsg | ||
return crs_spec | ||
if isinstance(crs_spec, int): | ||
return f"EPSG:{crs_spec}" | ||
return crs_spec.to_wkt() | ||
|
||
|
||
@cachetools.cached({}, key=_make_crs_key) # type: ignore[misc] | ||
def _make_crs(crs: Union[str, _CRS]) -> Tuple[_CRS, str, Optional[int]]: | ||
if isinstance(crs, str): | ||
crs = _CRS.from_user_input(crs) | ||
epsg = crs.to_epsg() | ||
if epsg is not None: | ||
crs_str = f"EPSG:{epsg}" | ||
else: | ||
crs_str = crs.to_wkt() | ||
return (crs, crs_str, crs.to_epsg()) | ||
def _make_crs(crs: Union[str, int, _CRS]) -> Tuple[_CRS, str, Optional[int]]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Allow passing in integer EPSG code. |
||
epsg = False | ||
crs = _CRS.from_user_input(crs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always call |
||
crs_str = crs.srs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if crs_str.upper().startswith("EPSG:"): | ||
epsg = int(crs_str.split(":", maxsplit=1)[-1]) | ||
return (crs, crs_str, epsg) | ||
|
||
|
||
def _make_crs_transform_key(from_crs, to_crs, always_xy): | ||
|
@@ -171,20 +171,20 @@ def __init__(self, crs_spec: Any): | |
elif isinstance(crs_spec, dict): | ||
self._crs, self._str, self._epsg = _make_crs(_CRS.from_dict(crs_spec)) | ||
else: | ||
try: | ||
epsg = crs_spec.to_epsg() | ||
except AttributeError: | ||
epsg = None | ||
if epsg is not None: | ||
self._crs, self._str, self._epsg = _make_crs(f"EPSG:{epsg}") | ||
return | ||
try: | ||
wkt = crs_spec.to_wkt() | ||
except AttributeError: | ||
wkt = None | ||
if wkt is not None: | ||
self._crs, self._str, self._epsg = _make_crs(wkt) | ||
return | ||
try: | ||
epsg = crs_spec.to_epsg() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use EPSG as backup to WKT. Likely never used. Will raise exception in the future if no WKT (see: pyproj4/pyproj#1036). |
||
except AttributeError: | ||
epsg = None | ||
if epsg is not None: | ||
self._crs, self._str, self._epsg = _make_crs(epsg) | ||
return | ||
|
||
raise CRSError( | ||
"Expect string or any object with `.to_epsg()` or `.to_wkt()` methods" | ||
|
@@ -213,11 +213,14 @@ def to_epsg(self) -> Optional[int]: | |
""" | ||
EPSG Code of the CRS or None | ||
""" | ||
if self._epsg is not False: | ||
snowman2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self._epsg | ||
self._epsg = self._crs.to_epsg() | ||
return self._epsg | ||
snowman2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@property | ||
def epsg(self) -> Optional[int]: | ||
return self._epsg | ||
return self.to_epsg() | ||
|
||
@property | ||
def semi_major_axis(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove all
to_epsg
calls.