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

Issue #259 Make pyproj an optional dependency when parsing CRS strin… #458

Merged
merged 1 commit into from
Aug 9, 2023
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
30 changes: 22 additions & 8 deletions openeo/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from urllib.parse import urljoin

import requests
import pyproj.crs
import shapely.geometry.base
from deprecated import deprecated

Expand Down Expand Up @@ -675,11 +674,10 @@ def crs_to_epsg_code(crs: Union[str, int, dict, None]) -> Optional[int]:
if crs in (None, "", {}):
return None

# TODO: decide: are more fine-grained checks more helpful than always raising EPSGCodeNotFound?
if not isinstance(crs, (int, str, dict)):
raise TypeError("The allowed type for the parameter 'crs' are: str, int, dict and None")

# if We want to stop processing as soon as we have an int value, then we
# If we want to stop processing as soon as we have an int value, then we
# should not accept values that are complete non-sense, as best as we can.
crs_intermediate = crs
if isinstance(crs, int):
Expand All @@ -693,10 +691,26 @@ def crs_to_epsg_code(crs: Union[str, int, dict, None]) -> Optional[int]:
# So we need to process it with pyproj, below.
logger.debug("crs_to_epsg_code received crs input that was not an int: crs={crs}, exception caught: {exc}")

if isinstance(crs_intermediate, int):
if crs_intermediate <= 0:
raise ValueError(f"When crs is an integer value it has to be > 0.")
else:
return crs_intermediate

try:
converted_crs = pyproj.crs.CRS.from_user_input(crs_intermediate)
except pyproj.exceptions.CRSError as exc:
logger.error(f"Could not convert CRS string to EPSG code: crs={crs}, exception: {exc}", exc_info=True)
raise ValueError(crs) from exc
import pyproj.crs
except ImportError as exc:
message = (
f"Cannot convert CRS string: {crs}. "
+ "Need pyproj to convert this CRS string but the pyproj library is not installed."
)
logger.error(message)
raise ValueError(message) from ImportError
else:
return converted_crs.to_epsg()
try:
converted_crs = pyproj.crs.CRS.from_user_input(crs)
except pyproj.exceptions.CRSError as exc:
logger.error(f"Could not convert CRS string to EPSG code: crs={crs}, exception: {exc}", exc_info=True)
raise ValueError(crs) from exc
else:
return converted_crs.to_epsg()
Loading