Skip to content

Commit 3b6f130

Browse files
lopezvoliverpre-commit-ci[bot]giswqs
authored
Improve vector tile layer arguments (#703)
* Allow to send options directly as str * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Rearranged arguments positions * More detailed style example * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updated docstring --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Qiusheng Wu <giswqs@gmail.com>
1 parent 8e30645 commit 3b6f130

File tree

3 files changed

+144
-14
lines changed

3 files changed

+144
-14
lines changed

examples/notebooks/17_vector_tile_layer.ipynb

+138-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"metadata": {},
6161
"outputs": [],
6262
"source": [
63-
"m = leafmap.Map()"
63+
"m = leafmap.Map(center=(52.204793, 360.121558), zoom=9)"
6464
]
6565
},
6666
{
@@ -104,7 +104,14 @@
104104
"id": "10",
105105
"metadata": {},
106106
"source": [
107-
"One can customize the vector tile layer style if needed. More info can be found at https://ipyleaflet.readthedocs.io/en/latest/api_reference/vector_tile.html"
107+
"One can customize the vector tile layer style if needed. More info can be found at \n",
108+
"https://ipyleaflet.readthedocs.io/en/latest/layers/vector_tile.html \n",
109+
"\n",
110+
"Conditional styling ([example here](https://github.com/iwpnd/folium-vectorgrid)) currently works only with folium. Use:\n",
111+
"\n",
112+
"```python\n",
113+
"import leafmap.foliumap as leafmap \n",
114+
"```"
108115
]
109116
},
110117
{
@@ -114,7 +121,122 @@
114121
"metadata": {},
115122
"outputs": [],
116123
"source": [
117-
"vector_tile_layer_styles = {}"
124+
"water_style = dict(\n",
125+
" fill=\"true\",\n",
126+
" weight=1,\n",
127+
" fillColor=\"#06cccc\",\n",
128+
" color=\"#06cccc\",\n",
129+
" fillOpacity=0.2,\n",
130+
" opacity=0.4,\n",
131+
")\n",
132+
"\n",
133+
"waterway_style = dict(\n",
134+
" weight=1, fillColor=\"#2375e0\", color=\"#2375e0\", fillOpacity=0.2, opacity=0.4\n",
135+
")\n",
136+
"\n",
137+
"admin_style = dict(\n",
138+
" weight=1, fillColor=\"pink\", color=\"pink\", fillOpacity=0.2, opacity=0.4\n",
139+
")\n",
140+
"\n",
141+
"landcover_style = dict(\n",
142+
" fill=\"true\",\n",
143+
" weight=1,\n",
144+
" fillColor=\"#53e033\",\n",
145+
" color=\"#53e033\",\n",
146+
" fillOpacity=0.2,\n",
147+
" opacity=0.4,\n",
148+
")\n",
149+
"\n",
150+
"landuse_style = dict(\n",
151+
" fill=\"true\",\n",
152+
" weight=1,\n",
153+
" fillColor=\"#e5b404\",\n",
154+
" color=\"#e5b404\",\n",
155+
" fillOpacity=0.2,\n",
156+
" opacity=0.4,\n",
157+
")\n",
158+
"\n",
159+
"park_style = dict(\n",
160+
" fill=\"true\",\n",
161+
" weight=1,\n",
162+
" fillColor=\"#84ea5b\",\n",
163+
" color=\"#84ea5b\",\n",
164+
" fillOpacity=0.2,\n",
165+
" opacity=0.4,\n",
166+
")\n",
167+
"\n",
168+
"boundary_style = dict(\n",
169+
" weight=1, fillColor=\"#c545d3\", color=\"#c545d3\", fillOpacity=0.2, opacity=0.4\n",
170+
")\n",
171+
"\n",
172+
"\n",
173+
"aeroway = dict(\n",
174+
" weight=1, fillColor=\"#51aeb5\", color=\"#51aeb5\", fillOpacity=0.2, opacity=0.4\n",
175+
")\n",
176+
"\n",
177+
"road = dict(\n",
178+
" weight=1, fillColor=\"#f2b648\", color=\"#f2b648\", fillOpacity=0.2, opacity=0.4\n",
179+
")\n",
180+
"\n",
181+
"transit = dict(\n",
182+
" weight=0.5, fillColor=\"#f2b648\", color=\"#f2b648\", fillOpacity=0.2, opacity=0.4\n",
183+
")\n",
184+
"\n",
185+
"buildings = dict(\n",
186+
" fill=\"true\",\n",
187+
" weight=1,\n",
188+
" fillColor=\"#2b2b2b\",\n",
189+
" color=\"#2b2b2b\",\n",
190+
" fillOpacity=0.2,\n",
191+
" opacity=0.4,\n",
192+
")\n",
193+
"\n",
194+
"water_name = dict(\n",
195+
" weight=1, fillColor=\"#022c5b\", color=\"#022c5b\", fillOpacity=0.2, opacity=0.4\n",
196+
")\n",
197+
"\n",
198+
"transportation_name = dict(\n",
199+
" weight=1, fillColor=\"#bc6b38\", color=\"#bc6b38\", fillOpacity=0.2, opacity=0.4\n",
200+
")\n",
201+
"\n",
202+
"place = dict(\n",
203+
" weight=1, fillColor=\"#f20e93\", color=\"#f20e93\", fillOpacity=0.2, opacity=0.4\n",
204+
")\n",
205+
"\n",
206+
"housenumber = dict(\n",
207+
" weight=1, fillColor=\"#ef4c8b\", color=\"#ef4c8b\", fillOpacity=0.2, opacity=0.4\n",
208+
")\n",
209+
"\n",
210+
"poi = dict(weight=1, fillColor=\"#3bb50a\", color=\"#3bb50a\", fillOpacity=0.2, opacity=0.4)\n",
211+
"\n",
212+
"earth = dict(\n",
213+
" fill=\"true\",\n",
214+
" weight=1,\n",
215+
" fillColor=\"#c0c0c0\",\n",
216+
" color=\"#c0c0c0\",\n",
217+
" fillOpacity=0.2,\n",
218+
" opacity=0.4,\n",
219+
")\n",
220+
"\n",
221+
"vector_tile_layer_styles = dict(\n",
222+
" water=water_style,\n",
223+
" waterway=waterway_style,\n",
224+
" admin=admin_style,\n",
225+
" andcover=landcover_style,\n",
226+
" landuse=landuse_style,\n",
227+
" park=park_style,\n",
228+
" boundaries=boundary_style,\n",
229+
" aeroway=aeroway,\n",
230+
" roads=road,\n",
231+
" transit=transit,\n",
232+
" buildings=buildings,\n",
233+
" water_name=water_name,\n",
234+
" transportation_name=transportation_name,\n",
235+
" places=place,\n",
236+
" housenumber=housenumber,\n",
237+
" pois=poi,\n",
238+
" earth=earth,\n",
239+
")"
118240
]
119241
},
120242
{
@@ -132,7 +254,7 @@
132254
"metadata": {},
133255
"outputs": [],
134256
"source": [
135-
"m.add_vector_tile_layer(url, attribution, vector_tile_layer_styles)\n",
257+
"m.add_vector_tile_layer(url, vector_tile_layer_styles, attribution=attribution)\n",
136258
"m"
137259
]
138260
}
@@ -142,6 +264,18 @@
142264
"display_name": "Python 3",
143265
"language": "python",
144266
"name": "python3"
267+
},
268+
"language_info": {
269+
"codemirror_mode": {
270+
"name": "ipython",
271+
"version": 3
272+
},
273+
"file_extension": ".py",
274+
"mimetype": "text/x-python",
275+
"name": "python",
276+
"nbconvert_exporter": "python",
277+
"pygments_lexer": "ipython3",
278+
"version": "3.12.2"
145279
}
146280
},
147281
"nbformat": 4,

