Skip to content

TopoJSON fails with Polygon/MultiPolygon objects #1816

Closed

Description

@lazka

Describe the bug

I got a topojson file and want to draw it with folium.
This failed with:

  File ".../lib/python3.11/site-packages/folium/features.py", line 981, in style_data
    geometries = recursive_get(self.data, self.object_path.split("."))[
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'geometries'

To Reproduce

topo_json = {
  "type": "Topology",
  "objects": {
    "A": {
      "type": "Polygon",
      "id": "A",
      "bbox": [
        9.47021484375, 46.34692761055676, 17.259521484375, 49.059469847170526
      ],
      "arcs": [[0]]
    }
  },
  "arcs": [
    [
      [28, 4614],
      [437, 219],
      [607, -875],
      [99, 738],
      [818, -109],
      [56, -437],
      [338, 54],
      [1185, 1255],
      [874, -626],
      [-310, 1657],
      [423, 782],
      [325, 189],
      [169, 832],
      [141, 107],
      [240, -161],
      [112, 882],
      [424, -507],
      [268, -214],
      [832, 1599],
      [394, -80],
      [1072, -718],
      [282, -187],
      [212, 347],
      [606, -480],
      [71, -829],
      [-155, -511],
      [451, -1430],
      [-183, -1224],
      [-508, -81],
      [-127, 272],
      [-99, -163],
      [240, -246],
      [56, -300],
      [-239, -713],
      [-28, -1294],
      [-311, -110],
      [-366, -609],
      [14, -611],
      [-268, 222],
      [-480, -389],
      [-733, -112],
      [-451, -753],
      [-2892, 1226],
      [-324, 1247],
      [-395, -249],
      [-663, -55],
      [-253, -665],
      [-748, 249],
      [-70, 443],
      [-367, -470],
      [-663, 747],
      [-141, 881],
      [141, 440],
      [-113, 820]
    ]
  ],
  "transform": {
    "scale": [0.0007790085649189919, 0.0002712813517965561],
    "translate": [9.47021484375, 46.34692761055676]
  },
  "bbox": [
    9.47021484375, 46.34692761055676, 17.259521484375, 49.059469847170526
  ],
  "properties": {
  }
}

def fixup_topo_json_for_folium(topo_json: dict):
    # Folium only supports TopoJSON with GeometryCollection, so hack it together
    for key in topo_json["objects"].keys():
        orig = topo_json["objects"][key]
        new = {"type": "GeometryCollection", "geometries": [orig]}
        topo_json["objects"][key] = new

import folium
map = folium.Map()
# fixup_topo_json_for_folium(topo_json)
folium.TopoJson(topo_json, "objects.A").add_to(map)
map.save("bug.html")

Expected behavior

It draws the topojson

Environment (please complete the following information):

  • Browser [e.g. chrome, firefox]: Firefox
  • Jupyter Notebook or html files? html
  • Python version (check it with import sys; print(sys.version_info)) 3.11
  • folium version (check it with import folium; print(folium.__version__)) 0.14.0
  • branca version (check it with import branca; print(branca.__version__)) 0.6.0

Additional context

It looks like it only supports GeometryCollection and not any of the other types, I've included my current workaround in the reproducer called fixup_topo_json_for_folium() which makes it work by wrapping all objects in a dummy GeometryCollection.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions