Skip to content

Commit

Permalink
Add support for PMTiles
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Sep 20, 2023
1 parent da6701f commit 92586cd
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
24 changes: 24 additions & 0 deletions ipyleaflet/leaflet.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,30 @@ def redraw(self):
self.send({'msg': 'redraw'})


class PMTilesLayer(Layer):
"""PMTilesLayer class, with Layer as parent class.
PMTiles layer.
Attributes
----------
url: string, default ""
Url to the vector tile service.
attribution: string, default ""
Vector tile service attribution.
style: dict, default {}
CSS Styles to apply to the vector data.
"""

_view_name = Unicode('LeafletPMTilesLayerView').tag(sync=True)
_model_name = Unicode('LeafletPMTilesLayerModel').tag(sync=True)

url = Unicode().tag(sync=True, o=True)
attribution = Unicode().tag(sync=True, o=True)
style = Dict().tag(sync=True, o=True)


class VectorLayer(Layer):
"""VectorLayer abstract class."""

Expand Down
5 changes: 4 additions & 1 deletion js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
"proj4": "^2.6.0",
"proj4leaflet": "^1.0.1",
"spin.js": "^4.1.0",
"stream-browserify": "^3.0.0"
"stream-browserify": "^3.0.0",
"pmtiles": "^2.5.0",
"maplibre-lib": "^2.2.1",
"maplibre-leaflet": "^0.0.17"
},
"devDependencies": {
"@jupyterlab/builder": "^3.6.0",
Expand Down
1 change: 1 addition & 0 deletions js/src/jupyter-leaflet.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './layers/AwesomeIcon.js';
export * from './layers/Popup.js';
export * from './layers/RasterLayer.js';
export * from './layers/TileLayer.js';
export * from './layers/PMTilesLayer.js';
export * from './layers/VectorTileLayer.js';
export * from './layers/LocalTileLayer.js';
export * from './layers/WMSLayer.js';
Expand Down
55 changes: 55 additions & 0 deletions js/src/layers/PMTilesLayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

const layer = require('./Layer.js');

export class LeafletPMTilesLayerModel extends layer.LeafletLayerModel {
defaults() {
return {
...super.defaults(),
_view_name: 'LeafletPMTilesLayerView',
_model_name: 'LeafletPMTilesLayerModel',
url: '',
style: {},
};
}
}

export class LeafletPMTilesLayerView extends layer.LeafletLayerView {
render() {
this.create_obj();
this.listenTo(this.model, 'change:url', this.url_changed.bind(this));
this.listenTo(this.model, 'change:style', this.style_changed.bind(this));
}

create_obj() {
var protocol = new pmtiles.Protocol();
maplibregl.addProtocol("pmtiles", protocol.tile);

var mapStyle = {
...this.model.get('style'),
sources: {
...this.model.get('style').sources,
"pmtiles_source": {
"type": "vector",
"url": "pmtiles://" + this.model.get('url')
}
}
};

this.obj = L.maplibreGL({
style: mapStyle
});
}

url_changed() {
var newUrl = "pmtiles://" + this.model.get('url');
var currentStyle = this.obj.getStyle();
currentStyle.sources["pmtiles_source"].url = newUrl;
this.obj.setStyle(currentStyle);
}

style_changed() {
this.obj.setStyle(this.model.get('style'));
}
}
3 changes: 3 additions & 0 deletions js/src/leaflet.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ require('leaflet-fullscreen');
require('leaflet-transform');
require('leaflet.awesome-markers');
require('leaflet-search');
require('pmtiles');
require('maplibre-lib');
require('maplibre-leaflet');

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

0 comments on commit 92586cd

Please sign in to comment.