我正试着用Folium和Patily用Python 制作一张全息 map .数据来自哥伦比亚广播公司:https://www.cbs.nl/nl-nl/onze-diensten/open-data/statline-als-open-data/cartografie.叶合唱起作用,但情节合唱不起作用.传说正在显示,但 map 不显示.我做错了什么?

#Make choropleth map with folium and with plotly

#CBS Open Data
#https://www.cbs.nl/nl-nl/onze-diensten/open-data/statline-als-open-data/cartografie

#Plotly
#https://plotly.com/python/mapbox-county-choropleth/


#Libraries
import pandas as pd
import geopandas as gpd
import folium
import cbsodata
import plotly.express as px

# Find out which columns are available
metadata = pd.DataFrame(cbsodata.get_meta('83765NED', 'DataProperties'))

# Download birth rates and delete spaces from regional identifiers
data = pd.DataFrame(cbsodata.get_data('83765NED', select = ['WijkenEnBuurten', 'Codering_3', 'GeboorteRelatief_25']))
data['Codering_3'] = data['Codering_3'].str.strip()

# Retrieve data with municipal boundaries from PDOK
geodata_url = 'https://cartomap.github.io/nl/wgs84/gemeente_2023.geojson'
municipal_boundaries = gpd.read_file(geodata_url)

# Link data from Statistics Netherlands to geodata
municipal_boundaries = pd.merge(municipal_boundaries, data,
                               left_on = "statcode", 
                               right_on = "Codering_3")

#CRS: EPSG 3857 (web mercator projection wgs84)
municipal_boundaries.crs
municipal_boundaries = municipal_boundaries.to_crs(epsg = 3857)


#First column: Geoid, geometry column and  data columns
gdf_choro = municipal_boundaries.copy()
gdf_choro['geoid'] = gdf_choro.index.astype(str)
gdf_choro = gdf_choro[['geoid', 'geometry', 'statnaam', 'GeboorteRelatief_25']]
gdf_choro.head(3)

#Center
nld_lat = 52.2130
nld_lon = 5.2794
nld_coordinates = (nld_lat, nld_lon)

#Folium base map
map_nld = folium.Map(location=nld_coordinates, tiles='cartodbpositron', zoom_start=6, control_scale=True)

#Folium choropleth
map_nld = folium.Map(location=nld_coordinates, tiles='cartodbpositron', zoom_start=6, control_scale=True)
folium.Choropleth(geo_data=gdf_choro,
                  data=gdf_choro,
                  columns=['geoid', 'GeboorteRelatief_25'],
                  key_on='feature.id',
                  fill_color='Blues',                  
                  legend_name='Geboorterelatief'
                 ).add_to(map_nld)
map_nld

#Plotly choropleth
fig = px.choropleth_mapbox(gdf_choro,
                           geojson=gdf_choro['geometry'],
                           locations=gdf_choro.geoid,
                           color='GeboorteRelatief_25',
                           center={'lat': 52.213, 'lon':5.2794},
                           mapbox_style='cartodbpositron',
                           zoom=6)
fig.show()

推荐答案

有几个因素可能会阻止 map 在Ploly中显示.第一个问题是Ploly中的地理坐标系必须是ESPG 4326;第二个问题是您为地理信息指定的是一个Geodataframe而不是Geojson,因此您需要指定gpd.__geo_interface__才能使其等同于Geojson.第三,Mapbox的风格名称是Carto-Position.

#municipal_boundaries = municipal_boundaries.to_crs(epsg=3857)
municipal_boundaries = municipal_boundaries.to_crs(epsg=4326)

fig = px.choropleth_mapbox(gdf_choro,
                           geojson=gdf_choro.__geo_interface__,#['geometry']
                           locations=gdf_choro.geoid,
                           color='GeboorteRelatief_25',
                           featureidkey='properties.geoid',
                           center={'lat': 52.213, 'lon':5.2794},
                           mapbox_style='carto-positron',
                           zoom=6)
fig.show()

enter image description here

Python相关问答推荐

如何以实现以下所述的预期行为的方式添加两只Pandas pyramme

Pandas使用过滤器映射多列

在后台运行的Python函数

如何观察cv2.erode()的中间过程?

如何在超时的情况下同步运行Matplolib服务器端?该过程随机挂起

Python:记录而不是在文件中写入询问在多文件项目中记录的最佳实践

是什么导致对Python脚本的jQuery Ajax调用引发500错误?

计算相同形状的两个张量的SSE损失

由于NEP 50,向uint 8添加-256的代码是否会在numpy 2中失败?

从收件箱中的列中删除html格式

如何从具有不同len的列表字典中创建摘要表?

在Wayland上使用setCellWidget时,try 编辑QTable Widget中的单元格时,PyQt 6崩溃

django禁止直接分配到多对多集合的前端.使用user.set()

Python键入协议默认值

运输问题分支定界法&

cv2.matchTemplate函数匹配失败

pandas在第1列的id,第2列的标题,第3列的值,第3列的值?

pandas:对多级列框架的列进行排序/重新排序

人口全部乱序 - Python—Matplotlib—映射

使用Python异步地持久跟踪用户输入