From aed327cc2024495792884dbce5b5e724b235a797 Mon Sep 17 00:00:00 2001 From: KhadeerBasha1232 Date: Fri, 20 Nov 2026 19:50:49 +0530 Subject: [PATCH] Allow float feature ids when styling GeoJson RFC 7946 allows any number as a feature id. find_identifier picks feature.id when every feature has a unique id, but get_feature_id then asserted the value was a str or int, so a style_function or highlight_function on data with ids like 1.5 failed with a bare AssertionError. Float ids work fine in the generated Javascript switch, so accept them too. Co-Authored-By: Claude Opus 5.5 --- folium/features.py | 12 ++++++++---- tests/test_features.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/folium/features.py b/folium/features.py index 136f5e652b..ae8f91ac13 100644 --- a/folium/features.py +++ b/folium/features.py @@ -866,7 +866,7 @@ def render(self, **kwargs): super().render() -TypeStyleMapping = dict[str, str | list[str | int]] +TypeStyleMapping = dict[str, str | list[str | int | float]] class GeoJsonStyleMapper: @@ -913,8 +913,12 @@ def _create_mapping(self, func: Callable, switch: str) -> TypeStyleMapping: self._set_default_key(mapping) return mapping - def get_feature_id(self, feature: dict) -> str | int: - """Return a value identifying the feature.""" + def get_feature_id(self, feature: dict) -> str | int | float: + """Return a value identifying the feature. + + A GeoJSON feature id can be a string or any number (RFC 7946), so + float ids are allowed as well. + """ prefix = "feature.properties[" if self.feature_identifier.startswith(prefix): key = json.loads(self.feature_identifier[len(prefix) : -1]) @@ -922,7 +926,7 @@ def get_feature_id(self, feature: dict) -> str | int: else: fields = self.feature_identifier.split(".")[1:] value = functools.reduce(operator.getitem, fields, feature) - assert isinstance(value, (str, int)) + assert isinstance(value, (str, int, float)) return value @staticmethod diff --git a/tests/test_features.py b/tests/test_features.py index 5f2ae7b37c..e8f33d119f 100644 --- a/tests/test_features.py +++ b/tests/test_features.py @@ -467,6 +467,38 @@ def test_geojson_empty_features_with_styling(): m.get_root().render() +def test_geojson_style_float_feature_ids(): + """Float feature ids are valid GeoJSON and can be used for styling.""" + data = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "id": feature_id, + "properties": {}, + "geometry": {"type": "Point", "coordinates": [0, 0]}, + } + for feature_id in (1.5, 2.5, 3.5) + ], + } + m = Map() + geojson = GeoJson( + data, + style_function=lambda f: {"color": "red" if f["id"] == 1.5 else "blue"}, + highlight_function=lambda f: {"weight": 3}, + ).add_to(m) + assert geojson.feature_identifier == "feature.id" + rendered = m.get_root().render() + assert geojson.style_map == { + '{"color": "red"}': [1.5], + "default": '{"color": "blue"}', + } + assert geojson.highlight_map == {"default": '{"weight": 3}'} + assert "case 1.5:" in rendered + # The original ids are kept. + assert [f["id"] for f in geojson.data["features"]] == [1.5, 2.5, 3.5] + + def test_geojson_event_handler(): """Test that event handlers are properly generated""" m = Map()