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

Conversation

snowman2
Copy link
Contributor

@snowman2 snowman2 commented Oct 10, 2022

Closes #1321

If this is merged, I will be happy to port it into odc-geo.

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.

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.

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).

@snowman2
Copy link
Contributor Author

Note: I will be happy to update the tests if this is the desired pattern to move forward with.

@SpacemanPaul
Copy link
Contributor

Note: I will be happy to update the tests if this is the desired pattern to move forward with.

I will have a look at the tests later this week - I suspect some of them are not your fault.

@SpacemanPaul
Copy link
Contributor

SpacemanPaul commented Oct 13, 2022

@snowman2 I think a good chunk of the test issues are due to this no longer working:

from datacube.utils.geometry import CRS
crs = CRS("EPSG:4326")

assert crs.epsg == 4326

With this PR crs.epsg returns None.

@SpacemanPaul SpacemanPaul self-requested a review October 13, 2022 07:11
Copy link
Contributor

@SpacemanPaul SpacemanPaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the changes I suggest should just about do it. We might need to make some changes to the (experimental) postgis tests as well.

datacube/utils/geometry/_base.py Outdated Show resolved Hide resolved
datacube/utils/geometry/_base.py Show resolved Hide resolved
@snowman2
Copy link
Contributor Author

Good catches @SpacemanPaul 👍. I updated the PR based on your comments.

@snowman2 snowman2 force-pushed the crs_wkt_cache branch 3 times, most recently from ade84ea to 0e6f0aa Compare October 13, 2022 15:02
@codecov
Copy link

codecov bot commented Oct 13, 2022

Codecov Report

Base: 92.34% // Head: 92.33% // Decreases project coverage by -0.00% ⚠️

Coverage data is based on head (d2d7d0d) compared to base (e0dfd94).
Patch coverage: 86.36% of modified lines in pull request are covered.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1322      +/-   ##
===========================================
- Coverage    92.34%   92.33%   -0.01%     
===========================================
  Files          127      127              
  Lines        13581    13585       +4     
===========================================
+ Hits         12541    12544       +3     
- Misses        1040     1041       +1     
Impacted Files Coverage Δ
datacube/utils/geometry/_base.py 99.34% <85.71%> (-0.26%) ⬇️
datacube/drivers/postgis/_spatial.py 94.31% <100.00%> (+1.13%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

epsg = int(crs_str.split(":", maxsplit=1)[-1])
else:
crs_str = crs
crs = _CRS.from_user_input(crs)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always call _CRS.from_user_input as it supports all of these types. Also, if it is a _CRS, it just returns the original _CRS ref.

def _make_crs(crs: Union[str, int, _CRS]) -> Tuple[_CRS, str, Optional[int]]:
epsg = False
crs = _CRS.from_user_input(crs)
crs_str = crs.srs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

srs is the attribute for the user input string.

Copy link
Contributor

@SpacemanPaul SpacemanPaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks heaps @snowman2

@SpacemanPaul SpacemanPaul merged commit 0535335 into opendatacube:develop Oct 13, 2022
@snowman2 snowman2 deleted the crs_wkt_cache branch October 13, 2022 23:50
@snowman2
Copy link
Contributor Author

Thanks @SpacemanPaul 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PERF: to_epsg causes data loading to be slow for CRS without EPSG code
2 participants