2

I'm trying to colorize each region on the map based on the residence of each user. For instance, if the user lives in Porto, the regions Braga and Aveiro should be red. On the other case, if the user lives in Lisbon, Setúbal must be red.

Each user is in a feature group.

I would like to keep iterating the dataframe

Here is a simple sample of the script:

import folium
import json
import pandas as pd

path = r'file.geojson'
with open(path) as file:
    data = json.load(file)

list_users = [
    [1,'Lisboa'],
    [2,'Porto'],
    [3,'Porto']
]
df_users = pd.DataFrame(list_users,columns = ['ID','Place'])


my_map = folium.Map(location=[39, -5],
        zoom_start=7, min_zoom=4, max_zoom=15,
        max_bounds=True,
        tiles="cartodbpositron"
        )

d = {} # dict to save the feature groups

for _,user in df_users.iterrows():


    fg_name = "FG_{0}".format(user.ID)
    d[fg_name] = folium.FeatureGroup(name=user.ID,overlay=True,show=False)
    fg_value = d[fg_name]


    if user.Place == 'Lisboa':
        list_red_places = ['Setúbal']
    elif user.Place == 'Porto':
        list_red_places = ['Braga','Aveiro']


    folium.GeoJson(data=data,
                style_function=lambda x: {'fillColor': 'red' if x['properties']['Distrito'] in list_red_places else 'green',
                            'fillOpacity': .5 if x['properties']['Distrito'] in list_red_places else 0.1,
                            'weight': 0.1    
                        }).add_to(fg_value) 


    fg_value.add_to(my_map)
folium.LayerControl().add_to(my_map)
my_map

All the features have the same red and green regions. I believe that the issue is in the stylefuncion since that the lambda function retrieves only the last iteration.

All the 3 users have the same red regions. the 1st user should have a different one.

Here's the map for the 3 users:

User 1:

User 2:

User 3:

Jorge Mendes
  • 77
  • 1
  • 9

1 Answers1

2

I struggled with my assignments as well. After reading the description in Folium: color mapping when adding GeoJson to the map, I guessed that the cause was the same content, with the style function being executed after the execution of the loop process. I didn't see the geojson presentation, so I got it from here.

import folium

my_map = folium.Map(location=[39, -5],
        zoom_start=7, min_zoom=4, max_zoom=15,
        max_bounds=True,
        tiles="cartodbpositron"
        )

fg_1 = folium.FeatureGroup(name='1', overlay=True, show=False)
fg_2 = folium.FeatureGroup(name='2', overlay=True, show=False)
fg_3 = folium.FeatureGroup(name='3', overlay=True, show=False)

       
list_red_places = ['Setúbal']
folium.GeoJson(
    geo_data,
    style_function=lambda x: {
        'fillColor': 'red'
        if x['properties']['name'] in list_red_places
        else 'green',
        'fillOpacity': .5
        if x['properties']['name'] in list_red_places 
        else 0.1,
        'weight': 0.1 
    },
).add_to(fg_1)

list_red_places2 = ['Braga','Aveiro']
folium.GeoJson(
    geo_data,
    style_function=lambda x: {
        'fillColor': 'red'
        if x['properties']['name'] in list_red_places2
        else 'green',
        'fillOpacity': .5
        if x['properties']['name'] in list_red_places2 
        else 0.1,
        'weight': 0.1 
    },
).add_to(fg_2)

fg_1.add_to(my_map)
fg_2.add_to(my_map)
#fg_3.add_to(my_map)

folium.LayerControl().add_to(my_map)

my_map

FG_1 enter image description here

FG_2 enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32