Folium-ChoropletMap 与 key_on 相关的问题:不覆盖 choroplet 地图

发布于 2025-01-15 19:46:29 字数 3613 浏览 4 评论 0原文

我对以下代码有一个奇怪的问题:

    m10=folium.Map(location=[41.9027835,12.4963655],tiles='openstreetmap',zoom_start=5) 
    df.reset_index(inplace = True)  
    folium.Choropleth(
    geo_data = df.to_json(),
    data = df,
    columns=['TERRITORIO', var],
    key_on='feature.properties.TERRITORIO',
    fill_color='YlGnBu', 
    fill_opacity=0.6, 
    line_opacity=1,
    nan_fill_color='black',
    legend_name= get_title_(file_name),
    smooth_factor=0).add_to(m10)

    folium.features.GeoJson(df,
                        name='Labels',
                        style_function=lambda x: {'color':'transparent','fillColor':'transparent','weight':0},
                        tooltip=folium.features.GeoJsonTooltip(fields=[var],
                                                                aliases = [indicator], 
                                                                labels=True,
                                                                sticky=False
                                                                            )
                       ).add_to(m10)

我在两个不同的地理数据帧上使用同一段代码。对于第一个(较小的)数据框,我没有任何问题。 输入图片此处描述

但是,当我尝试对另一个进行相同操作时,我看不到 choroplet 地图图层。 输入图片这里的描述

这是第一个数据集(索引重置后):

    TERRITORIO  ...                                           geometry
