Styling and labels · Angular, React and OpenLayers

Street names along the streets

Render curved street names along Kraków Old Town road centrelines over Esri satellite imagery in OpenLayers using line placement and decluttering.

Satellite imagery of Kraków's Old Town with street names in white bold letters along the streets.
A screenshot of the example. Map data © OpenStreetMap contributors. Imagery: Esri World Imagery (Esri, Maxar, Earthstar Geographics and the GIS User Community).

What this example shows

Over high-resolution Esri aerial imagery of Kraków’s Old Town and Kazimierz (fenced with [minZoom]="14"), bold white street names curve character-by-character directly along twisting medieval lanes and riverside boulevards without ever rendering upside down.

When you need it

Satellite imagery, custom engineering plans and drone orthophotos carry no road names of their own, and horizontal point labels obscure adjacent buildings while failing to show which street corridor they name.

Curved vector line placement produces sharp, rotatable cartographic street lettering at any zoom level.

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.

street-labels.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import type { FeatureLike } from 'ol/Feature';
import { getCenter } from 'ol/extent';
import GeoJSON from 'ol/format/GeoJSON';
import { transformExtent } from 'ol/proj';
import Fill from 'ol/style/Fill';
import Style from 'ol/style/Style';
import Text from 'ol/style/Text';
import {
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  LayerVectorComponent,
  MapComponent,
  SourceImageTileComponent,
  SourceVectorComponent,
  ViewComponent,
} from 'cc-openlayers';

const style = new Style({
  text: new Text({
    // The generic family stays unquoted: in quotes, "sans-serif" would name a font of that name.
    font: 'bold 11px "Open Sans", "Arial Unicode MS", sans-serif',
    placement: 'line',
    fill: new Fill({ color: '#ffffff' }),
  }),
});

/** The street's name, written along its line. */
function streetName(feature: FeatureLike): Style {
  style.getText()?.setText(feature.get('name'));
  return style;
}

/** The Old Town, Kazimierz and the streets around them, in Web Mercator. */
const viewExtent = transformExtent([19.915, 50.046, 19.96, 50.07], 'EPSG:4326', 'EPSG:3857');

/**
 * The names of the streets of Kraków's Old Town and Kazimierz written along them (a text style with
 * `placement: 'line'`), decluttered, over Esri's World Imagery; the view stays within the streets' extent.
 */
@Component({
  selector: 'app-street-labels',
  imports: [
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    LayerVectorComponent,
    MapComponent,
    SourceImageTileComponent,
    SourceVectorComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [extent]="viewExtent" [center]="center" [zoom]="17" [minZoom]="14" />
      <aol-layer-tile>
        <aol-source-imagetile
          url="https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
          [maxZoom]="19"
          attributions="Tiles © Esri — Source: Esri, Maxar, Earthstar Geographics, and the GIS User Community"
        />
      </aol-layer-tile>
      <aol-layer-vector [declutter]="true" [style]="style">
        <aol-source-vector
          url="assets/krakow/old-town-streets.geojson"
          [format]="format"
          attributions='Streets © <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
        />
      </aol-layer-vector>
    </aol-map>
  `,
})
export class StreetLabels {
  protected readonly viewExtent = viewExtent;
  protected readonly center = getCenter(viewExtent);
  protected readonly format = new GeoJSON();
  protected readonly style = streetName;
}

Architecture and OpenLayers mechanics

Configuring the Text style with placement: 'line' instructs OpenLayers to lay the label along each LineString, drop the whole name as soon as two adjacent characters bend by more than maxAngle (45° by default), flip east-to-west geometry runs automatically so text always reads upright left-to-right, and omit names longer than the available segment.

A single shared Style instance is mutated per feature with the current street’s name for zero allocation overhead, while [declutter]="true" eliminates intersection collisions and view [extent] + [minZoom]="14" keep the camera over the street dataset.

How it works

  • placement: 'line' curves each street name character by character along the road geometry.
  • OpenLayers lays the name along the line and drops the whole label when adjacent characters bend past 45° or the segment is shorter than the text.
  • Lines digitised from east to west are reversed automatically during text layout so labels never appear upside down.
  • Reusing one Style object across all streets—updating only its text string per call—keeps rendering fast alongside [declutter]="true" and [minZoom]="14".

Pitfalls

  • Never wrap CSS generic font-family keywords such as sans-serif in quotes inside Canvas font strings: "sans-serif" looks for a literal font named sans-serif and falls back to browser default Times/serif when Open Sans is absent—keep generic fallback families unquoted.
  • OpenStreetMap splits long avenues into many short way segments wherever attributes or junctions change—merge contiguous segments by name in preprocessing if short blocks drop long street names.

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.