Skip to content

Commit 04ce29a

Browse files
authored
Merge pull request #1255 from snowman2/to_cf_bug
BUG: Add horizontal_datum_name for geographic CRS in CRS.to_cf
2 parents 79a8602 + 9405668 commit 04ce29a

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

docs/history.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Latest
1414
- REF: Raise error when :meth:`.CRS.to_wkt`, :meth:`.CRS.to_json`, or :meth:`.CRS.to_proj4` returns None (issue #1036)
1515
- CLN: Remove `AzumuthalEquidistantConversion` & :class:`LambertAzumuthalEqualAreaConversion`. :class:`AzimuthalEquidistantConversion` & :class:`LambertAzimuthalEqualAreaConversion` should be used instead (pull #1219)
1616
- BUG: Fix Derived Projected CRS support (issue #1182)
17+
- BUG: Add horizontal_datum_name for geographic CRS in :meth:`.CRS.to_cf` (issue #1251)
18+
- BUG: Add datum ensemble support to :class:`.GeographicCRS` (pull #1255)
1719

1820
3.4.1
1921
-----

pyproj/crs/_cf1x8.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434

3535

3636
def _horizontal_datum_from_params(cf_params):
37+
datum_name = cf_params.get("horizontal_datum_name")
38+
if datum_name and datum_name not in ("undefined", "unknown"):
39+
try:
40+
return Datum.from_name(datum_name)
41+
except CRSError:
42+
pass
3743
# step 1: build ellipsoid
3844
ellipsoid = None
3945
ellipsoid_name = cf_params.get("reference_ellipsoid_name")
@@ -46,7 +52,7 @@ def _horizontal_datum_from_params(cf_params):
4652
radius=cf_params.get("earth_radius"),
4753
)
4854
except CRSError:
49-
if ellipsoid_name:
55+
if ellipsoid_name and ellipsoid_name not in ("undefined", "unknown"):
5056
ellipsoid = Ellipsoid.from_name(ellipsoid_name)
5157

5258
# step 2: build prime meridian
@@ -58,19 +64,16 @@ def _horizontal_datum_from_params(cf_params):
5864
longitude=cf_params["longitude_of_prime_meridian"],
5965
)
6066
except KeyError:
61-
if prime_meridian_name:
67+
if prime_meridian_name and prime_meridian_name not in ("undefined", "unknown"):
6268
prime_meridian = PrimeMeridian.from_name(prime_meridian_name)
6369

6470
# step 3: build datum
65-
datum_name = cf_params.get("horizontal_datum_name")
6671
if ellipsoid or prime_meridian:
6772
return CustomDatum(
6873
name=datum_name or "undefined",
6974
ellipsoid=ellipsoid or "WGS 84",
7075
prime_meridian=prime_meridian or "Greenwich",
7176
)
72-
if datum_name:
73-
return Datum.from_name(datum_name)
7477
return None
7578

7679

pyproj/crs/crs.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,8 @@ def to_cf(
693693
# handle geographic CRS
694694
if self.geodetic_crs:
695695
cf_dict["geographic_crs_name"] = self.geodetic_crs.name
696+
if self.geodetic_crs.datum:
697+
cf_dict["horizontal_datum_name"] = self.geodetic_crs.datum.name
696698

697699
if self.is_geographic:
698700
if self.coordinate_operation:
@@ -711,15 +713,11 @@ def to_cf(
711713
self.coordinate_operation.method_name.lower()
712714
](self.coordinate_operation)
713715
)
714-
if self.datum:
715-
cf_dict["horizontal_datum_name"] = self.datum.name
716716
else:
717717
cf_dict["grid_mapping_name"] = "latitude_longitude"
718718
return cf_dict
719719

720720
# handle projected CRS
721-
if self.is_projected and self.datum:
722-
cf_dict["horizontal_datum_name"] = self.datum.name
723721
coordinate_operation = None
724722
if not self.is_bound and self.is_projected:
725723
coordinate_operation = self.coordinate_operation
@@ -1758,31 +1756,35 @@ class GeographicCRS(CustomConstructorCRS):
17581756
def __init__(
17591757
self,
17601758
name: str = "undefined",
1761-
datum: Any = "urn:ogc:def:datum:EPSG::6326",
1759+
datum: Any = "urn:ogc:def:ensemble:EPSG::6326",
17621760
ellipsoidal_cs: Optional[Any] = None,
17631761
) -> None:
17641762
"""
17651763
Parameters
17661764
----------
17671765
name: str, default="undefined"
17681766
Name of the CRS.
1769-
datum: Any, default="urn:ogc:def:datum:EPSG::6326"
1767+
datum: Any, default="urn:ogc:def:ensemble:EPSG::6326"
17701768
Anything accepted by :meth:`pyproj.crs.Datum.from_user_input` or
17711769
a :class:`pyproj.crs.datum.CustomDatum`.
17721770
ellipsoidal_cs: Any, optional
17731771
Input to create an Ellipsoidal Coordinate System.
17741772
Anything accepted by :meth:`pyproj.crs.CoordinateSystem.from_user_input`
17751773
or an Ellipsoidal Coordinate System created from :ref:`coordinate_system`.
17761774
"""
1775+
datum = Datum.from_user_input(datum).to_json_dict()
17771776
geographic_crs_json = {
17781777
"$schema": "https://proj.org/schemas/v0.2/projjson.schema.json",
17791778
"type": "GeographicCRS",
17801779
"name": name,
1781-
"datum": Datum.from_user_input(datum).to_json_dict(),
17821780
"coordinate_system": CoordinateSystem.from_user_input(
17831781
ellipsoidal_cs or Ellipsoidal2DCS()
17841782
).to_json_dict(),
17851783
}
1784+
if datum["type"] == "DatumEnsemble":
1785+
geographic_crs_json["datum_ensemble"] = datum
1786+
else:
1787+
geographic_crs_json["datum"] = datum
17861788
super().__init__(geographic_crs_json)
17871789

17881790

test/crs/test_crs_cf.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def test_cf_from_latlon():
221221
"grid_mapping_name": "latitude_longitude",
222222
"geographic_crs_name": "undefined",
223223
"reference_ellipsoid_name": "undefined",
224+
"horizontal_datum_name": "undefined",
224225
}
225226
cf_dict = crs.to_cf()
226227
assert cf_dict.pop("crs_wkt").startswith("GEOGCRS[")
@@ -253,6 +254,7 @@ def test_cf_from_latlon__named():
253254
"reference_ellipsoid_name": "WGS 84",
254255
"longitude_of_prime_meridian": 0.0,
255256
"prime_meridian_name": "Greenwich",
257+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
256258
"geographic_crs_name": "WGS 84",
257259
"grid_mapping_name": "latitude_longitude",
258260
}
@@ -360,7 +362,7 @@ def test_cf_rotated_latlon():
360362
"reference_ellipsoid_name": "WGS 84",
361363
"longitude_of_prime_meridian": 0.0,
362364
"prime_meridian_name": "Greenwich",
363-
"horizontal_datum_name": "World Geodetic System 1984",
365+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
364366
"grid_mapping_name": "rotated_latitude_longitude",
365367
"grid_north_pole_latitude": 32.5,
366368
"grid_north_pole_longitude": 170.0,
@@ -532,7 +534,7 @@ def test_cf_lambert_conformal_conic_1sp():
532534
"reference_ellipsoid_name": "WGS 84",
533535
"longitude_of_prime_meridian": 0.0,
534536
"prime_meridian_name": "Greenwich",
535-
"horizontal_datum_name": "World Geodetic System 1984",
537+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
536538
"grid_mapping_name": "lambert_conformal_conic",
537539
"longitude_of_central_meridian": 265.0,
538540
"false_easting": 0.0,
@@ -596,7 +598,7 @@ def test_cf_lambert_conformal_conic_2sp(standard_parallel):
596598
"reference_ellipsoid_name": "WGS 84",
597599
"longitude_of_prime_meridian": 0.0,
598600
"prime_meridian_name": "Greenwich",
599-
"horizontal_datum_name": "World Geodetic System 1984",
601+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
600602
"grid_mapping_name": "lambert_conformal_conic",
601603
"standard_parallel": (25.0, 30.0),
602604
"latitude_of_projection_origin": 25.0,
@@ -751,7 +753,7 @@ def test_geos_crs_sweep():
751753
"reference_ellipsoid_name": "WGS 84",
752754
"longitude_of_prime_meridian": 0.0,
753755
"prime_meridian_name": "Greenwich",
754-
"horizontal_datum_name": "World Geodetic System 1984",
756+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
755757
"grid_mapping_name": "geostationary",
756758
"sweep_angle_axis": "x",
757759
"perspective_point_height": 1.0,
@@ -799,7 +801,7 @@ def test_geos_crs_fixed_angle_axis():
799801
"reference_ellipsoid_name": "WGS 84",
800802
"longitude_of_prime_meridian": 0.0,
801803
"prime_meridian_name": "Greenwich",
802-
"horizontal_datum_name": "World Geodetic System 1984",
804+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
803805
"grid_mapping_name": "geostationary",
804806
"sweep_angle_axis": "x",
805807
"perspective_point_height": 1.0,
@@ -899,7 +901,7 @@ def test_mercator_b():
899901
"reference_ellipsoid_name": "WGS 84",
900902
"longitude_of_prime_meridian": 0.0,
901903
"prime_meridian_name": "Greenwich",
902-
"horizontal_datum_name": "World Geodetic System 1984",
904+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
903905
"grid_mapping_name": "mercator",
904906
"standard_parallel": 21.354,
905907
"longitude_of_projection_origin": 10.0,
@@ -1203,6 +1205,7 @@ def test_azimuthal_equidistant():
12031205
assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
12041206
assert cf_dict == expected_cf
12051207
# test roundtrip
1208+
expected_cf["horizontal_datum_name"] = "World Geodetic System 1984 ensemble"
12061209
_test_roundtrip(expected_cf, "PROJCRS[")
12071210
# test coordinate system
12081211
assert crs.cs_to_cf() == [
@@ -1230,7 +1233,7 @@ def test_lambert_azimuthal_equal_area():
12301233
"reference_ellipsoid_name": "WGS 84",
12311234
"longitude_of_prime_meridian": 0.0,
12321235
"prime_meridian_name": "Greenwich",
1233-
"horizontal_datum_name": "World Geodetic System 1984",
1236+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
12341237
"grid_mapping_name": "lambert_azimuthal_equal_area",
12351238
"latitude_of_projection_origin": 1.0,
12361239
"longitude_of_projection_origin": 2.0,
@@ -1270,7 +1273,7 @@ def test_lambert_cylindrical_equal_area():
12701273
"reference_ellipsoid_name": "WGS 84",
12711274
"longitude_of_prime_meridian": 0.0,
12721275
"prime_meridian_name": "Greenwich",
1273-
"horizontal_datum_name": "World Geodetic System 1984",
1276+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
12741277
"grid_mapping_name": "lambert_cylindrical_equal_area",
12751278
"standard_parallel": 1.0,
12761279
"longitude_of_central_meridian": 2.0,
@@ -1310,7 +1313,7 @@ def test_mercator_a():
13101313
"reference_ellipsoid_name": "WGS 84",
13111314
"longitude_of_prime_meridian": 0.0,
13121315
"prime_meridian_name": "Greenwich",
1313-
"horizontal_datum_name": "World Geodetic System 1984",
1316+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
13141317
"grid_mapping_name": "mercator",
13151318
"standard_parallel": 0.0,
13161319
"longitude_of_projection_origin": 2.0,
@@ -1351,7 +1354,7 @@ def test_orthographic():
13511354
"reference_ellipsoid_name": "WGS 84",
13521355
"longitude_of_prime_meridian": 0.0,
13531356
"prime_meridian_name": "Greenwich",
1354-
"horizontal_datum_name": "World Geodetic System 1984",
1357+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
13551358
"grid_mapping_name": "orthographic",
13561359
"latitude_of_projection_origin": 1.0,
13571360
"longitude_of_projection_origin": 2.0,
@@ -1391,7 +1394,7 @@ def test_polar_stereographic_a():
13911394
"reference_ellipsoid_name": "WGS 84",
13921395
"longitude_of_prime_meridian": 0.0,
13931396
"prime_meridian_name": "Greenwich",
1394-
"horizontal_datum_name": "World Geodetic System 1984",
1397+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
13951398
"grid_mapping_name": "polar_stereographic",
13961399
"latitude_of_projection_origin": 90.0,
13971400
"straight_vertical_longitude_from_pole": 1.0,
@@ -1432,7 +1435,7 @@ def test_polar_stereographic_b():
14321435
"reference_ellipsoid_name": "WGS 84",
14331436
"longitude_of_prime_meridian": 0.0,
14341437
"prime_meridian_name": "Greenwich",
1435-
"horizontal_datum_name": "World Geodetic System 1984",
1438+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
14361439
"grid_mapping_name": "polar_stereographic",
14371440
"standard_parallel": 0.0,
14381441
"straight_vertical_longitude_from_pole": 1.0,
@@ -1472,7 +1475,7 @@ def test_stereographic():
14721475
"reference_ellipsoid_name": "WGS 84",
14731476
"longitude_of_prime_meridian": 0.0,
14741477
"prime_meridian_name": "Greenwich",
1475-
"horizontal_datum_name": "World Geodetic System 1984",
1478+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
14761479
"grid_mapping_name": "stereographic",
14771480
"latitude_of_projection_origin": 0.0,
14781481
"longitude_of_projection_origin": 1.0,
@@ -1513,7 +1516,7 @@ def test_sinusoidal():
15131516
"reference_ellipsoid_name": "WGS 84",
15141517
"longitude_of_prime_meridian": 0.0,
15151518
"prime_meridian_name": "Greenwich",
1516-
"horizontal_datum_name": "World Geodetic System 1984",
1519+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
15171520
"grid_mapping_name": "sinusoidal",
15181521
"longitude_of_projection_origin": 0.0,
15191522
"false_easting": 1.0,
@@ -1552,7 +1555,7 @@ def test_vertical_perspective():
15521555
"reference_ellipsoid_name": "WGS 84",
15531556
"longitude_of_prime_meridian": 0.0,
15541557
"prime_meridian_name": "Greenwich",
1555-
"horizontal_datum_name": "World Geodetic System 1984",
1558+
"horizontal_datum_name": "World Geodetic System 1984 ensemble",
15561559
"grid_mapping_name": "vertical_perspective",
15571560
"perspective_point_height": 50.0,
15581561
"latitude_of_projection_origin": 0.0,
@@ -1889,7 +1892,7 @@ def test_3d_ellipsoidal_cs_depth():
18891892
"name": "WGS 84 (geographic 3D)",
18901893
"datum": {
18911894
"type": "GeodeticReferenceFrame",
1892-
"name": "World Geodetic System 1984",
1895+
"name": "World Geodetic System 1984 ensemble",
18931896
"ellipsoid": {
18941897
"name": "WGS 84",
18951898
"semi_major_axis": 6378137,

0 commit comments

Comments
 (0)