0         Nord  ...  MULTIPOLYGON (((9.85086 44.02340, 9.85063 44.0...
1       Centro  ...  MULTIPOLYGON (((10.31417 42.35043, 10.31424 42...
2  Mezzogiorno  ...  MULTIPOLYGON (((8.41112 38.86296, 8.41127 38.8...

这是第二个数据集(索引重置后)

       TERRITORIO  ...                                           geometry
0         Abruzzo  ...  MULTIPOLYGON (((930273.425 4714737.743, 930147...
1      Basilicata  ...  MULTIPOLYGON (((1073851.435 4445828.604, 10738...
2        Calabria  ...  MULTIPOLYGON (((1083350.847 4416684.239, 10833...
3        Campania  ...  MULTIPOLYGON (((1037266.901 4449456.848, 10372...
4  Emilia-Romagna  ...  MULTIPOLYGON (((618335.211 4893983.160, 618329...

这些是插入的json文件: 第一:

{"type": "FeatureCollection", "features": [{"id": "0", "type": "Feature", "properties": {"INDICATORE": "Densit\\u00e0 di verde storico", "NOTE": null, "Shape_Area": 57926800546.7, "Shape_Leng": 2670893.51269, "TERRITORIO": "Nord", "UNITA_MISURA": "per 100 m2", "V_2004": null, "V_2005": null, "V_2006": null, "V_2007": null, "V_2008": null, "V_2009": null, "V_2010": null, "V_2011": 2.4, "V_2012": 2.4, "V_2013": 2.4, "V_2014": 2.4, "V_2015": 2.4, "V_2016": 2.4, "V_2017": 2.4, "V_2018": 2.4, "V_2019": null, "index": 0}, "geometry": {"type": "MultiPolygon", "coordinates": ...

第二:

{"type": "FeatureCollection", "features": [{"id": "0", "type": "Feature", "properties": {"INDICATORE": "Densit\\u00e0 e rilevanza del patrimonio museale", "NOTE": null, "Shape_Area": 10831496151.0, "Shape_Leng": 664538.009079, "TERRITORIO": "Abruzzo", "UNITA_MISURA": "per 100 km2", "V_2004": null, "V_2005": null, "V_2006": null, "V_2007": null, "V_2008": null, "V_2009": null, "V_2010": null, "V_2011": null, "V_2012": null, "V_2013": null, "V_2014": null, "V_2015": 0.22, "V_2016": null, "V_2017": 0.13, "V_2018": 0.11, "V_2019": null}, "geometry": {"type": "MultiPolygon", "coordinates":... 

我真的不明白为什么一个有效而另一个无效。

您有什么建议吗?

预先感谢您的宝贵时间!

I have a strange issue with the following piece of code:

    m10=folium.Map(location=[41.9027835,12.4963655],tiles='openstreetmap',zoom_start=5) 
    df.reset_index(inplace = True)  
    folium.Choropleth(
    geo_data = df.to_json(),
    data = df,
    columns=['TERRITORIO', var],
    key_on='feature.properties.TERRITORIO',
    fill_color='YlGnBu', 
    fill_opacity=0.6, 
    line_opacity=1,
    nan_fill_color='black',
    legend_name= get_title_(file_name),
    smooth_factor=0).add_to(m10)

    folium.features.GeoJson(df,
                        name='Labels',
                        style_function=lambda x: {'color':'transparent','fillColor':'transparent','weight':0},
                        tooltip=folium.features.GeoJsonTooltip(fields=[var],
                                                                aliases = [indicator], 
                                                                labels=True,
                                                                sticky=False
                                                                            )
                       ).add_to(m10)

I use the same piece of code on two different geodataframes. With the first (smaller) dataframe I have no issues.
enter image description here

However, when I try to do the same with the other one I do not see the choroplet map layer.
enter image description here

This is the first dataset (after the reset of the index):

    TERRITORIO  ...                                           geometry
0         Nord  ...  MULTIPOLYGON (((9.85086 44.02340, 9.85063 44.0...
1       Centro  ...  MULTIPOLYGON (((10.31417 42.35043, 10.31424 42...
2  Mezzogiorno  ...  MULTIPOLYGON (((8.41112 38.86296, 8.41127 38.8...

This is the second dataset (after the reset of the index)

       TERRITORIO  ...                                           geometry
0         Abruzzo  ...  MULTIPOLYGON (((930273.425 4714737.743, 930147...
1      Basilicata  ...  MULTIPOLYGON (((1073851.435 4445828.604, 10738...
2        Calabria  ...  MULTIPOLYGON (((1083350.847 4416684.239, 10833...
3        Campania  ...  MULTIPOLYGON (((1037266.901 4449456.848, 10372...
4  Emilia-Romagna  ...  MULTIPOLYGON (((618335.211 4893983.160, 618329...

These are insted the json files:
first:

{"type": "FeatureCollection", "features": [{"id": "0", "type": "Feature", "properties": {"INDICATORE": "Densit\\u00e0 di verde storico", "NOTE": null, "Shape_Area": 57926800546.7, "Shape_Leng": 2670893.51269, "TERRITORIO": "Nord", "UNITA_MISURA": "per 100 m2", "V_2004": null, "V_2005": null, "V_2006": null, "V_2007": null, "V_2008": null, "V_2009": null, "V_2010": null, "V_2011": 2.4, "V_2012": 2.4, "V_2013": 2.4, "V_2014": 2.4, "V_2015": 2.4, "V_2016": 2.4, "V_2017": 2.4, "V_2018": 2.4, "V_2019": null, "index": 0}, "geometry": {"type": "MultiPolygon", "coordinates": ...

second:

{"type": "FeatureCollection", "features": [{"id": "0", "type": "Feature", "properties": {"INDICATORE": "Densit\\u00e0 e rilevanza del patrimonio museale", "NOTE": null, "Shape_Area": 10831496151.0, "Shape_Leng": 664538.009079, "TERRITORIO": "Abruzzo", "UNITA_MISURA": "per 100 km2", "V_2004": null, "V_2005": null, "V_2006": null, "V_2007": null, "V_2008": null, "V_2009": null, "V_2010": null, "V_2011": null, "V_2012": null, "V_2013": null, "V_2014": null, "V_2015": 0.22, "V_2016": null, "V_2017": 0.13, "V_2018": 0.11, "V_2019": null}, "geometry": {"type": "MultiPolygon", "coordinates":... 

I really do not understand why one works and the other does not.

Do you have any suggestion?

Thank you in advance for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

活泼老夫 2025-01-22 19:46:29
  • 我找不到意大利 Nord、Centro、Mezzogiorno 的任何几何图形,因此通过溶解区域几何图形进行了合成,
  • 代码使用了设置函数和变量,使 MWE
  • 可以通过 #regions==False 在几何图形之间切换, North/central/south==True if True: both 生成适当的 folium 地图,
  • 在您的问题中很明显,您的两组几何图形正在使用不同的 CRS。第一个数据集看起来像 EPSG:4326(因此有效)。第二个看起来像 UTM CRS(以米为单位而不是度数的点),需要投影到 EPSG:4326
import folium
import geopandas as gpd
import numpy as np

# make SO code runnable, get some geometry and set columns / variables used by code
df = gpd.read_file("https://github.com/openpolis/geojson-italy/raw/master/geojson/limits_IT_regions.geojson").sort_values("reg_name")
df["var"] = np.random.randint(1,10,len(df))
df["TERRITORIO"] = df["reg_istat_code_num"]
df["NCM"] = np.where(df["reg_istat_code_num"]<9,"Nord", np.where(df["reg_istat_code_num"]<15,"Centro", "Mezzogiorno"))
var = "var"
file_name="regions"
indicator = "some number"
def get_title_(file_name):
    return file_name

# regions==False, north/central/south==True
if True:
    df = df.dissolve("NCM")
    file_name = "ncm"
    
# unchanged code
m10 = folium.Map(location=[41.9027835, 12.4963655], tiles="openstreetmap", zoom_start=5)
df.reset_index(inplace=True)
folium.Choropleth(
    geo_data=df.to_json(),
    data=df,
    columns=["TERRITORIO", var],
    key_on="feature.properties.TERRITORIO",
    fill_color="YlGnBu",
    fill_opacity=0.6,
    line_opacity=1,
    nan_fill_color="black",
    legend_name=get_title_(file_name),
    smooth_factor=0,
).add_to(m10)

folium.features.GeoJson(
    df,
    name="Labels",
    style_function=lambda x: {
        "color": "transparent",
        "fillColor": "transparent",
        "weight": 0,
    },
    tooltip=folium.features.GeoJsonTooltip(
        fields=[var], aliases=[indicator], labels=True, sticky=False
    ),
).add_to(m10)

m10
  • I can't find any geometry for Nord, Centro, Mezzogiorno Italy, so have sythesized by dissolving regions geometry
  • have setup functions and variables used by your code to make this a MWE
  • can switch between geometries by # regions==False, north/central/south==True if True: both generate appropriate folium maps
  • it's clear in your question that your two sets of geometry are using different CRS. First data set looks like EPSG:4326 (hence works). Second looks like a UTM CRS (points in meters not degrees) that would need to be projected to EPSG:4326
import folium
import geopandas as gpd
import numpy as np

# make SO code runnable, get some geometry and set columns / variables used by code
df = gpd.read_file("https://github.com/openpolis/geojson-italy/raw/master/geojson/limits_IT_regions.geojson").sort_values("reg_name")
df["var"] = np.random.randint(1,10,len(df))
df["TERRITORIO"] = df["reg_istat_code_num"]
df["NCM"] = np.where(df["reg_istat_code_num"]<9,"Nord", np.where(df["reg_istat_code_num"]<15,"Centro", "Mezzogiorno"))
var = "var"
file_name="regions"
indicator = "some number"
def get_title_(file_name):
    return file_name

# regions==False, north/central/south==True
if True:
    df = df.dissolve("NCM")
    file_name = "ncm"
    
# unchanged code
m10 = folium.Map(location=[41.9027835, 12.4963655], tiles="openstreetmap", zoom_start=5)
df.reset_index(inplace=True)
folium.Choropleth(
    geo_data=df.to_json(),
    data=df,
    columns=["TERRITORIO", var],
    key_on="feature.properties.TERRITORIO",
    fill_color="YlGnBu",
    fill_opacity=0.6,
    line_opacity=1,
    nan_fill_color="black",
    legend_name=get_title_(file_name),
    smooth_factor=0,
).add_to(m10)

folium.features.GeoJson(
    df,
    name="Labels",
    style_function=lambda x: {
        "color": "transparent",
        "fillColor": "transparent",
        "weight": 0,
    },
    tooltip=folium.features.GeoJsonTooltip(
        fields=[var], aliases=[indicator], labels=True, sticky=False
    ),
).add_to(m10)

m10
他不在意 2025-01-22 19:46:29

修改crs我能够克服上述问题。

    # Create the folium map 
m10=folium.Map(location=[41.9027835,12.4963655],tiles='openstreetmap',zoom_start=5)

# Data

df.to_crs(crs = 4326, inplace = True)

df.reset_index(就地= True)

folium.Choropleth(
geo_data = df.to_json(),
data = df,
columns=['TERRITORIO', var],
key_on='feature.properties.TERRITORIO',
fill_color='YlGnBu', 
fill_opacity=0.6, 
line_opacity=1,
nan_fill_color='black',
legend_name= get_title_(file_name),
smooth_factor=0).add_to(m10)

Modifying the crs I was able to overcome the above issiue.

    # Create the folium map 
m10=folium.Map(location=[41.9027835,12.4963655],tiles='openstreetmap',zoom_start=5)

# Data

df.to_crs(crs = 4326, inplace = True)

df.reset_index(inplace = True)

folium.Choropleth(
geo_data = df.to_json(),
data = df,
columns=['TERRITORIO', var],
key_on='feature.properties.TERRITORIO',
fill_color='YlGnBu', 
fill_opacity=0.6, 
line_opacity=1,
nan_fill_color='black',
legend_name= get_title_(file_name),
smooth_factor=0).add_to(m10)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文