NPS-style protected areas and textures: national parks keep the full sage fill, other protected areas a translucent gray-green with a dashed outline, military areas red hatch + outline; forest (z12+) and grass (z13+) textures drawn on canvas and registered on styleimagemissing, with night variants; legend rows
CI / check (push) Successful in 20s
CI / deploy (push) Successful in 18s

This commit is contained in:
randy committed 2026-11-23 19:19:11 +01:00
1 parent 31b27a0852
commit 25f5c42a1b
4 files changed
+132 -6

No files matched your search

+119 -2
View File
@@ -9,7 +9,7 @@
// same patched style, so the two can't drift apart.
export function patchOnlineStyle(style) {
return insertTrackOnlyLayer(parkTilesStyle(style));
return insertTrackOnlyLayer(protectedAreasAndTextures(parkTilesStyle(style)));
}
const PARK_TILES_PATCH = {
@@ -87,6 +87,115 @@ function parkTilesStyle(style) {
return style;
}
// OpenMapTiles' park `class` is the OSM protection_title lowercased when
// there is one ("národný_park", "nationalpark", …), else the boundary/leisure
// tag ("national_park", "nature_reserve", "protected_area"), so national
// parks are told apart by substring.
// ponytail: a handful of languages; add a word when a park shows up gray.
const NATIONAL_PARK = ["any", ...["nation", "národn", "narodn", "nemzeti", "nazional", "nacional"].map((word) => ["in", word, ["coalesce", ["get", "class"], ""]])];
// National parks keep the flat Park Tiles sage; other protected areas
// (nature reserves, landscape areas, Natura 2000, …) get a translucent
// gray-green with a dashed edge, so the map tells "national park rules"
// from "some protection" at a glance. Military areas red-hatched. Forest
// and meadow get a tree/grass texture over their flat fill from zoom 12/13.
// The pattern images are drawn at runtime (patternImage below, on
// MapLibre's styleimagemissing), so there's no sprite sheet to extend and
// they work offline alike.
const PROTECTED_FILL = "rgba(140, 165, 120, 0.18)";
const PROTECTED_LINE = "#7d9468";
function protectedAreasAndTextures(style) {
const find = (id) => style.layers.find((layer) => layer.id === id);
const park = find("park");
const outline = find("park_outline");
const band = find("park_tintband");
if (!park || !outline) return style;
const sage = park.paint["fill-color"];
park.paint["fill-color"] = ["case", ["!", NATIONAL_PARK], PROTECTED_FILL, sage];
park.paint["fill-outline-color"] = ["case", ["!", NATIONAL_PARK], "rgba(0, 0, 0, 0)", sage];
const protectedOutline = {
...outline,
id: "park_outline_protected",
filter: ["all", outline.filter, ["!", NATIONAL_PARK]],
paint: { "line-color": PROTECTED_LINE, "line-width": 1.2, "line-dasharray": [3, 2] },
};
outline.filter = ["all", outline.filter, NATIONAL_PARK];
if (band) band.filter = ["all", band.filter, NATIONAL_PARK];
const textures = [
{ id: "landcover_wood_pattern", source: "landcover", class: "wood", pattern: "forest", minzoom: 12 },
{ id: "landcover_grass_pattern", source: "landcover", class: "grass", pattern: "grass", minzoom: 13 },
{ id: "landuse_military", source: "landuse", class: "military", pattern: "military", minzoom: 8 },
].map((t) => ({
id: t.id,
type: "fill",
source: park.source,
"source-layer": t.source,
minzoom: t.minzoom,
filter: ["==", ["get", "class"], t.class],
paint: { "fill-pattern": `maptool-pattern-${t.pattern}` },
}));
const military = {
...textures[2],
id: "landuse_military_outline",
type: "line",
paint: { "line-color": "#c0392b", "line-width": 1.2, "line-opacity": 0.7 },
};
// Above the park group (a national park's sage is opaque), below water.
const index = style.layers.indexOf(outline) + 1;
style.layers.splice(index, 0, protectedOutline, ...textures, military);
return style;
}
const PATTERNS = {
forest: { day: "rgba(110, 130, 90, 0.45)", night: "rgba(120, 150, 110, 0.35)" },
grass: { day: "rgba(130, 140, 95, 0.45)", night: "rgba(120, 140, 105, 0.3)" },
military: { day: "rgba(192, 57, 43, 0.4)", night: "rgba(192, 57, 43, 0.35)" },
};
// "maptool-pattern-<name>[-night]" → { width, height, data } for
// map.addImage, drawn on a canvas at 2× (pixelRatio 2); null for other ids.
export function patternImage(id) {
const match = id.match(/^maptool-pattern-(\w+?)(-night)?$/);
const colors = match && PATTERNS[match[1]];
if (!colors) return null;
const size = 48;
const ctx = document.createElement("canvas").getContext("2d", { willReadFrequently: true });
ctx.canvas.width = ctx.canvas.height = size;
ctx.strokeStyle = ctx.fillStyle = match[2] ? colors.night : colors.day;
ctx.lineWidth = 2;
ctx.lineCap = "round";
if (match[1] === "forest") {
// Two little trees, staggered, so the texture doesn't read as a grid.
for (const [x, y] of [[12, 14], [36, 38]]) {
ctx.beginPath();
ctx.arc(x, y - 3, 6, 0, 2 * Math.PI);
ctx.fill();
ctx.fillRect(x - 1, y + 2, 2, 5);
}
} else if (match[1] === "grass") {
for (const [x, y] of [[10, 16], [34, 40], [30, 12]]) {
ctx.beginPath();
for (const dx of [-4, 0, 4]) {
ctx.moveTo(x + dx * 0.4, y);
ctx.lineTo(x + dx, y - 6);
}
ctx.stroke();
}
} else {
// Diagonal hatch, seamless across tile edges.
ctx.lineWidth = 3;
ctx.beginPath();
for (const offset of [-size, 0, size]) {
ctx.moveTo(offset, size);
ctx.lineTo(offset + size, 0);
}
ctx.stroke();
}
return { image: ctx.getImageData(0, 0, size, size), options: { pixelRatio: 2 } };
}
// Liberty's own "road_service_track" layer paints service roads (paved
// driveways/parking aisles) and actual unpaved tracks the same color —
// fine for a general-purpose map, not for this app's "can the camper get
@@ -137,8 +246,16 @@ const NIGHT_PATCH = {
water: { "fill-color": "#12202b" },
waterway_river: { "line-color": "#1a3040" },
waterway_other: { "line-color": "#1a3040" },
park: { "fill-color": "#1f2b22", "fill-outline-color": "#1f2b22" },
park: {
"fill-color": ["case", ["!", NATIONAL_PARK], "rgba(120, 150, 110, 0.12)", "#1f2b22"],
"fill-outline-color": ["case", ["!", NATIONAL_PARK], "rgba(0, 0, 0, 0)", "#1f2b22"],
},
park_outline: { "line-color": "#4f6b3a" },
park_outline_protected: { "line-color": "#46593a" },
landcover_wood_pattern: { "fill-pattern": "maptool-pattern-forest-night" },
landcover_grass_pattern: { "fill-pattern": "maptool-pattern-grass-night" },
landuse_military: { "fill-pattern": "maptool-pattern-military-night" },
landuse_military_outline: { "line-color": "#8a3a30" },
park_tintband: { "line-color": "#2b3a26" },
building: { "fill-color": "#262c32", "fill-outline-color": "#30373e" },
boundary_2: { "line-color": "#4a4f55" },