I'm currently applying a map visualization app using Streamlit and Folium. I'm trying to display multiple livestock (chicken, pig, cow) on the map, each with a different color. However, I'm facing an issue where only one color is displayed in the app.
Problem Description: I'm seeking assistance on how to use Streamlit-Folium to apply different colors to multiple livestock on the map.
Code Example:
def display_map(df, selected_states, selected_livestocks):
if selected_states:
df = df[df['SIG_KOR_NM'].isin(selected_states)]
if 'ALL' not in selected_livestocks:
df = df[df['property_type_name'].isin(selected_livestocks)]
color_mapping = {
'Unknown': 'black',
'chiecken': 'green',
'pig': 'pink',
'cow': 'orange'
}
if not df.empty:
df['geometry'] = df['geometry_coordinates'].apply(
lambda x: Polygon(ast.literal_eval(x)[0]) if pd.notnull(x) else None
)
map = folium.Map(location=[35.4681593,128.4765802], zoom_start=1, scrollWheelZoom=False, tiles='CartoDB positron')
for _, row in df.iterrows():
if row['geometry'] is not None:
if row['geometry'].is_valid:
# st.write(row)
property_type_name = row['property_type_name']
color = color_mapping.get(row['property_type_name'], 'gray')
folium.GeoJson(row['geometry'],
style_function = lambda x: {
'color': color_mapping.get(property_type_name, 'gray'),
'fillColor': color_mapping.get(property_type_name, 'gray')}).add_to(map)
folium_static(map)
else :
map = folium.Map(location=[ , ], zoom_start=7, scrollWheelZoom=False, tiles='CartoDB positron')
return map
I've made several attempts to solve this problem, including the following:
solved!