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.

OpenStreetMap of Kraków with PNG, PNG plus world file and GeoTIFF export options below.
A screenshot of the example. Map data © OpenStreetMap contributors.

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

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.

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.