Search code examples
pythongeojsonfolium

Can't Resolve: ValueError: Cannot render objects with any missing geometries


Below is the code I have written to replicate my actual issue. I would like to plot polygonal coordinates on a map using folium in order to see the statistics of each "buro_name" based on my sample dataframe. I have scoured stack and have not been able to resolve the issue that occurs with the error printout provided below. Any input would be greatly appreciated!

Code to replicate error:

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')

Error printout is as follows:

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],.......

Solution

  • There are 2 errors:

    1.

    geo_data=geo_data['features'],
    

    should be

    geo_data=geo_data,
    
    key_on='properties.buro_name',
    

    should be

    key_on='feature.properties.boro_name',
    

    enter image description here