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.

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.tsimport 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">© 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.tsimport { 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;
} 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.
osm-vector-tiles.react.tsximport { useMemo } from 'react';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
Map,
useTileGrid,
VectorTileLayer,
VectorTileSource,
View,
} from 'cc-openlayers/react';
import TopoJSON from 'ol/format/TopoJSON.js';
import { attributionOptions, osmCredit, osmRules, osmTileUrl, tileGridOptions } from './osm-vector-tiles.shared';
/** Three TopoJSON object groups retain their own attributes and resolution-dependent styling. */
export function OsmVectorTilesReact() {
const format = useMemo(
() => new TopoJSON({ dataProjection: 'EPSG:3857', layerName: 'layer', layers: ['water', 'roads', 'buildings'] }),
[]
);
const grid = useTileGrid(tileGridOptions);
return (
<Map height="480px" ariaLabel="Layered OpenStreetMap 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" style={osmRules}>
<VectorTileSource
projection="EPSG:3857"
maxZoom={14}
wrapX={false}
tileUrlFunction={osmTileUrl}
attributions={osmCredit}
format={format}
tileGrid={grid}
/>
</VectorTileLayer>
</Map>
);
}osm-vector-tiles.shared.tsimport 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">© 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;How the React version works
A single canvas vector-tile layer reads three TopoJSON object groups: water, roads and buildings. The decoder exposes each object name as the layer attribute. Four ordered flat-style rules distinguish water, railways, classified roads and buildings shown below 10 projected map units per pixel. The original Nextzen service needs a key. This finite offline adaptation uses actual OpenFreeMap OpenMapTiles geometry around Rynek Główny in Kraków. It retains TopoJSON decoding rather than replacing the format with MVT. Road rules read real class attributes; the original numeric layer value is preserved as osm_layer. There is no claim of Nextzen schema or full cartographic parity.
What both versions share
Both versions use the same bounded URL function, flat-style rules, expanded attribution and 512-pixel XYZ source grid in EPSG:3857. Thirteen native cells at zooms 13 and 14 cover the fixture. The view starts at zoom 15, allows zooms 13 to 19 and overzooms existing source tiles. Outside the fixture the URL function returns undefined, so there are no requests for missing tiles. The empty background beyond the recorded area is intentional.
From Angular to React
| Angular | React |
|---|---|
A caller-owned TopoJSON instance projected through <aol-format-instance [format]> | A memoized TopoJSON instance passed as format |
<aol-tilegrid [tileSize]="512" [minZoom]="13" [maxZoom]="14"> | useTileGrid with the same native limits |
<aol-source-vectortile [tileUrlFunction]> | <VectorTileSource tileUrlFunction> |
Who creates, updates and disposes
The library creates and owns map, layer and tile-source instances. Angular's generic format adapter exposes the exact caller-owned format, announces replacement and lets the existing source adapter replace and dispose its old source. It never disposes the caller's format. React memoizes its decoder and grid; its existing source lifecycle handles replacement and StrictMode cleanup. The examples add no renderer or transport implementation.
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.
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.