7

I am trying to display the following geojson file in a folium map in Python but it just shows an empty map with none of the data.

Here are the steps I have tried:

  1. I tried using the python code below but nothing shows up.

  2. I tried other geojson files in the github repository below using the same code and the data show up without any issue, so it looks like my python code is fine

  3. I opened the "census_tracts_2010.geojson" file in github and Mapshaper, the data showed up perfectly without any issue, so it doesn't look like the geojson file is corrupted

Could anyone please let me know how I can fix it?

Geojson file: https://github.com/dwillis/nyc-maps/blob/master/census_tracts_2010.geojson

Python code:

import folium
m = folium.Map(location=[40.66393072,-73.93827499], zoom_start=13)
m.choropleth(geo_path="census_tracts_2010.geojson")
m.save(outfile='datamap.html')

Thanks a lot!

vkc
  • 556
  • 2
  • 8
  • 18
  • You're not getting the html file out? I just did and it gets created. I'm using Anaconda Python 3.5.2. But if you mean the red dots that show up in Mapshaper - no I'm not getting those either. – Amorpheuses Feb 08 '17 at 07:56
  • No, I used the code above and I only get an empty NYC map, none of the census tracts polygons showed up. If it matters, I am using folium 0.2.1, Jupyter Notebook, Anaconda Python 3.5.2 in Windows 10. – vkc Feb 08 '17 at 19:41

3 Answers3

4

That file is not a GeoJson it is a TopoJson. You need to use folium.TopoJson instead.

import folium

m = folium.Map(location=[40.66393072,-73.93827499], zoom_start=13)

folium.TopoJson(
    open('census_tracts_2010.geojson'),
    object_path='objects.nyct2010',
).add_to(m)

m
ocefpaf
  • 569
  • 4
  • 15
  • Thanks a lot! I didn't realize it's TopoJson, now it works perfectly. I used this in the end as I am going to plot a choropleth: `m.choropleth(geo_path="/share/PythonProjects/nyc-maps/census_tracts_2010.geojson", topojson="objects.nyct2010")` – vkc Mar 07 '17 at 22:51
3

You need to open the geojson file.

    m.choropleth(open("census_tracts_2010.geojson"))

Take a look at the examples https://folium.readthedocs.io/en/latest/quickstart.html

  • 1
    I did look at the examples and I tried what you suggested but I got this error: "TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper". I also tried m.choropleth(geo_str=open("census_tracts_2010.geojson").read()) and I got the same empty NYC map. Did you try it and it works for you (showing the polygons of census tracts)? – vkc Feb 10 '17 at 23:24
3

Try this: m.add_child(folium.GeoJson(data = open("census_tracts_2010.geojson"))) and then call m.save() fun

  • I just tried it but I still got an empty NYC map, were you able to see the census tract polygons show up on the map? Thanks. – vkc Mar 02 '17 at 19:03