python-visualization / folium

Python Data. Leaflet.js Maps.
https://python-visualization.github.io/folium/
MIT License
6.85k stars 2.22k forks source link

Folium can't parse TopoJSON objects with dashes `-` in name #828

Closed kevhill closed 1 year ago

kevhill commented 6 years ago

This works just fine in an ipython notebook

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

but changing the object to include a - renders the map, but not the polygons

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

Problem description

Literally took me 3 hours to diagnose, as the first topojson file I ever tried to render used -s

Expected Output

Ideal fix would just be to render all objects, as there is no topojson specification that precludes -s. Consolation prize would be a friendly error message when object_path contains a -

Output of folium.__version__

0.5.0

olavrel commented 6 years ago

By saving the two cases to html and inspecting the results, it seems the culprit the usage of dot notation on accessing object properties. Line 69: ....objects.two-squares Using bracket notation would have worked fine I guess? ...objects["two-squares"].

amrutharajashekar commented 1 year ago

Hi, can i work on this issue ? I would like to contribute .

Conengmo commented 1 year ago

@amrutha1098 you are very welcome to! Do you want some pointers? ~I assume this issue is referring to this line: https://github.com/python-visualization/folium/blob/main/folium/features.py#L746.~ It would be nice if you can create a test case that fails given the current code, but passes with the fix you make.