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.

Kraków's Old Town at zoom 15 with three restaurants in red and two hotels in blue, each named on the map, and below it the counts of all, hotel and restaurant features.
A screenshot of the example. Restaurants and hotels: a sample GetFeatureInfo answer. Map data © OpenStreetMap contributors.

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

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

  • WMSGetFeatureInfo parses GML GetFeatureInfo payloads; calling readFeatures without 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 layers relies 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.
  • WMSGetFeatureInfo parses GML payloads only—use OpenLayers’ GeoJSON parser instead when querying GeoServer endpoints with INFO_FORMAT=application/json.

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.