Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/plugins/test_timestamped_geo_json.py
1601 views
1
"""
2
Test TimestampedGeoJson
3
-----------------------
4
5
"""
6
7
import numpy as np
8
9
import folium
10
from folium import plugins
11
from folium.template import Template
12
from folium.utilities import normalize
13
14
15
def test_timestamped_geo_json():
16
coordinates = [
17
[
18
[
19
[lon - 8 * np.sin(theta), -47 + 6 * np.cos(theta)]
20
for theta in np.linspace(0, 2 * np.pi, 25)
21
],
22
[
23
[lon - 4 * np.sin(theta), -47 + 3 * np.cos(theta)]
24
for theta in np.linspace(0, 2 * np.pi, 25)
25
],
26
]
27
for lon in np.linspace(-150, 150, 7)
28
]
29
data = {
30
"type": "FeatureCollection",
31
"features": [
32
{
33
"type": "Feature",
34
"geometry": {
35
"type": "Point",
36
"coordinates": [0, 0],
37
},
38
"properties": {"times": [1435708800000 + 12 * 86400000]},
39
},
40
{
41
"type": "Feature",
42
"geometry": {
43
"type": "MultiPoint",
44
"coordinates": [[lon, -25] for lon in np.linspace(-150, 150, 49)],
45
},
46
"properties": {
47
"times": [
48
1435708800000 + i * 86400000 for i in np.linspace(0, 25, 49)
49
]
50
},
51
},
52
{
53
"type": "Feature",
54
"geometry": {
55
"type": "LineString",
56
"coordinates": [[lon, 25] for lon in np.linspace(-150, 150, 25)],
57
},
58
"properties": {
59
"times": [
60
1435708800000 + i * 86400000 for i in np.linspace(0, 25, 25)
61
],
62
"style": {"color": "red"},
63
},
64
},
65
{
66
"type": "Feature",
67
"geometry": {
68
"type": "MultiLineString",
69
"coordinates": [
70
[
71
[lon - 4 * np.sin(theta), 47 + 3 * np.cos(theta)]
72
for theta in np.linspace(0, 2 * np.pi, 25)
73
]
74
for lon in np.linspace(-150, 150, 13)
75
],
76
},
77
"properties": {
78
"times": [
79
1435708800000 + i * 86400000 for i in np.linspace(0, 25, 13)
80
]
81
},
82
},
83
{
84
"type": "Feature",
85
"geometry": {
86
"type": "MultiPolygon",
87
"coordinates": coordinates,
88
},
89
"properties": {
90
"times": [
91
1435708800000 + i * 86400000 for i in np.linspace(0, 25, 7)
92
]
93
},
94
},
95
],
96
}
97
98
m = folium.Map([47, 3], zoom_start=1)
99
tgj = plugins.TimestampedGeoJson(data).add_to(m)
100
101
out = normalize(m._parent.render())
102
103
# Verify the imports.
104
assert (
105
'<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>'
106
in out
107
)
108
assert (
109
'<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>'
110
in out
111
)
112
assert (
113
'<script src="https://cdn.jsdelivr.net/npm/[email protected]/iso8601.min.js"></script>'
114
in out
115
)
116
assert (
117
'<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.timedimension.min.js"></script>' # noqa
118
in out
119
) # noqa
120
assert (
121
'<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/default.min.css"/>'
122
in out
123
) # noqa
124
assert (
125
'<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.timedimension.control.css"/>' # noqa
126
in out
127
) # noqa
128
assert (
129
'<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>'
130
in out
131
)
132
133
# Verify that the script is okay.
134
tmpl = Template(
135
"""
136
L.Control.TimeDimensionCustom = L.Control.TimeDimension.extend({
137
_getDisplayDateFormat: function(date){
138
var newdate = new moment(date);
139
console.log(newdate)
140
return newdate.format("{{this.date_options}}");
141
}
142
});
143
{{this._parent.get_name()}}.timeDimension = L.timeDimension(
144
{
145
period: {{ this.period|tojson }},
146
}
147
);
148
var timeDimensionControl = new L.Control.TimeDimensionCustom(
149
{{ this.options|tojavascript }}
150
);
151
{{this._parent.get_name()}}.addControl(this.timeDimensionControl);
152
153
var geoJsonLayer = L.geoJson({{this.data}}, {
154
pointToLayer: function (feature, latLng) {
155
if (feature.properties.icon == 'marker') {
156
if(feature.properties.iconstyle){
157
return new L.Marker(latLng, {
158
icon: L.icon(feature.properties.iconstyle)});
159
}
160
//else
161
return new L.Marker(latLng);
162
}
163
if (feature.properties.icon == 'circle') {
164
if (feature.properties.iconstyle) {
165
return new L.circleMarker(latLng, feature.properties.iconstyle)
166
};
167
//else
168
return new L.circleMarker(latLng);
169
}
170
//else
171
172
return new L.Marker(latLng);
173
},
174
style: function (feature) {
175
return feature.properties.style;
176
},
177
onEachFeature: function(feature, layer) {
178
if (feature.properties.popup) {
179
layer.bindPopup(feature.properties.popup);
180
}
181
if (feature.properties.tooltip) {
182
layer.bindTooltip(feature.properties.tooltip);
183
}
184
}
185
})
186
187
var {{this.get_name()}} = L.timeDimension.layer.geoJson(
188
geoJsonLayer,
189
{
190
updateTimeDimension: true,
191
addlastPoint: {{ this.add_last_point|tojson }},
192
duration: {{ this.duration }},
193
}
194
).addTo({{this._parent.get_name()}});
195
"""
196
) # noqa
197
expected = normalize(tmpl.render(this=tgj))
198
assert expected in out
199
200
bounds = m.get_bounds()
201
assert bounds == [[-53.0, -158.0], [50.0, 158.0]], bounds
202
203