Skip to content

Read GeoJson files as UTF-8 regardless of the locale - #2331

Open
b84330808 wants to merge 1 commit into
python-visualization:mainfrom
b84330808:fix-geojson-file-utf8
Open

b84330808 wants to merge 1 commit into
python-visualization:mainfrom
b84330808:fix-geojson-file-utf8

Conversation

@b84330808

Copy link
Copy Markdown

GeoJson("file.geojson") opens the file in text mode without an encoding, so it gets decoded with the locale encoding. GeoJSON is UTF-8 (RFC 7946), so on Windows any non-ASCII property is either garbled or raises. The repo's own tests/kuntarajat.geojson doesn't load under a Japanese locale:

UnicodeDecodeError: 'cp932' codec can't decode byte 0x84 in position 1700295: illegal multibyte sequence

With cp1252 (e.g. the Windows CI runners) the Finnish names load as mojibake instead, which may be what #1483 ran into.

The file is now read as bytes and handed to json.load, which detects the encoding itself. A UTF-8 BOM (some Windows tools write one) also stops breaking the load; before, that raised JSONDecodeError: Unexpected UTF-8 BOM on every platform.

The new test loads a file with non-ASCII properties, with and without a BOM. Without the fix the BOM case fails everywhere and the plain UTF-8 case fails on Windows.

GeoJson(filename) opened the file in text mode without an encoding, so
it was decoded with the locale encoding. On Windows that garbles or
fails on non-ASCII properties (the bundled kuntarajat.geojson raises
UnicodeDecodeError under cp932). Read the file as bytes and let json
detect the encoding, which also accepts a UTF-8 BOM.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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