Skip to content

Commit ab0b7a0

Browse files
author
Martin Journois
committed
Implement _get_self_bounds for TopoJson
1 parent d70db11 commit ab0b7a0

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

folium/features.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,13 @@ def __init__(self, data, object_path):
316316
super(TopoJson, self).__init__()
317317
self._name = 'TopoJson'
318318
if 'read' in dir(data):
319+
self.embed = True
319320
self.data = data.read()
320321
elif type(data) is dict:
322+
self.embed = True
321323
self.data = json.dumps(data)
322324
else:
325+
self.embed = False
323326
self.data = data
324327

325328
self.object_path = object_path
@@ -349,8 +352,33 @@ def _get_self_bounds(self):
349352
"""Computes the bounds of the object itself (not including it's children)
350353
in the form [[lat_min, lon_min], [lat_max, lon_max]]
351354
"""
352-
raise NotImplementedError
353-
return [[self.location[0],self.location[1]],[self.location[0],self.location[1]]]
355+
if not self.embed:
356+
raise ValueError('Cannot compute bounds of non-embedded TopoJSON.')
357+
358+
data = json.loads(self.data)
359+
360+
xmin,xmax,ymin,ymax = None, None, None, None
361+
362+
for arc in data['arcs']:
363+
x,y = 0,0
364+
for dx, dy in arc:
365+
x += dx
366+
y += dy
367+
xmin = none_min(x, xmin)
368+
xmax = none_max(x, xmax)
369+
ymin = none_min(y, ymin)
370+
ymax = none_max(y, ymax)
371+
return [
372+
[
373+
data['transform']['translate'][0] + data['transform']['scale'][0] * xmin,
374+
data['transform']['translate'][1] + data['transform']['scale'][1] * ymin,
375+
],
376+
[
377+
data['transform']['translate'][0] + data['transform']['scale'][0] * xmax,
378+
data['transform']['translate'][1] + data['transform']['scale'][1] * ymax,
379+
]
380+
381+
]
354382

355383
class ColorScale(MacroElement):
356384
def __init__(self, color_domain, color_code, caption=""):

tests/test_folium.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,10 @@ def test_topo_json(self):
447447
topojson_str = topo_json._template.module.script(topo_json)
448448
assert ''.join(topojson_str.split())[:-1] in ''.join(out.split())
449449

450+
bounds = self.map.get_bounds()
451+
assert bounds == [[-124.56617536999985, 41.99187135900012],
452+
[-116.46422312599977, 46.28768217800006]], bounds
453+
450454
def test_map_build(self):
451455
"""Test map build."""
452456

0 commit comments

Comments
 (0)