Maps with Altair and GeoPandas

Hi!
Has anyone successfully implemented altair maps on their streamlit apps?

I keep getting this error

the code itself is just reading a shapefile:

import geopandas as gpd
import altair as alt
import streamlit as st

test = gpd.read_file('oakland-2.shp')
chart = alt.Chart(test).mark_geoshape()
st.altair_chart(chart)

it seems to work fine in jupyter lab

any help would be greatly appreciated!

Hi @chekos !

Unfortunately, Streamlit’s internals don’t natively handle the geometry dtype from geopandas, so you need to work around this type and convert it into some native dtype Streamlit can handle.

You can take a look at this solution and this solution and see if one of those helps.

Do tell us about your progress !

Cheers,
Fanilo

Thanks!

This is very helpful.

I tried changing the geodataframe to a json object and I think I’m getting closer to a solution but it still doesn’t work.

import geopandas as gpd
import altair as alt
import streamlit as st

test = gpd.read_file('oakland-2.shp')
inline_data = alt.InlineData(test.to_json())
chart = alt.Chart(inline_data).mark_geoshape()
st.altair_chart(chart)

It works on jupyter lab but not on streamlit

Now, I don’t get any errors - I just can’t see the data :laughing:

Hmmm yeah, something’s odd in the compiled Vega on Streamlit’s side. In Jupyterlab the values of the shape are inlined in the compiled Vega but they don’t appear on Streamlit’s side.

I’m not sure, it looks like Streamlit’s marshalling deletes Altair Inline data but that does look strange, let’s dig further :sweat_smile:

1 Like