How to use a geojson file with dictionary format to draw a choropleth map

138 Views Asked by At

I am trying to build a choropleth map using a GEOJSON file which format is a dictionary. in the GEOJSON file, when I run this piece of code, I get:

looking for key_on

data['features'][0]

I get the answer:

{'type': 'Feature',
 'geometry': {'coordinates': [[[7.4470632, 51.388113],
    [7.4516509, 51.3909219],
    [7.4550327, 51.3950023],

and so on until:

[7.4673614, 51.3899947]]],
  'type': 'Polygon'},
 'properties': {'krs_code': '05914',
  'lan_code': '05',
  'geo_point_2d': [51.4002031332, 7.487857752],
  'plz_name': 'Hagen',
  'plz_name_long': '58099 Hagen',
  'lan_name': 'Nordrhein-Westfalen',
  'name': '58099',
  'plz_code': '58099',
  'krs_name': 'Kreisfreie Stadt Hagen'}}

The main problem, I have a file that has the same variable as in the krs_code (it has the name of geo_plz) However, when I try to draw the choropleth map with the following code, I got the error:

# creating map
M = folium.Map(location= [50,10], zoom_start=6)
folium.Choropleth(
geo_data= country_geo,
data= df2,
 columns=['geo_plz','totalRent'],
key_on='feature.krs_code',
carto_db= 'feature',
fill_color = 'PuOr',
 fill_opacity=0.6,
line_opacity=0.1,
 legend_name ='Total Rent'
 ).add_to(M)
M

Here is the following error:

ValueError: key_on 'krs_code' not found in GeoJSON.

Is anyone found the same error and how to solve it? It seems to me I need to use a data dictionary in the map, but I have no idea how to solve this.

1

There are 1 best solutions below

0
ckbuzin On

I think I found the solution. I just need to change the key_on value to: feature.properties.krs_code

This is because I am using a data dictionary. For sure this causes another problem (because of my data set) but this error message was fixed, and I managed to get the map I am looking for.