Can't Resolve: ValueError: Cannot render objects with any missing geometries
Can't Resolve: ValueError: Cannot render objects with any missing geometries
下面是我为复制我的实际问题而编写的代码。我想使用 folium 在地图上绘制多边形坐标,以便根据我的 sample
数据框查看每个“buro_name”的统计数据。我已经搜索了堆栈,但无法解决下面提供的错误打印输出中出现的问题。任何输入将不胜感激!
复制错误的代码:
import folium
import json
import requests
import pandas as pd
import urllib.request
buro_name = ['Bronx', 'Brooklyn', 'Manhattan', 'Queens', 'Staten Island']
num_data = [1234, 33432, 23423, 123123, 900]
sample = pd.DataFrame({'buro_name':buro_name, 'data':num_data})
nyc_geojson = "https://data.cityofnewyork.us/api/geospatial/tqmj-j8zm?method=export&format=GeoJSON"
with urllib.request.urlopen(nyc_geojson) as url:
geo_data = json.loads(url.read().decode())
# To save the json file locally, uncomment
# with open('geo_json_countys.json', 'w', encoding='utf-8') as f:
# json.dump(data, f, ensure_ascii=False, indent=4)
m = folium.Map(location=[40.7128, -74.0060], zoom_start=11)
folium.Choropleth(
geo_data=geo_data['features'],
name='choropleth',
data=sample,
columns=['buro_name', 'data'],
key_on='properties.buro_name',
fill_color='YlGn',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Unemployment Rate (%)'
).add_to(m)
folium.LayerControl().add_to(m)
m.save('maps/First_Attempt.html')
错误打印如下:
ValueError: Cannot render objects with any missing geometries: [{'type': 'Feature', 'properties': {'boro_code': '1', 'boro_name': 'Manhattan', 'shape_area': '636603803.361', 'shape_leng': '361611.82395'}, 'geometry': {'type': 'MultiPolygon', 'coordinates': [[[[-74.01092841268026, 40.68449147254294], [-74.01193259977079, 40.683887749154934], [-74.01217596614636, 40.684095185628465], [-74.01011625533792, 40.68534159773662], [-74.0087859013092, 40.686146602298905],.......
有 2 个错误:
1.
geo_data=geo_data['features'],
应该是
geo_data=geo_data,
key_on='properties.buro_name',
应该是
key_on='feature.properties.boro_name',
下面是我为复制我的实际问题而编写的代码。我想使用 folium 在地图上绘制多边形坐标,以便根据我的 sample
数据框查看每个“buro_name”的统计数据。我已经搜索了堆栈,但无法解决下面提供的错误打印输出中出现的问题。任何输入将不胜感激!
复制错误的代码:
import folium
import json
import requests
import pandas as pd
import urllib.request
buro_name = ['Bronx', 'Brooklyn', 'Manhattan', 'Queens', 'Staten Island']
num_data = [1234, 33432, 23423, 123123, 900]
sample = pd.DataFrame({'buro_name':buro_name, 'data':num_data})
nyc_geojson = "https://data.cityofnewyork.us/api/geospatial/tqmj-j8zm?method=export&format=GeoJSON"
with urllib.request.urlopen(nyc_geojson) as url:
geo_data = json.loads(url.read().decode())
# To save the json file locally, uncomment
# with open('geo_json_countys.json', 'w', encoding='utf-8') as f:
# json.dump(data, f, ensure_ascii=False, indent=4)
m = folium.Map(location=[40.7128, -74.0060], zoom_start=11)
folium.Choropleth(
geo_data=geo_data['features'],
name='choropleth',
data=sample,
columns=['buro_name', 'data'],
key_on='properties.buro_name',
fill_color='YlGn',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Unemployment Rate (%)'
).add_to(m)
folium.LayerControl().add_to(m)
m.save('maps/First_Attempt.html')
错误打印如下:
ValueError: Cannot render objects with any missing geometries: [{'type': 'Feature', 'properties': {'boro_code': '1', 'boro_name': 'Manhattan', 'shape_area': '636603803.361', 'shape_leng': '361611.82395'}, 'geometry': {'type': 'MultiPolygon', 'coordinates': [[[[-74.01092841268026, 40.68449147254294], [-74.01193259977079, 40.683887749154934], [-74.01217596614636, 40.684095185628465], [-74.01011625533792, 40.68534159773662], [-74.0087859013092, 40.686146602298905],.......
有 2 个错误:
1.
geo_data=geo_data['features'],
应该是
geo_data=geo_data,
key_on='properties.buro_name',
应该是
key_on='feature.properties.boro_name',