mapwire 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +83 -0
- package/LICENSE +21 -0
- package/README.md +547 -2
- package/THIRD_PARTY_NOTICES.md +34 -0
- package/create-map.js +90 -0
- package/examples/react/App.jsx +31 -0
- package/examples/vanilla/index.html +27 -0
- package/examples/vue/App.vue +25 -0
- package/fonts/LICENSE-APACHE-2.0.txt +202 -0
- package/fonts/MaterialIcons-Regular.ttf +0 -0
- package/fonts/README.md +10 -0
- package/index.d.ts +25 -0
- package/index.js +24 -0
- package/llms.txt +46 -0
- package/mapwire.d.ts +26 -0
- package/mapwire.js +12 -0
- package/package.json +68 -4
- package/poi-markers.d.ts +38 -0
- package/poi-markers.js +110 -0
- package/style.css +12 -0
- package/theme/palette.js +103 -0
- package/theme/poi-categories.js +52 -0
- package/theme/style.js +384 -0
package/poi-markers.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { POI_GROUPS, poiMarkerId } from './theme/poi-categories.js';
|
|
2
|
+
|
|
3
|
+
export const ICON_FONT_FAMILY = 'Material Icons';
|
|
4
|
+
export const ICON_FONT_URL = new URL('./fonts/MaterialIcons-Regular.ttf', import.meta.url).href;
|
|
5
|
+
export const MARKER_BACKING = '#9aa0a6';
|
|
6
|
+
export const MARKER_BACKING_SATELLITE = '#ffffff';
|
|
7
|
+
export const MARKER_CSS_WIDTH = 38;
|
|
8
|
+
export const DEFAULT_DENSITY = 2;
|
|
9
|
+
|
|
10
|
+
const iconFonts = new Map();
|
|
11
|
+
|
|
12
|
+
export function loadIconFont(url = ICON_FONT_URL) {
|
|
13
|
+
if (!iconFonts.has(url)) {
|
|
14
|
+
const loading = (async () => {
|
|
15
|
+
const face = new FontFace(ICON_FONT_FAMILY, 'url(' + url + ')');
|
|
16
|
+
await face.load();
|
|
17
|
+
document.fonts.add(face);
|
|
18
|
+
return ICON_FONT_FAMILY;
|
|
19
|
+
})().catch((error) => {
|
|
20
|
+
iconFonts.delete(url);
|
|
21
|
+
throw error;
|
|
22
|
+
});
|
|
23
|
+
iconFonts.set(url, loading);
|
|
24
|
+
}
|
|
25
|
+
return iconFonts.get(url);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function deviceDensity() {
|
|
29
|
+
if (typeof window === 'undefined') return DEFAULT_DENSITY;
|
|
30
|
+
return Math.min(3, Math.max(1, window.devicePixelRatio || DEFAULT_DENSITY));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Draw the MapWire POI pin: a muted backing pin, a category circle and a white Material glyph.
|
|
35
|
+
* The category circle is centred in the bitmap, so the renderer's centre anchor marks the place.
|
|
36
|
+
*/
|
|
37
|
+
export function drawPoiMarker(context, codepoint, colour, options = {}) {
|
|
38
|
+
const width = options.width ?? MARKER_CSS_WIDTH;
|
|
39
|
+
const height = options.height ?? width * 0.9;
|
|
40
|
+
const backing = options.backing ?? MARKER_BACKING;
|
|
41
|
+
const cx = width / 2;
|
|
42
|
+
const cy = height / 2;
|
|
43
|
+
const outer = width * 0.3;
|
|
44
|
+
const inner = width * 0.24;
|
|
45
|
+
const tip = height - width * 0.025;
|
|
46
|
+
|
|
47
|
+
context.save();
|
|
48
|
+
context.translate(0, width * 0.03);
|
|
49
|
+
context.fillStyle = 'rgba(0, 0, 0, 0.24)';
|
|
50
|
+
context.beginPath();
|
|
51
|
+
context.ellipse(cx, tip, width * 0.14, width * 0.045, 0, 0, Math.PI * 2);
|
|
52
|
+
context.fill();
|
|
53
|
+
context.restore();
|
|
54
|
+
|
|
55
|
+
context.fillStyle = backing;
|
|
56
|
+
context.beginPath();
|
|
57
|
+
context.arc(cx, cy, outer, 0, Math.PI * 2);
|
|
58
|
+
context.fill();
|
|
59
|
+
context.beginPath();
|
|
60
|
+
context.moveTo(cx - outer * 0.42, cy + outer * 0.55);
|
|
61
|
+
context.lineTo(cx, tip);
|
|
62
|
+
context.lineTo(cx + outer * 0.42, cy + outer * 0.55);
|
|
63
|
+
context.closePath();
|
|
64
|
+
context.fill();
|
|
65
|
+
|
|
66
|
+
context.fillStyle = colour;
|
|
67
|
+
context.beginPath();
|
|
68
|
+
context.arc(cx, cy, inner, 0, Math.PI * 2);
|
|
69
|
+
context.fill();
|
|
70
|
+
|
|
71
|
+
context.fillStyle = '#ffffff';
|
|
72
|
+
context.font = inner * 1.15 + 'px ' + ICON_FONT_FAMILY;
|
|
73
|
+
context.textAlign = 'center';
|
|
74
|
+
context.textBaseline = 'middle';
|
|
75
|
+
context.fillText(String.fromCodePoint(codepoint), cx, cy);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function poiMarkerImage(codepoint, colour, options = {}) {
|
|
79
|
+
const density = options.pixelRatio ?? deviceDensity();
|
|
80
|
+
const cssWidth = options.width ?? MARKER_CSS_WIDTH;
|
|
81
|
+
const cssHeight = options.height ?? cssWidth * 0.9;
|
|
82
|
+
const canvas = document.createElement('canvas');
|
|
83
|
+
canvas.width = Math.round(cssWidth * density);
|
|
84
|
+
canvas.height = Math.round(cssHeight * density);
|
|
85
|
+
const context = canvas.getContext('2d');
|
|
86
|
+
drawPoiMarker(context, codepoint, colour, {
|
|
87
|
+
...options,
|
|
88
|
+
width: canvas.width,
|
|
89
|
+
height: canvas.height,
|
|
90
|
+
});
|
|
91
|
+
return context.getImageData(0, 0, canvas.width, canvas.height);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Register MapWire's POI images on a loaded MapLibre style.
|
|
96
|
+
* Call again after a style reload; createMap() does this automatically.
|
|
97
|
+
*/
|
|
98
|
+
export async function registerPoiMarkers(map, options = {}) {
|
|
99
|
+
await loadIconFont(options.fontUrl ?? ICON_FONT_URL);
|
|
100
|
+
const density = options.pixelRatio ?? deviceDensity();
|
|
101
|
+
const backing = options.satellite ? MARKER_BACKING_SATELLITE : MARKER_BACKING;
|
|
102
|
+
const added = [];
|
|
103
|
+
for (const [category, codepoint, colour] of options.groups ?? POI_GROUPS) {
|
|
104
|
+
const id = poiMarkerId(category);
|
|
105
|
+
if (map.hasImage?.(id)) continue;
|
|
106
|
+
map.addImage(id, poiMarkerImage(codepoint, colour, { backing, pixelRatio: density }), { pixelRatio: density });
|
|
107
|
+
added.push(id);
|
|
108
|
+
}
|
|
109
|
+
return added;
|
|
110
|
+
}
|
package/style.css
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MapWire layout helpers. Import MapLibre's own stylesheet separately:
|
|
3
|
+
*
|
|
4
|
+
* import "maplibre-gl/dist/maplibre-gl.css";
|
|
5
|
+
* import "mapwire/style.css";
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
.mapwire-container,
|
|
9
|
+
.mapwire-map {
|
|
10
|
+
position: relative;
|
|
11
|
+
min-block-size: 320px;
|
|
12
|
+
}
|
package/theme/palette.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/** Semantic colour tokens for the three MapWire themes. */
|
|
2
|
+
export const LIGHT = {
|
|
3
|
+
canvas: '#f8f7f7',
|
|
4
|
+
water: '#90daee',
|
|
5
|
+
waterLine: '#7bcce4',
|
|
6
|
+
vegetation: '#d3f8e1',
|
|
7
|
+
field: '#a9eac2',
|
|
8
|
+
wetland: '#cdeee0',
|
|
9
|
+
pedestrian: '#dbe0e8',
|
|
10
|
+
retail: '#fdf9ef',
|
|
11
|
+
campus: '#f0eded',
|
|
12
|
+
building: '#e8e9ed',
|
|
13
|
+
buildingEdge: '#d6d9e6',
|
|
14
|
+
road: {
|
|
15
|
+
local: '#aab9c9',
|
|
16
|
+
service: '#9bacbc',
|
|
17
|
+
arterial: '#aab9c9',
|
|
18
|
+
highway: '#8aa4c0',
|
|
19
|
+
edgeLocal: '#94a1af',
|
|
20
|
+
edgeService: '#8593a2',
|
|
21
|
+
edgeArterial: '#94a1af',
|
|
22
|
+
edgeHighway: '#5e75a0',
|
|
23
|
+
},
|
|
24
|
+
trail: '#7fcdb0',
|
|
25
|
+
cycleway: '#007b8b',
|
|
26
|
+
text: '#3c4043',
|
|
27
|
+
secondaryText: '#5f6368',
|
|
28
|
+
halo: '#ffffff',
|
|
29
|
+
boundary: '#9aa0a6',
|
|
30
|
+
boundaryLocal: '#b4b8bc',
|
|
31
|
+
terrain: ['#6b7280', '#ffffff', '#9aa0a6', 0.3],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const DARK = {
|
|
35
|
+
canvas: '#162640',
|
|
36
|
+
water: '#000d2a',
|
|
37
|
+
waterLine: '#001b3a',
|
|
38
|
+
vegetation: '#0d3847',
|
|
39
|
+
field: '#0d4956',
|
|
40
|
+
wetland: '#0b403f',
|
|
41
|
+
pedestrian: '#2a3546',
|
|
42
|
+
retail: '#1c2638',
|
|
43
|
+
campus: '#263448',
|
|
44
|
+
building: '#1c3b69',
|
|
45
|
+
buildingEdge: '#2e3d6d',
|
|
46
|
+
road: {
|
|
47
|
+
local: '#3d5a77',
|
|
48
|
+
service: '#2a4056',
|
|
49
|
+
arterial: '#476789',
|
|
50
|
+
highway: '#476789',
|
|
51
|
+
edgeLocal: '#07090e',
|
|
52
|
+
edgeService: '#07090e',
|
|
53
|
+
edgeArterial: '#07090e',
|
|
54
|
+
edgeHighway: '#07090e',
|
|
55
|
+
},
|
|
56
|
+
trail: '#167055',
|
|
57
|
+
cycleway: '#1f8f9c',
|
|
58
|
+
text: '#c3cad6',
|
|
59
|
+
secondaryText: '#d5dae2',
|
|
60
|
+
halo: '#1a2230',
|
|
61
|
+
boundary: '#8c95a3',
|
|
62
|
+
boundaryLocal: '#6f7887',
|
|
63
|
+
terrain: ['#0a1018', '#3a4a68', '#0a1018', 0.42],
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const AMOLED = {
|
|
67
|
+
...DARK,
|
|
68
|
+
canvas: '#000000',
|
|
69
|
+
water: '#04080c',
|
|
70
|
+
waterLine: '#061018',
|
|
71
|
+
vegetation: '#050e0a',
|
|
72
|
+
field: '#07140f',
|
|
73
|
+
wetland: '#06120f',
|
|
74
|
+
pedestrian: '#0a0c0f',
|
|
75
|
+
retail: '#000000',
|
|
76
|
+
campus: '#000000',
|
|
77
|
+
building: '#0a0c0f',
|
|
78
|
+
buildingEdge: '#14171a',
|
|
79
|
+
road: {
|
|
80
|
+
local: '#1a1d22',
|
|
81
|
+
service: '#111418',
|
|
82
|
+
arterial: '#22262c',
|
|
83
|
+
highway: '#22262c',
|
|
84
|
+
edgeLocal: '#000000',
|
|
85
|
+
edgeService: '#000000',
|
|
86
|
+
edgeArterial: '#000000',
|
|
87
|
+
edgeHighway: '#000000',
|
|
88
|
+
},
|
|
89
|
+
trail: '#1a3a28',
|
|
90
|
+
cycleway: '#0d2d36',
|
|
91
|
+
text: '#d0d4dc',
|
|
92
|
+
secondaryText: '#d0d4dc',
|
|
93
|
+
halo: '#000000',
|
|
94
|
+
boundary: '#535b66',
|
|
95
|
+
boundaryLocal: '#3d444e',
|
|
96
|
+
terrain: ['#000000', '#1a2030', '#000000', 0.28],
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export function paletteFor(mode) {
|
|
100
|
+
if (mode === 'dark') return DARK;
|
|
101
|
+
if (mode === 'amoled') return AMOLED;
|
|
102
|
+
return LIGHT;
|
|
103
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** Material Icons codepoint and marker colour for each POI category. */
|
|
2
|
+
export const POI_GROUPS = [
|
|
3
|
+
['food', 0xe56c, '#E8710A'],
|
|
4
|
+
['shop', 0xe8cc, '#4285F4'],
|
|
5
|
+
['lodging', 0xe53a, '#C2185B'],
|
|
6
|
+
['fuel', 0xe546, '#1967D2'],
|
|
7
|
+
['parking', 0xe54f, '#1A73E8'],
|
|
8
|
+
['park', 0xea63, '#188038'],
|
|
9
|
+
['health', 0xe548, '#D93025'],
|
|
10
|
+
['edu', 0xe80c, '#00897B'],
|
|
11
|
+
['civic', 0xe84f, '#5F6368'],
|
|
12
|
+
['culture', 0xea36, '#9334E6'],
|
|
13
|
+
['sport', 0xeb43, '#E37400'],
|
|
14
|
+
['transit', 0xe530, '#1A73E8'],
|
|
15
|
+
['default', 0xe55f, '#5F6368'],
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
/** OpenMapTiles poi class values grouped by marker category. */
|
|
19
|
+
export const POI_CLASS_GROUPS = {
|
|
20
|
+
food: ['restaurant', 'fast_food', 'cafe', 'bar', 'pub', 'food_court', 'ice_cream', 'bakery', 'food', 'beer', 'deli', 'confectionery'],
|
|
21
|
+
shop: ['shop', 'grocery', 'supermarket', 'convenience', 'clothing_store', 'mall', 'department_store', 'jewelry', 'gift', 'books', 'furniture', 'hardware', 'florist', 'mobile_phone', 'optician', 'hairdresser', 'laundry', 'butcher', 'greengrocer', 'marketplace', 'car', 'bicycle', 'outdoor', 'chemist', 'shoes', 'toys'],
|
|
22
|
+
lodging: ['lodging'],
|
|
23
|
+
fuel: ['fuel'],
|
|
24
|
+
parking: ['parking', 'bicycle_parking'],
|
|
25
|
+
park: ['park', 'garden', 'nature_reserve', 'golf', 'pitch', 'playground', 'dog_park', 'picnic_site'],
|
|
26
|
+
health: ['hospital', 'pharmacy', 'doctors', 'dentist', 'veterinary', 'clinic'],
|
|
27
|
+
edu: ['school', 'college', 'university', 'library', 'kindergarten'],
|
|
28
|
+
civic: ['bank', 'atm', 'post', 'police', 'fire_station', 'town_hall', 'courthouse', 'place_of_worship', 'cemetery', 'community_centre'],
|
|
29
|
+
culture: ['museum', 'art_gallery', 'cinema', 'theatre', 'attraction', 'gallery', 'information', 'artwork', 'viewpoint', 'aquarium', 'zoo'],
|
|
30
|
+
sport: ['stadium', 'sports', 'sports_centre', 'fitness_centre', 'swimming_pool'],
|
|
31
|
+
transit: ['bus', 'railway', 'aerodrome', 'station', 'subway', 'tram', 'ferry_terminal', 'airport'],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const definition = (group) => POI_GROUPS.find(([name]) => name === group) ?? POI_GROUPS.at(-1);
|
|
35
|
+
|
|
36
|
+
function towardWhite(hex, amount) {
|
|
37
|
+
const value = Number.parseInt(hex.slice(1), 16);
|
|
38
|
+
const channel = (shift) => {
|
|
39
|
+
const original = (value >> shift) & 255;
|
|
40
|
+
return Math.round(original + (255 - original) * amount);
|
|
41
|
+
};
|
|
42
|
+
return '#' + [16, 8, 0].map((shift) => channel(shift).toString(16).padStart(2, '0')).join('').toUpperCase();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function poiLabelColor(group, dark) {
|
|
46
|
+
const base = definition(group)[2];
|
|
47
|
+
return dark ? towardWhite(base, 0.55) : base;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function poiMarkerId(group) {
|
|
51
|
+
return 'mapwire-poi-' + group;
|
|
52
|
+
}
|
package/theme/style.js
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import { paletteFor } from './palette.js';
|
|
2
|
+
import { POI_CLASS_GROUPS, poiLabelColor, poiMarkerId } from './poi-categories.js';
|
|
3
|
+
|
|
4
|
+
export const TERRAIN_TILE_URL = 'https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png';
|
|
5
|
+
|
|
6
|
+
const group = (...items) => items;
|
|
7
|
+
|
|
8
|
+
const ROAD_GROUPS = {
|
|
9
|
+
highway: {
|
|
10
|
+
metres: 22, singleCarriageway: 13, cap: 60,
|
|
11
|
+
fills: group('road_motorway', 'road_motorway_link', 'bridge_motorway', 'bridge_motorway_link', 'tunnel_motorway', 'tunnel_motorway_link'),
|
|
12
|
+
},
|
|
13
|
+
arterial: {
|
|
14
|
+
metres: 17, singleCarriageway: 11, cap: 50,
|
|
15
|
+
fills: group('road_trunk_primary', 'bridge_trunk_primary', 'tunnel_trunk_primary'),
|
|
16
|
+
},
|
|
17
|
+
secondary: {
|
|
18
|
+
metres: 13, singleCarriageway: 9, cap: 42,
|
|
19
|
+
fills: group('road_secondary_tertiary', 'bridge_secondary_tertiary', 'tunnel_secondary_tertiary'),
|
|
20
|
+
},
|
|
21
|
+
local: {
|
|
22
|
+
metres: 10, singleCarriageway: 10, cap: 34,
|
|
23
|
+
fills: group('road_minor', 'bridge_street', 'tunnel_minor', 'tunnel_street', 'road_link', 'bridge_link', 'tunnel_link'),
|
|
24
|
+
},
|
|
25
|
+
service: {
|
|
26
|
+
metres: 3.5, singleCarriageway: 3.5, cap: 14,
|
|
27
|
+
fills: group('road_service_track', 'bridge_service_track', 'tunnel_service_track'),
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const METRES_PER_PIXEL_AT_ZOOM_ZERO = 78271.517 * Math.cos((40 * Math.PI) / 180);
|
|
32
|
+
const twinIds = new Set();
|
|
33
|
+
const roadEdgeIds = new Set([
|
|
34
|
+
'road_motorway_casing', 'road_motorway_link_casing', 'bridge_motorway_casing', 'bridge_motorway_link_casing',
|
|
35
|
+
'road_trunk_primary_casing', 'bridge_trunk_primary_casing',
|
|
36
|
+
'road_secondary_tertiary_casing', 'bridge_secondary_tertiary_casing',
|
|
37
|
+
'road_minor_casing', 'road_link_casing', 'bridge_street_casing', 'bridge_link_casing',
|
|
38
|
+
'road_service_track_casing', 'bridge_service_track_casing',
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
function layerById(style, id) {
|
|
42
|
+
return (style.layers ?? []).find((layer) => layer.id === id);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function patchPaint(style, id, values) {
|
|
46
|
+
const layer = layerById(style, id);
|
|
47
|
+
if (layer) layer.paint = { ...(layer.paint ?? {}), ...values };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function patchLayout(style, id, values) {
|
|
51
|
+
const layer = layerById(style, id);
|
|
52
|
+
if (!layer) return;
|
|
53
|
+
const next = { ...(layer.layout ?? {}), ...values };
|
|
54
|
+
for (const key of Object.keys(next)) if (next[key] === undefined) delete next[key];
|
|
55
|
+
layer.layout = next;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function hide(style, id) {
|
|
59
|
+
patchLayout(style, id, { visibility: 'none' });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function setRange(style, id, minzoom, maxzoom) {
|
|
63
|
+
const layer = layerById(style, id);
|
|
64
|
+
if (!layer) return;
|
|
65
|
+
if (minzoom != null) layer.minzoom = minzoom;
|
|
66
|
+
if (maxzoom != null) layer.maxzoom = maxzoom;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function addNear(style, anchor, layer, position = 'after') {
|
|
70
|
+
const index = (style.layers ?? []).findIndex((item) => item.id === anchor);
|
|
71
|
+
if (index < 0) {
|
|
72
|
+
style.layers.push(layer);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
style.layers.splice(position === 'before' ? index : index + 1, 0, layer);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function interpolate(axis, stops) {
|
|
79
|
+
return ['interpolate', ['linear'], axis, ...stops.flat()];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function visibleWidth(metres, cap, zoom, scale) {
|
|
83
|
+
const value = metres * scale;
|
|
84
|
+
return Math.min(cap, Math.max(0, value / (METRES_PER_PIXEL_AT_ZOOM_ZERO / 2 ** zoom)));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function baseRoadWidth(kind, zoom) {
|
|
88
|
+
const start = kind === 'local' ? 12.5 : kind === 'service' ? 15.5 : kind === 'highway' ? 5 : 6.5;
|
|
89
|
+
if (zoom <= start) return 0;
|
|
90
|
+
if (kind === 'local') return 0.8 + ((zoom - start) / 7.5) ** 1.18 * 18;
|
|
91
|
+
if (kind === 'service') return 0.6 + ((zoom - start) / 4.5) ** 1.1 * 10;
|
|
92
|
+
if (kind === 'highway') return 0.5 + ((zoom - 5) / 15) ** 1.3 * 22;
|
|
93
|
+
return 0.7 + ((zoom - start) / 13.5) ** 1.25 * 17;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function widthExpressions(kind, entry, scale) {
|
|
97
|
+
const fill = [];
|
|
98
|
+
const casing = [];
|
|
99
|
+
for (let zoom = 5; zoom <= 20; zoom += 0.5) {
|
|
100
|
+
const base = baseRoadWidth(kind, zoom);
|
|
101
|
+
const twoWay = Math.max(base, visibleWidth(entry.metres, entry.cap, zoom, scale));
|
|
102
|
+
const oneWay = Math.max(base, visibleWidth(zoom >= 11 ? entry.singleCarriageway : entry.metres, entry.cap, zoom, scale));
|
|
103
|
+
const edge = Math.min(1.2, Math.max(0.5, (zoom - 13) * 0.18));
|
|
104
|
+
const oneway = ['==', ['coalesce', ['get', 'oneway'], 0], 1];
|
|
105
|
+
fill.push([zoom, Math.abs(twoWay - oneWay) < 0.1 ? twoWay : ['case', oneway, oneWay, twoWay]]);
|
|
106
|
+
casing.push([zoom, Math.abs(twoWay - oneWay) < 0.1 ? twoWay + edge : ['case', oneway, oneWay + edge, twoWay + edge]]);
|
|
107
|
+
}
|
|
108
|
+
return { fill: interpolate(['zoom'], fill), casing: interpolate(['zoom'], casing) };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function casingId(fillId) {
|
|
112
|
+
if (fillId === 'bridge_street') return 'bridge_street_casing';
|
|
113
|
+
if (fillId === 'tunnel_street') return 'tunnel_street_casing';
|
|
114
|
+
return fillId + '_casing';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function vectorSource(style) {
|
|
118
|
+
const sources = style.sources ?? {};
|
|
119
|
+
if (sources.openmaptiles) return 'openmaptiles';
|
|
120
|
+
return Object.keys(sources).find((id) => sources[id]?.type === 'vector');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function vectorLayer(source, id, type, sourceLayer, { filter, minzoom, paint, layout } = {}) {
|
|
124
|
+
return {
|
|
125
|
+
id,
|
|
126
|
+
type,
|
|
127
|
+
source,
|
|
128
|
+
'source-layer': sourceLayer,
|
|
129
|
+
...(filter ? { filter } : {}),
|
|
130
|
+
...(minzoom != null ? { minzoom } : {}),
|
|
131
|
+
...(paint ? { paint } : {}),
|
|
132
|
+
...(layout ? { layout } : {}),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function installTwins(style) {
|
|
137
|
+
const source = vectorSource(style);
|
|
138
|
+
if (!source) return;
|
|
139
|
+
const polygon = ['match', ['geometry-type'], ['Polygon', 'MultiPolygon'], true, false];
|
|
140
|
+
const line = ['match', ['geometry-type'], ['LineString', 'MultiLineString'], true, false];
|
|
141
|
+
const classIn = (...classes) => ['match', ['get', 'class'], classes, true, false];
|
|
142
|
+
|
|
143
|
+
const layers = [
|
|
144
|
+
['mapwire-wetland', 'fill', 'landcover', ['==', ['get', 'class'], 'wetland'], 12, null, 'landcover_wetland', 'after'],
|
|
145
|
+
['mapwire-pedestrian', 'fill', 'transportation', polygon, null, null, 'road_area_pattern', 'after'],
|
|
146
|
+
['mapwire-field', 'fill', 'landuse', classIn('pitch', 'playground', 'track', 'stadium'), 13, null, 'park', 'after'],
|
|
147
|
+
['mapwire-retail', 'fill', 'landuse', classIn('commercial', 'retail'), 12, null, 'park', 'after'],
|
|
148
|
+
['mapwire-trail', 'line', 'transportation', ['all', line, ['==', ['get', 'class'], 'path'], ['match', ['get', 'subclass'], ['path', 'bridleway'], true, false]], 14, 'line', 'road_minor', 'before'],
|
|
149
|
+
['mapwire-cycle', 'line', 'transportation', ['all', line, ['==', ['get', 'class'], 'path'], ['==', ['get', 'subclass'], 'cycleway']], 14, 'line', 'road_minor', 'before'],
|
|
150
|
+
];
|
|
151
|
+
|
|
152
|
+
for (const [id, type, sourceLayer, filter, minzoom, paintKind, anchor, position] of layers) {
|
|
153
|
+
if (layerById(style, id)) continue;
|
|
154
|
+
const paint = paintKind === 'line'
|
|
155
|
+
? { 'line-width': interpolate(['zoom'], [[14, 0.5], [16, 1.8], [19, 4]]) }
|
|
156
|
+
: undefined;
|
|
157
|
+
const layout = paintKind === 'line' ? { 'line-cap': 'round' } : undefined;
|
|
158
|
+
addNear(style, anchor, vectorLayer(source, id, type, sourceLayer, { filter, minzoom, paint, layout }), position);
|
|
159
|
+
twinIds.add(id);
|
|
160
|
+
}
|
|
161
|
+
hide(style, 'landcover_wetland');
|
|
162
|
+
hide(style, 'road_area_pattern');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function applyBuildings(style, palette) {
|
|
166
|
+
patchPaint(style, 'building', { 'fill-color': palette.building, 'fill-outline-color': palette.buildingEdge });
|
|
167
|
+
setRange(style, 'building', 16, 24);
|
|
168
|
+
patchPaint(style, 'building-3d', {
|
|
169
|
+
'fill-extrusion-color': palette.building,
|
|
170
|
+
'fill-extrusion-opacity': 1,
|
|
171
|
+
'fill-extrusion-vertical-gradient': true,
|
|
172
|
+
'fill-extrusion-height': interpolate(['zoom'], [
|
|
173
|
+
[16, 0],
|
|
174
|
+
[17, ['*', ['coalesce', ['get', 'render_height'], 8], 0.3]],
|
|
175
|
+
[19, ['coalesce', ['get', 'render_height'], 8]],
|
|
176
|
+
]),
|
|
177
|
+
'fill-extrusion-base': interpolate(['zoom'], [
|
|
178
|
+
[16, 0],
|
|
179
|
+
[17, ['*', ['coalesce', ['get', 'render_min_height'], 0], 0.3]],
|
|
180
|
+
[19, ['coalesce', ['get', 'render_min_height'], 0]],
|
|
181
|
+
]),
|
|
182
|
+
});
|
|
183
|
+
setRange(style, 'building-3d', 17, null);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function applyTerrain(style, palette) {
|
|
187
|
+
const [shadow, highlight, accent, exaggeration] = palette.terrain;
|
|
188
|
+
const terrainPaint = {
|
|
189
|
+
'hillshade-shadow-color': shadow,
|
|
190
|
+
'hillshade-highlight-color': highlight,
|
|
191
|
+
'hillshade-accent-color': accent,
|
|
192
|
+
'hillshade-exaggeration': exaggeration,
|
|
193
|
+
};
|
|
194
|
+
const existing = layerById(style, 'mapwire-hillshade');
|
|
195
|
+
if (existing) {
|
|
196
|
+
existing.paint = { ...(existing.paint ?? {}), ...terrainPaint };
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
style.sources['mapwire-terrain'] = {
|
|
200
|
+
type: 'raster-dem',
|
|
201
|
+
tiles: [TERRAIN_TILE_URL],
|
|
202
|
+
encoding: 'terrarium',
|
|
203
|
+
tileSize: 256,
|
|
204
|
+
};
|
|
205
|
+
const firstRoad = style.layers.find((layer) => layer.id.startsWith('road_'));
|
|
206
|
+
addNear(style, firstRoad?.id, {
|
|
207
|
+
id: 'mapwire-hillshade',
|
|
208
|
+
type: 'hillshade',
|
|
209
|
+
source: 'mapwire-terrain',
|
|
210
|
+
maxzoom: 16,
|
|
211
|
+
paint: terrainPaint,
|
|
212
|
+
}, 'before');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function applyLand(style, palette) {
|
|
216
|
+
patchPaint(style, 'background', { 'background-color': palette.canvas });
|
|
217
|
+
patchPaint(style, 'water', { 'fill-color': palette.water });
|
|
218
|
+
for (const id of group('waterway_river', 'waterway_other', 'waterway_tunnel')) patchPaint(style, id, { 'line-color': palette.waterLine });
|
|
219
|
+
for (const id of group('park', 'landcover_grass', 'landcover_wood')) patchPaint(style, id, { 'fill-color': palette.vegetation, 'fill-opacity': 1 });
|
|
220
|
+
patchPaint(style, 'mapwire-wetland', { 'fill-color': palette.wetland, 'fill-opacity': 1 });
|
|
221
|
+
patchPaint(style, 'mapwire-field', { 'fill-color': palette.field, 'fill-opacity': 1 });
|
|
222
|
+
patchPaint(style, 'mapwire-retail', { 'fill-color': palette.retail, 'fill-opacity': 1 });
|
|
223
|
+
patchPaint(style, 'mapwire-pedestrian', { 'fill-color': palette.pedestrian });
|
|
224
|
+
patchPaint(style, 'mapwire-trail', { 'line-color': palette.trail });
|
|
225
|
+
patchPaint(style, 'mapwire-cycle', { 'line-color': palette.cycleway });
|
|
226
|
+
patchPaint(style, 'landuse_pitch', { 'fill-color': palette.field, 'fill-opacity': 1 });
|
|
227
|
+
patchPaint(style, 'landuse_track', { 'fill-color': palette.field, 'fill-opacity': 1 });
|
|
228
|
+
patchPaint(style, 'landuse_school', { 'fill-color': palette.campus, 'fill-opacity': 1 });
|
|
229
|
+
patchPaint(style, 'aeroway_fill', { 'fill-color': palette.retail, 'fill-opacity': 1 });
|
|
230
|
+
patchPaint(style, 'aeroway_runway', { 'line-color': palette.road.service });
|
|
231
|
+
patchPaint(style, 'aeroway_taxiway', { 'line-color': palette.road.service });
|
|
232
|
+
|
|
233
|
+
const keep = new Set(['park', 'landcover_grass', 'landcover_wood', 'landuse_pitch', 'landuse_track', 'landuse_school']);
|
|
234
|
+
for (const layer of style.layers) {
|
|
235
|
+
if (layer.type !== 'fill' || keep.has(layer.id) || twinIds.has(layer.id)) continue;
|
|
236
|
+
if (layer.id.startsWith('landuse_') || layer.id.startsWith('landcover_')) {
|
|
237
|
+
layer.paint = { ...(layer.paint ?? {}), 'fill-color': palette.canvas, 'fill-opacity': 1 };
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function applyRoads(style, palette, scale) {
|
|
243
|
+
for (const [kind, entry] of Object.entries(ROAD_GROUPS)) {
|
|
244
|
+
const widths = widthExpressions(kind, entry, scale);
|
|
245
|
+
const colour = kind === 'service' ? palette.road.service : kind === 'highway' ? palette.road.highway : palette.road.local;
|
|
246
|
+
for (const fill of entry.fills) {
|
|
247
|
+
patchPaint(style, fill, { 'line-color': colour, 'line-width': widths.fill });
|
|
248
|
+
patchPaint(style, casingId(fill), { 'line-width': widths.casing });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
for (const layer of style.layers) {
|
|
252
|
+
if (!roadEdgeIds.has(layer.id)) continue;
|
|
253
|
+
const colour = layer.id.includes('service') ? palette.road.edgeService
|
|
254
|
+
: layer.id.includes('minor') || layer.id.includes('street') || layer.id.includes('link') ? palette.road.edgeLocal
|
|
255
|
+
: layer.id.includes('trunk') || layer.id.includes('secondary') ? palette.road.edgeArterial
|
|
256
|
+
: palette.road.edgeHighway;
|
|
257
|
+
layer.paint = { ...(layer.paint ?? {}), 'line-color': colour };
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function applyLabels(style, palette) {
|
|
262
|
+
for (const layer of style.layers) {
|
|
263
|
+
if (layer.type !== 'symbol') continue;
|
|
264
|
+
layer.paint = {
|
|
265
|
+
...(layer.paint ?? {}),
|
|
266
|
+
'text-color': palette.text,
|
|
267
|
+
'text-halo-color': palette.halo,
|
|
268
|
+
'text-halo-width': layer.id.startsWith('highway-name') ? 1.8 : 1.1,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
for (const id of group('highway-name-minor', 'highway-name-major')) {
|
|
272
|
+
patchLayout(style, id, {
|
|
273
|
+
'text-font': ['Noto Sans Bold'],
|
|
274
|
+
'text-field': ['coalesce', ['get', 'name:en'], ['get', 'name:latin'], ['get', 'name']],
|
|
275
|
+
'symbol-spacing': interpolate(['zoom'], [[16, 150], [18, 300], [21, 900]]),
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
setRange(style, 'highway-name-minor', 13.5, null);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function classExpression(value) {
|
|
282
|
+
const expression = ['match', ['get', 'class']];
|
|
283
|
+
for (const [category, classes] of Object.entries(POI_CLASS_GROUPS)) expression.push(classes, value(category));
|
|
284
|
+
expression.push(value('default'));
|
|
285
|
+
return expression;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function applyPoi(style, dark, iconSize) {
|
|
289
|
+
const icons = classExpression(poiMarkerId);
|
|
290
|
+
const colours = classExpression((category) => poiLabelColor(category, dark));
|
|
291
|
+
for (const id of group('poi_r1', 'poi_r7', 'poi_r20', 'poi_transit')) {
|
|
292
|
+
const layer = layerById(style, id);
|
|
293
|
+
if (!layer) continue;
|
|
294
|
+
patchLayout(style, id, {
|
|
295
|
+
'icon-image': icons,
|
|
296
|
+
'icon-size': iconSize,
|
|
297
|
+
'symbol-sort-key': ['coalesce', ['get', 'rank'], 999],
|
|
298
|
+
'text-variable-anchor': ['right', 'left', 'top', 'bottom'],
|
|
299
|
+
'text-radial-offset': 1.3,
|
|
300
|
+
'text-justify': 'auto',
|
|
301
|
+
'text-font': ['Noto Sans Regular'],
|
|
302
|
+
'text-anchor': undefined,
|
|
303
|
+
'text-offset': undefined,
|
|
304
|
+
});
|
|
305
|
+
if (!JSON.stringify(layer.filter ?? []).includes('"has","name"')) {
|
|
306
|
+
layer.filter = layer.filter ? ['all', layer.filter, ['has', 'name']] : ['has', 'name'];
|
|
307
|
+
}
|
|
308
|
+
layer.paint = { ...(layer.paint ?? {}), 'text-color': colours };
|
|
309
|
+
}
|
|
310
|
+
setRange(style, 'poi_r7', 15, null);
|
|
311
|
+
setRange(style, 'poi_transit', 16, null);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function applyBorders(style, palette) {
|
|
315
|
+
patchPaint(style, 'boundary_2', {
|
|
316
|
+
'line-color': palette.boundary,
|
|
317
|
+
'line-opacity': interpolate(['zoom'], [[0, 0.55], [4, 1]]),
|
|
318
|
+
'line-width': interpolate(['zoom'], [[3, 0.8], [6, 1.25], [12, 2]]),
|
|
319
|
+
});
|
|
320
|
+
const local = layerById(style, 'boundary_3');
|
|
321
|
+
if (local) {
|
|
322
|
+
local.filter = ['all',
|
|
323
|
+
['>=', ['get', 'admin_level'], 3],
|
|
324
|
+
['<=', ['get', 'admin_level'], 4],
|
|
325
|
+
['!=', ['coalesce', ['get', 'maritime'], 0], 1],
|
|
326
|
+
['!=', ['coalesce', ['get', 'disputed'], 0], 1],
|
|
327
|
+
['!', ['has', 'claimed_by']],
|
|
328
|
+
];
|
|
329
|
+
local.minzoom = 4;
|
|
330
|
+
local.paint = {
|
|
331
|
+
...(local.paint ?? {}),
|
|
332
|
+
'line-color': palette.boundaryLocal,
|
|
333
|
+
'line-dasharray': [3, 2],
|
|
334
|
+
'line-width': interpolate(['zoom'], [[4, 0.8], [8, 1.2], [12, 1.6]]),
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
patchPaint(style, 'boundary_disputed', { 'line-color': palette.boundary });
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function removeClutter(style) {
|
|
341
|
+
for (const id of group(
|
|
342
|
+
'park_outline', 'road_path_pedestrian', 'bridge_path_pedestrian', 'bridge_path_pedestrian_casing',
|
|
343
|
+
'tunnel_path_pedestrian', 'road_major_rail_hatching', 'bridge_major_rail_hatching',
|
|
344
|
+
'tunnel_major_rail_hatching', 'road_transit_rail_hatching', 'bridge_transit_rail_hatching',
|
|
345
|
+
'tunnel_transit_rail_hatching',
|
|
346
|
+
)) hide(style, id);
|
|
347
|
+
const pathNames = layerById(style, 'highway-name-path');
|
|
348
|
+
if (pathNames) {
|
|
349
|
+
pathNames.filter = ['all',
|
|
350
|
+
['==', ['get', 'class'], 'path'],
|
|
351
|
+
['match', ['get', 'subclass'], 'path', true, 'bridleway', true, 'cycleway', true, 'pedestrian', true, false],
|
|
352
|
+
['!=', ['coalesce', ['get', 'indoor'], 0], 1],
|
|
353
|
+
];
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Add MapWire's Google Maps-inspired visual treatment to an OpenMapTiles style.
|
|
359
|
+
*
|
|
360
|
+
* The input is cloned. Public OpenMapTiles ids such as water, road_minor and poi_r1 identify the
|
|
361
|
+
* layers being styled; MapWire's own added layers use the mapwire- prefix.
|
|
362
|
+
*/
|
|
363
|
+
export function mapwireStyle(base, options = {}) {
|
|
364
|
+
const style = typeof base === 'string' ? JSON.parse(base) : structuredClone(base);
|
|
365
|
+
style.layers ??= [];
|
|
366
|
+
style.sources ??= {};
|
|
367
|
+
const mode = options.mode ?? 'light';
|
|
368
|
+
const palette = paletteFor(mode);
|
|
369
|
+
|
|
370
|
+
installTwins(style);
|
|
371
|
+
applyLand(style, palette);
|
|
372
|
+
applyBuildings(style, palette);
|
|
373
|
+
applyRoads(style, palette, options.roadWidthScale ?? 1);
|
|
374
|
+
applyLabels(style, palette);
|
|
375
|
+
applyPoi(style, mode !== 'light', options.poiIconSize ?? 0.8);
|
|
376
|
+
applyBorders(style, palette);
|
|
377
|
+
removeClutter(style);
|
|
378
|
+
if (options.topography) applyTerrain(style, palette);
|
|
379
|
+
if (options.glyphs) style.glyphs = options.glyphs;
|
|
380
|
+
style.light = { ...(style.light ?? {}), intensity: 0.22 };
|
|
381
|
+
return style;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
export default mapwireStyle;
|