Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change structure of pmtilesRules, allow icon sheet, require returned promise. #1015

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
node-version: latest
- run: sudo apt-get install xvfb
- run: npm install
- run: npx playwright install --with-deps
- run: npm install -g grunt-cli
- run: grunt default
- run: xvfb-run --auto-servernum -- npx playwright test --grep-invert="popupTabNavigation\.test\.js|layerContextMenuKeyboard\.test\.js" --workers=1 --retries=3
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
}
],
"scripts": {
"postinstall": "npx playwright install --with-deps",
"test-serve": "node test/server.js",
"postinstall": "npx playwright install",
"test": "npx playwright test --retries=3 --workers=1"
},
"devDependencies": {
Expand Down
40 changes: 23 additions & 17 deletions src/map-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ export class HTMLLinkElement extends HTMLElement {
if (this.type === 'application/pmtiles+stylesheet') {
const pmtilesStyles = new URL(this.href, this.getBase()).href;
import(pmtilesStyles)
.then((module) => {
this._pmtilesRules = module.pmtilesRules;
.then((module) => module.pmtilesRulesReady)
.then((initializedRules) => {
this._pmtilesRules = initializedRules;
})
.catch((reason) => {
console.error(
Expand Down Expand Up @@ -399,19 +400,24 @@ export class HTMLLinkElement extends HTMLElement {
s +
')'
)?.querySelector(s);
let options = {
zoomBounds: this.getZoomBounds(),
extentBounds: this.getBounds(),
crs: M[this.parentExtent.units],
zIndex: this.zIndex,
pane: this.parentExtent._extentLayer.getContainer(),
linkEl: this,
pmtilesRules: pmtilesStylesheetLink?._pmtilesRules
};
this._templatedLayer = templatedPMTilesLayer(
this._templateVars,
options
).addTo(this.parentExtent._extentLayer);
if (pmtilesStylesheetLink) {
await pmtilesStylesheetLink.whenReady();
let options = {
zoomBounds: this.getZoomBounds(),
extentBounds: this.getBounds(),
crs: M[this.parentExtent.units],
zIndex: this.zIndex,
pane: this.parentExtent._extentLayer.getContainer(),
linkEl: this,
pmtilesRules: pmtilesStylesheetLink?._pmtilesRules
};
this._templatedLayer = templatedPMTilesLayer(
this._templateVars,
options
).addTo(this.parentExtent._extentLayer);
} else {
console.warn('Stylesheet not found for ' + this._templateVars.template);
}
} else if (this.rel === 'tile') {
this._templatedLayer = templatedTileLayer(this._templateVars, {
zoomBounds: this.getZoomBounds(),
Expand Down Expand Up @@ -967,7 +973,7 @@ export class HTMLLinkElement extends HTMLElement {
break;
case 'stylesheet':
if (this.type === 'application/pmtiles+stylesheet') {
ready = _pmtilesRules;
ready = '_pmtilesRules';
} else {
resolve();
}
Expand All @@ -986,7 +992,7 @@ export class HTMLLinkElement extends HTMLElement {
resolve();
}
interval = setInterval(testForLinkReady, 300, this);
failureTimer = setTimeout(linkNotDefined, 10000);
failureTimer = setTimeout(linkNotDefined, 1000);
function testForLinkReady(linkElement) {
if (linkElement[ready]) {
clearInterval(interval);
Expand Down
4 changes: 4 additions & 0 deletions src/mapml/layers/TemplatedPMTilesLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ window.L = Leaflet;

import { Util } from '../utils/Util.js';
import * as protomapsL from 'protomaps-leaflet';
window.protomapsL = protomapsL;

export var TemplatedPMTilesLayer = Layer.extend({
initialize: function (template, options) {
Expand Down Expand Up @@ -42,6 +43,9 @@ export var TemplatedPMTilesLayer = Layer.extend({
extend(this._pmtilesOptions, {
labelRules: paintRules.rules.LABEL_RULES
});
if (paintRules.sheet) {
extend(this._pmtilesOptions, { tasks: [paintRules.sheet.load()] });
}
} else if (paintRules?.theme?.theme) {
extend(this._pmtilesOptions, { theme: paintRules.theme.theme });
} else {
Expand Down
177 changes: 95 additions & 82 deletions src/pmtilesRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@
* as described here: https://docs.protomaps.com/pmtiles/leaflet, and as
* exemplified below.
*/
const sheet = new protomapsL.Sheet(`
<html>
<body>
<svg id="icon_0" width="99px" height="97px" xmlns="http://www.w3.org/2000/svg">
<image href="data:image/svg+xml;base64,PHN2ZyBpZD0iaWNvbl82NCIgd2lkdGg9IjE2cHgiIGhlaWdodD0iMTZweCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgICAgPGltYWdlIGhyZWY9ImRhdGE6aW1hZ2UvcG5nO2Jhc2U2NCxpVkJPUncwS0dnb0FBQUFOU1VoRVVnQUFBQkFBQUFBUUNBWUFBQUFmOC85aEFBQUFDWEJJV1hNQUFBN0VBQUFPeEFHVkt3NGJBQUFBMlVsRVFWUjRuTldQTVdvQ1lSQ0Z2MElDZ2xZaFZnRUwzZmNyZGlsczBteVJUdkFLa2xaSXdKM2ZpTjE2aGhoeUFCRkVjZ2J2WUtFbmtCUkpiNWtFSlRheXNNdFd5WVBYRFBPOW1RZC9YcjVHSlRkc1Rhb21wdm12TzBZbVB1T1FRcjRQeE5yRXR3L29wQzdITFM1TVRFd3NEdmFPdHdOOERCRGIwenh5eklkMWJoSkQraTFLWHN4T1lJSjN2a0dZM2ozZ1BnRmVqUnRjWnVvK2ROeWVCM2l4Skt0TXZQeUNtMGc4bS9neXNYOXlsRlBoT0tUZ3hZYzVYZ2ZYRkkrVnhKMkpkeE85MUlDQmFFZU83dm44b2M2VkQzak1YT1AvNkFmS0Ywd3dXeU9Oc3dBQUFBQkpSVTVFcmtKZ2dnPT0iIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIvPgogICAgPC9zdmc+" width="99" height="97" />
</svg>
</body>
</html>
`);

class SpearfishSymbolizer {
constructor(options) {
Expand Down Expand Up @@ -88,87 +97,91 @@ class SpearfishSymbolizer {

*/
const pmtilesRules = new Map();
pmtilesRules.set(
'https://data.source.coop/protomaps/openstreetmap/tiles/v3.pmtiles',
{ theme: { theme: 'light' } }
);
pmtilesRules.set(
'https://data.source.coop/protomaps/openstreetmap/tiles/v3.pmtiles?theme=light',
{ theme: { theme: 'light' } }
);
pmtilesRules.set(
'https://data.source.coop/protomaps/openstreetmap/tiles/v3.pmtiles?theme=dark',
{ theme: { theme: 'dark' } }
);
pmtilesRules.set(
'https://api.protomaps.com/tiles/v3/{bing}/{bong}/{bang}.mvt?key=41392fb7515533a5',
{ theme: { theme: 'light' } }
);
pmtilesRules.set(
'http://localhost:8080/geoserver/gwc/service/wmts/rest/spearfish/OSMTILE/{foo}/{baz}/{bar}?format=application/vnd.mapbox-vector-tile',
{
rules: {
PAINT_RULES: [
{
dataLayer: 'streams',
symbolizer: new protomapsL.LineSymbolizer({
color: 'steelblue',
width: 2
})
},
{
dataLayer: 'roads',
symbolizer: new protomapsL.LineSymbolizer({
color: 'maroon',
width: 2
})
},
{
dataLayer: 'restricted',
symbolizer: new protomapsL.PolygonSymbolizer({
fill: 'red',
opacity: 0.5
})
},
{
dataLayer: 'restricted',
symbolizer: new protomapsL.LineSymbolizer({
color: 'red',
width: 2
})
},
{
dataLayer: 'archsites',
symbolizer: new SpearfishSymbolizer({
color: 'red',
shape: 'square'
})
},
{
dataLayer: 'bugsites',
symbolizer: new SpearfishSymbolizer({
color: 'black',
shape: 'circle'
})
}
],
LABEL_RULES: [
{
dataLayer: 'archsites',
symbolizer: new protomapsL.CenteredTextSymbolizer({
labelProps: ['str1'],
fill: 'white',
width: 2,
stroke: 'black',
font: '600 16px sans-serif'
}),
// note that filter is a property of a rule, not an option to a symbolizer
filter: (z, f) => {
return f.props.str1.trim().toLowerCase() !== 'no name';
const pmtilesRulesReady = sheet.load().then(() => {
pmtilesRules.set(
'https://data.source.coop/protomaps/openstreetmap/tiles/v3.pmtiles',
{ theme: { theme: 'light' } }
);
pmtilesRules.set(
'https://data.source.coop/protomaps/openstreetmap/tiles/v3.pmtiles?theme=light',
{ theme: { theme: 'light' } }
);
pmtilesRules.set(
'https://data.source.coop/protomaps/openstreetmap/tiles/v3.pmtiles?theme=dark',
{ theme: { theme: 'dark' } }
);
pmtilesRules.set(
'https://api.protomaps.com/tiles/v3/{bing}/{bong}/{bang}.mvt?key=41392fb7515533a5',
{ theme: { theme: 'light' } }
);
pmtilesRules.set(
'http://localhost:8080/geoserver/gwc/service/wmts/rest/spearfish/OSMTILE/{foo}/{baz}/{bar}?format=application/vnd.mapbox-vector-tile',
{
rules: {
PAINT_RULES: [
{
dataLayer: 'streams',
symbolizer: new protomapsL.LineSymbolizer({
color: 'steelblue',
width: 2
})
},
{
dataLayer: 'roads',
symbolizer: new protomapsL.LineSymbolizer({
color: 'maroon',
width: 2
})
},
{
dataLayer: 'restricted',
symbolizer: new protomapsL.PolygonSymbolizer({
fill: 'red',
opacity: 0.5
})
},
{
dataLayer: 'restricted',
symbolizer: new protomapsL.LineSymbolizer({
color: 'red',
width: 2
})
},
{
dataLayer: 'archsites',
symbolizer: new SpearfishSymbolizer({
color: 'red',
shape: 'square'
})
},
{
dataLayer: 'bugsites',
symbolizer: new SpearfishSymbolizer({
color: 'black',
shape: 'circle'
})
}
}
]
],
LABEL_RULES: [
{
dataLayer: 'archsites',
symbolizer: new protomapsL.CenteredTextSymbolizer({
labelProps: ['str1'],
fill: 'white',
width: 2,
stroke: 'black',
font: '600 16px sans-serif'
}),
// note that filter is a property of a rule, not an option to a symbolizer
filter: (z, f) => {
return f.props.str1.trim().toLowerCase() !== 'no name';
}
}
]
}
}
}
);
export { pmtilesRules };
);
return pmtilesRules;
});

export { pmtilesRules, pmtilesRulesReady };
Binary file added test/e2e/data/tiles/osmtile/11/740/433.mvt
Binary file not shown.
Binary file added test/e2e/data/tiles/osmtile/11/740/434.mvt
Binary file not shown.
Binary file added test/e2e/data/tiles/osmtile/11/741/433.mvt
Binary file not shown.
Binary file added test/e2e/data/tiles/osmtile/11/741/434.mvt
Binary file not shown.
19 changes: 16 additions & 3 deletions test/e2e/elements/map-link/pmtiles/darkTheme.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
const sheet = new protomapsL.Sheet(`
<html>
<body>
<svg id="icon_0" width="99px" height="97px" xmlns="http://www.w3.org/2000/svg">
<image href="data:image/svg+xml;base64,PHN2ZyBpZD0iaWNvbl82NCIgd2lkdGg9IjE2cHgiIGhlaWdodD0iMTZweCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgICAgPGltYWdlIGhyZWY9ImRhdGE6aW1hZ2UvcG5nO2Jhc2U2NCxpVkJPUncwS0dnb0FBQUFOU1VoRVVnQUFBQkFBQUFBUUNBWUFBQUFmOC85aEFBQUFDWEJJV1hNQUFBN0VBQUFPeEFHVkt3NGJBQUFBMlVsRVFWUjRuTldQTVdvQ1lSQ0Z2MElDZ2xZaFZnRUwzZmNyZGlsczBteVJUdkFLa2xaSXdKM2ZpTjE2aGhoeUFCRkVjZ2J2WUtFbmtCUkpiNWtFSlRheXNNdFd5WVBYRFBPOW1RZC9YcjVHSlRkc1Rhb21wdm12TzBZbVB1T1FRcjRQeE5yRXR3L29wQzdITFM1TVRFd3NEdmFPdHdOOERCRGIwenh5eklkMWJoSkQraTFLWHN4T1lJSjN2a0dZM2ozZ1BnRmVqUnRjWnVvK2ROeWVCM2l4Skt0TXZQeUNtMGc4bS9neXNYOXlsRlBoT0tUZ3hZYzVYZ2ZYRkkrVnhKMkpkeE85MUlDQmFFZU83dm44b2M2VkQzak1YT1AvNkFmS0Ywd3dXeU9Oc3dBQUFBQkpSVTVFcmtKZ2dnPT0iIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIvPgogICAgPC9zdmc+" width="99" height="97" />
</svg>
</body>
</html>
`);
const pmtilesRules = new Map();
pmtilesRules.set('http://localhost:30001/spearfish.pmtiles?theme=dark', {
theme: { theme: 'dark' }
const pmtilesRulesReady = sheet.load().then(() => {
pmtilesRules.set('http://localhost:30001/spearfish.pmtiles?theme=dark', {
theme: { theme: 'dark' }
});
return pmtilesRules;
});
export { pmtilesRules };

export { pmtilesRules, pmtilesRulesReady };
19 changes: 16 additions & 3 deletions test/e2e/elements/map-link/pmtiles/lightTheme.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
const sheet = new protomapsL.Sheet(`
<html>
<body>
<svg id="icon_0" width="99px" height="97px" xmlns="http://www.w3.org/2000/svg">
<image href="data:image/svg+xml;base64,PHN2ZyBpZD0iaWNvbl82NCIgd2lkdGg9IjE2cHgiIGhlaWdodD0iMTZweCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgICAgPGltYWdlIGhyZWY9ImRhdGE6aW1hZ2UvcG5nO2Jhc2U2NCxpVkJPUncwS0dnb0FBQUFOU1VoRVVnQUFBQkFBQUFBUUNBWUFBQUFmOC85aEFBQUFDWEJJV1hNQUFBN0VBQUFPeEFHVkt3NGJBQUFBMlVsRVFWUjRuTldQTVdvQ1lSQ0Z2MElDZ2xZaFZnRUwzZmNyZGlsczBteVJUdkFLa2xaSXdKM2ZpTjE2aGhoeUFCRkVjZ2J2WUtFbmtCUkpiNWtFSlRheXNNdFd5WVBYRFBPOW1RZC9YcjVHSlRkc1Rhb21wdm12TzBZbVB1T1FRcjRQeE5yRXR3L29wQzdITFM1TVRFd3NEdmFPdHdOOERCRGIwenh5eklkMWJoSkQraTFLWHN4T1lJSjN2a0dZM2ozZ1BnRmVqUnRjWnVvK2ROeWVCM2l4Skt0TXZQeUNtMGc4bS9neXNYOXlsRlBoT0tUZ3hZYzVYZ2ZYRkkrVnhKMkpkeE85MUlDQmFFZU83dm44b2M2VkQzak1YT1AvNkFmS0Ywd3dXeU9Oc3dBQUFBQkpSVTVFcmtKZ2dnPT0iIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIvPgogICAgPC9zdmc+" width="99" height="97" />
</svg>
</body>
</html>
`);
const pmtilesRules = new Map();
pmtilesRules.set('http://localhost:30001/spearfish.pmtiles?theme=light', {
theme: { theme: 'light' }
const pmtilesRulesReady = sheet.load().then(() => {
pmtilesRules.set('http://localhost:30001/spearfish.pmtiles?theme=light', {
theme: { theme: 'light' }
});
return pmtilesRules;
});
export { pmtilesRules };

export { pmtilesRules, pmtilesRulesReady };
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ test.describe('pmtiles map-link with associated style can be in a remote mapml d
layer.evaluate((l) => (l.checked = true));
await page.waitForTimeout(500);
await expect(viewer).toHaveScreenshot('pmtiles-dark.png', {
maxDiffPixels: 100
maxDiffPixels: 200
});
});

test('map-link in remote content selects correct stylesheet link from context', async () => {
const viewer = page.getByTestId('viewer');
await expect(viewer).toBeTruthy();
const layer = viewer.getByTestId('dark');
layer.evaluate((l) => (l.checked = true));
await page.waitForTimeout(500);
const darkExtent = viewer.getByTestId('dark-me');
const lightExtent = viewer.getByTestId('light-me');

Expand All @@ -38,7 +41,7 @@ test.describe('pmtiles map-link with associated style can be in a remote mapml d
});
await page.waitForTimeout(500);
await expect(viewer).toHaveScreenshot('pmtiles-light.png', {
maxDiffPixels: 100
maxDiffPixels: 200
});
});
test('map-link in local content selects correct stylesheet link from context', async () => {
Expand All @@ -59,7 +62,7 @@ test.describe('pmtiles map-link with associated style can be in a remote mapml d
});
await page.waitForTimeout(500);
await expect(viewer).toHaveScreenshot('pmtiles-local-light.png', {
maxDiffPixels: 100
maxDiffPixels: 200
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading