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

PERF: CRS - use WKT for cache and lazy load EPSG code #1322

Merged
merged 3 commits into from
Oct 13, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 23 additions & 15 deletions datacube/utils/geometry/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Copy link
Contributor Author

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.

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()
Copy link
Contributor Author

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.

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):
Expand Down Expand Up @@ -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()
Copy link
Contributor Author

@snowman2 snowman2 Oct 10, 2022

Choose a reason for hiding this comment

The 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"
Expand Down Expand Up @@ -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):
Expand Down