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.

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.tsimport { 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)));
}
} 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.
wms-capabilities.react.tsximport { useEffect, useState } from 'react';
import WMSCapabilities from 'ol/format/WMSCapabilities.js';
/** The result of parsing a WMS GetCapabilities response (the national orthophoto service) shown as JSON. */
export function WmsCapabilities() {
const [log, setLog] = useState('');
useEffect(() => {
const parser = new WMSCapabilities();
let current = true;
void fetch('assets/wms-capabilities.xml')
.then((response) => response.text())
.then((text) => {
if (current) setLog(JSON.stringify(parser.read(text), null, 2));
});
return () => {
current = false;
};
}, []);
return (
<pre>
<code>{log}</code>
</pre>
);
}How the React version works
The GetCapabilities document of the orthophoto service parsed by WMSCapabilities and shown as JSON. The example has no map.
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 |
|---|---|
a fetch in the constructor with PendingTasks | useEffect with a cancel flag |
<pre>{{ log() }}</pre> | <pre><code>{log}</code></pre> |
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
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
WMSCapabilitiesparses OGC WMS GetCapabilities XML into a plain nested JavaScript object printed withJSON.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.
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.