Skip to content

Commit b77e472

Browse files
author
Martin Journois
committed
GeoJson Element
1 parent 5555543 commit b77e472

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

folium/features.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,3 +811,41 @@ def render(self, **kwargs):
811811
Template("""function vega_parse(spec, div) {
812812
vg.parse.spec(spec, function(chart) { chart({el:div}).update(); });}"""),
813813
name='vega_parse')
814+
815+
class GeoJson(MacroElement):
816+
def __init__(self, data):
817+
"""Creates a GeoJson plugin to append into a map with
818+
Map.add_plugin.
819+
820+
Parameters
821+
----------
822+
data: file, dict or str.
823+
The geo-json data you want to plot.
824+
If file, then data will be read in the file and fully embeded in Leaflet's javascript.
825+
If dict, then data will be converted to json and embeded in the javascript.
826+
If str, then data will be passed to the javascript as-is.
827+
828+
examples :
829+
# providing file
830+
GeoJson(open('foo.json'))
831+
832+
# providing dict
833+
GeoJson(json.load(open('foo.json')))
834+
835+
# providing string
836+
GeoJson(open('foo.json').read())
837+
"""
838+
super(GeoJson, self).__init__()
839+
self._name = 'GeoJson'
840+
if 'read' in dir(data):
841+
self.data = data.read()
842+
elif type(data) is dict:
843+
self.data = json.dumps(data)
844+
else:
845+
self.data = data
846+
847+
self._template = Template(u"""
848+
{% macro script(this, kwargs) %}
849+
var {{this.get_name()}} = L.geoJson({{this.data}}).addTo({{this._parent.get_name()}});
850+
{% endmacro %}
851+
""")

0 commit comments

Comments
 (0)