ipyleaflet
ipyleaflet copied to clipboard
Choropleth key_on cannot reach the id attribute if it is not located at the root of "features" in the geojson file.
Hi, I'm working with a geojson file in which the ID attribute for each feature isn't at the root of "features" but a level below in a "properties" object. Using the Chloropeth "key_on" attribute I expected to be able to reach it using key_on= 'properties.id', but I get KeyError: 'properties.id'
Here is a minimal example :
import ipyleaflet
import json
import pandas as pd
import os
import requests
from ipywidgets import link, FloatSlider
from branca.colormap import linear
geo_json_data = {"type":"FeatureCollection","features":[{"type":"Feature","properties":{"id":"01","name":"reg1"},"geometry":{"type":"Polygon","coordinates":[[[3,46],[3,48],[5,48],[5,46]]]}},{"type":"Feature","properties":{"id":"02","name":"reg2"},"geometry":{"type":"Polygon","coordinates":[[[3,45],[3,43],[5,43],[5,45]]]}},{"type":"Feature","properties":{"id":"03","name":"reg3"},"geometry":{"type":"Polygon","coordinates":[[[3,41],[5,41],[5,39],[3,39]]]}}]}
df = pd.DataFrame({'id': ['01', '02', '03'], 'number': [10, 20, 30]})
pd_data = dict(zip(df['id'].tolist(), df['number'].tolist()))
layer = ipyleaflet.Choropleth(
geo_data=geo_json_data,
choro_data=pd_data,
colormap=linear.YlOrRd_04,
border_color='black',
key_on='properties.id',
style={'fillOpacity': 0.8, 'dashArray': '5, 5'})
m = ipyleaflet.Map(center = (43,3), zoom = 5)
m.add_layer(layer)
m
If I manually edit my geojson file to have an ID attribute outside of "properties" at the root of "features" with key_on='id' it works perfectly.
And here is a working example using "properties.id" with the Folium library which creates Choropleth in a very similar way :
import folium
df = pd.DataFrame({'id': ['01', '02', '03'], 'number': [10, 20, 30]})
geo_json_data ={"type":"FeatureCollection","features":[{"type":"Feature","properties":{"id":"01","name":"reg1"},"geometry":{"type":"Polygon","coordinates":[[[3,46],[3,48],[5,48],[5,46]]]}},{"type":"Feature","properties":{"id":"02","name":"reg2"},"geometry":{"type":"Polygon","coordinates":[[[3,45],[3,43],[5,43],[5,45]]]}},{"type":"Feature","properties":{"id":"03","name":"reg3"},"geometry":{"type":"Polygon","coordinates":[[[3,41],[5,41],[5,39],[3,39]]]}}]}
m = folium.Map(location=[43, 3], zoom_start=6)
folium.Choropleth(
geo_data=geo_json_data,
name="choropleth",
data=df,
columns=["id", "number"],
key_on="properties.id",
fill_color="YlGn",
fill_opacity=0.7,
line_opacity=0.2,
legend_name="Unemployment Rate (%)",
).add_to(m)
m
I was working with some shapefiles on folium and everything worked well, so I tried to make the switch to ipyleaflet.Choropleth but when converting those shape files to json, I also realized this issue.
But even when I manually edited my geojson file to have an ID attribute outside of "properties", ipyleaflet.Choropleth still points me an error with the id value, but no comment "KeyError: 'D4'".
This are my files: https://github.com/gonzalezhomar/Tessalia/blob/main/geodistfed.geojson https://github.com/gonzalezhomar/Tessalia/blob/main/geodistloc.geojson
Why is not compatible with shape files??