Projections · Angular, React and OpenLayers

Keep the scale when the projection changes

Preserve true ground scale at the map centre when switching OpenLayers views between WGS84 and Web Mercator using getPointResolution ratios.

OpenStreetMap of Central Europe in WGS 84 with Poland at the top, a scale bar of four steps to 500 km labelled 1 : 7,163,815 in the bottom left corner and the View projection select set to WGS 84 below the map.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Centred on Poland with a four-step 1 : N cartographic scale bar in the bottom-left corner, switching the View projection dropdown between EPSG:3857 (Web Mercator) and EPSG:4326 (WGS 84) keeps both the geographic centre and the ground coverage per pixel consistent across the transition.

When you need it

Naively copying view.getZoom() or raw view.getResolution() when switching between a metre-based projection and a degree-based projection causes jarring scale jumps.

Converting resolution through OpenLayers’ getPointResolution preserves the user’s visual working scale across any pair of coordinate systems.

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.

projection-and-scale.shared.ts
import { getPointResolution, get as getProjection, transform } from 'ol/proj.js';
import type View from 'ol/View.js';

/**
 * The resolution a view in the projection of the code should have to keep the scale of the given view (measured at its
 * centre); undefined when the code is unknown or the view has no centre or resolution yet.
 */
export function resolutionInProjection(view: View, code: string): number | undefined {
  const current = view.getProjection();
  const next = getProjection(code);
  const center = view.getCenter();
  const resolution = view.getResolution();
  if (!next || !center || resolution === undefined) return undefined;
  const newCenter = transform(center, current, next);
  const currentMetersPerUnit = current.getMetersPerUnit() ?? 1;
  const nextMetersPerUnit = next.getMetersPerUnit() ?? 1;
  const currentPointResolution =
    getPointResolution(current, 1 / currentMetersPerUnit, center, 'm') * currentMetersPerUnit;
  const nextPointResolution = getPointResolution(next, 1 / nextMetersPerUnit, newCenter, 'm') * nextMetersPerUnit;
  return (resolution * currentPointResolution) / nextPointResolution;
}
projection-and-scale.ts
import { ChangeDetectionStrategy, Component, signal, viewChild } from '@angular/core';
import {
  ControlScaleLineComponent,
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';
import { resolutionInProjection } from './projection-and-scale.shared';

/** A scale bar that keeps its scale when the view changes between Web Mercator and geographic coordinates. */
@Component({
  selector: 'app-projection-and-scale',
  imports: [
    ControlScaleLineComponent,
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceOsmComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-control-scaleline units="metric" [bar]="true" [steps]="4" [text]="true" [minWidth]="140" />
      <aol-view [zoom]="6" [projection]="projection()" [resolution]="resolution()">
        <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>
        View projection
        <select (change)="changeProjection($event)">
          <option value="EPSG:3857" [selected]="projection() === 'EPSG:3857'">Spherical Mercator (EPSG:3857)</option>
          <option value="EPSG:4326" [selected]="projection() === 'EPSG:4326'">WGS 84 (EPSG:4326)</option>
        </select>
      </label>
    </div>
  `,
})
export class ProjectionAndScale {
  readonly projection = signal('EPSG:4326');
  /** Set when the projection changes, so the new view keeps the scale of the old one; the initial zoom decides before. */
  readonly resolution = signal<number | undefined>(undefined);
  private readonly map = viewChild.required(MapComponent);

  protected changeProjection(event: Event): void {
    const code = (event.target as HTMLSelectElement).value;
    const resolution = resolutionInProjection(this.map().instance.getView(), code);
    if (resolution === undefined) return;
    this.resolution.set(resolution);
    this.projection.set(code);
  }
}

Architecture and OpenLayers mechanics

Inside changeProjection, the current view centre is projected via transform(center, current, next). Calling getPointResolution at the centre in both the old and new projections measures how many ground metres one map unit covers in each CRS, scaling the old resolution by their ratio to set resolution alongside projection (starting at [zoom]="6" before the first switch).

Configuring the scale control with [bar]="true", [steps]="4" and [text]="true" makes the preserved 1 : N ratio directly verifiable.

How it works

  • changeProjection reprojects the active centre via transform(center, current, next) when switching CRS.
  • getPointResolution measures ground metres per map unit at the centre in both projections so their ratio preserves ground scale.
  • Initial startup uses [zoom]="6", after which explicit resolution and projection signals build each replacement view.
  • Scale bar inputs [bar]="true", [steps]="4" and [text]="true" display the representative fraction before and after switching.

Pitfalls

  • In non-conformal projections such as EPSG:4326, east–west and north–south pixel ground lengths differ by ~36% at 50° N—OpenLayers’ metric scale computation averages both directions, so neither axis matches conformal EPSG:3857 identically.
  • Displayed 1 : N ratios assume the OGC standard 0.28 mm pixel pitch (~90.7 DPI) rather than querying physical monitor DPI.

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.