Skip to content

Commit 691511a

Browse files
iwpndConengmo
andauthored
feat: add Leaflet.VectorGrid plugin (#1576)
* feat: add Leaflet.VectorGrid plugin * typos * notebook styling Co-authored-by: Frank <[email protected]>
1 parent 009fe83 commit 691511a

File tree

4 files changed

+461
-0
lines changed

4 files changed

+461
-0
lines changed

examples/plugin-vector-tiles.ipynb

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 81,
6+
"id": "09534296",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from folium.plugins import VectorGridProtobuf\n",
11+
"import folium"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 30,
17+
"id": "c044c77f",
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"styles = {\n",
22+
" \"water\": {\n",
23+
" \"fill\": True,\n",
24+
" \"weight\": 1,\n",
25+
" \"fillColor\": \"#06cccc\",\n",
26+
" \"color\": \"#06cccc\",\n",
27+
" \"fillOpacity\": 0.2,\n",
28+
" \"opacity\": 0.4\n",
29+
" },\n",
30+
" \"admin\": {\n",
31+
" \"weight\": 1,\n",
32+
" \"fillColor\": \"pink\",\n",
33+
" \"color\": \"pink\",\n",
34+
" \"fillOpacity\": 0.2,\n",
35+
" \"opacity\": 0.4\n",
36+
" },\n",
37+
" \"waterway\": {\n",
38+
" \"weight\": 1,\n",
39+
" \"fillColor\": \"#2375e0\",\n",
40+
" \"color\": \"#2375e0\",\n",
41+
" \"fillOpacity\": 0.2,\n",
42+
" \"opacity\": 0.4\n",
43+
" },\n",
44+
" \"landcover\": {\n",
45+
" \"fill\": True,\n",
46+
" \"weight\": 1,\n",
47+
" \"fillColor\": \"#53e033\",\n",
48+
" \"color\": \"#53e033\",\n",
49+
" \"fillOpacity\": 0.2,\n",
50+
" \"opacity\": 0.4\n",
51+
" },\n",
52+
" \"landuse\": {\n",
53+
" \"fill\": True,\n",
54+
" \"weight\": 1,\n",
55+
" \"fillColor\": \"#e5b404\",\n",
56+
" \"color\": \"#e5b404\",\n",
57+
" \"fillOpacity\": 0.2,\n",
58+
" \"opacity\": 0.4\n",
59+
" },\n",
60+
" \"park\": {\n",
61+
" \"fill\": True,\n",
62+
" \"weight\": 1,\n",
63+
" \"fillColor\": \"#84ea5b\",\n",
64+
" \"color\": \"#84ea5b\",\n",
65+
" \"fillOpacity\": 0.2,\n",
66+
" \"opacity\": 0.4\n",
67+
" },\n",
68+
" \"boundary\": {\n",
69+
" \"weight\": 1,\n",
70+
" \"fillColor\": \"#c545d3\",\n",
71+
" \"color\": \"#c545d3\",\n",
72+
" \"fillOpacity\": 0.2,\n",
73+
" \"opacity\": 0.4\n",
74+
" },\n",
75+
" \"aeroway\": {\n",
76+
" \"weight\": 1,\n",
77+
" \"fillColor\": \"#51aeb5\",\n",
78+
" \"color\": \"#51aeb5\",\n",
79+
" \"fillOpacity\": 0.2,\n",
80+
" \"opacity\": 0.4\n",
81+
" },\n",
82+
" \"building\": {\n",
83+
" \"fill\": True,\n",
84+
" \"weight\": 1,\n",
85+
" \"fillColor\": \"#2b2b2b\",\n",
86+
" \"color\": \"#2b2b2b\",\n",
87+
" \"fillOpacity\": 0.2,\n",
88+
" \"opacity\": 0.4\n",
89+
" },\n",
90+
" \"water_name\": {\n",
91+
" \"weight\": 1,\n",
92+
" \"fillColor\": \"#022c5b\",\n",
93+
" \"color\": \"#022c5b\",\n",
94+
" \"fillOpacity\": 0.2,\n",
95+
" \"opacity\": 0.4\n",
96+
" },\n",
97+
" \"transportation_name\": {\n",
98+
" \"weight\": 1,\n",
99+
" \"fillColor\": \"#bc6b38\",\n",
100+
" \"color\": \"#bc6b38\",\n",
101+
" \"fillOpacity\": 0.2,\n",
102+
" \"opacity\": 0.4\n",
103+
" },\n",
104+
" \"place\": {\n",
105+
" \"weight\": 1,\n",
106+
" \"fillColor\": \"#f20e93\",\n",
107+
" \"color\": \"#f20e93\",\n",
108+
" \"fillOpacity\": 0.2,\n",
109+
" \"opacity\": 0.4\n",
110+
" },\n",
111+
" \"housenumber\": {\n",
112+
" \"weight\": 1,\n",
113+
" \"fillColor\": \"#ef4c8b\",\n",
114+
" \"color\": \"#ef4c8b\",\n",
115+
" \"fillOpacity\": 0.2,\n",
116+
" \"opacity\": 0.4\n",
117+
" },\n",
118+
" \"poi\": {\n",
119+
" \"weight\": 1,\n",
120+
" \"fillColor\": \"#3bb50a\",\n",
121+
" \"color\": \"#3bb50a\",\n",
122+
" \"fillOpacity\": 0.2,\n",
123+
" \"opacity\": 0.4\n",
124+
" },\n",
125+
" \"road\": {\n",
126+
" \"weight\": 1,\n",
127+
" \"fillColor\": \"#f2b648\",\n",
128+
" \"color\": \"#f2b648\",\n",
129+
" \"fillOpacity\": 0.2,\n",
130+
" \"opacity\": 0.4\n",
131+
" },\n",
132+
"}"
133+
]
134+
},
135+
{
136+
"cell_type": "code",
137+
"execution_count": 92,
138+
"id": "725693fc",
139+
"metadata": {},
140+
"outputs": [],
141+
"source": [
142+
"vectorTileLayerStyles = {}\n",
143+
"vectorTileLayerStyles[\"aerodrome_label\"] = []\n",
144+
"vectorTileLayerStyles[\"aeroway\"] = []\n",
145+
"vectorTileLayerStyles[\"area_name\"] = []\n",
146+
"vectorTileLayerStyles[\"boundary\"] = styles[\"admin\"]\n",
147+
"vectorTileLayerStyles[\"building\"] = []\n",
148+
"vectorTileLayerStyles[\"building_ln\"] = []\n",
149+
"vectorTileLayerStyles[\"construct\"] = []\n",
150+
"vectorTileLayerStyles[\"contour_line\"] = []\n",
151+
"vectorTileLayerStyles[\"landcover\"] = styles[\"landcover\"]\n",
152+
"vectorTileLayerStyles[\"landuse\"] = styles[\"landuse\"]\n",
153+
"vectorTileLayerStyles[\"mountain_peak\"] = []\n",
154+
"vectorTileLayerStyles[\"park\"] = styles[\"park\"]\n",
155+
"vectorTileLayerStyles[\"place\"] = []\n",
156+
"vectorTileLayerStyles[\"poi\"] = []\n",
157+
"vectorTileLayerStyles[\"spot_elevation\"] = []\n",
158+
"vectorTileLayerStyles[\"transportation\"] = styles[\"road\"]\n",
159+
"vectorTileLayerStyles[\"transportation_name\"] = []\n",
160+
"vectorTileLayerStyles[\"water\"] = styles[\"water\"]\n",
161+
"vectorTileLayerStyles[\"waterway\"] = styles[\"water\"]\n",
162+
"vectorTileLayerStyles[\"water_name\"] = []"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 97,
168+
"id": "49c9bf7a",
169+
"metadata": {},
170+
"outputs": [
171+
{
172+
"data": {
173+
"text/html": [
174+
"<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><span style=\"color:#565656\">Make this Notebook Trusted to load map: File -> Trust Notebook</span><iframe src=\"about:blank\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" data-html=%3C%21DOCTYPE%20html%3E%0A%3Chead%3E%20%20%20%20%0A%20%20%20%20%3Cmeta%20http-equiv%3D%22content-type%22%20content%3D%22text/html%3B%20charset%3DUTF-8%22%20/%3E%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%3Cscript%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20L_NO_TOUCH%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20L_DISABLE_3D%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20%3C/script%3E%0A%20%20%20%20%0A%20%20%20%20%3Cstyle%3Ehtml%2C%20body%20%7Bwidth%3A%20100%25%3Bheight%3A%20100%25%3Bmargin%3A%200%3Bpadding%3A%200%3B%7D%3C/style%3E%0A%20%20%20%20%3Cstyle%3E%23map%20%7Bposition%3Aabsolute%3Btop%3A0%3Bbottom%3A0%3Bright%3A0%3Bleft%3A0%3B%7D%3C/style%3E%0A%20%20%20%20%3Cscript%20src%3D%22https%3A//cdn.jsdelivr.net/npm/leaflet%401.6.0/dist/leaflet.js%22%3E%3C/script%3E%0A%20%20%20%20%3Cscript%20src%3D%22https%3A//code.jquery.com/jquery-1.12.4.min.js%22%3E%3C/script%3E%0A%20%20%20%20%3Cscript%20src%3D%22https%3A//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js%22%3E%3C/script%3E%0A%20%20%20%20%3Cscript%20src%3D%22https%3A//cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.js%22%3E%3C/script%3E%0A%20%20%20%20%3Clink%20rel%3D%22stylesheet%22%20href%3D%22https%3A//cdn.jsdelivr.net/npm/leaflet%401.6.0/dist/leaflet.css%22/%3E%0A%20%20%20%20%3Clink%20rel%3D%22stylesheet%22%20href%3D%22https%3A//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css%22/%3E%0A%20%20%20%20%3Clink%20rel%3D%22stylesheet%22%20href%3D%22https%3A//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css%22/%3E%0A%20%20%20%20%3Clink%20rel%3D%22stylesheet%22%20href%3D%22https%3A//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css%22/%3E%0A%20%20%20%20%3Clink%20rel%3D%22stylesheet%22%20href%3D%22https%3A//cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css%22/%3E%0A%20%20%20%20%3Clink%20rel%3D%22stylesheet%22%20href%3D%22https%3A//cdn.jsdelivr.net/gh/python-visualization/folium/folium/templates/leaflet.awesome.rotate.min.css%22/%3E%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cmeta%20name%3D%22viewport%22%20content%3D%22width%3Ddevice-width%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20initial-scale%3D1.0%2C%20maximum-scale%3D1.0%2C%20user-scalable%3Dno%22%20/%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cstyle%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23map_4b05f680d71e4c9bb2f663b45a87e500%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20position%3A%20relative%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20width%3A%20100.0%25%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20height%3A%20100.0%25%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20left%3A%200.0%25%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20top%3A%200.0%25%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C/style%3E%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%3Cscript%20src%3D%22https%3A//unpkg.com/leaflet.vectorgrid%40latest/dist/Leaflet.VectorGrid.bundled.js%22%3E%3C/script%3E%0A%3C/head%3E%0A%3Cbody%3E%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cdiv%20class%3D%22folium-map%22%20id%3D%22map_4b05f680d71e4c9bb2f663b45a87e500%22%20%3E%3C/div%3E%0A%20%20%20%20%20%20%20%20%0A%3C/body%3E%0A%3Cscript%3E%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20map_4b05f680d71e4c9bb2f663b45a87e500%20%3D%20L.map%28%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22map_4b05f680d71e4c9bb2f663b45a87e500%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20center%3A%20%5B46.8%2C%208.2%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20crs%3A%20L.CRS.EPSG3857%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20zoom%3A%2014%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20zoomControl%3A%20true%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20preferCanvas%3A%20false%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%29%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20var%20vector_grid_protobuf_653d81da7d714651bbab3d81229a2551%20%3D%20L.vectorGrid.protobuf%28%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27https%3A//vectortiles3.geo.admin.ch/tiles/ch.swisstopo.leichte-basiskarte.vt/v1.0.0/%7Bz%7D/%7Bx%7D/%7By%7D.pbf%27%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22vectorTileLayerStyles%22%3A%20%7B%22aerodrome_label%22%3A%20%5B%5D%2C%20%22aeroway%22%3A%20%5B%5D%2C%20%22area_name%22%3A%20%5B%5D%2C%20%22boundary%22%3A%20%7B%22color%22%3A%20%22pink%22%2C%20%22fillColor%22%3A%20%22pink%22%2C%20%22fillOpacity%22%3A%200.2%2C%20%22opacity%22%3A%200.4%2C%20%22weight%22%3A%201%7D%2C%20%22building%22%3A%20%5B%5D%2C%20%22building_ln%22%3A%20%5B%5D%2C%20%22construct%22%3A%20%5B%5D%2C%20%22contour_line%22%3A%20%5B%5D%2C%20%22landcover%22%3A%20%7B%22color%22%3A%20%22%2353e033%22%2C%20%22fill%22%3A%20true%2C%20%22fillColor%22%3A%20%22%2353e033%22%2C%20%22fillOpacity%22%3A%200.2%2C%20%22opacity%22%3A%200.4%2C%20%22weight%22%3A%201%7D%2C%20%22landuse%22%3A%20%7B%22color%22%3A%20%22%23e5b404%22%2C%20%22fill%22%3A%20true%2C%20%22fillColor%22%3A%20%22%23e5b404%22%2C%20%22fillOpacity%22%3A%200.2%2C%20%22opacity%22%3A%200.4%2C%20%22weight%22%3A%201%7D%2C%20%22mountain_peak%22%3A%20%5B%5D%2C%20%22park%22%3A%20%7B%22color%22%3A%20%22%2384ea5b%22%2C%20%22fill%22%3A%20true%2C%20%22fillColor%22%3A%20%22%2384ea5b%22%2C%20%22fillOpacity%22%3A%200.2%2C%20%22opacity%22%3A%200.4%2C%20%22weight%22%3A%201%7D%2C%20%22place%22%3A%20%5B%5D%2C%20%22poi%22%3A%20%5B%5D%2C%20%22spot_elevation%22%3A%20%5B%5D%2C%20%22transportation%22%3A%20%7B%22color%22%3A%20%22%23f2b648%22%2C%20%22fillColor%22%3A%20%22%23f2b648%22%2C%20%22fillOpacity%22%3A%200.2%2C%20%22opacity%22%3A%200.4%2C%20%22weight%22%3A%201%7D%2C%20%22transportation_name%22%3A%20%5B%5D%2C%20%22water%22%3A%20%7B%22color%22%3A%20%22%2306cccc%22%2C%20%22fill%22%3A%20true%2C%20%22fillColor%22%3A%20%22%2306cccc%22%2C%20%22fillOpacity%22%3A%200.2%2C%20%22opacity%22%3A%200.4%2C%20%22weight%22%3A%201%7D%2C%20%22water_name%22%3A%20%5B%5D%2C%20%22waterway%22%3A%20%7B%22color%22%3A%20%22%2306cccc%22%2C%20%22fill%22%3A%20true%2C%20%22fillColor%22%3A%20%22%2306cccc%22%2C%20%22fillOpacity%22%3A%200.2%2C%20%22opacity%22%3A%200.4%2C%20%22weight%22%3A%201%7D%7D%7D%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.addTo%28map_4b05f680d71e4c9bb2f663b45a87e500%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%3C/script%3E onload=\"this.contentDocument.open();this.contentDocument.write( decodeURIComponent(this.getAttribute('data-html')));this.contentDocument.close();\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
175+
],
176+
"text/plain": [
177+
"<folium.folium.Map at 0x123d388e0>"
178+
]
179+
},
180+
"execution_count": 97,
181+
"metadata": {},
182+
"output_type": "execute_result"
183+
}
184+
],
185+
"source": [
186+
"url = \"https://vectortiles3.geo.admin.ch/tiles/ch.swisstopo.leichte-basiskarte.vt/v1.0.0/{z}/{x}/{y}.pbf\"\n",
187+
"m = folium.Map(tiles=None, location=[46.8, 8.2], zoom_start=14)\n",
188+
"\n",
189+
"options = {\n",
190+
" \"vectorTileLayerStyles\": vectorTileLayerStyles\n",
191+
"}\n",
192+
"\n",
193+
"vc = VectorGridProtobuf(url, \"folium_layer_name\", options)\n",
194+
"m.add_child(vc)\n",
195+
"m"
196+
]
197+
}
198+
],
199+
"metadata": {
200+
"kernelspec": {
201+
"display_name": "Python 3",
202+
"language": "python",
203+
"name": "python3"
204+
},
205+
"language_info": {
206+
"codemirror_mode": {
207+
"name": "ipython",
208+
"version": 3
209+
},
210+
"file_extension": ".py",
211+
"mimetype": "text/x-python",
212+
"name": "python",
213+
"nbconvert_exporter": "python",
214+
"pygments_lexer": "ipython3",
215+
"version": "3.7.10"
216+
}
217+
},
218+
"nbformat": 4,
219+
"nbformat_minor": 5
220+
}

