Tile and image services · Angular, React and OpenLayers

Read a WMS service's capabilities

Parse an OGC WMS GetCapabilities XML document into structured JavaScript JSON in OpenLayers using the built-in WMSCapabilities format parser.

The start of the parsed capabilities of GUGiK's orthophotomap WMS as JSON: version 1.3.0 and the service's name, Polish title, abstract and keywords.
A screenshot of the example. Capabilities: GUGiK's orthophotomap WMS.

What this example shows

Instead of hardcoding layer identifiers, this example parses the OGC GetCapabilities XML document of GUGiK’s national orthophotomap WMS and displays the resulting structured JavaScript object formatted as readable JSON—revealing service metadata, supported operations, image formats and the nested layer hierarchy.

When you need it

GIS catalog browsers, user-configurable “Add WMS URL” dialogs and automated service-health tools must discover available layer names, titles, bounding boxes and projections dynamically at runtime.

Using OpenLayers’ native capabilities parser handles OGC XML namespaces and parent-to-child metadata inheritance automatically without brittle custom DOM XML queries.

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.

wms-capabilities.ts
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import WMSCapabilities from 'ol/format/WMSCapabilities';

/** The result of parsing a WMS GetCapabilities response (the national orthophoto service) shown as JSON. */
@Component({
  selector: 'app-wms-capabilities',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `<pre><code>{{ log() }}</code></pre>`,
})
export class WmsCapabilities {
  readonly log = signal('');

  constructor() {
    const parser = new WMSCapabilities();
    void fetch('assets/wms-capabilities.xml')
      .then((response) => response.text())
      .then((text) => this.log.set(JSON.stringify(parser.read(text), null, 2)));
  }
}

Architecture and OpenLayers mechanics

Instantiating WMSCapabilities and reading the XML string converts the service’s metadata into a plain JavaScript tree serialised here via JSON.stringify. Following the OGC WMS specification, the parser propagates inherited properties—such as supported CRS lists and bounding boxes—from outer grouping nodes down into leaf layers.

Inspecting the layer tree also distinguishes non-drawable folder groups (which carry a Title only, such as Ortofotomapy dla Polski and ORTOFOTOMAPA) from actual requestable layers that possess a machine Name (here Raster).

How it works

  • WMSCapabilities parses OGC WMS GetCapabilities XML into a plain nested JavaScript object printed with JSON.stringify.
  • Only layer nodes carrying a Name property can be requested in GetMap calls; title-only parent nodes act purely as organisational groups.
  • Inherited attributes such as coordinate reference systems are copied automatically from parent layer groups onto child layers.

Pitfalls

  • Fetching GetCapabilities XML directly from a browser requires the remote WMS server to send permissive CORS headers, whereas bundled static XML snapshots avoid CORS issues at the cost of aging if upstream layers change.
  • Inspect URLs extracted from raw capability documents carefully—some servers double-encode query ampersands (such as percent-encoded ampersands (%26) in legend links) and need normalisation before use.

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.