Issue with Displaying Different Colors for Multiple Livestock in Streamlit-Folium

60 Views Asked by At

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:

1

There are 1 best solutions below

0
Lucas On
    def display_map(self, df, selected_states, selected_livestocks, color_mapping):

    if '전체' not in selected_states:
        df = df[df['sig_kor_nm'].isin(selected_states)]
    if '전체' not in selected_livestocks:
        df = df[df['prt_type_nm'].isin(selected_livestocks)]
    

    df_copy = df.copy()
    df_copy.loc[:, 'geometry'] = df_copy['geom_coordinates'].apply(lambda x: Polygon(ast.literal_eval(x)[0]) if pd.notnull(x) else None)
    df = df_copy
    gdf = gpd.GeoDataFrame(df, geometry='geometry')
   

    m = folium.Map(location=[36.429, 127.977], scrollWheelZoom=True, tiles='cartodbdark_matter', zoom_start=6.5)  # MapQuest Open Aerial /cartodbdark_matter

    geojson_data = gdf.to_json()
    folium.GeoJson(geojson_data,
                   style_function=lambda x: {
                       'color': color_mapping.get(x['properties']['prt_type_nm'], 'gray'),
                       'fillColor': color_mapping.get(x['properties']['prt_type_nm'], 'gray')
                       }
                       ).add_to(m)
    m.save('map.html')

solved!