Search code examples
pythongeojsontopojsonfolium

Python Folium Topojson file not rendering


Hello I am trying to render this map with Folium inside a jupyter notebook.

https://github.com/kthotav/TopoJSON-Maps/blob/master/usa/usa-states/colorado/colorado-counties.json

As this is TopoJson, it should be pretty straightforward

m = folium.Map([39, -105], zoom_start=7)
folium.TopoJson(
    open('./data/colorado-counties.json'),
    object_path='objects.colorado-counties'
).add_to(m)
m

This renders the base layer, but does not draw the polygons for the counties.

I found this other example on stackoverflow and it renders just fine with essentially the same code.

m = folium.Map(location=[40.7,-74], zoom_start=10)
folium.TopoJson(
    open('./data/nyc_census_tracts_2010.geojson'),
    object_path='objects.nyct2010',
).add_to(m)
m

I can't find any large scale differences between the two files. Is there some topojson version incompatibility?


Solution

  • OK, figured it out. folium can't parse object names with -s in them. Probably a javascript translation thing?

    fun test

    works

    m = folium.Map([0, 0], zoom_start=7)
    folium.TopoJson(
        {
          "type":"Topology",
          "transform":{
            "scale": [1,1],
            "translate": [0,0]
          },
          "objects":{ 
            "two_squares":{
              "type": "GeometryCollection",
              "geometries":[
                {"type": "Polygon", "arcs":[[0,1]],"properties": {"name": "Left_Polygon" }},
                {"type": "Polygon", "arcs":[[2,-1]],"properties": {"name": "Right_Polygon" }}
              ]
            }
          },
          "arcs": [
            [[1,2],[0,-2]],
            [[1,0],[-1,0],[0,2],[1,0]],
            [[1,2],[1,0],[0,-2],[-1,0]]
          ]
        },
        object_path='objects.two_squares'
    ).add_to(m)
    m
    

    does not work

    m = folium.Map([0, 0], zoom_start=7)
    folium.TopoJson(
        {
          "type":"Topology",
          "transform":{
            "scale": [1,1],
            "translate": [0,0]
          },
          "objects":{ 
            "two-squares":{
              "type": "GeometryCollection",
              "geometries":[
                {"type": "Polygon", "arcs":[[0,1]],"properties": {"name": "Left_Polygon" }},
                {"type": "Polygon", "arcs":[[2,-1]],"properties": {"name": "Right_Polygon" }}
              ]
            }
          },
          "arcs": [
            [[1,2],[0,-2]],
            [[1,0],[-1,0],[0,2],[1,0]],
            [[1,2],[1,0],[0,-2],[-1,0]]
          ]
        },
        object_path='objects.two-squares'
    ).add_to(m)
    m