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.

Central Kraków rendered from local MVT data, with green land cover, pale buildings, classified road and railway lines and decluttered place labels.
A screenshot of the example. OpenFreeMap; © OpenMapTiles; data from OpenStreetMap contributors, ODbL.

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.ts
import 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">&copy; 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.ts
import { 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;
}

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 layer clobbers OpenMapTiles’ numeric layer elevation 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.

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.