Skip to content

Commit

Permalink
BUG: Prepend Derived to CRS type name if CRS is derived (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 committed Sep 20, 2021
1 parent e8632da commit 6173770
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Change Log

Latest
-------
- BUG: Prepend "Derived" to CRS type name if CRS is derived (issue #932)

3.2.1
------
Expand Down
2 changes: 1 addition & 1 deletion pyproj/_crs.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ cdef class _CRS(Base):
cdef PJ_TYPE _type
cdef PJ_PROJ_INFO projpj_info
cdef readonly str srs
cdef readonly str type_name
cdef readonly str _type_name
cdef readonly Ellipsoid _ellipsoid
cdef readonly object _area_of_use
cdef readonly PrimeMeridian _prime_meridian
Expand Down
18 changes: 16 additions & 2 deletions pyproj/_crs.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2339,7 +2339,7 @@ cdef class _CRS(Base):
self._geodetic_crs = None
self._coordinate_system = None
self._coordinate_operation = None
self.type_name = "undefined"
self._type_name = None

def __init__(self, const char *proj_string):
self.context = pyproj_context_create()
Expand All @@ -2356,10 +2356,24 @@ cdef class _CRS(Base):
# set proj information
self.srs = proj_string
self._type = proj_get_type(self.projobj)
self.type_name = _CRS_TYPE_MAP[self._type]
self._set_base_info()
CRSError.clear()

@property
def type_name(self):
"""
Returns
-------
str:
The name of the type of the CRS object.
"""
if self._type_name is not None:
return self._type_name
self._type_name = _CRS_TYPE_MAP[self._type]
if self.is_derived:
self._type_name = f"Derived {self._type_name}"
return self._type_name

@property
def axis_info(self):
"""
Expand Down
17 changes: 6 additions & 11 deletions pyproj/crs/crs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1759,16 +1759,11 @@ class DerivedGeographicCRS(CustomConstructorCRS):
This class is for building a Derived Geographic CRS
"""

_expected_types = ("Geographic CRS", "Geographic 2D CRS", "Geographic 3D CRS")

def _check_type(self):
"""
This validates that the type of the CRS is expected
when using the from_* methods.
"""
super()._check_type()
if not self.is_derived:
raise CRSError("CRS is not a Derived Geographic CRS")
_expected_types = (
"Derived Geographic CRS",
"Derived Geographic 2D CRS",
"Derived Geographic 3D CRS",
)

def __init__(
self,
Expand Down Expand Up @@ -1870,7 +1865,7 @@ class ProjectedCRS(CustomConstructorCRS):
This class is for building a Projected CRS.
"""

_expected_types = ("Projected CRS",)
_expected_types = ("Projected CRS", "Derived Projected CRS")

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion test/crs/test_crs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ def test_from_authority__ignf():

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


Expand Down
8 changes: 4 additions & 4 deletions test/crs/test_crs_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_make_projected_crs(tmp_path):
aeaop = AlbersEqualAreaConversion(0, 0)
pc = ProjectedCRS(conversion=aeaop, name="Albers")
assert pc.name == "Albers"
assert pc.type_name == "Projected CRS"
assert pc.type_name == "Derived Projected CRS"
assert pc.coordinate_operation == aeaop
assert_can_pickle(pc, tmp_path)

Expand Down Expand Up @@ -105,15 +105,15 @@ def test_make_derived_geographic_crs(tmp_path):
conversion = RotatedLatitudeLongitudeConversion(o_lat_p=0, o_lon_p=0)
dgc = DerivedGeographicCRS(base_crs=GeographicCRS(), conversion=conversion)
assert dgc.name == "undefined"
assert dgc.type_name == "Geographic 2D CRS"
assert dgc.type_name == "Derived Geographic 2D CRS"
assert dgc.coordinate_operation == conversion
assert dgc.is_derived
assert_can_pickle(dgc, tmp_path)


def test_derived_geographic_crs__from_methods():
crs_str = "+proj=ob_tran +o_proj=longlat +o_lat_p=0 +o_lon_p=0 +lon_0=0"
with pytest.raises(CRSError, match="CRS is not a Derived Geographic CRS"):
with pytest.raises(CRSError, match="Invalid type Geographic 2D CRS"):
DerivedGeographicCRS.from_epsg(4326)
assert_maker_inheritance_valid(
DerivedGeographicCRS.from_string(crs_str), DerivedGeographicCRS
Expand Down Expand Up @@ -240,7 +240,7 @@ def test_compund_crs(tmp_path):
)
assert compcrs.name == "NAD83 / Pennsylvania South + NAVD88 height"
assert compcrs.type_name == "Compound CRS"
assert compcrs.sub_crs_list[0].type_name == "Projected CRS"
assert compcrs.sub_crs_list[0].type_name == "Derived Projected CRS"
assert compcrs.sub_crs_list[1].type_name == "Vertical CRS"
assert_can_pickle(compcrs, tmp_path)

Expand Down

0 comments on commit 6173770

Please sign in to comment.