Tile and image services · Angular, React and OpenLayers
A WMTS layer from its capabilities
Create an OpenLayers WMTS layer automatically from parsed GetCapabilities XML using optionsFromCapabilities over OpenStreetMap in Kraków.

What this example shows
Centred on Wawel Castle and the Vistula bend at zoom 15, GUGiK’s aerial orthophotomap renders at 70% opacity over OpenStreetMap with zero hardcoded resolutions, origins or matrix IDs—deriving its entire WMTS source configuration automatically from the service’s GetCapabilities document.
When you need it
Reading WMTS configuration directly from capabilities is the safest production pattern whenever an external geoportal may add zoom levels, adjust matrix bounds or switch between KVP and RESTful URL encodings.
Your code specifies only the desired layer identifier and matrix set/projection, while OpenLayers computes the exact WMTSTileGrid, URLs, format and style.
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.
wmts-layer-from-capabilities.tsimport { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import WMTSCapabilities from 'ol/format/WMTSCapabilities';
import { optionsFromCapabilities, type Options } from 'ol/source/WMTS';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
SourceTileWMTSComponent,
ViewComponent,
} from 'cc-openlayers';
/**
* A WMTS layer whose source options come from a stored GetCapabilities response (the national orthophoto), at 70 %
* opacity over the street map around Wawel; the attribution is given by hand, as the options read from the
* capabilities carry none.
*/
@Component({
selector: 'app-wmts-layer-from-capabilities',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
SourceTileWMTSComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-view [zoom]="15">
<aol-coordinate [x]="19.9355" [y]="50.054" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
@if (options(); as wmts) {
<aol-layer-tile [opacity]="0.7">
<aol-source-tilewmts
[urls]="wmts.urls"
[layer]="wmts.layer"
[matrixSet]="wmts.matrixSet"
[format]="wmts.format"
[projection]="wmts.projection"
[requestEncoding]="wmts.requestEncoding"
[style]="wmts.style"
[tileGrid]="wmts.tileGrid"
[wrapX]="wmts.wrapX"
attributions='Tiles © <a href="https://www.geoportal.gov.pl/" target="_blank">GUGiK</a>'
/>
</aol-layer-tile>
}
</aol-map>
`,
})
export class WmtsLayerFromCapabilities {
readonly options = signal<Options | undefined>(undefined);
constructor() {
const parser = new WMTSCapabilities();
void fetch('assets/wmts-capabilities.xml')
.then((response) => response.text())
.then((text) => {
const options = optionsFromCapabilities(parser.read(text), {
layer: 'ORTOFOTOMAPA',
matrixSet: 'EPSG:3857',
});
if (options) this.options.set(options);
});
}
} 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.
wmts-layer-from-capabilities.react.tsximport { useEffect, useState } from 'react';
import WMTSCapabilities from 'ol/format/WMTSCapabilities.js';
import { optionsFromCapabilities, type Options } from 'ol/source/WMTS.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
Map,
OSMSource,
TileLayer,
View,
WMTSSource,
} from 'cc-openlayers/react';
/**
* A WMTS layer whose source options come from a stored GetCapabilities response (the national orthophoto), at 70 %
* opacity over the street map around Wawel; the attribution is given by hand, as the options read from the
* capabilities carry none.
*/
export function WmtsLayerFromCapabilities() {
const [options, setOptions] = useState<Options | undefined>(undefined);
useEffect(() => {
let current = true;
const parser = new WMTSCapabilities();
void fetch('assets/wmts-capabilities.xml')
.then((response) => response.text())
.then((text) => {
const wmts = optionsFromCapabilities(parser.read(text), {
layer: 'ORTOFOTOMAPA',
matrixSet: 'EPSG:3857',
});
if (current && wmts) setOptions(wmts);
});
return () => {
current = false;
};
}, []);
return (
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={15}>
<Coordinate x={19.9355} y={50.054} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
{options && (
<TileLayer opacity={0.7}>
<WMTSSource
urls={options.urls}
layer={options.layer}
matrixSet={options.matrixSet}
format={options.format}
projection={options.projection}
requestEncoding={options.requestEncoding}
style={options.style}
tileGrid={options.tileGrid}
wrapX={options.wrapX}
attributions='Tiles © <a href="https://www.geoportal.gov.pl/" target="_blank">GUGiK</a>'
/>
</TileLayer>
)}
</Map>
);
}How the React version works
optionsFromCapabilities builds the WMTS source options from a capabilities document; until then only OpenStreetMap shows.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection.
From Angular to React
| Angular | React |
|---|---|
@if (options()) around the layer | {options && <TileLayer …>} |
the source's [options] | the options spread as WMTSSource props |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular. Fetching the document lives in a useEffect with a flag that ignores an answer after unmount; under StrictMode the effect runs twice, and the flag drops the first fetch.
Architecture and OpenLayers mechanics
After parsing the XML with WMTSCapabilities, calling OpenLayers’ optionsFromCapabilities helper for layer ORTOFOTOMAPA and matrixSet EPSG:3857 extracts a complete options object—including URLs, format, style, request encoding, wrapX and a ready-built WMTSTileGrid.
Gating the layer behind @if waits until those asynchronous options resolve so aol-source-tilewmts mounts cleanly with its complete configuration in one pass.
How it works
optionsFromCapabilitieslocatesORTOFOTOMAPAand itsEPSG:3857matrix set in the parsed capabilities and generates full source options including the tile grid.- Resolved properties bind directly onto
aol-source-tilewmts(URLs, layer, matrix set, format, projection, request encoding, style, tile grid and wrapX). - Wrapping the layer in
@ifdefers creation until capabilities parsing completes so the source initialises atomically. - Rendering at 70% opacity blends crisp aerial roofs with OpenStreetMap street names underneath.
Pitfalls
optionsFromCapabilitiesdoes not copy ServiceProvider metadata into source credits—passattributionsexplicitly so required legal notices appear on the map.- If you pass a
matrixSetname that the layer does not advertise,optionsFromCapabilitiessilently falls back to the layer’s first listed matrix set (here EPSG:2180), returning null only if that CRS is unregistered in OpenLayers.
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.