|
17 | 17 | from folium.utilities import JsCode |
18 | 18 |
|
19 | 19 |
|
| 20 | +@pytest.mark.parametrize("geometry_type", ["Polygon", "MultiPolygon"]) |
| 21 | +def test_topojson_non_collection_geometry(geometry_type): |
| 22 | + """TopoJson supports objects that are not GeometryCollections.""" |
| 23 | + style = {"color": "red"} |
| 24 | + topology = { |
| 25 | + "type": "Topology", |
| 26 | + "objects": { |
| 27 | + "shape": { |
| 28 | + "type": geometry_type, |
| 29 | + "arcs": [[0]] if geometry_type == "Polygon" else [[[0]]], |
| 30 | + } |
| 31 | + }, |
| 32 | + "arcs": [[[0, 0], [1, 0], [0, 1], [-1, -1]]], |
| 33 | + "transform": {"scale": [1, 1], "translate": [0, 0]}, |
| 34 | + } |
| 35 | + |
| 36 | + map_ = folium.Map() |
| 37 | + folium.TopoJson( |
| 38 | + topology, "objects.shape", style_function=lambda feature: style |
| 39 | + ).add_to(map_) |
| 40 | + |
| 41 | + map_.get_root().render() |
| 42 | + |
| 43 | + assert topology["objects"]["shape"]["properties"]["style"] == style |
| 44 | + |
| 45 | + |
| 46 | +def test_topojson_non_collection_geometry_without_type(): |
| 47 | + """TopoJson does not require a type key to apply styles.""" |
| 48 | + topology = { |
| 49 | + "type": "Topology", |
| 50 | + "objects": {"shape": {"arcs": [[0]]}}, |
| 51 | + "arcs": [[[0, 0], [1, 0], [0, 1], [-1, -1]]], |
| 52 | + "transform": {"scale": [1, 1], "translate": [0, 0]}, |
| 53 | + } |
| 54 | + |
| 55 | + topojson = folium.TopoJson( |
| 56 | + topology, |
| 57 | + "objects.shape", |
| 58 | + style_function=lambda feature: {"color": "red"}, |
| 59 | + ) |
| 60 | + |
| 61 | + topojson.style_data() |
| 62 | + |
| 63 | + assert topology["objects"]["shape"]["properties"]["style"] == {"color": "red"} |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize("detail_type", [folium.GeoJsonPopup, folium.GeoJsonTooltip]) |
| 67 | +def test_topojson_non_collection_geometry_detail(detail_type): |
| 68 | + """TopoJson popup and tooltip fields support non-collection geometries.""" |
| 69 | + topology = { |
| 70 | + "type": "Topology", |
| 71 | + "objects": { |
| 72 | + "shape": { |
| 73 | + "type": "Polygon", |
| 74 | + "arcs": [[0]], |
| 75 | + "properties": {"name": "A"}, |
| 76 | + } |
| 77 | + }, |
| 78 | + "arcs": [[[0, 0], [1, 0], [0, 1], [-1, -1]]], |
| 79 | + "transform": {"scale": [1, 1], "translate": [0, 0]}, |
| 80 | + } |
| 81 | + |
| 82 | + map_ = folium.Map() |
| 83 | + topojson = folium.TopoJson(topology, "objects.shape").add_to(map_) |
| 84 | + detail_type(fields=["name"]).add_to(topojson) |
| 85 | + |
| 86 | + assert "name" in map_.get_root().render() |
| 87 | + |
| 88 | + |
20 | 89 | @pytest.fixture |
21 | 90 | def tmpl(): |
22 | 91 | yield (""" |
|
0 commit comments