1import pytest
2
3from pyproj.crs import (
4    CRS,
5    CoordinateOperation,
6    CoordinateSystem,
7    Datum,
8    Ellipsoid,
9    PrimeMeridian,
10)
11
12
13def test_crs_to_json_dict():
14    aeqd_crs = CRS(proj="aeqd", lon_0=-80, lat_0=40.5)
15    json_dict = aeqd_crs.to_json_dict()
16    assert json_dict["type"] == "ProjectedCRS"
17
18
19def test_crs_to_json():
20    aeqd_crs = CRS(proj="aeqd", lon_0=-80, lat_0=40.5)
21    json_data = aeqd_crs.to_json()
22    assert "ProjectedCRS" in json_data
23    assert "\n" not in json_data
24
25
26def test_crs_to_json__pretty():
27    aeqd_crs = CRS(proj="aeqd", lon_0=-80, lat_0=40.5)
28    json_data = aeqd_crs.to_json(pretty=True)
29    assert "ProjectedCRS" in json_data
30    assert json_data.startswith('{\n  "')
31
32
33def test_crs_to_json__pretty__indenation():
34    aeqd_crs = CRS(proj="aeqd", lon_0=-80, lat_0=40.5)
35    json_data = aeqd_crs.to_json(pretty=True, indentation=4)
36    assert "ProjectedCRS" in json_data
37    assert json_data.startswith('{\n    "')
38
39
40def test_crs_from_json():
41    aeqd_crs = CRS(proj="aeqd", lon_0=-80, lat_0=40.5)
42    assert CRS.from_json(aeqd_crs.to_json()) == aeqd_crs
43
44
45def test_crs_from_json_dict():
46    aeqd_crs = CRS(proj="aeqd", lon_0=-80, lat_0=40.5)
47    assert CRS.from_json_dict(aeqd_crs.to_json_dict()) == aeqd_crs
48
49
50@pytest.mark.parametrize(
51    "property_name, expected_type",
52    [
53        ("coordinate_operation", "Conversion"),
54        ("datum", "GeodeticReferenceFrame"),
55        ("ellipsoid", "Ellipsoid"),
56        ("prime_meridian", "PrimeMeridian"),
57        ("coordinate_system", "CoordinateSystem"),
58    ],
59)
60def test_properties_to_json(property_name, expected_type):
61    aeqd_crs = CRS(proj="aeqd", lon_0=-80, lat_0=40.5)
62    json_data = getattr(aeqd_crs, property_name).to_json()
63    assert expected_type in json_data
64    assert "\n" not in json_data
65
66
67@pytest.mark.parametrize(
68    "property_name, expected_type",
69    [
70        ("coordinate_operation", "Conversion"),
71        ("datum", "GeodeticReferenceFrame"),
72        ("ellipsoid", "Ellipsoid"),
73        ("prime_meridian", "PrimeMeridian"),
74        ("coordinate_system", "CoordinateSystem"),
75    ],
76)
77def test_properties_to_json__pretty(property_name, expected_type):
78    aeqd_crs = CRS(proj="aeqd", lon_0=-80, lat_0=40.5)
79    json_data = getattr(aeqd_crs, property_name).to_json(pretty=True)
80    assert expected_type in json_data
81    assert json_data.startswith('{\n  "')
82
83
84@pytest.mark.parametrize(
85    "property_name, expected_type",
86    [
87        ("coordinate_operation", "Conversion"),
88        ("datum", "GeodeticReferenceFrame"),
89        ("ellipsoid", "Ellipsoid"),
90        ("prime_meridian", "PrimeMeridian"),
91        ("coordinate_system", "CoordinateSystem"),
92    ],
93)
94def test_properties_to_json__pretty__indentation(property_name, expected_type):
95    aeqd_crs = CRS(proj="aeqd", lon_0=-80, lat_0=40.5)
96    json_data = getattr(aeqd_crs, property_name).to_json(pretty=True, indentation=4)
97    assert expected_type in json_data
98    assert json_data.startswith('{\n    "')
99
100
101@pytest.mark.parametrize(
102    "property_name, expected_type",
103    [
104        ("coordinate_operation", "Conversion"),
105        ("datum", "GeodeticReferenceFrame"),
106        ("ellipsoid", "Ellipsoid"),
107        ("prime_meridian", "PrimeMeridian"),
108    ],
109)
110def test_properties_to_json_dict(property_name, expected_type):
111    aeqd_crs = CRS(proj="aeqd", lon_0=-80, lat_0=40.5)
112    assert getattr(aeqd_crs, property_name).to_json_dict()["type"] == expected_type
113
114
115@pytest.mark.parametrize(
116    "property_name, init_class",
117    [
118        ("coordinate_operation", CoordinateOperation),
119        ("datum", Datum),
120        ("ellipsoid", Ellipsoid),
121        ("prime_meridian", PrimeMeridian),
122    ],
123)
124def test_properties_from_json_dict(property_name, init_class):
125    prop = getattr(CRS.from_epsg(26915), property_name)
126    assert init_class.from_json_dict(prop.to_json_dict()) == prop
127
128
129def test_coordinate_system_from_json_dict():
130    # separate test from other properties due to
131    # https://github.com/OSGeo/PROJ/issues/1818
132    aeqd_cs = CRS(proj="aeqd", lon_0=-80, lat_0=40.5).coordinate_system
133    assert CoordinateSystem.from_json_dict(aeqd_cs.to_json_dict()) == aeqd_cs
134
135
136def test_coordinate_system_from_json():
137    # separate test from other properties due to
138    # https://github.com/OSGeo/PROJ/issues/1818
139    aeqd_cs = CRS(proj="aeqd", lon_0=-80, lat_0=40.5).coordinate_system
140    assert CoordinateSystem.from_json(aeqd_cs.to_json()) == aeqd_cs
141