question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Choropleth map not showing anything but empty map

See original GitHub issue

I tried to plot the Choropleth map from folium but its not showing the gradient colours for different boroughs of london

shapefile link(used ‘statistical-gis-boundaries-london.zip’ ): https://data.london.gov.uk/dataset/statistical-gis-boundary-files-london

csv data( used ‘london-borough-profiles.csv’ ): https://data.london.gov.uk/dataset/london-borough-profiles

I used the chropleth geojson data from the above shapefile which I converted to geojson through a code I stumbled upon online.



import shapefile
# read the shapefile
fïlepath = "statistical-gis-boundaries-london/ESRI/London_Borough_Excluding_MHW.shp"
reader = shapefile.Reader(fïlepath)
fields = reader.fields[1:]
field_names = [field[0] for field in fields]
buffer = []
for sr in reader.shapeRecords():
   atr = dict(zip(field_names, sr.record))
   geom = sr.shape.__geo_interface__
   buffer.append(dict(type="Feature", \
    geometry=geom, properties=atr)) 

# write the GeoJSON file
from json import dumps
geojson = open("london-borough.json", "w")
geojson.write(dumps({"type": "FeatureCollection",\
"features": buffer}, indent=2) + "\n")
geojson.close()

I made sure that the “on_key” value and a column in columns are same(same data)

done data cleaning:

state_data = pd.read_csv("london-borough-profiles.csv")
state_data = state_data[['Area_name', 'Population_density_(per_hectare)_2017']]
state_data['Population_density_(per_hectare)_2017'] = state_data['Population_density_(per_hectare)_2017'].replace(".",np.nan)
state_data['Population_density_(per_hectare)_2017'] = state_data['Population_density_(per_hectare)_2017'].astype(float)
state_data = state_data.dropna()
I think this might be because of the geojson data.

Any help will be appreciated. code:

m = folium.Map(location=[51.5074, -0.1278], zoom_start=10)
london_geo = "london-borough.json"
folium.Choropleth(
    geo_data=london_geo,
    name='choropleth',
    data=state_data,
    columns=['Area_name', 'Population_density_(per_hectare)_2017'],
    key_on='feature.properties.NAME',
    fill_color='YlGn',
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name='Population_density_(per_hectare)_2017'
).add_to(m)

folium.LayerControl().add_to(m)

m

No error messages, just the map is showing up with no color gradient for the boroughs

folium version : - 0.8.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
loramfcommented, Jan 14, 2020

@drkrthnblk I have found the solution to your problem. The reason why the chloropath does not show up on the map is not to do with the key_on but the coordinates in the JSON file. The beginning of the JSON file, “london-borough.json”, looks like this:

{“type”: “FeatureCollection”, “crs”: { “type”: “name”, “properties”: [{ “name”: “urn:ogc:def:crs:OGC:1.3:CRS84” } }, “features”: [{ “type”: “Feature”, “properties”: { “NAME”: “Kingston upon Thames”, “GSS_CODE”: “E09000021”, “HECTARES”: 3726.117, “NONLD_AREA”: 0.0, “ONS_INNER”: “F”, “SUB_2009”: “”,“SUB_2006”: “” }, “geometry”: { “type”: “Polygon”, “coordinates”: [ [ [ 516401.6, 160201.8 ], [ 516407.3, 160210.5 ], [ 516413.3, 160217.4 ], …

As you can see all the coordinates are extremely large, whereas on the Folium map London should be around [50, -0.25]. This is because the shapefile coordinates were stored under a different coordinate system to the Folium map, whose system is called EPSG 4326.

By projecting the coordinates onto the correct system the chloropleth will show up on the map. Here is my code for doing this (note that you can import the shapefile directly using geopandas):

import geopandas as gpd data_source = 'data/statistical-gis-boundaries-london/ESRI/London_Borough_Excluding_MHW.shp' data = gpd.read_file(fp) Now project coordinates to EPSG 4326: data['geometry'] = data['geometry'].to_crs(epsg=4326) data.to_file("london-borough.json", driver="GeoJSON")

Now if you look at “london-borough.json” you’ll see that the first few coordinates are as such: [ [ -0.329102847925613, 51.328483175466303 ], [ -0.329018221029286, 51.328560212318934 ], [ -0.32892987768375, 51.328621007587756 ], …

To check the London boroughs can now be plotted on the Folium map you can run the following code: m = folium.Map(location = [51.505453, -0.268839]) 'folium.GeoJson('data/london-borough.json').add_to(m) m

and you should be able to plot the chloropleth: m = folium.Map(location = [51.505453, -0.268839]) folium.Choropleth( geo_data='data/london-borough.json', name='choropleth', data= .........., columns= .........., key_on= .........., fill_color='YlGn', fill_opacity=0.7, line_opacity=0.2 ).add_to(m) m Note that ‘folium.Chloropleth’ is used instead of ‘.choropleth’ which is now deprecated. Also, I am using Jupyter Notebook - this did not work on Google Chrome so I had to switch to Firefox.

0reactions
Conengmocommented, Mar 6, 2020

Thanks for the help on this @loramf! I’m closing the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Showing no error but blank in choropleth map using folium
I am getting a blank output from the choropleth map. I have tried different versions of joining jsan data but nothing working as...
Read more >
Choropleth map error: Empty map - Plotly Python
I get empty map (with no layers by municipalities and number of households). Anybody knows why this might happen? import pandas as pd...
Read more >
Choropleth Map with plotly.express doesn't show map, just ...
The code was executed with no error but there was no map showing, only the legend.
Read more >
Blank or missing map tiles | Help - Mapbox docs
WebGL is not supported ... Mapbox GL JS maps (including the Mapbox Studio style editor and dataset editor) can fail to display because...
Read more >
Folium and Choropleth Map: From Zero to Pro | by Sharone Li
Choropleth maps are probably one of the most popular geospatial data ... and makes working with geospatial data in python so much easier....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found