folium/plugins/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from folium.plugins.time_slider_choropleth import TimeSliderChoropleth
3434
from folium.plugins.timestamped_geo_json import TimestampedGeoJson
3535
from folium.plugins.timestamped_wmstilelayer import TimestampedWmsTileLayers
36+
from folium.plugins.vectorgrid_protobuf import VectorGridProtobuf
3637

3738
__all__ = [
3839
'AntPath',
@@ -63,4 +64,5 @@
6364
'TimeSliderChoropleth',
6465
'TimestampedGeoJson',
6566
'TimestampedWmsTileLayers',
67+
'VectorGridProtobuf'
6668
]
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
2+
from folium.elements import JSCSSMixin
3+
from folium.map import Layer
4+
from jinja2 import Template
5+
6+
7+
class VectorGridProtobuf(JSCSSMixin, Layer):
8+
"""
9+
Add vector tile layers based on https://github.com/Leaflet/Leaflet.VectorGrid.
10+
11+
Parameters
12+
----------
13+
url: url to tile provider
14+
e.g. https://free-{s}.tilehosting.com/data/v3/{z}/{x}/{y}.pbf?token={token}
15+
layer_name: string, default "VectorGridLayer"
16+
name of the layer
17+
options: dict or str, VectorGrid.protobuf options
18+
19+
For convenience you can pass VectorGrid.protobuf options as python dictionary or string.
20+
Strings allow plain JavaScript to be passed, therefore allow for conditional styling (see examples).
21+
22+
Additionally the url might contain any string literals like {token}, or {key}
23+
that can be passed as attribute to the options dict and will be substituted.
24+
25+
Every layer inside the tile layer has to be styled separately.
26+
27+
Examples
28+
--------
29+
30+
Options as dict:
31+
32+
>>> m = folium.Map()
33+
>>> url = "https://free-{s}.tilehosting.com/data/v3/{z}/{x}/{y}.pbf?token={token}"
34+
>>> options = {
35+
... "subdomain": "tilehosting",
36+
... "token": "af6P2G9dztAt1F75x7KYt0Hx2DJR052G",
37+
... "vectorTileLayerStyles": {
38+
... "layer_name_one": {
39+
... "fill": True,
40+
... "weight": 1,
41+
... "fillColor": 'green',
42+
... "color": 'black',
43+
... "fillOpacity":0.6,
44+
... "opacity":0.6
45+
... },
46+
... "layer_name_two": {
47+
... "fill": True,
48+
... "weight": 1,
49+
... "fillColor": 'red',
50+
... "color": 'black',
51+
... "fillOpacity":0.6,
52+
... "opacity":0.6
53+
... }
54+
... }
55+
... }
56+
57+
>>> VectorGridProtobuf(url,"layer_name",options).add_to(m)
58+
59+
Options as string allows to pass functions
60+
61+
>>> m = folium.Map()
62+
>>> url = "https://free-{s}.tilehosting.com/data/v3/{z}/{x}/{y}.pbf?token={token}"
63+
>>> options = '''{
64+
... "subdomain": "tilehosting",
65+
... "token": "af6P2G9dztAt1F75x7KYt0Hx2DJR052G",
66+
... "vectorTileLayerStyles": {
67+
... all: function(f) {
68+
... if (f.type === 'parks') {
69+
... return {
70+
... "fill": true,
71+
... "weight": 1,
72+
... "fillColor": 'green',
73+
... "color": 'black',
74+
... "fillOpacity":0.6,
75+
... "opacity":0.6
76+
... };
77+
... }
78+
... if (f.type === 'water') {
79+
... return {
80+
... "fill": true,
81+
... "weight": 1,
82+
... "fillColor": 'purple',
83+
... "color": 'black',
84+
... "fillOpacity":0.6,
85+
... "opacity":0.6
86+
... };
87+
... }
88+
... }
89+
... }
90+
}'''
91+
92+
>>> VectorGridProtobuf(url,"layer_name",options).add_to(m)
93+
94+
95+
For more info, see: https://leaflet.github.io/Leaflet.VectorGrid/vectorgrid-api-docs.html#styling-vectorgrids.
96+
"""
97+
98+
_template = Template(
99+
"""
100+
{% macro script(this, kwargs) -%}
101+
var {{ this.get_name() }} = L.vectorGrid.protobuf(
102+
'{{ this.url }}',
103+
{% if this.options is defined %}
104+
{{ this.options if this.options is string else this.options|tojson }})
105+
.addTo({{ this._parent.get_name() }});
106+
{% else %}
107+
{{ this.options }}).addTo({{ this._parent.get_name() }});
108+
{% endif %}
109+
{%- endmacro %}
110+
"""
111+
) # noqa
112+
113+
default_js = [
114+
(
115+
"vectorGrid",
116+
"https://unpkg.com/leaflet.vectorgrid@latest/dist/Leaflet.VectorGrid.bundled.js",
117+
)
118+
]
119+
120+
def __init__(self, url, layer_name, options=None):
121+
self.layer_name = layer_name if layer_name else "VectorGridProtobufLayer"
122+
123+
super(VectorGridProtobuf, self).__init__(name=self.layer_name)
124+
125+
self.url = url
126+
self._name = "VectorGridProtobuf"
127+
128+
if options is not None:
129+
self.options = options

0 commit comments

Comments
 (0)