Vector data · Angular, React and OpenLayers
Classified Mapbox-format vector tiles
Style multi-layer Mapbox-format OpenMapTiles vector tiles around Kraków in OpenLayers with flat rules and decluttered neighbourhood labels.

What this example shows
Central Kraków opens at zoom 15 rendered entirely from a local 13-cell OpenFreeMap / OpenMapTiles MVT snapshot: green parks and land cover, pale building footprints, single-stroke roads and railways ordered by the numeric layer field, administrative borders and collision-decluttered Polish place and neighbourhood labels, overzooming smoothly to zoom 19.
When you need it
Self-hosted .pbf / .mvt basemaps pack a dozen thematic layers into one compact binary tile so OpenLayers can render crisp, rotatable street cartography at 60 fps without external style engines.
Writing native OpenLayers flat style rules directly against OpenMapTiles attributes keeps bundle size small and takes full advantage of OpenLayers’ fast RenderFeature MVT pipeline.
The code
The example uses cc-openlayers, my Angular component library for OpenLayers: each aol-… element creates the OpenLayers object it names, updates it from its inputs and disposes of it with the component.
mapbox-vector-tiles.html<aol-map height="480px" ariaLabel="Classified Mapbox-format tiles around Kraków">
<aol-interaction-default />
<aol-control-defaults [attributionOptions]="attributionOptions" />
<aol-view projection="EPSG:3857" [zoom]="15" [minZoom]="13" [maxZoom]="19">
<aol-coordinate [x]="19.9373" [y]="50.0617" srid="EPSG:4326" />
</aol-view>
<aol-layer-vectortile background="#f8f4f0" [declutter]="true" [style]="rules">
<aol-source-vectortile
projection="EPSG:3857"
[maxZoom]="14"
[wrapX]="false"
[tileUrlFunction]="tileUrl"
[attributions]="credit"
>
<aol-format-mvt layerName="source_layer" [layers]="layers" />
<aol-tilegrid [tileSize]="512" [minZoom]="13" [maxZoom]="14" />
</aol-source-vectortile>
</aol-layer-vectortile>
</aol-map>mapbox-vector-tiles.scss:host {
display: block;
}mapbox-vector-tiles.shared.tsimport type { Rule } from 'ol/style/flat.js';
import type { TileCoord } from 'ol/tilecoord.js';
import { grid } from '../../../assets/krakow/openmaptiles/metadata.json';
export const drawableLayers = [
'landcover',
'landuse',
'park',
'water',
'waterway',
'aeroway',
'building',
'transportation',
'boundary',
'place',
'water_name',
];
export const nativeGridOptions = { tileSize: grid.logicalTileSize, minZoom: grid.minZoom, maxZoom: grid.maxZoom };
/** Native XYZ cells are finite; view overzoom must never request absent source tiles. */
export function localMvtTileUrl([z, x, y]: TileCoord): string | undefined {
const level = grid.levels.find((candidate) => candidate.z === z);
return level && x >= level.minX && x <= level.maxX && y >= level.minY && y <= level.maxY
? new URL(`assets/krakow/openmaptiles/${z}/${x}/${y}.mvt`, document.baseURI).href
: undefined;
}
/** Flat rules use the replacement tiles' actual source groups, class values and numeric layer. */
export const cartographyRules: Rule[] = [
{
filter: ['==', ['get', 'source_layer'], 'landcover'],
style: {
'fill-color': [
'match',
['get', 'class'],
'wood',
'#e9eedf',
'grass',
'#d8e8c8',
'wetland',
'#cde2d7',
'farmland',
'#e9eedf',
'sand',
'#f2efdb',
'rock',
'#ddd',
'none',
],
'z-index': 0,
},
},
{
else: true,
filter: ['==', ['get', 'source_layer'], 'landuse'],
style: {
'fill-color': [
'match',
['get', 'class'],
'cemetery',
'#e0e4dd',
'hospital',
'#fde',
'school',
'#f0e8f8',
'kindergarten',
'#f0e8f8',
'university',
'#f0e8f8',
'education',
'#f0e8f8',
'pitch',
'#d8e8c8',
'playground',
'#d8e8c8',
'stadium',
'#d8e8c8',
'none',
],
'z-index': 1,
},
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'park'], ['==', ['geometry-type'], 'Polygon']],
style: { 'fill-color': '#d8e8c8', 'z-index': 2 },
},
{
else: true,
filter: ['==', ['get', 'source_layer'], 'water'],
style: { 'fill-color': '#a0c8f0', 'z-index': 3 },
},
{
else: true,
filter: ['==', ['get', 'source_layer'], 'waterway'],
style: { 'stroke-color': '#a0c8f0', 'stroke-width': 1, 'z-index': 4 },
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'aeroway'], ['==', ['geometry-type'], 'Polygon']],
style: { 'fill-color': '#f2efeb', 'z-index': 5 },
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'aeroway'], ['==', ['geometry-type'], 'LineString']],
style: { 'stroke-color': '#f0ede9', 'stroke-width': 1, 'z-index': 5 },
},
{
else: true,
filter: ['==', ['get', 'source_layer'], 'building'],
style: { 'fill-color': '#f2eae2', 'stroke-color': '#dfdbd7', 'stroke-width': 1, 'z-index': 6 },
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'transportation'], ['==', ['geometry-type'], 'Polygon']],
style: {
'fill-color': '#f0ede9',
'stroke-color': '#cfcdca',
'stroke-width': 1,
'z-index': ['+', 8, ['number', ['get', 'layer'], 0]],
},
},
{
else: true,
filter: [
'all',
['==', ['get', 'source_layer'], 'transportation'],
['==', ['geometry-type'], 'LineString'],
['match', ['get', 'class'], 'rail', true, 'transit', true, false],
],
style: {
'stroke-color': '#bbb',
'stroke-width': ['match', ['get', 'class'], 'rail', 2, 1],
'z-index': ['+', 20, ['number', ['get', 'layer'], 0]],
},
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'transportation'], ['==', ['geometry-type'], 'LineString']],
style: {
'stroke-color': [
'match',
['get', 'class'],
'primary',
'#e9ac77',
'secondary',
'#e9ac77',
'tertiary',
'#e9ac77',
'motorway',
'#e9ac77',
'trunk',
'#e9ac77',
'minor',
'#cfcdca',
'service',
'#cfcdca',
'path',
'#cba',
'track',
'#cba',
'none',
],
'stroke-width': 1,
'z-index': ['+', 10, ['number', ['get', 'layer'], 0]],
},
},
{
else: true,
filter: ['==', ['get', 'source_layer'], 'boundary'],
style: {
'stroke-color': ['case', ['==', ['get', 'maritime'], 1], '#a0c8f0', '#9e9cab'],
'stroke-width': 1,
'z-index': 30,
},
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'place'], ['==', ['get', 'class'], 'city']],
style: {
'text-value': ['get', 'name'],
'text-font': 'bold 11px sans-serif',
'text-fill-color': '#334',
'text-stroke-color': 'rgba(255,255,255,0.8)',
'text-stroke-width': 2,
'z-index': 40,
},
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'place'], ['<', ['resolution'], 10]],
style: {
'text-value': ['get', 'name'],
'text-font': 'bold 9px sans-serif',
'text-fill-color': '#633',
'text-stroke-color': 'rgba(255,255,255,0.8)',
'text-stroke-width': 1,
'z-index': 39,
},
},
{
else: true,
filter: ['==', ['get', 'source_layer'], 'water_name'],
style: {
'text-value': ['get', 'name'],
'text-font': 'italic 11px sans-serif',
'text-fill-color': '#74aee9',
'text-stroke-color': 'rgba(255,255,255,0.8)',
'text-stroke-width': 1,
'z-index': 38,
},
},
];
export const sourceCredit =
'<a href="https://openfreemap.org" target="_blank" rel="noopener">OpenFreeMap</a> <a href="https://www.openmaptiles.org/" target="_blank" rel="noopener">© OpenMapTiles</a> Data from <a href="https://www.openstreetmap.org/copyright" target="_blank" rel="noopener">OpenStreetMap</a>';
export const attributionOptions = { collapsible: false } as const;mapbox-vector-tiles.tsimport { ChangeDetectionStrategy, Component } from '@angular/core';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
FormatMVTComponent,
LayerVectorTileComponent,
MapComponent,
SourceVectorTileComponent,
TileGridComponent,
ViewComponent,
} from 'cc-openlayers';
import {
attributionOptions,
cartographyRules,
drawableLayers,
localMvtTileUrl,
sourceCredit,
} from './mapbox-vector-tiles.shared';
/** Real local MVT groups styled with flat expressions and decluttered place labels. */
@Component({
selector: 'app-mapbox-vector-tiles',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
FormatMVTComponent,
LayerVectorTileComponent,
MapComponent,
SourceVectorTileComponent,
TileGridComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './mapbox-vector-tiles.html',
styleUrl: './mapbox-vector-tiles.scss',
})
export class MapboxVectorTiles {
protected readonly rules = cartographyRules;
protected readonly layers = drawableLayers;
protected readonly tileUrl = localMvtTileUrl;
protected readonly credit = sourceCredit;
protected readonly attributionOptions = attributionOptions;
} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
mapbox-vector-tiles.react.tsximport {
Coordinate,
DefaultControls,
DefaultInteractions,
Map,
VectorTileLayer,
VectorTileSource,
View,
useMVTFormat,
useTileGrid,
} from 'cc-openlayers/react';
import {
attributionOptions,
cartographyRules,
drawableLayers,
localMvtTileUrl,
nativeGridOptions,
sourceCredit,
} from './mapbox-vector-tiles.shared';
const formatOptions = { layerName: 'source_layer', layers: drawableLayers };
/** Real local MVT groups styled with flat expressions and decluttered place labels. */
export function MapboxVectorTiles() {
const format = useMVTFormat(formatOptions);
const tileGrid = useTileGrid(nativeGridOptions);
return (
<Map height="480px" ariaLabel="Classified Mapbox-format tiles around Kraków">
<DefaultInteractions />
<DefaultControls attributionOptions={attributionOptions} />
<View projection="EPSG:3857" zoom={15} minZoom={13} maxZoom={19}>
<Coordinate x={19.9373} y={50.0617} srid="EPSG:4326" />
</View>
<VectorTileLayer background="#f8f4f0" declutter style={cartographyRules}>
<VectorTileSource
projection="EPSG:3857"
maxZoom={14}
wrapX={false}
tileGrid={tileGrid}
tileUrlFunction={localMvtTileUrl}
attributions={sourceCredit}
format={format}
/>
</VectorTileLayer>
</Map>
);
}mapbox-vector-tiles.shared.tsimport type { Rule } from 'ol/style/flat.js';
import type { TileCoord } from 'ol/tilecoord.js';
import { grid } from '../../../assets/krakow/openmaptiles/metadata.json';
export const drawableLayers = [
'landcover',
'landuse',
'park',
'water',
'waterway',
'aeroway',
'building',
'transportation',
'boundary',
'place',
'water_name',
];
export const nativeGridOptions = { tileSize: grid.logicalTileSize, minZoom: grid.minZoom, maxZoom: grid.maxZoom };
/** Native XYZ cells are finite; view overzoom must never request absent source tiles. */
export function localMvtTileUrl([z, x, y]: TileCoord): string | undefined {
const level = grid.levels.find((candidate) => candidate.z === z);
return level && x >= level.minX && x <= level.maxX && y >= level.minY && y <= level.maxY
? new URL(`assets/krakow/openmaptiles/${z}/${x}/${y}.mvt`, document.baseURI).href
: undefined;
}
/** Flat rules use the replacement tiles' actual source groups, class values and numeric layer. */
export const cartographyRules: Rule[] = [
{
filter: ['==', ['get', 'source_layer'], 'landcover'],
style: {
'fill-color': [
'match',
['get', 'class'],
'wood',
'#e9eedf',
'grass',
'#d8e8c8',
'wetland',
'#cde2d7',
'farmland',
'#e9eedf',
'sand',
'#f2efdb',
'rock',
'#ddd',
'none',
],
'z-index': 0,
},
},
{
else: true,
filter: ['==', ['get', 'source_layer'], 'landuse'],
style: {
'fill-color': [
'match',
['get', 'class'],
'cemetery',
'#e0e4dd',
'hospital',
'#fde',
'school',
'#f0e8f8',
'kindergarten',
'#f0e8f8',
'university',
'#f0e8f8',
'education',
'#f0e8f8',
'pitch',
'#d8e8c8',
'playground',
'#d8e8c8',
'stadium',
'#d8e8c8',
'none',
],
'z-index': 1,
},
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'park'], ['==', ['geometry-type'], 'Polygon']],
style: { 'fill-color': '#d8e8c8', 'z-index': 2 },
},
{
else: true,
filter: ['==', ['get', 'source_layer'], 'water'],
style: { 'fill-color': '#a0c8f0', 'z-index': 3 },
},
{
else: true,
filter: ['==', ['get', 'source_layer'], 'waterway'],
style: { 'stroke-color': '#a0c8f0', 'stroke-width': 1, 'z-index': 4 },
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'aeroway'], ['==', ['geometry-type'], 'Polygon']],
style: { 'fill-color': '#f2efeb', 'z-index': 5 },
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'aeroway'], ['==', ['geometry-type'], 'LineString']],
style: { 'stroke-color': '#f0ede9', 'stroke-width': 1, 'z-index': 5 },
},
{
else: true,
filter: ['==', ['get', 'source_layer'], 'building'],
style: { 'fill-color': '#f2eae2', 'stroke-color': '#dfdbd7', 'stroke-width': 1, 'z-index': 6 },
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'transportation'], ['==', ['geometry-type'], 'Polygon']],
style: {
'fill-color': '#f0ede9',
'stroke-color': '#cfcdca',
'stroke-width': 1,
'z-index': ['+', 8, ['number', ['get', 'layer'], 0]],
},
},
{
else: true,
filter: [
'all',
['==', ['get', 'source_layer'], 'transportation'],
['==', ['geometry-type'], 'LineString'],
['match', ['get', 'class'], 'rail', true, 'transit', true, false],
],
style: {
'stroke-color': '#bbb',
'stroke-width': ['match', ['get', 'class'], 'rail', 2, 1],
'z-index': ['+', 20, ['number', ['get', 'layer'], 0]],
},
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'transportation'], ['==', ['geometry-type'], 'LineString']],
style: {
'stroke-color': [
'match',
['get', 'class'],
'primary',
'#e9ac77',
'secondary',
'#e9ac77',
'tertiary',
'#e9ac77',
'motorway',
'#e9ac77',
'trunk',
'#e9ac77',
'minor',
'#cfcdca',
'service',
'#cfcdca',
'path',
'#cba',
'track',
'#cba',
'none',
],
'stroke-width': 1,
'z-index': ['+', 10, ['number', ['get', 'layer'], 0]],
},
},
{
else: true,
filter: ['==', ['get', 'source_layer'], 'boundary'],
style: {
'stroke-color': ['case', ['==', ['get', 'maritime'], 1], '#a0c8f0', '#9e9cab'],
'stroke-width': 1,
'z-index': 30,
},
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'place'], ['==', ['get', 'class'], 'city']],
style: {
'text-value': ['get', 'name'],
'text-font': 'bold 11px sans-serif',
'text-fill-color': '#334',
'text-stroke-color': 'rgba(255,255,255,0.8)',
'text-stroke-width': 2,
'z-index': 40,
},
},
{
else: true,
filter: ['all', ['==', ['get', 'source_layer'], 'place'], ['<', ['resolution'], 10]],
style: {
'text-value': ['get', 'name'],
'text-font': 'bold 9px sans-serif',
'text-fill-color': '#633',
'text-stroke-color': 'rgba(255,255,255,0.8)',
'text-stroke-width': 1,
'z-index': 39,
},
},
{
else: true,
filter: ['==', ['get', 'source_layer'], 'water_name'],
style: {
'text-value': ['get', 'name'],
'text-font': 'italic 11px sans-serif',
'text-fill-color': '#74aee9',
'text-stroke-color': 'rgba(255,255,255,0.8)',
'text-stroke-width': 1,
'z-index': 38,
},
},
];
export const sourceCredit =
'<a href="https://openfreemap.org" target="_blank" rel="noopener">OpenFreeMap</a> <a href="https://www.openmaptiles.org/" target="_blank" rel="noopener">© OpenMapTiles</a> Data from <a href="https://www.openstreetmap.org/copyright" target="_blank" rel="noopener">OpenStreetMap</a>';
export const attributionOptions = { collapsible: false } as const;How the React version works
A canvas vector-tile layer draws real Mapbox-format tiles around Rynek Główny in Kraków. Flat rules style land cover, land use, water, buildings, roads, railways and boundaries. Place labels use native names and one declutter group; neighbourhood labels appear only below 10 projected map units per pixel. The upstream example's account-bound Mapbox Streets source is replaced by a finite offline OpenFreeMap snapshot using the OpenMapTiles schema. The MVT format, source-group styling, fast RenderFeature decoding and label decluttering remain. This is adapted cartography, with no claim of original global Mapbox Streets coverage or schema parity.
What both versions share
Both versions share flat rules, the list of eleven source groups used by those rules, visible source credits and a URL guard derived from the fixture metadata. The full binary fixture preserves all sixteen original groups; POIs, house numbers and other unused groups are not decoded for this map. The decoder writes the source-group name into source_layer, preserving the original numeric layer for road draw order. The native source is a 512-pixel XYZ grid in EPSG:3857 at zooms 13 and 14. The view starts at zoom 15, allows zooms 13 through 19 and overzooms the final matrix. The URL function returns undefined beyond the recorded cells. Empty background outside that finite area is intentional.
From Angular to React
| Angular | React |
|---|---|
<aol-format-mvt layerName="source_layer" [layers]> | useMVTFormat with the same source-group name and selected layers |
A projected <aol-tilegrid> with explicit native limits | useTileGrid with the fixture's native grid declaration |
<aol-layer-vectortile [style] [declutter]="true"> | <VectorTileLayer style declutter> with the same flat rules |
Who creates, updates and disposes
The existing library map, layer, source, format and grid adapters own their normal OpenLayers lifecycle. They create the actual canvas vector-tile layer and MVT source; the application adds no source wrapper, renderer or transport. Removing the example detaches and disposes its source and layer. React uses the existing memoized format/grid hooks and releases its live map after StrictMode replay.
Architecture and OpenLayers mechanics
The MVT decoder filters incoming binary tiles down to the eleven OpenMapTiles groups used by the cartography and writes each group’s name into a custom source_layer attribute—preventing MVT’s default layerName (layer) from overwriting OpenMapTiles’ numeric bridge/tunnel layer road-stacking field.
A unified array of flat style rules evaluates source_layer, class, name and numeric layer attributes on a 512-pixel EPSG:3857 grid (native zooms 13–14 overzoomed through 19) with label decluttering enabled.
How it works
- An offline 13-tile OpenFreeMap MVT snapshot around Kraków replaces account-bound commercial tiles while keeping OpenLayers’ native MVT decoder, flat rules and label decluttering.
- Flat style expressions target real OpenMapTiles source_layer, class, name and numeric layer attributes rather than proprietary Mapbox Streets group names.
- Filtering the MVT decoder to the eleven styled groups keeps decoding on the fast RenderFeature path while ignoring unused POI and house-number groups.
- Numeric transport layer values control bridge/tunnel draw order, while resolution thresholds hide minor neighbourhood labels when zooming out.
- Native 512-pixel tiles at zooms 13–14 overzoom cleanly from initial zoom 15 up to zoom 19 without issuing out-of-bounds network requests.
Pitfalls
- MVT is an open protobuf specification—decoding
.mvt/ .pbf tiles in OpenLayers never requires a Mapbox account or token when using open OpenMapTiles / OpenFreeMap data. - Leaving MVT’s default layerName as
layerclobbers OpenMapTiles’ numericlayerelevation attribute on bridges and tunnels; remap the group name to source_layer. - Never paste Mapbox Streets style rules onto OpenMapTiles sources unedited—layer names and road class enums differ between schemas.
Related examples
The same feature in the OpenLayers examples .
Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.