Tile and image services · Angular, React and OpenLayers

A protected area's data from WMS tiles

Query Natura 2000 protected bird areas on click from a tiled GeoServer WMS in OpenLayers using TileWMS getFeatureInfoUrl and text/plain.

Natura 2000 bird areas around Kraków at zoom 9 over OpenStreetMap, and below the map the plain text answer for Puszcza Niepołomicka with its code PLB120002.
A screenshot of the example. Natura 2000 bird areas: Generalna Dyrekcja Ochrony Środowiska (GDOŚ). Map data © OpenStreetMap contributors.

What this example shows

Around Kraków at zoom 9, Natura 2000 Special Protection Areas for birds—such as Puszcza Niepołomicka—render as tiled WMS polygons over OpenStreetMap. Hovering over any shaded reserve turns the cursor into a hand, and clicking displays the reserve’s official name and European site code in a clean plain-text readout below the map.

When you need it

Environmental, geological and planning portals often prefer tiled WMS (TileWMS) over single-image WMS for fast browser caching while still needing accurate point attribute inspection on click.

OpenLayers’ TileWMS source exposes the exact same getFeatureInfoUrl signature as ImageWMS—translating map clicks into tile-local pixel indices automatically.

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-tile.ts
import { ChangeDetectionStrategy, Component, signal, viewChild } from '@angular/core';
import type MapBrowserEvent from 'ol/MapBrowserEvent';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  SourceTileWMSComponent,
  ViewComponent,
} from 'cc-openlayers';

/**
 * Natura 2000 bird areas around Kraków as a tiled WMS of the GeoServer of Poland's General Directorate for
 * Environmental Protection: a click asks the service about the area under it in plain text, and the pointer turns into
 * a hand over the areas' pixels.
 */
@Component({
  selector: 'app-getfeatureinfo-tile',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceOsmComponent,
    SourceTileWMSComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px" (singleClick)="query($event)" (pointerMove)="hover($event)">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="9"><aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" /></aol-view>
      <aol-layer-tile><aol-source-osm /></aol-layer-tile>
      <aol-layer-tile #areas>
        <aol-source-tilewms
          url="https://sdi.gdos.gov.pl/wms"
          [params]="{ LAYERS: 'GDOS:ObszarySpecjalnejOchrony', TILED: true }"
          crossOrigin="anonymous"
          attributions="Natura 2000: GDOŚ"
        />
      </aol-layer-tile>
    </aol-map>
    <div class="example-controls">
      <p class="example-hint">Click a bird area to see its data.</p>
      <pre>{{ info() }}</pre>
    </div>
  `,
})
export class GetfeatureinfoTile {
  readonly info = signal('');
  private readonly map = viewChild.required(MapComponent);
  private readonly view = viewChild.required(ViewComponent);
  private readonly areas = viewChild.required<LayerTileComponent>('areas');
  private readonly source = viewChild.required(SourceTileWMSComponent);

  protected query(event: MapBrowserEvent): void {
    this.info.set('');
    const url = this.source().instance.getFeatureInfoUrl(
      event.coordinate,
      this.view().instance.getResolution() as number,
      'EPSG:3857',
      { INFO_FORMAT: 'text/plain' }
    );
    if (url) {
      void fetch(url)
        .then((response) => response.text())
        .then((text) => this.info.set(text));
    }
  }

  protected hover(event: MapBrowserEvent): void {
    if (event.dragging) return;
    const data = this.areas().instance.getData(event.pixel) as ArrayLike<number> | null;
    const hit = !!data && data[3] > 0; // transparent pixels have zero for data[3]
    this.map().instance.getTargetElement().style.cursor = hit ? 'pointer' : '';
  }
}

Architecture and OpenLayers mechanics

When clicked, getFeatureInfoUrl on the tiled WMS source identifies the specific 256 × 256 tile covering that coordinate, computes the intra-tile column and row offsets (I and J in WMS 1.3.0), and requests INFO_FORMAT text/plain for display inside a pre block.

During (pointerMove), getData reads the RGBA tuple of the rendered tile pixel under the pointer, showing a hand cursor strictly where alpha is non-zero while attributions credits GDOŚ on the map.

How it works

  • getFeatureInfoUrl on the tiled WMS source resolves the 256-pixel tile under the click and calculates its internal I and J pixel coordinates.
  • Requesting INFO_FORMAT text/plain returns human-readable attribute lines rendered directly inside a pre element.
  • (pointerMove) checks tile pixel alpha via getData so empty transparent areas keep the default panning cursor.
  • attributions displays GDOŚ’s environmental data credit in the map corner.

Pitfalls

  • Inspecting tile pixel alpha with getData requires CORS-cleared images: omitting crossOrigin (or querying a non-CORS server) taints the canvas, makes getData return null, and prevents the hover cursor from ever activating.
  • Unlike GML or GeoJSON, text/plain formatting is completely vendor-specific—GeoServer prints attribute and value pairs whereas some MapServer setups output bare feature IDs, so verify each service’s supported formats in GetCapabilities.

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.