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 + GeoJson = 'NoneType' object has no attribute 'get'

See original GitHub issue

GeoJSON Format is like below :

{
  "type":"FeatureCollection",
  "features":[
        {
          "type":"Feature",
          "geometry":
          {
              "type":"Polygon",
              "coordinates":[[[-1.6704591323124895,49.62681486270549], .....
              {
                  "insee":"50173",
                  "nom":"Équeurdreville-Hainneville",
                  "wikipedia":"fr:Équeurdreville-Hainneville",
                  "surf_m2":12940306}},

Pandas DataFrame :

postal_count.head(5)
Out[95]: 
  Code_commune_INSEE  count
0              75120    723
1              75115    698
2              75112    671
3              75118    627
4              75111    622

Here is my code (snippet from this notebook)

map_france = folium.Map(location=[47.000000, 2.000000], zoom_start=6)

map_france.choropleth(
                    geo_str=open(path + 'simplified_communes100m.json').read(),
                    data=postal_count,
                    columns=['Code_commune_INSEE', 'count'],
                    key_on='feature.geometry.properties.insee',
                    fill_color='YlGn',
)
map_france.save(table_path+ '/choro_test1.html')

I’m stilling getting this error again and again :

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-83-ea0fd2c1c207> in <module>()
      8                     fill_color='YlGn',
      9 )
---> 10 map_france.save('/media/flo/Stockage/Data/MesAides/map/choro_test1.html')

/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/element.py in save(self, outfile, close_file, **kwargs)
    151 
    152         root = self.get_root()
--> 153         html = root.render(**kwargs)
    154         fid.write(html.encode('utf8'))
    155         if close_file:

/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/element.py in render(self, **kwargs)
    357         """Renders the HTML representation of the element."""
    358         for name, child in self._children.items():
--> 359             child.render(**kwargs)
    360         return self._template.render(this=self, kwargs=kwargs)
    361 

/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/element.py in render(self, **kwargs)
    665 
    666         for name, element in self._children.items():
--> 667             element.render(**kwargs)

/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/element.py in render(self, **kwargs)
    661         script = self._template.module.__dict__.get('script', None)
    662         if script is not None:
--> 663             figure.script.add_children(Element(script(self, kwargs)),
    664                                        name=self.get_name())
    665 

/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/jinja2/runtime.py in __call__(self, *args, **kwargs)
    434             raise TypeError('macro %r takes not more than %d argument(s)' %
    435                             (self.name, len(self.arguments)))
--> 436         return self._func(*arguments)
    437 
    438     def __repr__(self):

<template> in macro(l_this, l_kwargs)

/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/jinja2/runtime.py in call(_Context__self, _Context__obj, *args, **kwargs)
    194                 args = (__self.environment,) + args
    195         try:
--> 196             return __obj(*args, **kwargs)
    197         except StopIteration:
    198             return __self.environment.undefined('value was undefined because '

/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/features.py in style_data(self)
    352 
    353         for feature in self.data['features']:
--> 354             feature.setdefault('properties', {}).setdefault('style', {}).update(self.style_function(feature))  # noqa
    355         return json.dumps(self.data, sort_keys=True)
    356 

/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/folium.py in style_function(x)
    671                 "color": line_color,
    672                 "fillOpacity": fill_opacity,
--> 673                 "fillColor": color_scale_fun(x)
    674             }
    675 

/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/folium.py in color_scale_fun(x)
    659             def color_scale_fun(x):
    660                 return color_range[len(
--> 661                     [u for u in color_domain if
    662                      u <= color_data[get_by_key(x, key_on)]])]
    663         else:

/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/folium.py in <listcomp>(.0)
    660                 return color_range[len(
    661                     [u for u in color_domain if
--> 662                      u <= color_data[get_by_key(x, key_on)]])]
    663         else:
    664             def color_scale_fun(x):

/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/folium.py in get_by_key(obj, key)
    655                 return (obj.get(key, None) if len(key.split('.')) <= 1 else
    656                         get_by_key(obj.get(key.split('.')[0], None),
--> 657                                    '.'.join(key.split('.')[1:])))
    658 
    659             def color_scale_fun(x):

/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/folium.py in get_by_key(obj, key)
    655                 return (obj.get(key, None) if len(key.split('.')) <= 1 else
    656                         get_by_key(obj.get(key.split('.')[0], None),
--> 657                                    '.'.join(key.split('.')[1:])))
    658 
    659             def color_scale_fun(x):

/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/folium.py in get_by_key(obj, key)
    653 
    654             def get_by_key(obj, key):
--> 655                 return (obj.get(key, None) if len(key.split('.')) <= 1 else
    656                         get_by_key(obj.get(key.split('.')[0], None),
    657                                    '.'.join(key.split('.')[1:])))

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

I tried playing with key_on='feature.geometry.properties.insee' without any success.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
gowitheflowwcommented, May 3, 2016

There were 2 problems :

1 - The correct access to ‘insee’ parameters is : key_on='feature.properties.insee'

2- My DataFrame did not contain all the available ‘insee’ keys, so it was raising a KeyError

After these 2 problems solved, it worked like a charm!

2reactions
razaderacommented, Aug 14, 2018

@nittolese the same thing is happening to me. I get no error when I try to key on a lat/lon feature, but nothing shows up on the map.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Folium Choropleth + GeoJSON raises AttributeError: 'NoneType'
"Code_communes_INSEE" corresponds to the attribute "insee" in the GeoJSON. I'd like to do a choropleth using the variable "CP_count" in the ...
Read more >
[Folium] Choropleth not showing ('NoneType' object has no ...
[Folium] Choropleth not showing ('NoneType' object has no attribute 'get'). Hi,. I'm trying to plot some quantitative data from a dataframe ...
Read more >
Python - Issue to create choropleth map based on GeoJSON file
I'm trying to build a choropleth map, I cannot figure out why I get this error: AttributeError: 'NoneType' object has no attribute 'get'....
Read more >
folium - Jupyter Notebooks Gallery
A gallery of the most interesting jupyter notebooks online.
Read more >
[Example code]-Folium library error in choropleth
geo_path doesn't work because it is not a parameter for choropleth. You are correct in replacing it with geo_data. Your second error is...
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