Skip to content

Allow float feature ids when styling GeoJson - #2369

Open
KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:geojson-float-feature-ids
Open

KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:geojson-float-feature-ids

Conversation

@KhadeerBasha1232

Copy link
Copy Markdown
Contributor

Allow float feature ids when styling GeoJson

The problem

A GeoJSON feature id can be a string or any number (RFC 7946, section 3.2). With float ids, GeoJson fails to render as soon as a style_function or highlight_function is set:

import folium

def square(x):
    return [[[x, 0], [x + 10, 0], [x + 10, 10], [x, 10], [x, 0]]]

data = {
    "type": "FeatureCollection",
    "features": [
        {"type": "Feature", "id": fid, "properties": {"name": f"zone {fid}"},
         "geometry": {"type": "Polygon", "coordinates": square(x)}}
        for fid, x in ((101.1, 0), (101.2, 15), (101.3, 30))
    ],
}
colors = {101.1: "red", 101.2: "orange", 101.3: "green"}

m = folium.Map((5, 20), zoom_start=4)
folium.GeoJson(
    data,
    style_function=lambda f: {"color": colors[f["id"]], "fillOpacity": 0.4},
    highlight_function=lambda f: {"weight": 6},
    tooltip=folium.GeoJsonTooltip(fields=["name"]),
).add_to(m)
m.save("map.html")
  File "folium/features.py", line 911, in _create_mapping
    feature_id = self.get_feature_id(feature)
  File "folium/features.py", line 925, in get_feature_id
    assert isinstance(value, (str, int))
AssertionError

The cause

GeoJson.find_identifier() picks feature.id whenever every feature has a unique id, whatever its type. GeoJsonStyleMapper.get_feature_id() then asserts that the id is a str or int, so float ids fail with a bare AssertionError that doesn't say what's wrong.

The fix

Accept float in get_feature_id, and add it to the type hints. The id is only used as a case value in the generated switch(feature.id). tojson writes floats as plain numbers (case 101.1:), and they match there just like ints do.

I relaxed the assertion rather than making find_identifier skip float ids. Skipping them would fall through to the last option, which overwrites the user's ids with "0", "1", ... in the embedded data. That would break any on_each_feature code that relies on feature.id.

After

The example above renders. Each zone gets its own color, and in Chrome hovering a zone highlights it (weight 3 → 6) and shows its tooltip, with no console errors:

Map with a red, an orange and a green square, styled by their float feature ids

Tests

  • tests/test_features.py::test_geojson_style_float_feature_ids builds a style_function and highlight_function on features with float ids. It checks the style and highlight maps, case 1.5: in the output, and that the original ids are kept. It fails with the AssertionError without the change.
  • The rest of tests/test_features.py, ruff and black pass.

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 <noreply@anthropic.com>

This branch was previously deployed

1 inactive deployment
pypi — aed327cc Deployed Nov 20, 2026 by KhadeerBasha1232 via packages #2126
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant