Hi all, greeting from Brasil!
I’m trying to create a dash app to display information of covid-related deaths in Brasil, not only in the whole country but also in its states, intermediate regions and cities, so as population could be well informed before taking decisions.
I’m struggling to display information. So far I’'ve been able to compile all the information and produce a map, but such map is blank.
Here below the code:
import pandas as pd
import geopandas as gpd
import plotly.graph_objects as go
import plotly.offline as pyo
import json
Reading shapefile
path_zip_BR_estados = ‘zip://Deployment/Maps/br_unidades_da_federacao.zip’ #unidades da federação = estados = states
mapa_estados = gpd.read_file(path_zip_BR_estados)
Reading data about covid in Brasil.
data_covidbr = pd.read_csv(“https://brasil.io/dataset/covid19/caso?format=csv”,
dtype = {‘city_ibge_code’: ‘object’},
parse_dates = [‘date’] )
Organizing data to reach only state-related numbers
data_covidbr = data_covidbr[data_covidbr[‘place_type’] == ‘city’] ##removing summarized info about states.
last = data_covidbr[data_covidbr[‘is_last’]] #take only the most up-to-date values
df_states = last.groupby(‘state’).sum().reset_index()[[‘state’, ‘confirmed’, ‘deaths’]] ##summarizing from city level to state level: more accurate data
Converting shapefile to geojson
mapa_estados_WGS84 = mapa_estados.to_crs(epsg=4326)
mapa_estados_WGS84.to_file(‘area_states_json’, driver = “GeoJSON”)
with open(‘area_states_json’) as geofile:
area_states_json = json.load(geofile)
All geojson and data in place, let’s create the choropleth mapbox
trace = go.Choroplethmapbox(geojson=area_states_json,
featureidkey = ‘SIGLA_UF’,
locations=df_states[‘state’],
z=df_states[‘deaths’],
colorscale=“RdPu”,
zauto = True,
marker_opacity=0.5,
marker_line_width=0, visible = True)
data = [trace]
layout = go.Layout(title = ‘COVID-BR deaths in BRA states’)
fig = go.Figure(data = data, layout = layout)
fig.update_layout(mapbox_style=“carto-positron”,
mapbox_zoom=3,
mapbox_center = {“lat”: -15,
“lon”: -60})
fig.update_layout(margin={“r”:0,“t”:0,“l”:0,“b”:0})
pyo.plot(fig)
The output is an .html file in which we can see a nice map of South America but without no data on it.
What am I doing wrong?
Here below I’ll share the links for covid data and shapefile used:
covid br data: https://brasil.io/dataset/covid19/caso?format=csv (the same used in code).
states shapefile: https://www.ibge.gov.br/geociencias/organizacao-do-territorio/estrutura-territorial/15774-malhas.html?=&t=downloads (please look for br_unidades_da_federacao.zip file).
Many thanks in advance!
Fábio