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.

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.tsimport { 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}`);
} 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.
layer-extent.react.tsximport { useState } from 'react';
import { getTopLeft, getWidth } from 'ol/extent';
import { transformExtent } from 'ol/proj';
import { EXTENT } from 'ol/proj/epsg3857';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
Map,
OSMSource,
TileLayer,
View,
WMTSSource,
useWMTSTileGrid,
} from 'cc-openlayers/react';
const size = getWidth(EXTENT) / 256;
const origin = getTopLeft(EXTENT);
const resolutions = Array.from({ length: 20 }, (_, z) => size / 2 ** z);
const matrixIds = Array.from({ length: 20 }, (_, z) => `EPSG:3857:${z}`);
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]),
};
const names = Object.keys(EXTENTS);
/** The national orthophoto over OpenStreetMap, limited to the extent of a town chosen with the buttons. */
export function LayerExtent() {
const [town, setTown] = useState('Kraków');
const tileGrid = useWMTSTileGrid({ origin, resolutions, matrixIds });
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={9}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<TileLayer extent={EXTENTS[town]}>
<WMTSSource
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}
tileGrid={tileGrid}
/>
</TileLayer>
</Map>
<div className="example-controls">
{names.map((name) => (
<button key={name} type="button" aria-pressed={town === name} onClick={() => setTown(name)}>
{name}
</button>
))}
</div>
</>
);
}How the React version works
The orthophoto is limited to the extent of one town of Lesser Poland (Kraków, Tarnów, Nowy Sącz, Zakopane), chosen with buttons; outside it OpenStreetMap shows.
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. Both versions compute the WMTS tile grid of GUGiK's orthophoto (origin, 20 resolutions, ids EPSG:3857:z) in the same way; in React the useWMTSTileGrid hook creates it once from module constants. The towns' extents are converted from EPSG:4326 once, when the module loads.
From Angular to React
| Angular | React |
|---|---|
@for over the names with (click)="town.set(name)" | names.map(...) with key and onClick |
[extent]="extents[town()]" | extent={EXTENTS[town]} |
<aol-source-tilewmts> with <aol-tilegrid-wmts> inside | <WMTSSource tileGrid={tileGrid}> with useWMTSTileGrid(...) |
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. The extents are module constants, so the same button gives the same array and the adapter does not set the extent again.
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’saol-layer-tilebinds reactively to the selected city’s projected bounding box.transformExtentconverts 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-pressedmarks the active town button for screen-reader users.
Pitfalls
- Passing raw degree coordinates into
[extent]on a Web Mercator map withouttransformExtentshrinks the allowed box to roughly 31 centimetres near 0°, 0° in the Gulf of Guinea—making the layer appear completely invisible over Poland. - Layer
extentalways clips to an axis-aligned rectangle in map coordinates; when imagery must follow an irregular administrative border, combineextentwith polygon canvas compositing as shown in layer-clipping-vector.
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.