Vector data · Angular, React and OpenLayers

Layered OpenStreetMap vector tiles

Decode local OpenMapTiles TopoJSON vector tiles around Kraków in OpenLayers and style water, roads, railways and buildings by resolution.

OpenStreetMap geometry around Rynek Główny in Kraków, with pale blue water, gray buildings and classified road and railway lines on a light background.
A screenshot of the example. OpenFreeMap; © OpenMapTiles; data from OpenStreetMap contributors, ODbL.

What this example shows

Zoomed onto Rynek Główny and central Kraków at zoom 15, an offline 512-pixel TopoJSON vector-tile pyramid renders pale-blue water bodies, grey building footprints, railways and classified street networks from real OpenFreeMap / OpenMapTiles data—smoothly overzooming native zoom-14 tiles all the way to zoom 19.

When you need it

Multi-theme vector tiles pack water, transport and building layers into one request per tile so a single VectorTileLayer can apply scale-dependent cartographic rules without multiple network round-trips.

Using TopoJSON tiles demonstrates how OpenLayers exposes named topology object groups as a feature property for unified flat styling while preserving underlying numeric OSM layer attributes.

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.

osm-vector-tiles.html
<aol-map height="480px" ariaLabel="Layered OpenStreetMap 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" [style]="rules">
    <aol-source-vectortile
      projection="EPSG:3857"
      [maxZoom]="14"
      [wrapX]="false"
      [tileUrlFunction]="tileUrl"
      [attributions]="credit"
    >
      <aol-format-instance [format]="format" />
      <aol-tilegrid [tileSize]="512" [minZoom]="13" [maxZoom]="14" />
    </aol-source-vectortile>
  </aol-layer-vectortile>
</aol-map>
osm-vector-tiles.scss
:host {
  display: block;
}
osm-vector-tiles.shared.ts
import type { Rule } from 'ol/style/flat.js';
import type { TileCoord } from 'ol/tilecoord.js';

const nativeCells = [
  { z: 13, minX: 4549, maxX: 4550, minY: 2775, maxY: 2776 },
  { z: 14, minX: 9098, maxX: 9100, minY: 5551, maxY: 5553 },
];

/** The local fixture is finite; returning undefined avoids requests outside its native matrices. */
export function osmTileUrl([z, x, y]: TileCoord): string | undefined {
  const bounds = nativeCells.find((level) => level.z === z);
  return bounds && x >= bounds.minX && x <= bounds.maxX && y >= bounds.minY && y <= bounds.maxY
    ? new URL(`assets/krakow/osm-topojson/${z}/${x}/${y}.topojson`, document.baseURI).href
    : undefined;
}

/** Preserve the upstream four-rule structure while reading the actual OpenMapTiles attributes. */
export const osmRules: Rule[] = [
  { filter: ['==', ['get', 'layer'], 'water'], style: { 'fill-color': '#9db9e8' } },
  {
    else: true,
    filter: ['all', ['==', ['get', 'layer'], 'roads'], ['==', ['get', 'class'], 'rail']],
    style: { 'stroke-color': '#7de', 'stroke-width': 1, 'z-index': ['number', ['get', 'osm_layer'], 0] },
  },
  {
    else: true,
    filter: ['==', ['get', 'layer'], 'roads'],
    style: {
      'stroke-color': [
        'match',
        ['get', 'class'],
        'primary',
        '#776',
        'secondary',
        '#776',
        'tertiary',
        '#776',
        'minor',
        '#ccb',
        'service',
        '#ccb',
        'path',
        '#ccb',
        'track',
        '#ccb',
        'motorway',
        '#f39',
        'trunk',
        '#f39',
        'none',
      ],
      'stroke-width': ['match', ['get', 'class'], 'motorway', 1.5, 'trunk', 1.5, 1],
      'z-index': ['number', ['get', 'osm_layer'], 0],
    },
  },
  {
    else: true,
    filter: ['all', ['==', ['get', 'layer'], 'buildings'], ['<', ['resolution'], 10]],
    style: {
      'fill-color': '#6666',
      'stroke-color': '#4446',
      'stroke-width': 1,
      'z-index': ['number', ['get', 'osm_layer'], 0],
    },
  },
];

export const osmCredit =
  '<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;
export const tileGridOptions = { tileSize: 512, minZoom: 13, maxZoom: 14 } as const;
osm-vector-tiles.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  FormatInstanceComponent,
  LayerVectorTileComponent,
  MapComponent,
  SourceVectorTileComponent,
  TileGridComponent,
  ViewComponent,
} from 'cc-openlayers';
import TopoJSON from 'ol/format/TopoJSON.js';
import { attributionOptions, osmCredit, osmRules, osmTileUrl } from './osm-vector-tiles.shared';

/** Three TopoJSON object groups retain their own attributes and resolution-dependent styling. */
@Component({
  selector: 'app-osm-vector-tiles',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    FormatInstanceComponent,
    LayerVectorTileComponent,
    MapComponent,
    SourceVectorTileComponent,
    TileGridComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  templateUrl: './osm-vector-tiles.html',
  styleUrl: './osm-vector-tiles.scss',
})
export class OsmVectorTiles {
  protected readonly format = new TopoJSON({
    dataProjection: 'EPSG:3857',
    layerName: 'layer',
    layers: ['water', 'roads', 'buildings'],
  });
  protected readonly rules = osmRules;
  protected readonly tileUrl = osmTileUrl;
  protected readonly credit = osmCredit;
  protected readonly attributionOptions = attributionOptions;
}

Architecture and OpenLayers mechanics

Adapted from Nextzen’s retired hosted service to a self-contained 13-tile local OpenMapTiles snapshot in projected EPSG:3857 metres (zooms 13 and 14 on a 512-pixel grid), the TopoJSON decoder reads water, roads and buildings—exposing each topology object’s group name as layer while preserving the original numeric road stacking attribute as osm_layer.

Four ordered flat-style rules paint water fills, railway strokes, road classes and building footprints (gated below 10 map units per pixel), while a URL guard skips requests outside the 13 bundled cells and overzooms native tiles cleanly up to zoom 19.

How it works

  • A 13-cell local TopoJSON extract of OpenFreeMap / OpenMapTiles data around Kraków demonstrates multi-group TopoJSON tile decoding without external API keys.
  • The decoder maps water, roads and buildings object names to layer while preserving OpenMapTiles road classes and numeric osm_layer stacking values.
  • Four ordered flat-style rules draw water fills, rail lines, classified streets and resolution-gated buildings (below 10 map units per pixel).
  • Configured on a 512-pixel EPSG:3857 grid for native zooms 13–14, the view opens at zoom 15 and overzooms existing tiles seamlessly through zoom 19.

Pitfalls

  • Replacing TopoJSON with binary MVT here would bypass the multi-object TopoJSON layerName decoding pattern this example specifically illustrates.
  • OpenMapTiles attribute schemas differ from legacy Nextzen tiles—style rules must reference actual class and osm_layer fields present in the data.
  • Because this fixture stores projected Web Mercator metres rather than WGS84 degrees, the TopoJSON format must declare EPSG:3857 explicitly.
  • Keep native source maxZoom (14) distinct from view maxZoom (19) so overzooming reuses loaded level-14 tiles instead of requesting missing level-15+ URLs.

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.