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.

Wawel and the Vistula at zoom 15 in GUGiK's orthophotomap at 70 % opacity over OpenStreetMap, whose streets and names show through the photo.
A screenshot of the example. Orthophotomap © GUGiK. Map data © OpenStreetMap contributors.

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.ts
import { 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);
      });
  }
}

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

  • optionsFromCapabilities locates ORTOFOTOMAPA and its EPSG:3857 matrix 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 @if defers creation until capabilities parsing completes so the source initialises atomically.
  • Rendering at 70% opacity blends crisp aerial roofs with OpenStreetMap street names underneath.

Pitfalls

  • optionsFromCapabilities does not copy ServiceProvider metadata into source credits—pass attributions explicitly so required legal notices appear on the map.
  • If you pass a matrixSet name that the layer does not advertise, optionsFromCapabilities silently falls back to the layer’s first listed matrix set (here EPSG:2180), returning null only if that CRS is unregistered in OpenLayers.

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.