1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
import pytest
import altair.vegalite.v5 as alt
def geom_obj(geom):
class Geom:
pass
geom_obj = Geom()
geom_obj.__geo_interface__ = geom
return geom_obj
# correct translation of Polygon geometry to Feature type
def test_geo_interface_polygon_feature():
geom = {
"coordinates": [[(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]],
"type": "Polygon",
}
feat = geom_obj(geom)
with alt.data_transformers.enable(consolidate_datasets=False):
spec = alt.Chart(feat).mark_geoshape().to_dict()
assert spec["data"]["values"]["type"] == "Feature"
# merge geometry with empty properties dictionary
def test_geo_interface_removal_empty_properties():
geom = {
"geometry": {
"coordinates": [
[[6.90, 53.48], [5.98, 51.85], [6.07, 53.51], [6.90, 53.48]]
],
"type": "Polygon",
},
"id": None,
"properties": {},
"type": "Feature",
}
feat = geom_obj(geom)
with alt.data_transformers.enable(consolidate_datasets=False):
spec = alt.Chart(feat).mark_geoshape().to_dict()
assert spec["data"]["values"]["type"] == "Feature"
# only register metadata in the properties member
def test_geo_interface_register_foreign_member():
geom = {
"geometry": {
"coordinates": [
[[6.90, 53.48], [5.98, 51.85], [6.07, 53.51], [6.90, 53.48]]
],
"type": "Polygon",
},
"id": 2,
"properties": {"foo": "bah"},
"type": "Feature",
}
feat = geom_obj(geom)
with alt.data_transformers.enable(consolidate_datasets=False):
spec = alt.Chart(feat).mark_geoshape().to_dict()
with pytest.raises(KeyError):
spec["data"]["values"]["id"]
assert spec["data"]["values"]["foo"] == "bah"
# correct serializing of arrays and nested tuples
def test_geo_interface_serializing_arrays_tuples():
import array as arr
geom = {
"bbox": arr.array("d", [1, 2, 3, 4]),
"geometry": {
"coordinates": [
(
(6.90, 53.48),
(5.98, 51.85),
(6.07, 53.51),
(6.90, 53.48),
)
],
"type": "Polygon",
},
"id": 27,
"properties": {},
"type": "Feature",
}
feat = geom_obj(geom)
with alt.data_transformers.enable(consolidate_datasets=False):
spec = alt.Chart(feat).mark_geoshape().to_dict()
assert spec["data"]["values"]["geometry"]["coordinates"][0][0] == [6.9, 53.48]
# overwrite existing 'type' value in properties with `Feature`
def test_geo_interface_reserved_members():
geom = {
"geometry": {
"coordinates": [
[[6.90, 53.48], [5.98, 51.85], [6.07, 53.51], [6.90, 53.48]]
],
"type": "Polygon",
},
"id": 27,
"properties": {"type": "foo"},
"type": "Feature",
}
feat = geom_obj(geom)
with alt.data_transformers.enable(consolidate_datasets=False):
spec = alt.Chart(feat).mark_geoshape().to_dict()
assert spec["data"]["values"]["type"] == "Feature"
# an empty FeatureCollection is valid
def test_geo_interface_empty_feature_collection():
geom = {"type": "FeatureCollection", "features": []}
feat = geom_obj(geom)
with alt.data_transformers.enable(consolidate_datasets=False):
spec = alt.Chart(feat).mark_geoshape().to_dict()
assert spec["data"]["values"] == []
# Features in a FeatureCollection only keep properties and geometry
def test_geo_interface_feature_collection():
geom = {
"type": "FeatureCollection",
"features": [
{
"geometry": {
"coordinates": [
[[6.90, 53.48], [5.98, 51.85], [6.07, 53.51], [6.90, 53.48]]
],
"type": "Polygon",
},
"id": 27,
"properties": {"type": "foo", "id": 1, "geometry": 1},
"type": "Feature",
},
{
"geometry": {
"coordinates": [
[[8.90, 53.48], [7.98, 51.85], [8.07, 53.51], [8.90, 53.48]]
],
"type": "Polygon",
},
"id": 28,
"properties": {"type": "foo", "id": 2, "geometry": 1},
"type": "Feature",
},
],
}
feat = geom_obj(geom)
with alt.data_transformers.enable(consolidate_datasets=False):
spec = alt.Chart(feat).mark_geoshape().to_dict()
assert spec["data"]["values"][0]["id"] == 1
assert spec["data"]["values"][1]["id"] == 2
assert "coordinates" in spec["data"]["values"][0]["geometry"]
assert "coordinates" in spec["data"]["values"][1]["geometry"]
assert spec["data"]["values"][0]["type"] == "Feature"
assert spec["data"]["values"][1]["type"] == "Feature"
# typical output of a __geo_interface__ from geopandas GeoDataFrame
# notic that the index value is registerd as a commonly used identifier
# with the name "id" (in this case 49). Similar to serialization of a
# pandas DataFrame is the index not included in the output
def test_geo_interface_feature_collection_gdf():
geom = {
"bbox": (19.89, -26.82, 29.43, -17.66),
"features": [
{
"bbox": (19.89, -26.82, 29.43, -17.66),
"geometry": {
"coordinates": [
[[6.90, 53.48], [5.98, 51.85], [6.07, 53.51], [6.90, 53.48]]
],
"type": "Polygon",
},
"id": "49",
"properties": {
"continent": "Africa",
"gdp_md_est": 35900.0,
"id": "BWA",
"iso_a3": "BWA",
"name": "Botswana",
"pop_est": 2214858,
},
"type": "Feature",
}
],
"type": "FeatureCollection",
}
feat = geom_obj(geom)
with alt.data_transformers.enable(consolidate_datasets=False):
spec = alt.Chart(feat).mark_geoshape().to_dict()
assert spec["data"]["values"][0]["id"] == "BWA"
|