Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,10 +1072,25 @@ def __init__(
def style_data(self) -> None:
"""Applies self.style_function to each feature of self.data."""

# Write the styles into copies of the dicts on the object path, the
# geometries and their properties, so the dict that was passed in is
# left as it was and other layers made from it don't pick them up.
# The arcs, which hold most of the data, are not copied.
*keys, last_key = self.object_path.split(".")
self.data = parent = dict(self.data)
for key in keys:
parent[key] = dict(parent[key])
parent = parent[key]
parent[last_key] = dict(parent[last_key])
if "geometries" in parent[last_key]:
parent[last_key]["geometries"] = [
dict(geometry) for geometry in parent[last_key]["geometries"]
]
for feature in self._get_geometries():
feature.setdefault("properties", {}).setdefault("style", {}).update(
self.style_function(feature)
) # noqa
properties = dict(feature.get("properties", {}))
properties["style"] = dict(properties.get("style", {}))
feature["properties"] = properties
properties["style"].update(self.style_function(feature))

def _get_geometries(self) -> list:
"""Return the selected TopoJSON object as a list of geometries."""
Expand Down
42 changes: 39 additions & 3 deletions tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

"""

import copy
import json
import os
import warnings
Expand Down Expand Up @@ -34,13 +35,13 @@ def test_topojson_non_collection_geometry(geometry_type):
}

map_ = folium.Map()
folium.TopoJson(
topojson = folium.TopoJson(
topology, "objects.shape", style_function=lambda feature: style
).add_to(map_)

map_.get_root().render()

assert topology["objects"]["shape"]["properties"]["style"] == style
assert topojson.data["objects"]["shape"]["properties"]["style"] == style


def test_topojson_non_collection_geometry_without_type():
Expand All @@ -60,7 +61,42 @@ def test_topojson_non_collection_geometry_without_type():

topojson.style_data()

assert topology["objects"]["shape"]["properties"]["style"] == {"color": "red"}
assert topojson.data["objects"]["shape"]["properties"]["style"] == {"color": "red"}


def test_topojson_style_does_not_change_input_data():
"""Layers made from the same dict keep their own style."""
topology = {
"type": "Topology",
"objects": {
"shapes": {
"type": "GeometryCollection",
"geometries": [
{"type": "Polygon", "arcs": [[0]], "properties": {"name": "A"}}
],
}
},
"arcs": [[[0, 0], [1, 0], [0, 1], [-1, -1]]],
"transform": {"scale": [1, 1], "translate": [0, 0]},
}
original = copy.deepcopy(topology)

map_ = folium.Map()
red = folium.TopoJson(
topology, "objects.shapes", style_function=lambda feature: {"fillColor": "red"}
).add_to(map_)
blue = folium.TopoJson(
topology, "objects.shapes", style_function=lambda feature: {"color": "blue"}
).add_to(map_)

map_.get_root().render()

def get_style(layer):
return layer.data["objects"]["shapes"]["geometries"][0]["properties"]["style"]

assert get_style(red) == {"fillColor": "red"}
assert get_style(blue) == {"color": "blue"}
assert topology == original


@pytest.mark.parametrize("detail_type", [folium.GeoJsonPopup, folium.GeoJsonTooltip])
Expand Down