Skip to content

Commit 99181aa

Browse files
committed
fake clipping with a mask
1 parent 22ab295 commit 99181aa

3 files changed

Lines changed: 127 additions & 0 deletions

File tree

buildStyles.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import protomapsLayers from "protomaps-themes-base";
55
import { language_script_pairs } from "protomaps-themes-base";
66
import { viewport } from "@mapbox/geo-viewport";
77
import { bbox } from "@turf/bbox";
8+
import { mask } from "@turf/mask";
9+
import { simplify } from "@turf/simplify";
810

911
const region = JSON.parse(fs.readFileSync("region.geojson"));
12+
const simplifiedRegion = simplify(region, { tolerance: 0.01 }).features[0].geometry;
13+
14+
fs.writeFileSync("dist/region-mask.geojson", JSON.stringify(mask(region)));
1015

1116
// Determine tile URL
1217
let tileJson;
@@ -22,6 +27,24 @@ if (process.env.TILE_JSON) {
2227
for (const theme of ["light", "dark", "white", "grayscale", "black"]) {
2328
for (const { lang, full_name, script } of language_script_pairs) {
2429
const layers = protomapsLayers("protomaps", theme, lang, script);
30+
const bgIdx = layers.findIndex(({ id }) => id === "places_subplace");
31+
const bgColor = layers.find(({ id }) => id === "background").paint[
32+
"background-color"
33+
];
34+
layers.splice(bgIdx, 0, {
35+
id: "mask-fill",
36+
type: "fill",
37+
source: "mask",
38+
paint: { "fill-color": bgColor },
39+
});
40+
layers.forEach((l) => {
41+
if (l.id.startsWith("place") || l.id === "pois") {
42+
if (l.filter[0] === "==" && typeof l.filter[0] === "string") {
43+
l.filter = [l.filter[0], ["get", l.filter[1]], l.filter[2]];
44+
}
45+
l.filter = ["all", l.filter, ["within", simplifiedRegion]];
46+
}
47+
});
2548

2649
const style = {
2750
version: 8,
@@ -32,6 +55,13 @@ for (const theme of ["light", "dark", "white", "grayscale", "black"]) {
3255
'© <a href="https://openstreetmap.org">OpenStreetMap</a>',
3356
url: tileJson,
3457
},
58+
mask: {
59+
type: "geojson",
60+
data: new URL(
61+
"./region-mask.geojson",
62+
tileJson.replace(/^pmtiles:\/\//, ""),
63+
),
64+
},
3565
},
3666
layers: layers,
3767
sprite: `https://protomaps.github.io/basemaps-assets/sprites/v4/${theme}`,

package-lock.json

Lines changed: 95 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
"dependencies": {
3333
"@mapbox/geo-viewport": "^0.5.0",
3434
"@turf/bbox": "^7.2.0",
35+
"@turf/mask": "^7.2.0",
36+
"@turf/simplify": "^7.2.0",
3537
"protomaps-themes-base": "^4.3.1"
3638
},
3739
"devDependencies": {

0 commit comments

Comments
 (0)