Skip to content

Commit b62abaf

Browse files
author
Martin Journois
committed
Add geojson plugin
1 parent 32d4654 commit b62abaf

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

folium/plugins/geojson.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import json
4+
5+
from .plugin import Plugin
6+
7+
class GeoJson(Plugin):
8+
"""Adds a GeoJson layer on the map."""
9+
def __init__(self, data):
10+
"""Creates a GeoJson plugin to append into a map with
11+
Map.add_plugin.
12+
13+
Parameters
14+
----------
15+
data: file, dict or str.
16+
The geo-json data you want to plot.
17+
If file, then data will be read in the file and fully embeded in Leaflet's javascript.
18+
If dict, then data will be converted to json and embeded in the javascript.
19+
If str, then data will be passed to the javascript as-is.
20+
21+
examples :
22+
# providing file
23+
GeoJson(open('foo.json'))
24+
25+
# providing dict
26+
GeoJson(json.load(open('foo.json')))
27+
28+
# providing string
29+
GeoJson(open('foo.json').read())
30+
"""
31+
super(GeoJson, self).__init__()
32+
self.plugin_name = 'GeoJson'
33+
if type(data) is file:
34+
self.data = data.read()
35+
elif type(data) is dict:
36+
self.data = json.dumps(data)
37+
else:
38+
self.data = data
39+
40+
def render_js(self, nb):
41+
"""Generates the Javascript part of the plugin."""
42+
out = """
43+
var geojson_{nb} = L.geoJson({data}).addTo(map);
44+
""".format(nb=nb, data = self.data)
45+
return out

0 commit comments

Comments
 (0)