Nathan
Nathan

Reputation: 21

Folium choropleth key_on - I'm missing something obvious

I'm trying to make a choropleth of installed PV capacity by postcode. I've got the installation data in a pandas Series (tas_kW_series), index is postcode as int. I've downloaded the ESRI shape files for Australia, converted them to GeoJson (using an online tool). Visually the format is correct. I had to split the Geojson into states to keep the size down, the individual jsons work with a geojson layer, but not a choropleth

I can get the shapes to display on a Geojson layer, but the choropleth is failing, on the 'key_on' parameter I think, and I just can't figure out what's wrong with it - it maps to the correct part of the geojson, and the input data is a series so no need to provide column names.

I've had to add a new 'properties' field ('pc_int') with the postcode converted to 'int' to match the index of the series. This is in the json file

The data had more postcodes than the geojson, I've removed the excess

I've checked every way I know how that the structure of the geojson matches the 'feature.properties.pc_int' I'm using in the caller.

I've stepped through the source code, and I'm pretty sure it's correct

Postcode Data (following tas_kW_series.to_dict())
{7000: 2027.2620000000002,
 7001: 94.36,
 7002: 13.5,
 7004: 910.943,
 7005: 1644.2499999999998,
 7006: 25.1,
 7007: 612.258,
 7008: 1525.3100000000004,
 7009: 1478.017,
 7010: 1876.863,
 7011: 1770.325,
 7012: 205.06,
 7015: 1557.3830000000003,
 7016: 233.579,...

GeoJson (with some editing to remove polygon details) TAS_data = json.load(TAS.json)

TAS_data['features'][0] = 

{'type': 'Feature',
 'geometry': {'type': 'Polygon',
  'coordinates': [[[147.3199543290001, -42.85551239399996],
    [147.32003433700004, -42.85554737699994],
    [147.32014234000007, -42.85558237899994],
    [147.3202043540001, -42.855599380999934],...
...
    [147.31961900400006, -42.856079991999934],
    [147.31967809700006, -42.85600952499993],
    [147.3199543290001, -42.85551239399996]]]},
 'properties': {'POA_CODE16': '7000',
  'POA_NAME16': '7000',
  'AREASQKM16': 10.8544,
  'pc_int': 7000}}

Choropleth call -

folium.Choropleth(geo_data = 'TAS.json', 
                  data = tas_kW_series,
                  name = 'Tasmania STC Installations - Total Capacity (kW)',
                  key_on = 'feature.properties.pc_int',
                  bins = 10,
                  fill_opacity = 0.7,
                  line_opacity = 0.2,
                  legend_name = 'Installed Capacity (kW)').add_to(m)

traceback

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-46-7bcc2e3b37d6> in <module>
      7                   fill_opacity = 0.7,
      8                   line_opacity = 0.2,
----> 9                   legend_name = 'Installed Capacity (kW)').add_to(m)

~\AppData\Local\conda\conda\envs\mlbook\lib\site-packages\folium\features.py in __init__(self, geo_data, data, columns, key_on, bins, fill_color, nan_fill_color, fill_opacity, nan_fill_opacity, line_color, line_weight, line_opacity, name, legend_name, overlay, control, show, topojson, smooth_factor, highlight, **kwargs)
    932         self._name = 'Choropleth'
    933 
--> 934         if data is not None and not color_brewer(fill_color):
    935             raise ValueError('Please pass a valid color brewer code to '
    936                              'fill_local. See docstring for valid codes.')

~\AppData\Local\conda\conda\envs\mlbook\lib\site-packages\branca\utilities.py in color_brewer(color_code, n)
    147 
    148     if base_code not in core_schemes:
--> 149         raise ValueError(base_code + ' is not a valid ColorBrewer code')
    150 
    151     try:

ValueError: blue is not a valid ColorBrewer code

expected output is a choropleth layer on a folium map, shaded according to binning of values in tas_kW_series. actual output is traceback above.

Upvotes: 1

Views: 7029

Answers (2)

A. Traor&#233;
A. Traor&#233;

Reputation: 76

The error comes from thefill_color property of folium.Choropleth because blue is set by default:

fill_color: string, default 'blue'
|      Area fill color. Can pass a hex code, color name, or if you are
|      binding data, one of the following color brewer palettes:
|      'BuGn', 'BuPu', 'GnBu', 'OrRd', 'PuBu', 'PuBuGn', 'PuRd', 'RdPu',
|      'YlGn', 'YlGnBu', 'YlOrBr', and 'YlOrRd'.

Change it to normal color like Blues, Reds (with s since you have several bins) or something listed in color brewer palettes above like 'BuGn', 'BuPu' ...

Upvotes: 1

Nathan
Nathan

Reputation: 21

See Choropleth map using folium and pandas

The dataframe and geojson should only include the key_on data. It also helped to run the converted geoJSON through a geopandas dataframe, add the data for visualisation, and split into geodata and info data, including only the value to be used as key in the geojson.

tl;dr, worked perfectly after I removed the excess fields from the properties dictionary.

Upvotes: 0

Related Questions