Skip to content

Fix TopoJson rendering for single-geometry objects - #2258

Closed
adarshsm wants to merge 1 commit into
python-visualization:mainfrom
adarshsm:fix/1816-topojson-single-geometry
Closed

Fix TopoJson rendering for single-geometry objects#2258
adarshsm wants to merge 1 commit into
python-visualization:mainfrom
adarshsm:fix/1816-topojson-single-geometry

Conversation

@adarshsm

Copy link
Copy Markdown

Closes #1816.

The bug

A TopoJSON object can be either a GeometryCollection (which has a geometries list) or a single geometryPolygon, MultiPolygon, LineString, etc. — which has no geometries key and is itself the feature. TopoJson.style_data() assumed the former and did resolved_object["geometries"] unconditionally, so any single-geometry object crashed on render:

File ".../folium/features.py", line 981, in style_data
    geometries = recursive_get(self.data, self.object_path.split("."))["geometries"]
KeyError: 'geometries'

Minimal reproducer (from the issue, trimmed):

import folium

topo = {
    "type": "Topology",
    "transform": {"scale": [1, 1], "translate": [0, 0]},
    "objects": {"A": {"type": "Polygon", "id": "A", "arcs": [[0]]}},
    "arcs": [[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]],
}
folium.TopoJson(topo, "objects.A").add_to(folium.Map())  # KeyError: 'geometries' on render

The fix

Branch on the object's type: iterate geometries for a GeometryCollection (unchanged), otherwise treat the object itself as the single feature to style. The JS side (topojson.feature) already handles both shapes, so only the Python styling pass needed the guard.

Tests

Added test_topo_json_single_geometry, which renders a single-Polygon TopoJSON and asserts the style_function is applied to the geometry. It fails with KeyError on main and passes with this change; the existing GeometryCollection path is unchanged and still covered.

A TopoJSON object may be a single geometry (Polygon, MultiPolygon, ...)
rather than a GeometryCollection. Single geometries have no "geometries"
key, so TopoJson.style_data() raised KeyError: 'geometries' and the map
failed to render. Style the object itself in that case.

Closes python-visualization#1816
@adarshsm

Copy link
Copy Markdown
Author

Closing this — I hadn't spotted that #2250 already addresses this issue (and covers a second crash site in the popup/tooltip path that this PR misses). #2250 is the more complete fix; deferring to it. Apologies for the noise.

@adarshsm adarshsm closed this Jul 31, 2026
@adarshsm
adarshsm deleted the fix/1816-topojson-single-geometry branch July 31, 2026 12:01
@KhadeerBasha1232

Copy link
Copy Markdown

Thanks for deferring to this one, @adarshsm — and thanks again for catching the second crash site in the Popup/Tooltip path during review, that made the fix in #2250 much more complete. Appreciate all the help!

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.

TopoJSON fails with Polygon/MultiPolygon objects

2 participants