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

ENH: warn when export of CRS returns None #1037

Merged
merged 7 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Latest
- BUG: remove CustomConstructorCRS @abstractmethod decorator (pull #1018)
- BUG: Correct type annotation for AreaofUse.bounds (issue #1012)
- BUG: :func:`pyproj.datadir.get_data_dir` support for conda Windows (issue #1029)
- ENH: warn when export of CRS returns None (issue #1036)

3.3.0
-------
Expand Down
4 changes: 2 additions & 2 deletions pyproj/_crs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Base:
self,
version: Union[WktVersion, str] = WktVersion.WKT2_2019,
pretty: bool = False,
) -> str: ...
) -> Optional[str]: ...
def to_json(self, pretty: bool = False, indentation: int = 2) -> str: ...
def to_json_dict(self) -> dict: ...
def __str__(self) -> str: ...
Expand Down Expand Up @@ -222,7 +222,7 @@ class _CRS(Base):
def to_epsg(self, min_confidence: int = 70) -> Optional[int]: ...
def to_authority(
self, auth_name: Optional[str] = None, min_confidence: int = 70
): ...
) -> Optional[tuple]: ...
def list_authority(
self, auth_name: Optional[str] = None, min_confidence: int = 70
) -> List[AuthorityMatchInfo]: ...
Expand Down
35 changes: 30 additions & 5 deletions pyproj/crs/crs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ def to_wkt(
self,
version: Union[WktVersion, str] = WktVersion.WKT2_2019,
pretty: bool = False,
) -> str:
) -> Optional[str]:
"""
Convert the projection to a WKT string.

Expand All @@ -1200,7 +1200,15 @@ def to_wkt(
-------
str
martinfleis marked this conversation as resolved.
Show resolved Hide resolved
"""
return self._crs.to_wkt(version=version, pretty=pretty)
wkt = self._crs.to_wkt(version=version, pretty=pretty)
if wkt is None:
warnings.warn(
f"CRS cannot be converted to a WKT string of a '{version}' version. "
"Select a different version of a WKT string or edit your CRS.",
RuntimeWarning,
stacklevel=2,
)
return wkt

def to_json(self, pretty: bool = False, indentation: int = 2) -> str:
"""
Expand Down Expand Up @@ -1289,9 +1297,18 @@ def to_epsg(self, min_confidence: int = 70) -> Optional[int]:
Optional[int]:
The best matching EPSG code matching the confidence level.
"""
return self._crs.to_epsg(min_confidence=min_confidence)
epsg = self._crs.to_epsg(min_confidence=min_confidence)
if epsg is None:
warnings.warn(
"CRS cannot be converted to the EPSG code. Match not found.",
RuntimeWarning,
stacklevel=2,
)
return epsg

def to_authority(self, auth_name: Optional[str] = None, min_confidence: int = 70):
def to_authority(
self, auth_name: Optional[str] = None, min_confidence: int = 70
) -> Optional[tuple]:
"""
.. versionadded:: 2.2.0

Expand Down Expand Up @@ -1329,9 +1346,17 @@ def to_authority(self, auth_name: Optional[str] = None, min_confidence: int = 70
tuple(str, str) or None:
The best matching (<auth_name>, <code>) for the confidence level.
"""
return self._crs.to_authority(
authority = self._crs.to_authority(
auth_name=auth_name, min_confidence=min_confidence
)
if authority is None:
warnings.warn(
"CRS cannot be converted to the authority name and code. "
"Match not found.",
RuntimeWarning,
stacklevel=2,
)
return authority

def list_authority(
self, auth_name: Optional[str] = None, min_confidence: int = 70
Expand Down
72 changes: 72 additions & 0 deletions test/crs/test_crs.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,28 @@ def test_epsg():
assert CRS.from_user_input("epsg:4326").to_epsg() == 4326


def test_epsg_none():
wkt_string = (
'PROJCRS["unknown",BASEGEOGCRS["unknown",DATUM["unknown",'
'ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1,'
'ID["EPSG",9001]]]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199],'
'ID["EPSG",8901]]],CONVERSION["unknown",METHOD["Equidistant Cylindrical",'
'ID["EPSG",1028]],PARAMETER["Latitude of 1st standard parallel",0,'
'ANGLEUNIT["degree",0.0174532925199433],ID["EPSG",8823]],'
'PARAMETER["Longitude of natural origin",0,ANGLEUNIT["degree",0.0174532925199],'
'ID["EPSG",8802]],PARAMETER["False easting",0,'
'LENGTHUNIT["unknown",111319.490793274],ID["EPSG",8806]],'
'PARAMETER["False northing",0,LENGTHUNIT["unknown",111319.490793274],'
'ID["EPSG",8807]]],CS[Cartesian,3],AXIS["(E)",east,ORDER[1],'
'LENGTHUNIT["unknown",111319.490793274]],AXIS["(N)",north,ORDER[2],'
'LENGTHUNIT["unknown",111319.490793274]],AXIS["ellipsoidal height (h)",up,'
'ORDER[3],LENGTHUNIT["metre",1,ID["EPSG",9001]]]]'
)
crs = CRS.from_wkt(wkt_string)
with pytest.warns(RuntimeWarning, match="CRS cannot be converted to the EPSG code"):
crs.to_epsg()


def test_datum():
datum = CRS.from_epsg(4326).datum
assert "\n" in repr(datum)
Expand Down Expand Up @@ -889,6 +911,32 @@ def test_to_wkt_enum__invalid():
crs.to_wkt("WKT_INVALID")
martinfleis marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize(
"wkt_version",
["WKT2_2015", "WKT2_2015_SIMPLIFIED", "WKT1_GDAL", "WKT1_ESRI"],
)
def test_to_wkt_none_warning(wkt_version):
wkt_string = (
'PROJCRS["unknown",BASEGEOGCRS["unknown",DATUM["unknown",'
'ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1,'
'ID["EPSG",9001]]]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199],'
'ID["EPSG",8901]]],CONVERSION["unknown",METHOD["Equidistant Cylindrical",'
'ID["EPSG",1028]],PARAMETER["Latitude of 1st standard parallel",0,'
'ANGLEUNIT["degree",0.0174532925199433],ID["EPSG",8823]],'
'PARAMETER["Longitude of natural origin",0,ANGLEUNIT["degree",0.0174532925199],'
'ID["EPSG",8802]],PARAMETER["False easting",0,'
'LENGTHUNIT["unknown",111319.490793274],ID["EPSG",8806]],'
'PARAMETER["False northing",0,LENGTHUNIT["unknown",111319.490793274],'
'ID["EPSG",8807]]],CS[Cartesian,3],AXIS["(E)",east,ORDER[1],'
'LENGTHUNIT["unknown",111319.490793274]],AXIS["(N)",north,ORDER[2],'
'LENGTHUNIT["unknown",111319.490793274]],AXIS["ellipsoidal height (h)",up,'
'ORDER[3],LENGTHUNIT["metre",1,ID["EPSG",9001]]]]'
)
crs = CRS.from_wkt(wkt_string)
with pytest.warns(RuntimeWarning, match="CRS cannot be converted to a WKT string"):
crs.to_wkt(version=wkt_version)


def test_to_proj4_enum():
crs = CRS.from_epsg(4326)
with pytest.warns(UserWarning):
Expand Down Expand Up @@ -1160,6 +1208,30 @@ def test_from_authority__ignf():
assert cc.to_epsg() == 25828


def test_to_authority_none():
wkt_string = (
'PROJCRS["unknown",BASEGEOGCRS["unknown",DATUM["unknown",'
'ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1,'
'ID["EPSG",9001]]]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199],'
'ID["EPSG",8901]]],CONVERSION["unknown",METHOD["Equidistant Cylindrical",'
'ID["EPSG",1028]],PARAMETER["Latitude of 1st standard parallel",0,'
'ANGLEUNIT["degree",0.0174532925199433],ID["EPSG",8823]],'
'PARAMETER["Longitude of natural origin",0,ANGLEUNIT["degree",0.0174532925199],'
'ID["EPSG",8802]],PARAMETER["False easting",0,'
'LENGTHUNIT["unknown",111319.490793274],ID["EPSG",8806]],'
'PARAMETER["False northing",0,LENGTHUNIT["unknown",111319.490793274],'
'ID["EPSG",8807]]],CS[Cartesian,3],AXIS["(E)",east,ORDER[1],'
'LENGTHUNIT["unknown",111319.490793274]],AXIS["(N)",north,ORDER[2],'
'LENGTHUNIT["unknown",111319.490793274]],AXIS["ellipsoidal height (h)",up,'
'ORDER[3],LENGTHUNIT["metre",1,ID["EPSG",9001]]]]'
)
crs = CRS.from_wkt(wkt_string)
with pytest.warns(
RuntimeWarning, match="CRS cannot be converted to the authority name and code"
):
crs.to_authority()


def test_ignf_authority_repr():
assert repr(CRS.from_authority("IGNF", "ETRS89UTM28")).startswith(
"<Derived Projected CRS: IGNF:ETRS89UTM28>"
Expand Down