2
votes

I am trying to plot a choropleth plot of the UK using geojson data file which I downloaded from here: https://data.gov.uk/dataset/regions-december-2016-full-extent-boundaries-in-england2

Below is an example of the json data:

{
  "type":"FeatureCollection",
  "features":[
    {
      "type":"Feature",
      "properties":{"objectid":1,"rgn16cd":"E12000001","rgn16nm":"North East","bng_e":417313,"bng_n":600358,"long":-1.72889996,"lat":55.2970314,"st_areashape":8675727008.425964,"st_lengthshape":795456.8022925043},
      "geometry":{
        "type":"MultiPolygon",
        "coordinates":[[[[-2.0301237629331097,55.80991509288915],[-2.030069429494278,55.80991420787532],[-2.0300215494803053,55.80992140589199],[-2.0300040593387223,55.80993039246682],

My csv file looks like this: csv

I essentially just want to plot the Taxi column using folium.

The problem is the plot does not show anything. I used the following code.

import pandas as pd
import os
import json

# read in population data
df = pd.read_csv('map-data.csv')

import folium
from branca.utilities import split_six
state_geo = 'Regions_December_2016_Full_Extent_Boundaries_in_England.geojson'

m = folium.Map(location=[55, 4], zoom_start=5)
m.choropleth(
    geo_data=state_geo,
    data=df,
    columns=['LA-Code', 'Taxi'],
    key_on='feature.properties.rgn16cd',
    fill_color='YlGn',
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name='h',
    highlight=True
)

m

I think the problem relates the key_on argument. I can access the correct code in the json file using something like this:

geodata['features'][0]['properties']['rgn16cd']

which gives me back the correct LA code (E12000001) but it does not seem to work in the above code. I also tried using features instead of feature in the key_on argument but that gives me an error

AttributeError: 'NoneType' object has no attribute 'get'

Does anyone have any ideas what the problem is? Thanks.

1

1 Answers

1
votes

From the folium library's documentation on github:

To display it in a Jupyter notebook, simply ask for the object representation:

In : m

It is likely that the source of your issue is that you are not in a Jupiter notebook. Saving the map as a html file and opening it in a browser works fine, without changing the json file. Try the code below:

import pandas as pd
import folium

# read in population data
df = pd.read_csv('map-data.csv')
state_geo = 'Regions_December_2016.geojson'


m = folium.Map(location=[55, 4], zoom_start=5)
m.choropleth(
    geo_data=state_geo,
    data=df,
    columns=['LA-Code', 'Taxi'],
    key_on='feature.properties.rgn16cd',
    fill_color='YlGn',
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name='h',
    highlight=True
)

m.save("my_map.html")

To open the map from the script, you can call your web-browser through subprocess.call or os.system, by adding those line at the end of your script:

import os
os.system("firefox my_map.html")