-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,25 +116,28 @@ 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): | ||
def _make_crs(crs: Union[str, int, _CRS]) -> Tuple[_CRS, str, Optional[int]]: | ||
epsg = None | ||
if isinstance(crs, (str, int)): | ||
snowman2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
crs = _CRS.from_user_input(crs) | ||
epsg = crs.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. Remove all |
||
if epsg is not None: | ||
crs_str = f"EPSG:{epsg}" | ||
if isinstance(crs, int): | ||
crs_str = f"EPSG:{crs}" | ||
epsg = crs | ||
else: | ||
crs_str = crs.to_wkt() | ||
return (crs, crs_str, crs.to_epsg()) | ||
return (crs, crs_str, epsg) | ||
|
||
|
||
def _make_crs_transform_key(from_crs, to_crs, always_xy): | ||
|
@@ -171,20 +174,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 +216,16 @@ 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() | ||
if self._epsg is None: | ||
self._epsg = False | ||
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): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Allow passing in integer EPSG code.