Vector Tile Layer#

Example#

from ipyleaflet import Map, VectorTileLayer

from traitlets import Unicode, Dict

# This is a custom VectorTileLayer subclass, allowing to pass our api key to the url
class CustomVectorTileLayer(VectorTileLayer):
    api_key = Unicode('gCZXZglvRQa6sB2z7JzL1w').tag(sync=True, o=True)

water_style = dict(
fill="true",
weight=1,
fillColor="#06cccc",
color="#06cccc",
fillOpacity=0.2,
opacity=0.4,
)

waterway_style = dict(
    weight=1, fillColor="#2375e0", color="#2375e0", fillOpacity=0.2, opacity=0.4
)

admin_style = dict(
    weight=1, fillColor="pink", color="pink", fillOpacity=0.2, opacity=0.4
)

landcover_style = dict(
    fill="true",
    weight=1,
    fillColor="#53e033",
    color="#53e033",
    fillOpacity=0.2,
    opacity=0.4,
)

landuse_style = dict(
    fill="true",
    weight=1,
    fillColor="#e5b404",
    color="#e5b404",
    fillOpacity=0.2,
    opacity=0.4,
)

park_style = dict(
    fill="true",
    weight=1,
    fillColor="#84ea5b",
    color="#84ea5b",
    fillOpacity=0.2,
    opacity=0.4,
)

boundary_style = dict(
    weight=1, fillColor="#c545d3", color="#c545d3", fillOpacity=0.2, opacity=0.4
)


aeroway = dict(
    weight=1, fillColor="#51aeb5", color="#51aeb5", fillOpacity=0.2, opacity=0.4
)

road = dict(
    weight=1, fillColor="#f2b648", color="#f2b648", fillOpacity=0.2, opacity=0.4
)

transit = dict(
    weight=0.5, fillColor="#f2b648", color="#f2b648", fillOpacity=0.2, opacity=0.4
)

buildings = dict(
    fill="true",
    weight=1,
    fillColor="#2b2b2b",
    color="#2b2b2b",
    fillOpacity=0.2,
    opacity=0.4,
)

water_name = dict(
    weight=1, fillColor="#022c5b", color="#022c5b", fillOpacity=0.2, opacity=0.4
)

transportation_name = dict(
    weight=1, fillColor="#bc6b38", color="#bc6b38", fillOpacity=0.2, opacity=0.4
)

place = dict(
    weight=1, fillColor="#f20e93", color="#f20e93", fillOpacity=0.2, opacity=0.4
)

housenumber = dict(
    weight=1, fillColor="#ef4c8b", color="#ef4c8b", fillOpacity=0.2, opacity=0.4
)

poi = dict(weight=1, fillColor="#3bb50a", color="#3bb50a", fillOpacity=0.2, opacity=0.4)

earth = dict(
    fill="true",
    weight=1,
    fillColor="#c0c0c0",
    color="#c0c0c0",
    fillOpacity=0.2,
    opacity=0.4,
)

url = 'https://tile.nextzen.org/tilezen/vector/v1/512/all/{z}/{x}/{y}.mvt?api_key={apiKey}'
vector_tile_layer_styles = dict(
    water=water_style,
    waterway=waterway_style,
    admin=admin_style,
    andcover=landcover_style,
    landuse=landuse_style,
    park=park_style,
    boundaries=boundary_style,
    aeroway=aeroway,
    roads=road,
    transit=transit,
    buildings=buildings,
    water_name=water_name,
    transportation_name=transportation_name,
    places=place,
    housenumber=housenumber,
    pois=poi,
    earth=earth
)

m = Map(center=(52.204793, 360.121558), zoom=9)
vl = CustomVectorTileLayer(url=url, vector_tile_layer_styles=vector_tile_layer_styles)
m.add(vl)
m

Attributes and methods#

class ipyleaflet.leaflet.VectorTileLayer(*args: t.Any, **kwargs: t.Any)[source]#

VectorTileLayer class, with Layer as parent class.

Vector tile layer.

url#

Url to the vector tile service.

Type:

string, default “”

attribution#

Vector tile service attribution.

Type:

string, default “”

layer_styles#

CSS Styles to apply to the vector data.

Type:

dict or string, default {}. If string, it will be parsed as a javascript object (useful for defining styles that depend on properties and/or zoom).

min_zoom#

The minimum zoom level down to which this layer will be displayed (inclusive).

Type:

int, default 0

max_zoom#

The maximum zoom level up to which this layer will be displayed (inclusive).

Type:

int, default 18

min_native_zoom#

Minimum zoom number the tile source has available. If it is specified, the tiles on all zoom levels lower than min_native_zoom will be loaded from min_native_zoom level and auto-scaled.

Type:

int, default None

max_native_zoom#

Maximum zoom number the tile source has available. If it is specified, the tiles on all zoom levels higher than max_native_zoom will be loaded from max_native_zoom level and auto-scaled.

Type:

int, default None

opacity#

Opacity of the layer between 0. (fully transparent) and 1. (fully opaque).

Type:

float, default 1.

visible#

Whether the layer is visible or not.

Type:

boolean, default True

renderer#

Engine for rendering VectorTileLayers; either ‘canvas’ or ‘svg’. Use ‘svg’ for interactive layers.

Type:

string, default ‘svg’

interactive#

Whether the layer is interactive or not.

Type:

boolean, default False

feature_id#

Optional attribute name of a unique feature identifier.

Type:

string, default None

redraw()[source]#

Force redrawing the tiles.

This is especially useful when you are sure the server updated the tiles and you need to refresh the layer.

reset_feature_style(id: Int)[source]#

Reset feature style

Reverts the style to the layer’s deafult.

id#

The unique identifier for the feature to re-symbolize

Type:

int

set_feature_style(id: Int, layer_style: Dict)[source]#

Re-symbolize one feature.

Given the unique ID for a vector features, re-symbolizes that feature across all tiles it appears in. Reverts the effects of a previous set_feature_style call. get_feature_id must be defined for set_feature_style to work.

id#

The unique identifier for the feature to re-symbolize

Type:

int

layer_styles#

Style to apply to the feature

Type:

dict