Layers · Angular, React and OpenLayers

Limit a layer to an extent

Restrict an OpenLayers tile layer to a municipal bounding box with transformExtent so orthophoto tiles render and download only where needed.

A street map of Małopolska at zoom 9 with the orthophotomap shown only inside the rectangle around Kraków, and the buttons of the four towns below the map.
A screenshot of the example. Orthophotomap © GUGiK. Map data © OpenStreetMap contributors.

What this example shows

Across a regional OpenStreetMap view of Małopolska at zoom 9, GUGiK’s high-resolution aerial orthophotomap is confined strictly to a crisp rectangular window over Kraków. Clicking the Tarnów, Nowy Sącz or Zakopane buttons below the map moves that active imagery window immediately to the chosen city while leaving the surrounding street map untouched.

When you need it

High-resolution drone surveys, licensed commercial orthophotos and municipal raster datasets often cover specific cities or project corridors within a much larger regional map.

Setting a geographic extent on the overlay layer stops OpenLayers from requesting out-of-bounds tiles (preventing 404 traffic or wasted tile quota) and clips edge tiles cleanly along the boundary rectangle.

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.

layer-extent.ts
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  SourceTileWMTSComponent,
  TileGridWMTSComponent,
  ViewComponent,
} from 'cc-openlayers';
import { getTopLeft, getWidth } from 'ol/extent';
import { transformExtent } from 'ol/proj';
import { EXTENT } from 'ol/proj/epsg3857';

const size = getWidth(EXTENT) / 256;

function transform(extent: number[]): number[] {
  return transformExtent(extent, 'EPSG:4326', 'EPSG:3857');
}

/** Approximate bounds of towns of Małopolska. */
const EXTENTS: Record<string, number[]> = {
  Kraków: transform([19.78, 49.96, 20.09, 50.13]),
  Tarnów: transform([20.88, 49.96, 21.1, 50.06]),
  'Nowy Sącz': transform([20.6, 49.57, 20.78, 49.67]),
  Zakopane: transform([19.9, 49.24, 20.05, 49.33]),
};

/** The national orthophoto over OpenStreetMap, limited to the extent of a town chosen with the buttons. */
@Component({
  selector: 'app-layer-extent',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceOsmComponent,
    SourceTileWMTSComponent,
    TileGridWMTSComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <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 [extent]="extents[town()]">
        <aol-source-tilewmts
          attributions='Tiles © <a href="https://www.geoportal.gov.pl/" target="_blank">GUGiK</a>'
          url="https://mapy.geoportal.gov.pl/wss/service/PZGIK/ORTO/WMTS/StandardResolution"
          layer="ORTOFOTOMAPA"
          matrixSet="EPSG:3857"
          format="image/jpeg"
          projection="EPSG:3857"
          [style]="'default'"
          [wrapX]="true"
        >
          <aol-tilegrid-wmts [origin]="origin" [resolutions]="resolutions" [matrixIds]="matrixIds" />
        </aol-source-tilewmts>
      </aol-layer-tile>
    </aol-map>
    <div class="example-controls">
      @for (name of names; track name) {
        <button type="button" [attr.aria-pressed]="town() === name" (click)="town.set(name)">{{ name }}</button>
      }
    </div>
  `,
})
export class LayerExtent {
  readonly town = signal('Kraków');
  protected readonly extents = EXTENTS;
  protected readonly names = Object.keys(EXTENTS);
  protected readonly origin = getTopLeft(EXTENT);
  protected readonly resolutions = Array.from({ length: 20 }, (_, z) => size / 2 ** z);
  protected readonly matrixIds = Array.from({ length: 20 }, (_, z) => `EPSG:3857:${z}`);
}

Architecture and OpenLayers mechanics

Each city preset stores geographic [west, south, east, north] bounds in readable WGS84 degrees and converts them into projected Web Mercator metres via transformExtent. Binding that projected tuple to [extent] on the orthophotomap’s aol-layer-tile restricts both tile-pyramid scheduling and canvas drawing to the target box.

Buttons expose aria-pressed so assistive technologies announce which city’s imagery window is currently active.

How it works

  • [extent] on the orthophotomap’s aol-layer-tile binds reactively to the selected city’s projected bounding box.
  • transformExtent converts WGS84 degree coordinates into the map view’s Web Mercator metres expected by layer extents.
  • OpenLayers skips network tile requests completely outside the configured layer extent, never downloading imagery for unselected towns.
  • aria-pressed marks the active town button for screen-reader users.

Pitfalls

  • Passing raw degree coordinates into [extent] on a Web Mercator map without transformExtent shrinks the allowed box to roughly 31 centimetres near 0°, 0° in the Gulf of Guinea—making the layer appear completely invisible over Poland.
  • Layer extent always clips to an axis-aligned rectangle in map coordinates; when imagery must follow an irregular administrative border, combine extent with polygon canvas compositing as shown in layer-clipping-vector.

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.