【问题标题】:Folium GeoJson Custom Color MapFolium GeoJson 自定义颜色图
【发布时间】:2020-09-18 17:25:47
【问题描述】:

我尝试使用 Folium 在地图上显示一个多部分 shapefile。代码如下:

import folium
import gdal
import geopandas as gpd
import branca.colormap as cm

gdf = gpd.read_file("field.shp")

def rank_colormap(gdf):
    if gdf['RANK'] is 1.0:
        return 'red'
    if gdf['RANK'] is 2.0 or 3.0:
        return 'orange'
    if gdf['RANK'] is 4.0:
        return 'gold'
    if gdf['RANK'] is 5.0 or 6.0:
        return 'yellow'
    if gdf['RANK'] is 7.0:
        return 'greenyellow'
    if gdf['RANK'] is 8.0 or 9.0:
        return 'lime'
    elif gdf['RANK'] is 10.0:
        return 'green'

#create map object
m = folium.Map(location=[49.112675, -104.104781], zoom_start=15)
folium.GeoJson(data=gdf, style_function=lambda feature: {
        'fillColor': rank_colormap(gdf),
        'color': 'black',
        'weight': '0.5',
        'fill': True,
        'fill_opacity' : '1'
    }).add_to(m)
tile = folium.TileLayer(
        tiles = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
        attr = 'Esri',
        name = 'Esri Satellite',
        overlay = False,
        control = True
       ).add_to(m)


legendcolormap = cm.LinearColormap(colors=['red', 'orange', 'gold' ,'yellow', 'greenyellow', 'lime', 'green'], vmin=1, vmax=10)
legendcolormap.add_to(m)


#global tooltip
tooltip = 'Click For More Info'

gdf 当前看起来像这样我正在尝试将颜色映射到相应的“RANK”值:

但是,我的填充颜色没有正确调整,只剩下一种颜色:

如何正确地将颜色图分配给我的 folium 贴图?

【问题讨论】:

    标签: python folium


    【解决方案1】:

    解决了:

    def getcolor(feature):
        if feature['properties']['RANK'] == 1.0:
            return 'red'
        if feature['properties']['RANK'] == 2.0:
            return 'orange'
        if feature['properties']['RANK'] == 3.0:
            return 'orange'
        if feature['properties']['RANK'] == 4.0:
            return 'gold'
        if feature['properties']['RANK'] == 5.0:
            return 'yellow'
        if feature['properties']['RANK'] == 6.0:
            return 'yellow'
        if feature['properties']['RANK'] == 7.0:
            return 'greenyellow'
        if feature['properties']['RANK'] == 8.0:
            return 'lime'
        if feature['properties']['RANK'] == 9.0:
            return 'lime'
        if feature['properties']['RANK'] == 10.0:
            return 'green'
        else:
            return 'gray'
    
    folium.GeoJson(gdf1, smooth_factor = 1, style_function = lambda feature: {
            'fillColor': getcolor(feature),
            'weight': 0,
            'fillOpacity': 0.8,
    }).add_to(fg1)
    
    

    【讨论】:

      猜你喜欢
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 2021-12-29
      • 2021-05-03
      • 2022-08-19
      • 2020-09-16
      相关资源
      最近更新 更多