Skip to content

Commit dcbf516

Browse files
author
Martin Journois
committed
Add tests for plugins
1 parent b62abaf commit dcbf516

File tree

6 files changed

+113
-6
lines changed

6 files changed

+113
-6
lines changed

folium/plugins/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
from .scroll_zoom_toggler import ScrollZoomToggler
55
from .terminator import Terminator
66
from .boat_marker import BoatMarker
7-
from .layer import Layer, LayerControl
7+
from .layer import Layer, LayerControl
8+
from .geo_json import GeoJson

folium/plugins/boat_marker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self, position=None, heading=0, wind_heading=None, wind_speed=0, **
3737
def render_header(self, nb):
3838
"""Generates the HTML part of the plugin."""
3939
return """
40-
<script src="https://tomaszbrue.github.io/leaflet.boatmarker/js/leaflet.boatmarker.min.js"></script>
40+
<script src="https://thomasbrueggemann.github.io/leaflet.boatmarker/js/leaflet.boatmarker.min.js"></script>
4141
""" if nb==0 else ""
4242

4343
def render_js(self, nb):

folium/plugins/geojson.py renamed to folium/plugins/geo_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ def render_js(self, nb):
4242
out = """
4343
var geojson_{nb} = L.geoJson({data}).addTo(map);
4444
""".format(nb=nb, data = self.data)
45-
return out
45+
return out

folium/plugins/layer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
class Layer(Plugin):
66
"""Adds a layer to the map."""
7-
def __init__(self, layer_name = None, tile_url=None, min_zoom=1, max_zoom=18, attribution=''):
7+
def __init__(self, url=None, layer_name = None, min_zoom=1, max_zoom=18, attribution=''):
88
super(Layer, self).__init__()
99
self.plugin_name = 'Layer'
10-
self.tile_url = tile_url if tile_url is not None else '//otile1.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png'
10+
self.tile_url = url
1111
self.attribution = attribution
1212
self.min_zoom = min_zoom
1313
self.max_zoom = max_zoom

tests/folium_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import folium
1515
from folium.six import PY3
1616
from folium.plugins import ScrollZoomToggler, MarkerCluster
17-
17+
from test_plugins import testPlugins
1818

1919
def setup_data():
2020
'''Import economic data for testing'''

tests/test_plugins.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# -*- coding: utf-8 -*-
2+
'''
3+
Folium Plugins Tests
4+
--------------------
5+
6+
'''
7+
import folium
8+
from folium import plugins
9+
import numpy as np
10+
import json
11+
12+
class testPlugins(object):
13+
'''Test class for Folium plugins'''
14+
15+
def test_scroll_zoom_toggler(self):
16+
mapa = folium.Map([45.,3.], zoom_start=4)
17+
mapa.add_plugin(plugins.ScrollZoomToggler())
18+
mapa._build_map()
19+
20+
21+
def test_marker_cluster(self):
22+
N = 100
23+
data = np.array([
24+
np.random.uniform(low=35,high=60, size=N), # random latitudes in Europe
25+
np.random.uniform(low=-12,high=30, size=N), # random longitudes in Europe
26+
range(N), # popups are simple numbers
27+
]).T
28+
mapa = folium.Map([45.,3.], zoom_start=4)
29+
mapa.add_plugin(plugins.MarkerCluster(data))
30+
mapa._build_map()
31+
32+
def test_terminator(self):
33+
mapa = folium.Map([45.,3.], zoom_start=1)
34+
mapa.add_plugin(plugins.Terminator())
35+
mapa.add_plugin(plugins.ScrollZoomToggler())
36+
mapa._build_map()
37+
38+
def test_boat_marker(self):
39+
mapa = folium.Map([30.,0.], zoom_start=3)
40+
mapa.add_plugin(plugins.BoatMarker((34,-43), heading=45,
41+
wind_heading=150, wind_speed=45, color="#8f8"))
42+
mapa.add_plugin(plugins.BoatMarker((46,-30), heading=-20,
43+
wind_heading=46, wind_speed=25, color="#88f"))
44+
mapa._build_map()
45+
46+
def test_layer(self):
47+
mapa = folium.Map([48.,5.], zoom_start=6)
48+
mapa.add_plugin(plugins.Layer('//otile1.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png',
49+
layer_name='MapQuest'))
50+
mapa.add_plugin(plugins.LayerControl())
51+
mapa._build_map()
52+
53+
def test_geo_json(self):
54+
N=100
55+
lons = +5 - np.random.normal(size=N)
56+
lats = 48 - np.random.normal(size=N)
57+
58+
data = {
59+
"type": "FeatureCollection",
60+
"features": [
61+
{
62+
"type": "Feature",
63+
"geometry": {
64+
"type": "MultiPoint",
65+
"coordinates": [[lon, lat] for (lat,lon) in zip(lats,lons)],
66+
},
67+
"properties": {"prop0": "value0"}
68+
},
69+
],
70+
}
71+
72+
mapa = folium.Map([48.,5.], zoom_start=6)
73+
mapa.add_plugin(plugins.GeoJson(data))
74+
mapa._build_map()
75+
76+
open('geojson_plugin_test1.json','w').write(json.dumps(data))
77+
mapb = folium.Map([48.,5.], zoom_start=6)
78+
mapb.add_plugin(plugins.GeoJson(open('geojson_plugin_test1.json')))
79+
mapb._build_map()
80+
81+
data = {
82+
"type": "FeatureCollection",
83+
"features": [
84+
{
85+
"type": "Feature",
86+
"geometry": {
87+
"type": "MultiPolygon",
88+
"coordinates": [[[[lon+1e-4, lat+1e-4],
89+
[lon+1e-4, lat-1e-4],
90+
[lon-1e-4, lat-1e-4],
91+
[lon-1e-4, lat+1e-4]]] for (lat,lon) in zip(lats,lons)],
92+
},
93+
"properties": {"prop0": "value0"}
94+
},
95+
],
96+
}
97+
98+
mapc = folium.Map([48.,5.], zoom_start=6)
99+
mapc.add_plugin(plugins.GeoJson(data))
100+
mapc._build_map()
101+
102+
open('geojson_plugin_test2.json','w').write(json.dumps(data))
103+
mapd = folium.Map([48.,5.], zoom_start=6)
104+
mapd.add_plugin(plugins.GeoJson(open('geojson_plugin_test2.json')))
105+
mapd._build_map()
106+

0 commit comments

Comments
 (0)