Skip to content

Commit

Permalink
Add support for vector tiles (opengeos#352)
Browse files Browse the repository at this point in the history
* Add support for vector tiles

* Fixed vector tile text error

Former-commit-id: 3d64c05
  • Loading branch information
giswqs authored Jan 31, 2023
1 parent 5b368f6 commit ebd0671
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 33 deletions.
47 changes: 35 additions & 12 deletions leafmap/foliumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2633,6 +2633,41 @@ def add_text(

self.add_html(text, position=position, **kwargs)

def add_vector_tile(
self,
url,
attribution="",
styles={},
layer_name="Vector Tile",
**kwargs,
):
"""Adds a VectorTileLayer to the map. It wraps the folium.plugins.VectorGridProtobuf class. See
https://github.com/python-visualization/folium/blob/main/folium/plugins/vectorgrid_protobuf.py#L7
Args:
url (str, optional): The URL of the tile layer, such as
'https://tile.nextzen.org/tilezen/vector/v1/512/all/{z}/{x}/{y}.mvt?api_key=gCZXZglvRQa6sB2z7JzL1w'.
attribution (str, optional): The attribution to use. Defaults to ''.
styles (dict,optional): Style dict, specific to the vector tile source.
layer_name (str, optional): The layer name to use for the layer. Defaults to 'Vector Tile'.
kwargs: Additional keyword arguments to pass to the ipyleaflet.VectorTileLayer class.
"""

options = {}

for key, value in kwargs.items():
options[key] = value

if "vector_tile_layer_styles" in options:
styles = options["vector_tile_layer_styles"]
del options["vector_tile_layer_styles"]

if styles:
options["vectorTileLayerStyles"] = styles

vc = plugins.VectorGridProtobuf(url, layer_name, options)
self.add_child(vc)

def remove_labels(self, **kwargs):
"""Removes a layer from the map."""
print("The folium plotting backend does not support removing labels.")
Expand Down Expand Up @@ -2678,18 +2713,6 @@ def add_time_slider(
"The folium plotting backend does not support this function. Use the ipyleaflet plotting backend instead."
)

def add_vector_tile_layer(
self,
url="https://tile.nextzen.org/tilezen/vector/v1/512/all/{z}/{x}/{y}.mvt?api_key=gCZXZglvRQa6sB2z7JzL1w",
attribution="",
vector_tile_layer_styles=dict(),
**kwargs,
):
"""Adds a VectorTileLayer to the map."""
raise NotImplementedError(
"The folium plotting backend does not support this function. Use the ipyleaflet plotting backend instead."
)

def add_xy_data(
self,
in_csv,
Expand Down
34 changes: 27 additions & 7 deletions leafmap/leafmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,15 @@ def add_layer(self, layer):
self.remove_layer(existing_layer)
super().add_layer(layer)

def add_layer_control(self, position="topright"):
"""Adds a layer control to the map.
Args:
position (str, optional): The position of the layer control. Defaults to 'topright'.
"""

self.add(ipyleaflet.LayersControl(position=position))

def layer_opacity(self, name, value=1.0):
"""Changes layer opacity.
Expand Down Expand Up @@ -446,33 +455,44 @@ def add_tile_layer(
print("Failed to add the specified TileLayer.")
raise Exception(e)

def add_vector_tile_layer(
def add_vector_tile(
self,
url="https://tile.nextzen.org/tilezen/vector/v1/512/all/{z}/{x}/{y}.mvt?api_key=gCZXZglvRQa6sB2z7JzL1w",
url,
attribution="",
vector_tile_layer_styles=dict(),
styles={},
layer_name="Vector Tile",
**kwargs,
):
"""Adds a VectorTileLayer to the map.
"""Adds a VectorTileLayer to the map. It wraps the ipyleaflet.VectorTileLayer class. See
https://ipyleaflet.readthedocs.io/en/latest/layers/vector_tile.html
Args:
url (str, optional): The URL of the tile layer. Defaults to 'https://tile.nextzen.org/tilezen/vector/v1/512/all/{z}/{x}/{y}.mvt?api_key=gCZXZglvRQa6sB2z7JzL1w'.
url (str, optional): The URL of the tile layer, such as
'https://tile.nextzen.org/tilezen/vector/v1/512/all/{z}/{x}/{y}.mvt?api_key=gCZXZglvRQa6sB2z7JzL1w'.
attribution (str, optional): The attribution to use. Defaults to ''.
vector_tile_layer_styles (dict,optional): Style dict, specific to the vector tile source.
styles (dict,optional): Style dict, specific to the vector tile source.
layer_name (str, optional): The layer name to use for the layer. Defaults to 'Vector Tile'.
kwargs: Additional keyword arguments to pass to the ipyleaflet.VectorTileLayer class.
"""
if "vector_tile_layer_styles" in kwargs:
styles = kwargs["vector_tile_layer_styles"]
del kwargs["vector_tile_layer_styles"]
try:
vector_tile_layer = ipyleaflet.VectorTileLayer(
url=url,
attribution=attribution,
vector_tile_layer_styles=vector_tile_layer_styles,
vector_tile_layer_styles=styles,
**kwargs,
)
vector_tile_layer.name = layer_name
self.add_layer(vector_tile_layer)

except Exception as e:
print("Failed to add the specified VectorTileLayer.")
raise Exception(e)

add_vector_tile_layer = add_vector_tile

def add_osm_from_geocode(
self,
query,
Expand Down
6 changes: 4 additions & 2 deletions leafmap/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,10 @@ def all_layers_chk_changed(change):
)
except KeyError:
opacity = 1.0
else:
elif hasattr(layer, "opacity"):
opacity = layer.opacity
else:
opacity = 1.0

layer_opacity = widgets.FloatSlider(
value=opacity,
Expand Down Expand Up @@ -560,7 +562,7 @@ def layer_opacity_changed(change):

if layer in m.geojson_layers:
layer_opacity.observe(layer_opacity_changed, "value")
else:
elif hasattr(layer, "opacity"):
widgets.jsdlink((layer_opacity, "value"), (layer, "opacity"))

# widgets.jsdlink((layer_opacity, "value"), (layer, "opacity"))
Expand Down
10 changes: 0 additions & 10 deletions tests/test_foliumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,6 @@ def test_add_vector(self):
out_str = m.to_html()
assert "Countries" in out_str

def test_add_vector_tile_layer(self):
"""Check adding vector tile layer"""
with self.assertRaises(NotImplementedError):
m = leafmap.Map()
url = "https://tile.nextzen.org/tilezen/vector/v1/512/all/{z}/{x}/{y}.mvt?api_key=gCZXZglvRQa6sB2z7JzL1w"
attribution = "Nextzen"
m.add_vector_tile_layer(url, attribution)
out_str = m.to_html()
assert "Nextzen" in out_str

def test_add_wms_layer(self):
"""Check adding WMS layer"""
m = leafmap.Map()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_leafmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,12 @@ def test_add_vector(self):
out_str = m.to_html()
assert "Countries" in out_str

def test_add_vector_tile_layer(self):
def test_add_vector_tile(self):
"""Check adding vector tile layer"""
m = leafmap.Map()
url = "https://tile.nextzen.org/tilezen/vector/v1/512/all/{z}/{x}/{y}.mvt?api_key=gCZXZglvRQa6sB2z7JzL1w"
attribution = "Nextzen"
m.add_vector_tile_layer(url, attribution)
m.add_vector_tile(url, attribution)
out_str = m.to_html()
assert "Nextzen" in out_str

Expand Down

0 comments on commit ebd0671

Please sign in to comment.