|
1 | 1 | import pytest |
| 2 | +import json |
2 | 3 |
|
3 | 4 | import geoarrow.types as gt |
4 | 5 | from geoarrow.types.constants import ( |
|
9 | 10 | CoordType, |
10 | 11 | ) |
11 | 12 | from geoarrow.types.type_spec import TypeSpec |
12 | | -from geoarrow.types.crs import OGC_CRS84, UNSPECIFIED as UNSPECIFIED_CRS |
| 13 | +from geoarrow.types.crs import ( |
| 14 | + OGC_CRS84, |
| 15 | + UNSPECIFIED as UNSPECIFIED_CRS, |
| 16 | + StringCrs, |
| 17 | + ProjJsonCrs, |
| 18 | +) |
13 | 19 |
|
14 | 20 |
|
15 | 21 | def test_type_spec_repr(): |
@@ -54,6 +60,72 @@ def test_type_spec_extension_metadata(): |
54 | 60 | TypeSpec().extension_metadata() |
55 | 61 |
|
56 | 62 |
|
| 63 | +def test_type_spec_metadata_crs(): |
| 64 | + # StringCrs |
| 65 | + spec = TypeSpec(edge_type=EdgeType.PLANAR, crs=StringCrs("EPSG:32620")) |
| 66 | + assert spec.extension_metadata() == '{"crs": "EPSG:32620"}' |
| 67 | + |
| 68 | + # ProjJsonCrs |
| 69 | + spec = spec.override(crs=OGC_CRS84) |
| 70 | + assert json.loads(spec.extension_metadata())["crs"] == OGC_CRS84.to_json_dict() |
| 71 | + assert json.loads(spec.extension_metadata())["crs_type"] == "projjson" |
| 72 | + |
| 73 | + # Raw string |
| 74 | + spec = TypeSpec(edge_type=EdgeType.PLANAR, crs="EPSG:32620") |
| 75 | + assert spec.extension_metadata() == '{"crs": "EPSG:32620"}' |
| 76 | + |
| 77 | + # Raw bytes |
| 78 | + spec = TypeSpec(edge_type=EdgeType.PLANAR, crs="EPSG:32620".encode()) |
| 79 | + assert spec.extension_metadata() == '{"crs": "EPSG:32620"}' |
| 80 | + |
| 81 | + # Accidentally JSON-encoded string |
| 82 | + spec = TypeSpec(edge_type=EdgeType.PLANAR, crs='"EPSG:32620"') |
| 83 | + assert spec.extension_metadata() == '{"crs": "EPSG:32620"}' |
| 84 | + |
| 85 | + # UnspecifiedCrs |
| 86 | + with pytest.raises(ValueError, match="edge_type or crs is unspecified"): |
| 87 | + TypeSpec(crs=UNSPECIFIED_CRS).extension_metadata() |
| 88 | + |
| 89 | + |
| 90 | +def test_type_spec_metadata_crs_load(): |
| 91 | + spec = TypeSpec.from_extension_metadata('{"crs": "EPSG:32620"}') |
| 92 | + assert isinstance(spec.crs, StringCrs) |
| 93 | + |
| 94 | + spec = TypeSpec.from_extension_metadata('{"crs": {}, "crs_type": "projjson"}') |
| 95 | + assert isinstance(spec.crs, ProjJsonCrs) |
| 96 | + assert spec.crs.to_json_dict() == {} |
| 97 | + |
| 98 | + |
| 99 | +def test_type_spec_metadata_crs_sanitize(): |
| 100 | + crs_obj = TypeSpec().override(crs="EPSG:32620").crs |
| 101 | + assert isinstance(crs_obj, StringCrs) |
| 102 | + assert crs_obj._crs == "EPSG:32620" |
| 103 | + assert TypeSpec().override(crs=crs_obj).crs is crs_obj |
| 104 | + |
| 105 | + crs_obj = TypeSpec().override(crs=ProjJsonCrs({})).crs |
| 106 | + assert isinstance(crs_obj, ProjJsonCrs) |
| 107 | + assert TypeSpec().override(crs=crs_obj).crs is crs_obj |
| 108 | + |
| 109 | + |
| 110 | +def test_type_spec_metadata_crs_pyproj(): |
| 111 | + pyproj = pytest.importorskip("pyproj") |
| 112 | + |
| 113 | + spec = TypeSpec(edge_type=EdgeType.PLANAR, crs=pyproj.CRS("EPSG:32620")) |
| 114 | + metadata_obj = json.loads(spec.extension_metadata()) |
| 115 | + assert metadata_obj["crs"] == pyproj.CRS("EPSG:32620").to_json_dict() |
| 116 | + assert metadata_obj["crs_type"] == "projjson" |
| 117 | + |
| 118 | + spec2 = TypeSpec.from_extension_metadata(spec.extension_metadata()) |
| 119 | + assert isinstance(spec2.crs, ProjJsonCrs) |
| 120 | + assert pyproj.CRS(spec2.crs) == pyproj.CRS("EPSG:32620") |
| 121 | + assert spec2.crs == pyproj.CRS("EPSG:32620") |
| 122 | + assert pyproj.CRS("EPSG:32620") == spec2.crs |
| 123 | + |
| 124 | + crs_obj = TypeSpec().override(crs=pyproj.CRS("EPSG:32620")).crs |
| 125 | + assert isinstance(crs_obj, pyproj.CRS) |
| 126 | + assert TypeSpec().override(crs=crs_obj).crs is crs_obj |
| 127 | + |
| 128 | + |
57 | 129 | def test_type_spec_create(): |
58 | 130 | # From TypeSpec |
59 | 131 | spec = TypeSpec() |
|
0 commit comments