Skip to content

Commit 64ae68a

Browse files
authored
Migrate to vector tiles to add boundaries and better styling. (#247)
1 parent 350dab4 commit 64ae68a

3 files changed

Lines changed: 213 additions & 31 deletions

File tree

package-lock.json

Lines changed: 154 additions & 19 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"geojson": "^0.5.0",
2020
"jwt-decode": "^4.0.0",
2121
"jwt-encode": "^1.0.1",
22-
"ol": "^10.7.0",
22+
"ol": "^10.8.0",
23+
"ol-mapbox-style": "^13.4.0",
2324
"ol-pmtiles": "^2.0.2",
2425
"pmtiles": "^4.4.0",
2526
"vue": "^3.4.15",

src/layers/Label-Layer.ts

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,61 @@
1-
import TileLayer from 'ol/layer/Tile'
2-
import { XYZ } from 'ol/source'
1+
import type { FeatureLike } from 'ol/Feature'
2+
import VectorTileLayer from 'ol/layer/VectorTile'
3+
import { applyStyle } from 'ol-mapbox-style'
4+
5+
const POSITRON_STYLE_URL = 'https://basemaps.cartocdn.com/gl/positron-gl-style/style.json'
6+
7+
// Only render administrative boundaries and place/water/road labels
8+
// (no background, fills, or road geometry — keeps the layer transparent)
9+
const LABEL_AND_BOUNDARY_LAYERS = [
10+
'boundary_county',
11+
'boundary_state',
12+
'boundary_country_outline',
13+
'boundary_country_inner',
14+
'waterway_label',
15+
'watername_ocean',
16+
'watername_sea',
17+
'watername_lake',
18+
'watername_lake_line',
19+
'place_hamlet',
20+
'place_suburbs',
21+
'place_villages',
22+
'place_town',
23+
'place_country_2',
24+
'place_country_1',
25+
'place_state',
26+
'place_continent',
27+
'place_city_r6',
28+
'place_city_r5',
29+
'place_city_dot_r7',
30+
'place_city_dot_r4',
31+
'place_city_dot_r2',
32+
'place_city_dot_z7',
33+
'place_capital_dot_z7',
34+
'roadname_minor',
35+
'roadname_sec',
36+
'roadname_pri',
37+
'roadname_major',
38+
]
339

440
export default function createLabelLayer() {
5-
return new TileLayer({
6-
source: new XYZ({
7-
url: 'https://{a-d}.basemaps.cartocdn.com/dark_only_labels/{z}/{x}/{y}@2x.png',
8-
maxZoom: 20,
9-
minZoom: 0,
10-
crossOrigin: 'anonymous',
11-
attributions:
12-
'&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, &copy; <a href="https://carto.com/attributions">CARTO</a>',
13-
}),
41+
const layer = new VectorTileLayer({ declutter: true })
42+
43+
applyStyle(layer, POSITRON_STYLE_URL, { layers: LABEL_AND_BOUNDARY_LAYERS }).then(() => {
44+
const originalStyleFn = layer.getStyleFunction()!
45+
layer.setStyle((feature: FeatureLike, resolution: number) => {
46+
const styles = originalStyleFn(feature, resolution)
47+
if (!styles) return styles
48+
const arr = Array.isArray(styles) ? styles : [styles]
49+
for (const style of arr) {
50+
const text = style.getText()
51+
if (text) {
52+
text.getFill()?.setColor('#ffffff')
53+
text.getStroke()?.setColor('#333333')
54+
}
55+
}
56+
return arr
57+
})
1458
})
59+
60+
return layer
1561
}

0 commit comments

Comments
 (0)