Skip to content

Commit ba01ffa

Browse files
author
Martin Journois
committed
Add possibility to have unembedded GeoJSON.
1 parent 1366528 commit ba01ffa

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

folium/features.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import json
1010

1111
from .utilities import (color_brewer, _parse_size, legend_scaler,
12-
_locations_mirror, _locations_tolist, image_to_url)
12+
_locations_mirror, _locations_tolist, image_to_url,
13+
text_type, binary_type)
1314

1415
from .element import Element, Figure, JavascriptLink, CssLink, MacroElement
1516
from .map import TileLayer, Icon
@@ -192,7 +193,6 @@ def render(self, **kwargs):
192193
vg.parse.spec(spec, function(chart) { chart({el:div}).update(); });}"""), # noqa
193194
name='vega_parse')
194195

195-
196196
class GeoJson(MacroElement):
197197
def __init__(self, data, style_function=None):
198198
"""
@@ -213,8 +213,10 @@ def __init__(self, data, style_function=None):
213213
214214
Examples
215215
--------
216-
>>> # Providing file.
216+
>>> # Providing file that shall be embeded.
217217
>>> GeoJson(open('foo.json'))
218+
>>> # Providing filename that shall not be embeded.
219+
>>> GeoJson('foo.json')
218220
>>> # Providing dict.
219221
>>> GeoJson(json.load(open('foo.json')))
220222
>>> # Providing string.
@@ -227,32 +229,42 @@ def __init__(self, data, style_function=None):
227229
super(GeoJson, self).__init__()
228230
self._name = 'GeoJson'
229231
if 'read' in dir(data):
232+
self.embed = True
230233
self.data = json.load(data)
231234
elif type(data) is dict:
235+
self.embed = True
232236
self.data = data
237+
elif isinstance(data, text_type) or isinstance(data, binary_type):
238+
if data.lstrip()[0] in '[{': # This is a GeoJSON inline string
239+
self.embed = True
240+
self.data = json.loads(data)
241+
else: # This is a filename
242+
self.embed = False
243+
self.data = data
233244
else:
234-
self.data = json.loads(data)
235-
236-
if 'features' not in self.data.keys():
237-
# Catch case when GeoJSON is just a single Feature or a geometry.
238-
if not (isinstance(self.data, dict) and 'geometry' in self.data.keys()):
239-
# Catch case when GeoJSON is just a geometry.
240-
self.data = {'type' : 'Feature', 'geometry' : self.data}
241-
self.data = {'type' : 'FeatureCollection', 'features' : [self.data]}
245+
raise ValueError('Unhandled data type.')
242246

243247
if style_function is None:
244248
style_function = lambda x: {}
245249
self.style_function = style_function
246250

247251
self._template = Template(u"""
248252
{% macro script(this, kwargs) %}
249-
var {{this.get_name()}} = L.geoJson({{this.style_data()}})
253+
var {{this.get_name()}} = L.geoJson(
254+
{% if this.embed %}{{this.style_data()}}{% else %}"{{this.data}}"{% endif %})
250255
.addTo({{this._parent.get_name()}})
251256
.setStyle(function(feature) {return feature.properties.style;});
252257
{% endmacro %}
253258
""") # noqa
254259

255260
def style_data(self):
261+
if 'features' not in self.data.keys():
262+
# Catch case when GeoJSON is just a single Feature or a geometry.
263+
if not (isinstance(self.data, dict) and 'geometry' in self.data.keys()):
264+
# Catch case when GeoJSON is just a geometry.
265+
self.data = {'type' : 'Feature', 'geometry' : self.data}
266+
self.data = {'type' : 'FeatureCollection', 'features' : [self.data]}
267+
256268
for feature in self.data['features']:
257269
feature.setdefault('properties',{}).setdefault('style',{}).update(
258270
self.style_function(feature))

0 commit comments

Comments
 (0)