leafmap/foliumap.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3032,9 +3032,7 @@ def add_vector_tile(
30323032
https://github.com/python-visualization/folium/blob/main/folium/plugins/vectorgrid_protobuf.py#L7
30333033
30343034
Args:
3035-
url (str, optional): The URL of the tile layer, such as
3036-
'https://tile.nextzen.org/tilezen/vector/v1/512/all/{z}/{x}/{y}.mvt?api_key=gCZXZglvRQa6sB2z7JzL1w'.
3037-
attribution (str, optional): The attribution to use. Defaults to ''.
3035+
url (str, optional): The URL of the tile layer
30383036
styles (dict | str, optional): Style dict, specific to the vector tile source.
30393037
If styles is given as a string, it will be passed directly to folium.plugins.VectorGrid
30403038
directly, ignoring additional kwargs. See the "conditional styling" example in
@@ -3059,6 +3057,8 @@ def add_vector_tile(
30593057
vc = plugins.VectorGridProtobuf(url, layer_name, options)
30603058
self.add_child(vc)
30613059

3060+
add_vector_tile_layer = add_vector_tile
3061+
30623062
def to_gradio(
30633063
self, width: Optional[str] = "100%", height: Optional[str] = "500px", **kwargs
30643064
):

leafmap/leafmap.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -570,18 +570,15 @@ def add_tile_layer(
570570
def add_vector_tile(
571571
self,
572572
url,
573-
attribution="",
574-
styles={},
575-
layer_name="Vector Tile",
573+
styles: Optional[dict] = {},
574+
layer_name: Optional[str] = "Vector Tile",
576575
**kwargs,
577576
):
578577
"""Adds a VectorTileLayer to the map. It wraps the ipyleaflet.VectorTileLayer class. See
579578
https://ipyleaflet.readthedocs.io/en/latest/layers/vector_tile.html
580579
581580
Args:
582-
url (str, optional): The URL of the tile layer, such as
583-
'https://tile.nextzen.org/tilezen/vector/v1/512/all/{z}/{x}/{y}.mvt?api_key=gCZXZglvRQa6sB2z7JzL1w'.
584-
attribution (str, optional): The attribution to use. Defaults to ''.
581+
url (str, optional): The URL of the tile layer
585582
styles (dict,optional): Style dict, specific to the vector tile source.
586583
layer_name (str, optional): The layer name to use for the layer. Defaults to 'Vector Tile'.
587584
kwargs: Additional keyword arguments to pass to the ipyleaflet.VectorTileLayer class.
@@ -592,7 +589,6 @@ def add_vector_tile(
592589
try:
593590
vector_tile_layer = ipyleaflet.VectorTileLayer(
594591
url=url,
595-
attribution=attribution,
596592
vector_tile_layer_styles=styles,
597593
**kwargs,
598594
)

0 commit comments

Comments
 (0)