0

I have 3 categories of data :

  1. Hospitals
  2. Schools
  3. Parks

For each category I have the locations of these features and a 500m walking pedshed. I need to put all hospital locations and their pedsheds in one folium subGroup (and so on for the other 2 categories as well).

The problem is that the pins are coming out as different colours but the polygons are not. I need the pins and the polygons both to have the same colour.enter image description here

Here, 'sgroup' is the key in the dictionary which takes values 'Hospital', 'Pri School', and 'Park' while the 'data' is the corresponding geodataframe.

for sgroup, data in group_dict.items():
        cat = sgroup
        sgroup = plugins.FeatureGroupSubGroup(all_subgroups, f'{sgroup}s')
        
        
        feature_style = {'color': icon_dict.get(cat)[2], 'weight': 0.5, 'fillOpacity': 0.25}
        
        # folium.GeoJson(data.to_crs(4326).dissolve(), style_function=lambda x: feature_style).add_to(sgroup)
        
        for poly in data.to_crs(4326).geometry:
            folium.GeoJson(poly, style_function=lambda x: feature_style).add_to(sgroup)
            
        point_locs = ITC_locs_in_the_ward.query("type == @cat").to_crs(4326)
        for lat, long, text in zip(point_locs['Latitude'], point_locs['Longitude'], point_locs['Name']):
            folium.Marker(location=[lat, long],
                          popup=text,
                          icon=folium.Icon(icon=icon_dict.get(cat)[0],
                                           color=icon_dict.get(cat)[2],
                                           prefix=icon_dict.get(cat)[1])
                         ).add_to(sgroup)
        map_with_subgroups.add_child(sgroup)
Seismoditya
  • 21
  • 1
  • 3

1 Answers1

1

Figured it out. It turns out that you can't specify the feature_style dictionary and reference it inside the lambda function like I had done. See this SO page that talks about the same : Similar issue. Apparently, you have to put this style function dictionary inside the lambda function and also specify the color parameter (its a bit tough to explain). I just changed this line :

for poly in data.to_crs(4326).geometry:
    folium.GeoJson(poly, style_function=lambda x: feature_style).add_to(sgroup)

to this :

folium.GeoJson(data.to_crs(4326).dissolve(),
               style_function=lambda x, color=color: {'color': color,
                                                      'weight': 0.5,
                                                      'fillOpacity': 0.25}
                      ).add_to(sgroup)

Notice how I have defined the 'color' parameter in the lambda function. This is what the stack overflow question I linked above suggests.

HOWEVER, I would like to note that if you changed this color parameter in the lambda function to this : "color=clr" where 'clr' is of course a variable you have defined above, the code does not work! Maybe I don't understand lambda functions that well yet but the code works only when I specify the variable as "color=color". Weird. But that's how it is.

Seismoditya
  • 21
  • 1
  • 3