Skip to content

Commit beffc9e

Browse files
b84330808claude
andauthored
Read GeoJson files as UTF-8 regardless of locale (#2331)
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>
1 parent 5295920 commit beffc9e

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎folium/features.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,10 @@ def process_data(self, data: Any) -> dict:
756756
else: # This is a filename
757757
if not self.embed:
758758
self.embed_link = data
759-
with open(data) as f:
760-
return json.loads(f.read())
759+
# Read bytes so json can detect the encoding (UTF-8 per RFC 7946)
760+
# instead of decoding with the platform's locale encoding.
761+
with open(data, "rb") as f:
762+
return json.load(f)
761763
elif hasattr(data, "__geo_interface__"):
762764
self.embed = True
763765
if hasattr(data, "to_crs"):

‎tests/test_features.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,24 @@ def test_geojson_tooltip():
290290
), "GeoJsonTooltip GeometryCollection test failed."
291291

292292

293+
@pytest.mark.parametrize("encoding", ["utf-8", "utf-8-sig"])
294+
def test_geojson_from_utf8_file(tmp_path, encoding):
295+
data = {
296+
"type": "FeatureCollection",
297+
"features": [
298+
{
299+
"type": "Feature",
300+
"properties": {"name": "東京都", "alt": "Hämeenlinna"},
301+
"geometry": {"type": "Point", "coordinates": [139.69, 35.69]},
302+
}
303+
],
304+
}
305+
path = tmp_path / "data.geojson"
306+
path.write_text(json.dumps(data, ensure_ascii=False), encoding=encoding)
307+
308+
assert GeoJson(str(path)).data == data
309+
310+
293311
# GeoJsonMarker type validation.
294312
def test_geojson_marker():
295313
m = folium.Map([30.4, -97.5], zoom_start=10)

0 commit comments

Comments
 (0)