Plot colored polygons with geodataframe in folium

后端 未结 3 1238
被撕碎了的回忆
被撕碎了的回忆 2021-02-02 17:14

I\'m trying to plot radar data in folium, and I\'m almost there. I followed this example (Contour plot data (lat,lon,value) within boundaries and export GeoJSON) to get my data

3条回答
  •  无人及你
    2021-02-02 17:58

    I think I figured it out. In my previous code, polygon.get_facecolor() returns a list of RGBA values ranging from 0-1. I added this function (modified from this post):

    def convert_to_hex(rgba_color) :
        red = str(hex(int(rgba_color[0]*255)))[2:].capitalize()
        green = str(hex(int(rgba_color[1]*255)))[2:].capitalize()
        blue = str(hex(int(rgba_color[2]*255)))[2:].capitalize()
    
        if blue=='0':
            blue = '00'
        if red=='0':
            red = '00'
        if green=='0':
            green='00'
    
        return '#'+ red + green + blue
    

    to convert it to a hex string. Then:

    gdf['RGBA'] = convert_to_hex(colors)
    

    Then to plot the colors in folium, I do:

    maploc = folium.Map(location=[42.377157,-71.236088],zoom_start=10,tiles="Stamen Toner")
    
    colors = []
    folium.GeoJson(
        gdf,
        style_function=lambda feature: {
            'fillColor': feature['properties']['RGBA'],
            'color' : feature['properties']['RGBA'],
            'weight' : 1,
            'fillOpacity' : 0.5,
            }
        ).add_to(maploc)
    

    and that created a really nice looking plot! (The property name is a bit misleading - it's not actually RGBA values, but hex strings.)

提交回复
热议问题