Skip to content

Commit 6bd0cac

Browse files
authored
Add support for PMTiles (#1138)
* Add support for PMTiles * Fix yarnpkg error * Fix package.json * Remove maplibre-gl-leaflet * Update package * Fix linter issue * Fix linter issue * Change layer view * Incorporate jtmiclat code * Add PMTiles notebook
1 parent e4f3f63 commit 6bd0cac

File tree

6 files changed

+175
-1
lines changed

6 files changed

+175
-1
lines changed

examples/PMTiles.ipynb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# Set up for JupyterLite\n",
10+
"try:\n",
11+
" import piplite\n",
12+
" await piplite.install('ipyleaflet')\n",
13+
"except ImportError:\n",
14+
" pass"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": null,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"from ipyleaflet import Map, basemaps, PMTilesLayer"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"m = Map(center=[52.963529, 4.776306], zoom=7, basemap=basemaps.CartoDB.DarkMatter, scroll_wheel_zoom=True)\n",
33+
"m.layout.height = '600px'\n",
34+
"\n",
35+
"vl = PMTilesLayer(url=\"https://storage.googleapis.com/ahp-research/overture/pmtiles/overture.pmtiles\", \n",
36+
" style = {\n",
37+
" \"layers\": [\n",
38+
" {\n",
39+
" \"id\": \"admins\",\n",
40+
" \"source\": \"example_source\",\n",
41+
" \"source-layer\": \"admins\",\n",
42+
" \"type\": \"fill\",\n",
43+
" \"paint\": {\"fill-color\": \"#BDD3C7\", \"fill-opacity\": 0.1},\n",
44+
" },\n",
45+
" {\n",
46+
" \"id\": \"buildings\",\n",
47+
" \"source\": \"example_source\",\n",
48+
" \"source-layer\": \"buildings\",\n",
49+
" \"type\": \"fill\",\n",
50+
" \"paint\": {\"fill-color\": \"#FFFFB3\", \"fill-opacity\": 0.5},\n",
51+
" },\n",
52+
" {\n",
53+
" \"id\": \"places\",\n",
54+
" \"source\": \"example_source\",\n",
55+
" \"source-layer\": \"places\",\n",
56+
" \"type\": \"fill\",\n",
57+
" \"paint\": {\"fill-color\": \"#BEBADA\", \"fill-opacity\": 0.5},\n",
58+
" },\n",
59+
" {\n",
60+
" \"id\": \"roads\",\n",
61+
" \"source\": \"example_source\",\n",
62+
" \"source-layer\": \"roads\",\n",
63+
" \"type\": \"line\",\n",
64+
" \"paint\": {\"line-color\": \"#FB8072\"},\n",
65+
" },\n",
66+
" ],\n",
67+
" })\n",
68+
"m.add(vl)\n",
69+
"m"
70+
]
71+
}
72+
],
73+
"metadata": {
74+
"kernelspec": {
75+
"display_name": "Python 3 (ipykernel)",
76+
"language": "python",
77+
"name": "python3"
78+
},
79+
"language_info": {
80+
"codemirror_mode": {
81+
"name": "ipython",
82+
"version": 3
83+
},
84+
"file_extension": ".py",
85+
"mimetype": "text/x-python",
86+
"name": "python",
87+
"nbconvert_exporter": "python",
88+
"pygments_lexer": "ipython3",
89+
"version": "3.10.5"
90+
}
91+
},
92+
"nbformat": 4,
93+
"nbformat_minor": 4
94+
}

ipyleaflet/leaflet.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,35 @@ def redraw(self):
982982
self.send({'msg': 'redraw'})
983983

984984

985+
class PMTilesLayer(Layer):
986+
"""PMTilesLayer class, with Layer as parent class.
987+
988+
PMTiles layer.
989+
990+
991+
Attributes
992+
----------
993+
url: string, default ""
994+
Url to the PMTiles archive.
995+
attribution: string, default ""
996+
PMTiles archive attribution.
997+
style: dict, default {}
998+
CSS Styles to apply to the vector data.
999+
"""
1000+
1001+
_view_name = Unicode('LeafletPMTilesLayerView').tag(sync=True)
1002+
_model_name = Unicode('LeafletPMTilesLayerModel').tag(sync=True)
1003+
1004+
url = Unicode().tag(sync=True, o=True)
1005+
attribution = Unicode().tag(sync=True, o=True)
1006+
style = Dict().tag(sync=True, o=True)
1007+
1008+
def add_inspector(self):
1009+
"""Add an inspector to the layer.
1010+
"""
1011+
self.send({'msg': 'add_inspector'})
1012+
1013+
9851014
class VectorLayer(Layer):
9861015
"""VectorLayer abstract class."""
9871016

js/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"proj4": "^2.6.0",
4848
"proj4leaflet": "^1.0.1",
4949
"spin.js": "^4.1.0",
50-
"stream-browserify": "^3.0.0"
50+
"stream-browserify": "^3.0.0",
51+
"protomaps-leaflet": "^1.24.0"
5152
},
5253
"devDependencies": {
5354
"@jupyterlab/builder": "^4.0.8",

js/src/jupyter-leaflet.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export * from './layers/AwesomeIcon.js';
99
export * from './layers/Popup.js';
1010
export * from './layers/RasterLayer.js';
1111
export * from './layers/TileLayer.js';
12+
export * from './layers/PMTilesLayer.js';
1213
export * from './layers/VectorTileLayer.js';
1314
export * from './layers/LocalTileLayer.js';
1415
export * from './layers/WMSLayer.js';

js/src/layers/PMTilesLayer.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Jupyter Development Team.
2+
// Distributed under the terms of the Modified BSD License.
3+
4+
const layer = require('./Layer.js');
5+
const protomapsL = require('protomaps-leaflet');
6+
7+
export class LeafletPMTilesLayerModel extends layer.LeafletLayerModel {
8+
defaults() {
9+
return {
10+
...super.defaults(),
11+
_view_name: 'LeafletPMTilesLayerView',
12+
_model_name: 'LeafletPMTilesLayerModel',
13+
url: '',
14+
attribution: '',
15+
style: {},
16+
};
17+
}
18+
}
19+
20+
export class LeafletPMTilesLayerView extends layer.LeafletLayerView {
21+
create_obj() {
22+
var options = {
23+
...this.get_options(),
24+
url: this.model.get('url'),
25+
...protomapsL.json_style(this.model.get('style')),
26+
};
27+
this.obj = protomapsL.leafletLayer(options);
28+
}
29+
30+
model_events() {
31+
super.model_events();
32+
this.listenTo(
33+
this.model,
34+
'change:url',
35+
function () {
36+
this.obj.setUrl(this.model.get('url'));
37+
},
38+
this
39+
);
40+
}
41+
42+
handle_message(content) {
43+
if (content.msg == 'add_inspector') {
44+
this.obj.addInspector(this.map_view.obj);
45+
}
46+
}
47+
}
48+

js/src/leaflet.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require('leaflet-fullscreen');
1616
require('leaflet-transform');
1717
require('leaflet.awesome-markers');
1818
require('leaflet-search');
19+
require('protomaps-leaflet');
1920

2021
// Monkey patch GridLayer for smoother URL updates
2122
L.patchGridLayer = function (layer) {

0 commit comments

Comments
 (0)