2
votes

Folium renders the choropleth map with the police force boundaries but these are all grey and not colour matched for the data in the dataframe.

I have also ensured the new version of the documentation is followed i.e. folium.Choropleth. I have also checked to make sure I am key_on='feature.properties.pfa16nm' by checking the json in geojson.io Feature is spelt with a capital when checking the geojson however when I change it to this I get an error and no map renders. I have also renamed the file to only have the geojson as an extension and that didn't work.

import pandas as pd
import folium
import json
import os
adults_trafficked = pd.read_excel('Adults trafficked.xlsx')
force_boundaries = 'Police_Force_Areas_December_2016_Generalised_Clipped_Boundaries_in_England_and_Wales.geojson.json'

m = folium.Map([52.6333, -1.1333], zoom_start=4)
folium.Choropleth(
    geo_data=force_boundaries,
    data=adults_trafficked,
    columns=['Police_Force', 'Adults_Exploited'],
    key_on='feature.properties.pfa16nm',
    threshold_scale=[0, 25, 50, 75, 100, 125, 150, 175],
    fill_color='BuPu',
    legend_name='Trafficked Humans',
).add_to(m)
m

Output that I am getting enter image description here

I expect the Leaflet map to render with each police boundary shaded to the appropriate level based on the dataframe column data. The Chorpleth map renders perfectly with the boundaries however these are all grey and do not contain the tonal colour range one would expect. Please find the code, data and the json link here.

2
I have exactly the same problem, not able to find the solution. Question link: stackoverflow.com/questions/55563700/…Rahul
Please post your data and the geojson data on your drive and then share the link. The link that you have shared is for a page. We don't have enough time to search for the download button. Please post the minimal version of your problem so that we can help you quickly and effectively.Himanshu Poddar
Please update the data and geojson file so that I can answer your question.Himanshu Poddar
I have created a github with the necessary information github.com/Crime-Analytics/Human_Trafficking.git thank you for your anticipated help. My initial thinking is that I will have to edit the json file manually.Analyst
The data file doesn't match the pfa16nm in the json file. If you can make them match, your code should work. For example, excel file has Cumbria Constabulary whereas the json file has Cumbria.Baris Tasdelen

2 Answers

1
votes

The problem is that the excel file doesn't match the json file. When you use

columns=['Police_Force', 'Adults_Exploited'],
key_on='feature.properties.pfa16nm',

the Police_Force should match the pfa16nm in your json file.

This code will give you the pfa16nms in your json.

import json 
policejson = json.load(open('Police_Force_Areas_December_2016_Generalised_Clipped_Boundaries_in_England_and_Wales.geojson.json')) 
for x in policejson['features'] : 
    print (x['properties']['pfa16nm']) 

Then you need to make fix your excel file, and make sure that the Police_Force column matches the names in your json file.

1
votes

The problem is the name of the key and the name of the Police_Force in your database aren't matching. So after analysing your data as well as the json file I did some pre processing with your data so that the name matches with the key in our json file.

Here is a full fledge solution to your question.

# import libraries
import pandas as pd
import folium
import json
import webbrowser

# read data
adults_trafficked = pd.read_excel('Adults trafficked.xlsx')

# Pre processing of data
adults_trafficked['Police_Force'] = adults_trafficked['Police_Force'].replace('Police|Constabulary','', regex=True, ).replace('&','and', regex=True)
adults_trafficked.loc[adults_trafficked['Police_Force'] == "Metropolitan  Service",'Police_Force' ] = 'Metropolitan Police'

# remove any trailing or leading white spaces
adults_trafficked['Police_Force'] = adults_trafficked['Police_Force'].str.strip()

# border json file 
force_boundaries = 'Police_Force_Areas_December_2016_Generalised_Clipped_Boundaries_in_England_and_Wales.geojson.json'

# choropleth map
m = folium.Map([52.6333, -1.1333], zoom_start=7)

m.choropleth(
    geo_data=force_boundaries,
    data=adults_trafficked,
    columns=['Police_Force', 'Adults_Exploited'],
    key_on='feature.properties.pfa16nm',
    threshold_scale=[0, 40, 80, 120, 160, 200],
    fill_color='BuPu',
    legend_name='Trafficked Humans',
)
m.save('map.html')
webbrowser.open('map.html')

Note that the length of threshold_scale cannot be more than 6. I can see yours was 8. Also there are only 44 police_force data in your json file while the length of your dataset is 47. So those 3 data which didn't match were ignored by folium. This is what you will get

enter image description here

In case if you have trouble understanding any part of code please comment below.