Michal
Michal

Reputation: 259

Displaying country in choropleth map - UnicodeDecodeError: 'charmap' codec can't decode byte 0x88

I'm trying to display unemployment of Czech Republic's counties in choropleth map.

I have json coordinates and unemployment data saved in csv file. But Im getting this error:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x88 in position 211750: character maps to undefined.

Which is weird because when I run this code (basically same code with different data): https://python-graph-gallery.com/292-choropleth-map-with-folium/ everything works fine.

I have a feeling that it's not possible to display CZ counties in choropleth map or is it?

You can find files I use right here: https://github.com/MichalLeh/CZ-map

import pandas as pd
import folium

# Load the shape of the zone 
state_geo = 'J:/CZ-counties.json'
# Load the unemployment value of each state (county)
state_data = pd.read_csv('J:/CZ-unemploy.csv')
# Initialize the map:
m = folium.Map([15, 74], zoom_start=6)

# Add the color for the chloropleth:
choropleth = folium.Choropleth(
    geo_data=state_geo,
    name='choropleth',
    data=state_data,
    columns=['Name', 'Un'],
    key_on='feature.id',
    fill_color='YlGn',
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name='Unemployment',
).add_to(m)
folium.LayerControl(collapsed=True).add_to(m)
# Save to html
m.save('map.html')

Terminal output:

Traceback (most recent call last):
  File "J:\testCz", line 12, in <module>
    choropleth = folium.Choropleth(
File "C:\Users\Michal\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\folium\features.py", line 1247, in __init__
    self.geojson = GeoJson(
File "C:\Users\Michal\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\folium\features.py", line 453, in __init__
    self.data = self.process_data(data)
File "C:\Users\Michal\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\folium\features.py", line 491, in process_data
    return json.loads(f.read())
File "C:\ProgramFiles\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1008.0_x64__qbz5n2kfra8p0\lib\encodings\cp1250.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x88 in position 211750: character maps to undefined.

Upvotes: 1

Views: 629

Answers (1)

Benjo
Benjo

Reputation: 147

It's because your json file is UTF-8. Add the UTF-8 enconding to your Choropleth class parameters:

choropleth = folium.Choropleth(
geo_data=state_geo,
name='choropleth',
data=state_data,
columns=['Name', 'Un'],
key_on='feature.id',
fill_color='YlGn',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Unemployment',
enconding='UTF-8'
).add_to(m)

See post: Python - UnicodeDecodeError:

You could always run chardet to check:

import chardet    
rawdata=open(state_geo,'rb').read()
chardet.detect(rawdata)

Upvotes: -1

Related Questions