Printing and export · Angular, React and OpenLayers
Export a georeferenced map image
Export a north-up OpenLayers map of Kraków as a plain PNG, a ZIP archive with an ESRI world file, or a georeferenced RGBA GeoTIFF in Angular.

What this example shows
Beneath an OpenStreetMap view of Kraków, a format selector lets you export the current viewport either as a standard PNG image, a ZIP bundle pairing the PNG with a georeferencing world file, or a self-contained RGBA GeoTIFF embedding EPSG coordinate metadata.
When you need it
Engineers, GIS analysts and surveyors exporting web map views into QGIS, ArcGIS Pro or AutoCAD Civil 3D need spatial georeferencing alongside the rendered bitmap so the image drops straight onto its real-world coordinates.
Offering both classic sidecar world files and single-file RGBA GeoTIFFs covers legacy CAD tools and modern desktop GIS packages alike.
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.
export-map.html<aol-map height="480px">
<aol-interaction-default /><aol-control-defaults />
<aol-view [zoom]="13"><aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" /></aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
</aol-map>
<div class="example-controls">
<label
>File format
<select name="format" [disabled]="exporting()" (change)="choose($event)">
@for (option of formats; track option.value) {
<option [value]="option.value" [selected]="option.value === format()">{{ option.label }}</option>
}
</select>
</label>
<button type="button" [disabled]="exporting()" (click)="exportMap()">Export map</button>
<p class="example-hint">Exports face north. For a PNG with a world file, select EPSG:3857 in your GIS.</p>
@if (error()) {
<p role="alert">{{ error() }}</p>
}
</div>export-map.shared.tsimport type OlMap from 'ol/Map.js';
import { exportRasterCanvas, type RasterExportOptions } from 'cc-openlayers/io';
import { MapRenderer } from 'cc-openlayers/print';
export type ExportFormat = RasterExportOptions['format'];
export const formats: readonly { value: ExportFormat; label: string }[] = [
{ value: 'png', label: 'PNG' },
{ value: 'png-world', label: 'PNG + world file (ZIP)' },
{ value: 'geotiff', label: 'GeoTIFF' },
];
/** Renders a north-up picture at screen resolution without moving the live map. */
export async function mapToRaster(map: OlMap, format: ExportFormat): Promise<Blob> {
const size = map.getSize();
const view = map.getView();
const center = view.getCenter();
const resolution = view.getResolution();
if (!size || !center || !resolution) throw new Error('The map is not ready.');
const { canvas, complete, failures } = await new MapRenderer(map).render({
size,
center,
resolution,
rotation: 0,
background: '#fff',
});
if (!complete || failures.length) throw new Error('The rendered map is incomplete.');
const halfWidth = (size[0] * resolution) / 2;
const halfHeight = (size[1] * resolution) / 2;
return exportRasterCanvas(canvas, {
format,
extent: [center[0] - halfWidth, center[1] - halfHeight, center[0] + halfWidth, center[1] + halfHeight],
epsg: Number(view.getProjection().getCode().split(':')[1]),
});
}
/** Keeps the object URL alive until the browser has started the download. */
export function downloadRaster(blob: Blob, format: ExportFormat): void {
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `map.${format === 'geotiff' ? 'tif' : format === 'png-world' ? 'zip' : 'png'}`;
try {
link.click();
} finally {
setTimeout(() => URL.revokeObjectURL(url));
}
}export-map.tsimport { ChangeDetectionStrategy, Component, inject, PendingTasks, signal, viewChild } from '@angular/core';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
ViewComponent,
} from 'cc-openlayers';
import { downloadRaster, formats, mapToRaster, type ExportFormat } from './export-map.shared';
/** Exports a north-up map of Kraków as PNG, a georeferenced PNG archive or GeoTIFF. */
@Component({
selector: 'app-export-map',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './export-map.html',
})
export class ExportMap {
protected readonly formats = formats;
protected readonly format = signal<ExportFormat>('png');
protected readonly exporting = signal(false);
protected readonly error = signal('');
private readonly map = viewChild.required(MapComponent);
private readonly pending = inject(PendingTasks);
protected choose(event: Event): void {
this.format.set((event.target as HTMLSelectElement).value as ExportFormat);
}
protected exportMap(): void {
if (this.exporting()) return;
this.exporting.set(true);
this.error.set('');
const format = this.format();
void this.pending.run(async () => {
try {
downloadRaster(await mapToRaster(this.map().instance, format), format);
} catch {
this.error.set('The map could not be exported. Check that every layer has loaded and try again.');
} finally {
this.exporting.set(false);
}
});
}
} 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.
export-map.react.tsximport { useRef, useState } from 'react';
import type OlMap from 'ol/Map.js';
import { Coordinate, DefaultControls, DefaultInteractions, Map, OSMSource, TileLayer, View } from 'cc-openlayers/react';
import { downloadRaster, formats, mapToRaster, type ExportFormat } from './export-map.shared';
/** Exports the same north-up raster formats through the shared encoder. */
export function ExportMap() {
const map = useRef<OlMap>(null);
const busy = useRef(false);
const [format, setFormat] = useState<ExportFormat>('png');
const [exporting, setExporting] = useState(false);
const [error, setError] = useState('');
const exportMap = async () => {
if (!map.current || busy.current) return;
busy.current = true;
setExporting(true);
setError('');
try {
downloadRaster(await mapToRaster(map.current, format), format);
} catch {
setError('The map could not be exported. Check that every layer has loaded and try again.');
} finally {
busy.current = false;
setExporting(false);
}
};
return (
<>
<Map ref={map} height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={13}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
</Map>
<div className="example-controls">
<label>
File format
<select
name="format"
value={format}
disabled={exporting}
onChange={(event) => setFormat(event.target.value as ExportFormat)}
>
{formats.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
</label>
<button type="button" disabled={exporting} onClick={exportMap}>
Export map
</button>
<p className="example-hint">Exports face north. For a PNG with a world file, select EPSG:3857 in your GIS.</p>
{error ? <p role="alert">{error}</p> : null}
</div>
</>
);
}export-map.shared.tsimport type OlMap from 'ol/Map.js';
import { exportRasterCanvas, type RasterExportOptions } from 'cc-openlayers/io';
import { MapRenderer } from 'cc-openlayers/print';
export type ExportFormat = RasterExportOptions['format'];
export const formats: readonly { value: ExportFormat; label: string }[] = [
{ value: 'png', label: 'PNG' },
{ value: 'png-world', label: 'PNG + world file (ZIP)' },
{ value: 'geotiff', label: 'GeoTIFF' },
];
/** Renders a north-up picture at screen resolution without moving the live map. */
export async function mapToRaster(map: OlMap, format: ExportFormat): Promise<Blob> {
const size = map.getSize();
const view = map.getView();
const center = view.getCenter();
const resolution = view.getResolution();
if (!size || !center || !resolution) throw new Error('The map is not ready.');
const { canvas, complete, failures } = await new MapRenderer(map).render({
size,
center,
resolution,
rotation: 0,
background: '#fff',
});
if (!complete || failures.length) throw new Error('The rendered map is incomplete.');
const halfWidth = (size[0] * resolution) / 2;
const halfHeight = (size[1] * resolution) / 2;
return exportRasterCanvas(canvas, {
format,
extent: [center[0] - halfWidth, center[1] - halfHeight, center[0] + halfWidth, center[1] + halfHeight],
epsg: Number(view.getProjection().getCode().split(':')[1]),
});
}
/** Keeps the object URL alive until the browser has started the download. */
export function downloadRaster(blob: Blob, format: ExportFormat): void {
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `map.${format === 'geotiff' ? 'tif' : format === 'png-world' ? 'zip' : 'png'}`;
try {
link.click();
} finally {
setTimeout(() => URL.revokeObjectURL(url));
}
}How the React version works
Download a map of Kraków as PNG, PNG with a world file, or GeoTIFF using a shared raster encoder in Angular and React.
What both versions share
Both versions share mapToRaster and downloadRaster. The print module renders a separate north-up map. The IO module encodes PNG, PNG with a world file in a ZIP, or an RGBA GeoTIFF. The world file places pixel centres and the GeoTIFF records the coordinate system. Neither adapter implements the encoders.
From Angular to React
| Angular | React |
|---|---|
| signals hold format, progress and error | useState holds format, progress and error |
| viewChild reads MapComponent.instance | useRef holds the OpenLayers map |
| PendingTasks.run tracks the asynchronous export | async click handler awaits the same shared export |
Who creates, updates and disposes
The adapters own the visible map. MapRenderer owns the temporary hidden map and disposes it after rendering. The export handler always clears its busy state, shows an error on failure, and schedules object-URL revocation after the browser has started the download.
Architecture and OpenLayers mechanics
Inside Angular PendingTasks (disabling the Export button while busy and surfacing clear error messages if any layer has not finished loading), the off-screen print renderer captures a clean north-up raster at screen resolution without disturbing the live interactive map—even if the user rotated the on-screen view.
Shared IO encoders package the pixels as PNG, PNG + world file ZIP, or EPSG-tagged RGBA GeoTIFF and schedule revocation of the temporary download object URL for the next task after the click.
How it works
- An off-screen print renderer captures a north-up snapshot at screen resolution while leaving the visible map untouched.
- Shared raster encoders output plain PNG, a ZIP containing PNG plus its world file, or an EPSG-tagged RGBA GeoTIFF.
- Incomplete tile loads reject cleanly with a status message, the button locks during export, and blob URLs are revoked afterward.
Pitfalls
- Georeferenced raster exports are rendered north-up even when the user has rotated the interactive view on screen.
- Sidecar world files store affine pixel size and the centre of the upper-left pixel without naming a CRS—select EPSG:3857 explicitly when importing the world-file PNG into desktop GIS (or choose GeoTIFF, which embeds EPSG tags).
- Every exported layer must come from a CORS-enabled server so canvas pixels remain untainted, and map data attributions must accompany published exports.
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.