Tile and image services · Angular, React and OpenLayers
Features of one WMS answer split by layer
Parse multi-layer GML WMS GetFeatureInfo responses in OpenLayers with WMSGetFeatureInfo and split features by layer name into vector layers.

What this example shows
Over Kraków’s Old Town at zoom 15, a multi-layer WMS GetFeatureInfo response containing five points of interest is parsed into typed OpenLayers vector features, counted both in total and per category below the map, and mapped onto separate colour-coded vector layers—three restaurants in red and two hotels in blue, each labelled on the canvas.
When you need it
Single-click inspection on a multi-theme municipal WMS frequently queries parcels, buildings, utilities and POIs in a single HTTP round-trip.
Parsing the structured GML payload on the client—and splitting features by source layer name—lets your UI populate separate tabbed tables and apply distinct highlight styles per category.
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.
getfeatureinfo-layers.tsimport { ChangeDetectionStrategy, Component, signal, viewChild } from '@angular/core';
import type { FeatureLike } from 'ol/Feature';
import WMSGetFeatureInfo from 'ol/format/WMSGetFeatureInfo';
import CircleStyle from 'ol/style/Circle';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
import Text from 'ol/style/Text';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
} from 'cc-openlayers';
/** A point in the layer's colour, labelled with the feature's name. */
function labelledPoint(color: string) {
return (feature: FeatureLike) =>
new Style({
image: new CircleStyle({
radius: 7,
fill: new Fill({ color }),
stroke: new Stroke({ color: 'white', width: 2 }),
}),
text: new Text({
text: feature.get('name') as string,
offsetY: -16,
font: '600 13px sans-serif',
fill: new Fill({ color: '#1f2933' }),
stroke: new Stroke({ color: 'white', width: 3 }),
}),
});
}
/**
* The features of one GetFeatureInfo response counted for all layers and, with the format's `layers` option, per
* layer: a MapServer answer about the restaurants and hotels of Kraków's Old Town, each layer drawn on the map.
*/
@Component({
selector: 'app-getfeatureinfo-layers',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-view [zoom]="15">
<aol-coordinate [x]="19.937" [y]="50.0589" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
<aol-layer-vector [style]="restaurantStyle"><aol-source-vector #restaurants /></aol-layer-vector>
<aol-layer-vector [style]="hotelStyle"><aol-source-vector #hotels /></aol-layer-vector>
</aol-map>
<div class="example-controls">
<span>
All features <output>{{ all() }}</output>
</span>
<span>
Hotel features <output>{{ hotel() }}</output>
</span>
<span>
Restaurant features <output>{{ restaurant() }}</output>
</span>
</div>
`,
})
export class GetfeatureinfoLayers {
readonly all = signal('–');
readonly hotel = signal('–');
readonly restaurant = signal('–');
protected readonly restaurantStyle = labelledPoint('#c0392b');
protected readonly hotelStyle = labelledPoint('#2563eb');
private readonly restaurants = viewChild.required<SourceVectorComponent>('restaurants');
private readonly hotels = viewChild.required<SourceVectorComponent>('hotels');
constructor() {
void fetch('assets/wmsgetfeatureinfo/restaurant-hotel.xml')
.then((response) => response.text())
.then((response) => {
// the standard way to read the features
this.all.set(String(new WMSGetFeatureInfo().readFeatures(response).length));
// with the `layers` option the format returns only the features of those layers
const hotels = new WMSGetFeatureInfo({ layers: ['hotel'] }).readFeatures(response);
const restaurants = new WMSGetFeatureInfo({ layers: ['restaurant'] }).readFeatures(response);
this.hotel.set(String(hotels.length));
this.restaurant.set(String(restaurants.length));
// the answer was asked for in the map's projection, so the features need no transformation
this.hotels().instance.addFeatures(hotels);
this.restaurants().instance.addFeatures(restaurants);
});
}
} 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.
getfeatureinfo-layers.react.tsximport { useEffect, useRef, useState } from 'react';
import type { FeatureLike } from 'ol/Feature.js';
import WMSGetFeatureInfo from 'ol/format/WMSGetFeatureInfo.js';
import type OLVectorSource from 'ol/source/Vector.js';
import CircleStyle from 'ol/style/Circle.js';
import Fill from 'ol/style/Fill.js';
import Stroke from 'ol/style/Stroke.js';
import Style from 'ol/style/Style.js';
import Text from 'ol/style/Text.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
Map,
OSMSource,
TileLayer,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
/** A point in the layer's colour, labelled with the feature's name. */
function labelledPoint(color: string) {
return (feature: FeatureLike) =>
new Style({
image: new CircleStyle({
radius: 7,
fill: new Fill({ color }),
stroke: new Stroke({ color: 'white', width: 2 }),
}),
text: new Text({
text: feature.get('name') as string,
offsetY: -16,
font: '600 13px sans-serif',
fill: new Fill({ color: '#1f2933' }),
stroke: new Stroke({ color: 'white', width: 3 }),
}),
});
}
const restaurantStyle = labelledPoint('#c0392b');
const hotelStyle = labelledPoint('#2563eb');
/**
* The features of one GetFeatureInfo response counted for all layers and, with the format's `layers` option, per
* layer: a MapServer answer about the restaurants and hotels of Kraków's Old Town, each layer drawn on the map.
*/
export function GetfeatureinfoLayers() {
const [all, setAll] = useState('–');
const [hotel, setHotel] = useState('–');
const [restaurant, setRestaurant] = useState('–');
const restaurants = useRef<OLVectorSource>(null);
const hotels = useRef<OLVectorSource>(null);
useEffect(() => {
let current = true;
void fetch('assets/wmsgetfeatureinfo/restaurant-hotel.xml')
.then((response) => response.text())
.then((response) => {
if (!current) return;
// the standard way to read the features
setAll(String(new WMSGetFeatureInfo().readFeatures(response).length));
// with the `layers` option the format returns only the features of those layers
const hotelFeatures = new WMSGetFeatureInfo({ layers: ['hotel'] }).readFeatures(response);
const restaurantFeatures = new WMSGetFeatureInfo({ layers: ['restaurant'] }).readFeatures(response);
setHotel(String(hotelFeatures.length));
setRestaurant(String(restaurantFeatures.length));
// the answer was asked for in the map's projection, so the features need no transformation
hotels.current?.addFeatures(hotelFeatures);
restaurants.current?.addFeatures(restaurantFeatures);
});
return () => {
current = false;
};
}, []);
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={15}>
<Coordinate x={19.937} y={50.0589} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<VectorLayer style={restaurantStyle}>
<VectorSource ref={restaurants} />
</VectorLayer>
<VectorLayer style={hotelStyle}>
<VectorSource ref={hotels} />
</VectorLayer>
</Map>
<div className="example-controls">
<span>
All features <output>{all}</output>
</span>
<span>
Hotel features <output>{hotel}</output>
</span>
<span>
Restaurant features <output>{restaurant}</output>
</span>
</div>
</>
);
}How the React version works
The WMSGetFeatureInfo format reads one answer three ways: all features and, separately, the hotel and restaurant layers; the features go onto a map of the Old Town.
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 and addFeatures in afterNextRender | useEffect with a flag and refs to the sources |
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
OpenLayers’ WMSGetFeatureInfo format parses MapServer-style GML responses where each queried server layer wraps its hits inside a distinct XML element. Calling readFeatures with no filter returns all five features together, whereas passing the layers option (['hotel'] or ['restaurant']) extracts strictly the features belonging to that named layer.
Because the query already matches the map’s projection, each filtered array drops straight into its own vector source and styled layer without coordinate transformation.
How it works
WMSGetFeatureInfoparses GML GetFeatureInfo payloads; callingreadFeatureswithout options returns all five features across every queried layer.- Passing
layers(['hotel']or['restaurant']) isolates features per server layer—yielding two hotels and three restaurants. - Separate vector sources and styles render each category in its own colour with its name label above the point.
- Matching projections between the WMS query and map view lets parsed geometries load directly without reprojection.
Pitfalls
- Per-layer filtering via
layersrelies on each server layer using its own GML element name (as MapServer emits); servers that merge all hits into one generic GML FeatureCollection (such as certain GeoServer configurations) return the combined list unfiltered. WMSGetFeatureInfoparses GML payloads only—use OpenLayers’ GeoJSON parser instead when querying GeoServer endpoints with INFO_FORMAT=application/json.
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.