Skip to content

Commit 45e8426

Browse files
committed
Merge branch 'dev' into feature/color-utils
2 parents 6587bda + 2448bf3 commit 45e8426

File tree

7 files changed

+166
-3
lines changed

7 files changed

+166
-3
lines changed

docs/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## maplibre v0.2.7 (unreleased)
44

5+
* Add `sources.VectorTileSource` ([Martenz](https://github.com/Martenz))
6+
57
* Shiny
68
* Add `input.{output_id}_view_state` dict containing `{"center", "zoom", "bounds", "pitch", "bearing"}`
79
* Rename `input.{output_id}` to `input.{output_id}_clicked`

docs/examples/vector_tiles/app.html

Lines changed: 33 additions & 0 deletions
Large diffs are not rendered by default.

docs/examples/vector_tiles/app.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Example taken from here:
2+
# https://maplibre.org/maplibre-gl-js/docs/API/classes/VectorTileSource/
3+
# https://maplibre.org/maplibre-style-spec/sources/
4+
5+
import webbrowser
6+
7+
from maplibre import Layer, LayerType, Map, MapOptions, render_maplibregl
8+
from maplibre.basemaps import Carto
9+
from maplibre.controls import NavigationControl
10+
from maplibre.sources import VectorTileSource
11+
from shiny.express import input, render, ui
12+
13+
# Get layer ids and pbf url from here
14+
VECTOR_TILES_URL = "https://demotiles.maplibre.org/tiles/tiles.json"
15+
LAYER_ID = "countries"
16+
17+
vector_source = VectorTileSource(
18+
url=VECTOR_TILES_URL,
19+
# tiles=["https://demotiles.maplibre.org/tiles/{z}/{x}/{y}.pbf"],
20+
min_zoom=0,
21+
max_zoom=6,
22+
)
23+
24+
vector_layer = Layer(
25+
type=LayerType.FILL,
26+
id=LAYER_ID,
27+
source=vector_source,
28+
paint={"fill-color": "lightgreen", "fill-outline-color": "black"},
29+
source_layer="countries",
30+
)
31+
32+
33+
def create_map():
34+
m = Map(MapOptions(style=Carto.POSITRON, center=(11, 42), zoom=3, hash=True))
35+
m.add_control(NavigationControl())
36+
m.add_layer(vector_layer)
37+
m.add_tooltip(LAYER_ID)
38+
return m
39+
40+
41+
@render_maplibregl
42+
def render_map():
43+
return create_map()
44+
45+
46+
if __name__ == "__main__":
47+
file_name = "docs/examples/vector_tiles/app.html"
48+
49+
m = create_map()
50+
with open(file_name, "w") as f:
51+
f.write(m.to_html())
52+
53+
webbrowser.open(file_name)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!-- <a href="app.html" target="_blank">See example in action</a> -->
2+
3+
<iframe src="app.html" height="620px", width="100%" style="border:none;"></iframe>
4+
5+
```python
6+
-8<-- "vector_tiles/app.py"
7+
```
8+
9+
Run example:
10+
11+
``` bash
12+
shiny run docs/examples/vector_tiles/app.py
13+
```

maplibre/sources.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from enum import Enum
4-
from typing import Union
4+
from typing import Optional, Union
55

66
from pydantic import ConfigDict, Field, computed_field
77

@@ -35,7 +35,8 @@ class GeoJSONSource(Source):
3535
Examples:
3636
>>> from maplibre.sources import GeoJSONSource
3737
38-
>>> source = GeoJSONSource(data="https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson")
38+
>>> geojson = "https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson"
39+
>>> source = GeoJSONSource(data=geojson)
3940
"""
4041

4142
data: Union[str, dict]
@@ -64,7 +65,8 @@ class RasterTileSource(Source):
6465
6566
Examples:
6667
>>> from maplibre.sources import RasterTileSource
67-
>>> raster_source = RasterTileSource(
68+
69+
>>> raster_tile_source = RasterTileSource(
6870
... tiles=["https://tile.openstreetmap.org/{z}/{x}/{y}.png"],
6971
... tile_size=256,
7072
... min_zoom=0,
@@ -86,3 +88,40 @@ class RasterTileSource(Source):
8688
@property
8789
def type(self) -> str:
8890
return SourceType.RASTER.value
91+
92+
93+
class VectorTileSource(Source):
94+
"""Vector tile source
95+
96+
Examples:
97+
>>> from maplibre.sources import VectorTileSource
98+
>>> from maplibre import LayerType, Layer
99+
100+
>>> vector_tile_source = VectorTileSource(
101+
... tiles=["https://demotiles.maplibre.org/tiles/{z}/{x}/{y}.pbf"],
102+
... min_zoom=0,
103+
... max_zoom=6,
104+
... )
105+
106+
>>> layer = Layer(
107+
... type=LayerType.LINE,
108+
... id="countries",
109+
... source=vector_tile_source,
110+
... source_layer="countries",
111+
... paint={"fill-color": "lightgreen", "fill-outline-color": "black"},
112+
... )
113+
"""
114+
115+
attribution: str = None
116+
bounds: tuple = None
117+
max_zoom: int = Field(None, serialization_alias="maxzoom")
118+
min_zoom: int = Field(None, serialization_alias="minzoom")
119+
scheme: str = None
120+
tiles: Union[tuple, list] = None
121+
url: str = None
122+
volatile: bool = None
123+
124+
@computed_field
125+
@property
126+
def type(self) -> str:
127+
return SourceType.VECTOR.value

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ nav:
4141
- PMTiles: examples/pmtiles/index.md
4242
- Mapbox Draw Plugin: examples/mapbox_draw_plugin/index.md
4343
- Layer Switcher: examples/layer_switcher/index.md
44+
- Vector Tiles: examples/vector_tiles/index.md
4445
plugins:
4546
- search:
4647
- mkdocstrings:

tests/test_sources.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,25 @@ def test_geojson_source():
2121
"clusterRadius": 2,
2222
"clusterMinPoints": 10,
2323
}
24+
25+
26+
def test_vector_tile_source():
27+
# Prepare
28+
tiles = ["https://demotiles.maplibre.org/tiles/{z}/{x}/{y}.pbf"]
29+
min_zoom = 0
30+
max_zoom = 6
31+
32+
# Act
33+
vector_tile_source = VectorTileSource(
34+
tiles=tiles, min_zoom=min_zoom, max_zoom=max_zoom
35+
)
36+
print(vector_tile_source)
37+
print(vector_tile_source.to_dict())
38+
39+
# Assert
40+
assert vector_tile_source.to_dict() == {
41+
"maxzoom": max_zoom,
42+
"minzoom": min_zoom,
43+
"tiles": tiles,
44+
"type": "vector",
45+
}

0 commit comments

Comments
 (0)