Skip to content

Commit

Permalink
Lazy load Maplibre GL JS and add hillshading layer
Browse files Browse the repository at this point in the history
  • Loading branch information
nrenner committed Jun 14, 2022
1 parent f92d2e2 commit 13efb48
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 15 deletions.
5 changes: 5 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ table.dataTable {
z-index: 999;
}

/* Ensure z-index layer order works for Maplibre overlays, otherwise gets below base map after switch */
.leaflet-gl-layer {
position: absolute;
}

.navbar {
/* align with leaflet-control */
padding: 0.5rem 10px;
Expand Down
17 changes: 10 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ var paths = {
scriptsConfig: mainNpmFiles()
.filter(
(f) =>
// index.html
RegExp('url-search-params/.*\\.js', 'i').test(f) ||
RegExp('core-js-bundle/.*\\.js', 'i').test(f) ||
RegExp('regenerator-runtime/.*\\.js', 'i').test(f)
RegExp('regenerator-runtime/.*\\.js', 'i').test(f) ||
// dynamic import in MaplibreGlLazyLoader
RegExp('maplibre-gl/.*\\.js', 'i').test(f) ||
RegExp('@maplibre/maplibre-gl-leaflet/.*\\.js', 'i').test(f)
)
.concat([
// large lib as extra file for faster parallel loading (*.min.js already excluded from bundle)
Expand All @@ -60,7 +64,9 @@ var paths = {
!RegExp('.*\\.min\\.js', 'i').test(f) &&
!RegExp('url-search-params/.*\\.js', 'i').test(f) &&
!RegExp('core-js-bundle/.*\\.js', 'i').test(f) &&
!RegExp('regenerator-runtime/.*\\.js', 'i').test(f)
!RegExp('regenerator-runtime/.*\\.js', 'i').test(f) &&
!RegExp('maplibre-gl/.*\\.js', 'i').test(f) &&
!RegExp('@maplibre/maplibre-gl-leaflet/.*\\.js', 'i').test(f)
)
)
.concat([
Expand All @@ -83,7 +89,7 @@ var paths = {
fonts: mainNpmFiles().filter((f) => RegExp('font-awesome/fonts/.*', 'i').test(f)),
changelog: 'CHANGELOG.md',
locales: 'locales/*.json',
layers: 'layers/**/*.geojson',
layers: ['layers/**/*.geojson', 'layers/**/*.json'],
layersDestName: 'layers.js',
layersConfig: [
'layers/config/config.js',
Expand Down Expand Up @@ -219,10 +225,7 @@ gulp.task('watch', function () {
// Print paths to console, for manually debugging the gulp build
// (comment out corresponding line of paths to print)
gulp.task('log', function () {
// var src = paths.scripts
// var src = paths.styles
// var src = paths.images
// var src = paths.locales
// var src = paths.scriptsConfig;
var src = paths.scripts.concat(paths.styles).concat(paths.images).concat(paths.locales);

return gulp.src(src).pipe(gulpDebug());
Expand Down
17 changes: 17 additions & 0 deletions js/LayersConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,21 @@ BR.LayersConfig = L.Class.extend({
return new leafletOsmNotes();
},

createMvtLayer: function (props) {
const options = {};

if (props.url in BR.layerIndex) {
// url is key to style in local layers bundle (file name without '.json'),
// suggested file naming convention: `<layer id>-style.json`
options.style = BR.layerIndex[props.url];
} else {
// external URL to style.json
options.style = props.url;
}

return BR.maplibreGlLazyLoader(options);
},

createLayer: function (layerData) {
var props = layerData.properties;
var url = props.url;
Expand Down Expand Up @@ -326,6 +341,8 @@ BR.LayersConfig = L.Class.extend({
layer = this.createOverpassLayer(props.query, props.icon);
} else if (props.dataSource === 'OpenStreetMapNotesAPI') {
layer = this.createOpenStreetMapNotesLayer();
} else if (props.type === 'mvt') {
layer = this.createMvtLayer(props);
} else {
// JOSM
var josmUrl = url;
Expand Down
11 changes: 6 additions & 5 deletions js/control/LayersTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ BR.LayersTab = BR.ControlLayers.extend({
title: i18next.t('layers.opacity-slider'),
callback: function (opacity) {
for (var i = 0; i < self._layers.length; i++) {
if (!self._layers[i].overlay || !map.hasLayer(self._layers[i].layer)) {
const layer = self._layers[i].layer;
if (!self._layers[i].overlay || !map.hasLayer(layer)) {
continue;
}
if (self._layers[i].layer.setOpacity) {
self._layers[i].layer.setOpacity(opacity);
} else {
self._layers[i].layer.setStyle({ opacity: opacity });
if (layer.setOpacity) {
layer.setOpacity(opacity);
} else if (layer.setStyle) {
layer.setStyle({ opacity: opacity });
}
}
},
Expand Down
64 changes: 64 additions & 0 deletions js/util/MaplibreGlLazyLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Only load Maplibre bundles when layer is actually added, using dynamic imports
*/
BR.MaplibreGlLazyLoader = L.Layer.extend({
initialize: function (options) {
this.options = options;
},

onAdd: function (map) {
if (!('maplibreGL' in L)) {
this._load();
} else {
this._addGlLayer();
}
return this;
},

onRemove: function (map) {
this._map.removeLayer(this.glLayer);
this.glLayer = null;
return this;
},

// needed when overlay, also requires `position: absolute` (see css)
setZIndex: function (zIndex) {
this.options.zIndex = zIndex;
return this;
},

setOpacity: function (opacity) {
if (this.glLayer) {
const glMap = this.glLayer.getMaplibreMap();
if (glMap.getLayer('hillshading')) {
glMap.setPaintProperty('hillshading', 'hillshade-exaggeration', opacity);
} else {
glMap.getCanvas().style.opacity = opacity;
}
}
},

_load: async function () {
await import('./maplibre-gl.js');
await import('./leaflet-maplibre-gl.js');

this._addGlLayer();
},

_addGlLayer: function () {
this.glLayer = L.maplibreGL(this.options);
this._map.addLayer(this.glLayer);

this._updateZIndex();
},

_updateZIndex: function () {
if (this.glLayer && this.glLayer.getContainer() && this.options.zIndex != null) {
this.glLayer.getContainer().style.zIndex = this.options.zIndex;
}
},
});

BR.maplibreGlLazyLoader = function (options) {
return new BR.MaplibreGlLazyLoader(options);
};
2 changes: 1 addition & 1 deletion layers/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ BR.confLayers.languageDefaultLayers = [
];

BR.confLayers.defaultOverlays = [
'HikeBike.HillShading',
'terrarium-hillshading',
'Waymarked_Trails-Cycling',
'Waymarked_Trails-Hiking'
];
Expand Down
2 changes: 1 addition & 1 deletion layers/config/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ BR.confLayers.tree = {
},
'overlays': {
'worldwide': [
'HikeBike.HillShading',
'terrarium-hillshading',
'Waymarked_Trails-Cycling',
'Waymarked_Trails-Hiking',
'Waymarked_Trails-MTB',
Expand Down
21 changes: 21 additions & 0 deletions layers/mvt/terrarium-hillshading-style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"version": 8,
"sources": {
"dem": {
"type": "raster-dem",
"tiles": [
"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png"
],
"encoding": "terrarium",
"tileSize": 256,
"maxzoom": 15
}
},
"layers": [
{
"id": "hillshading",
"source": "dem",
"type": "hillshade"
}
]
}
15 changes: 15 additions & 0 deletions layers/mvt/terrarium-hillshading.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"geometry": null,
"properties": {
"attribution": {
"text": "Terrain Tiles from AWS",
"url": "https://registry.opendata.aws/terrain-tiles/"
},
"id": "terrarium-hillshading",
"name": "Hillshading",
"overlay": true,
"type": "mvt",
"url": "terrarium-hillshading-style"
},
"type": "Feature"
}
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@mapbox/maki": "^7.0.0",
"@mapbox/polyline": "^1.0.0",
"@mapbox/togeojson": "^0.16.0",
"@maplibre/maplibre-gl-leaflet": "^0.0.17",
"@turf/turf": "^6.2.0",
"Leaflet.vector-markers": "nrenner/Leaflet.vector-markers#2ef80c9",
"async": "~2.6.0",
Expand Down Expand Up @@ -77,6 +78,7 @@
"leaflet.snogylop": "^0.4.0",
"leaflet.stravasegments": "2.3.2",
"mapbbcode": "MapBBCode/mapbbcode#v1.2.0",
"maplibre-gl": "^2.1.9",
"osmtogeojson": "^3.0.0-beta.4",
"overpass-layer": "^3.1.0",
"regenerator-runtime": "^0.13.7",
Expand Down Expand Up @@ -371,6 +373,13 @@
"svgs/solid/phone-flip.svg",
"svgs/solid/beer.svg"
]
},
"maplibre-gl": {
"main": [
"dist/maplibre-gl.js",
"dist/maplibre-gl.js.map",
"dist/maplibre-gl.css"
]
}
}
}
Loading

0 comments on commit 13efb48

Please sign in to comment.