Skip to content

Quote GeoJson identifier properties that aren't valid JS identifiers - #2296

Merged
hansthen merged 1 commit into
python-visualization:mainfrom
adarshsm:fix/1782-geojson-identifier-special-keys
Oct 11, 2026
Merged

hansthen merged 1 commit into
python-visualization:mainfrom
adarshsm:fix/1782-geojson-identifier-special-keys

Conversation

@adarshsm

Copy link
Copy Markdown
Contributor

Closes #1782.

The bug

If the features in a GeoJson have no unique id, find_identifier() looks for a property with a unique value per feature. It then writes it into the styler/highlighter switch with dot notation: feature.properties.<key>. If the key isn't a valid JavaScript identifier, the generated code is wrong:

property key generated what JavaScript does
Time (UTC) (the issue's data) switch(feature.properties.Time (UTC)) calls feature.properties.Time(UTC) → ReferenceError: UTC is not defined
property-key switch(feature.properties.property-key) evaluates feature.properties.property - key → ReferenceError
osm.id n/a get_feature_id splits on . and raises KeyError: 'osm' in Python during render()

In the browser the styler throws when Leaflet styles the first feature, so the layer isn't drawn. That's why the reporter's features disappear only when style_function is passed: without a style or highlight function no identifier is needed.

I reproduced it with the demo.json attached to the issue on current main. All 35 features are LineStrings without id, and the first property with unique values is "Time (UTC)". I ran the generated _styler function in node against every feature:

  • before: ReferenceError: UTC is not defined
  • after: styled 35 of 35

The fix

  • find_identifier() keeps feature.properties.<key> when key.isidentifier(), so output for ordinary keys is unchanged. Other keys use bracket notation with the key encoded by the existing HTML-safe _to_escaped_json, e.g. feature.properties["Time (UTC)"].
  • GeoJsonStyleMapper.get_feature_id() reads that bracket form back with json.loads instead of splitting the expression on ..

Tests

  • test_geojson_find_identifier: the two property-key cases asserted the broken dot form. They now expect feature.properties["property-key"]. I added an identifier key (property_key) to cover the dot-notation branch.
  • New test_geojson_style_identifier_with_special_characters, parametrized over Time (UTC), @id and osm.id. It renders a styled map, checks the switch(...) expression, and checks the resulting style map.
  • All new or changed cases fail on main and pass with this change.
  • pytest tests (without selenium/playwright/snapshots): 380 passed. 5 failed, and those same 5 also fail on unmodified main in my environment: three PNG-export tests (ImportError), test_timedynamic_geo_json (geodatasets not installed) and test_icon_invalid_marker_colors (no warning emitted). black --check and ruff check pass on the touched files.

🤖 Generated with Claude Code

When GeoJson features have no unique `id`, `find_identifier` picks a
property with unique values and emits it with dot notation in the styler
and highlighter `switch`. A key like "Time (UTC)" became
`feature.properties.Time (UTC)`, which Javascript runs as a call and throws
`ReferenceError: UTC is not defined`, so nothing in the layer was drawn
(python-visualization#1782). A key like "property-key" turned into a subtraction, and a key
with a dot ("osm.id") made `get_feature_id` raise `KeyError` while
rendering.

Keys that aren't identifiers now use bracket notation with an escaped
JSON string, and `get_feature_id` reads that form back.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@hansthen
hansthen marked this pull request as ready for review October 11, 2026 11:40
@hansthen
hansthen merged commit 9cdf58e into python-visualization:main Oct 11, 2026
16 checks passed
@hansthen

Copy link
Copy Markdown
Collaborator

Good catch. I see no problems.

This branch was successfully deployed

1 active deployment
pypi — bdae86d7 Deployed Oct 11, 2026 by adarshsm via packages #1959
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.

Features not displayed with style_function

2 participants