Vector data · Angular, React and OpenLayers

Population symbols for Polish towns

Style Polish towns by population using shared OpenLayers flat expressions and inspect town names and counts in a point-anchored HTML overlay.

Circles of different sizes and colours mark Polish towns over OpenStreetMap.
A screenshot of the example. Towns and map data © OpenStreetMap contributors.

What this example shows

Over OpenStreetMap, Polish towns from a local GeoJSON dataset render as circles graduated in both radius and warm colour tone by population, while hovering or clicking snaps a crisp plain-text callout badge directly onto the town’s point coordinate (and URL state stays synced via the Link interaction).

When you need it

Settlement hierarchies, store-revenue maps and regional facility overviews communicate magnitude fastest through dual-encoded proportional circles (size + colour) paired with exact numeric tooltips on hover.

Sharing pure data-driven style expressions and plain-text formatters across Angular and React keeps multi-framework GIS codebases consistent and XSS-safe.

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.

populated-places.html
<aol-map height="480px" (pointerMove)="hover($event)" (olClick)="show($event)" (pointerleave)="hide()">
  <aol-interaction-default />
  <aol-control-defaults />
  <aol-interaction-link />
  <aol-view [zoom]="6"><aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" /></aol-view>
  <aol-layer-tile><aol-source-osm /></aol-layer-tile>
  <aol-layer-vector [style]="style"
    ><aol-source-geojson url="assets/poland/towns.geojson" featureProjection="EPSG:3857"
  /></aol-layer-vector>
  <aol-overlay positioning="bottom-center" [offset]="[0, -8]" [stopEvent]="false"
    ><aol-coordinate [x]="position()[0]" [y]="position()[1]" /><aol-content>
      @if (info()) {
        <span class="example-label">{{ info() }}</span>
      }
    </aol-content></aol-overlay
  >
</aol-map>
populated-places.shared.ts
import type { FeatureLike } from 'ol/Feature.js';
import type { FlatStyle } from 'ol/style/flat.js';

export const POPULATION_STYLE: FlatStyle = {
  'circle-stroke-color': '#ffffff',
  'circle-stroke-width': 0.75,
  'circle-radius': ['interpolate', ['linear'], ['get', 'population'], 1000, 3, 2000000, 14],
  'circle-fill-color': ['interpolate', ['linear'], ['get', 'population'], 1000, '#1976d2', 2000000, '#e64a19'],
};
export function townText(feature: FeatureLike): string {
  return `${feature.get('name')} — ${Number(feature.get('population')).toLocaleString('en-US')}`;
}
populated-places.ts
import { ChangeDetectionStrategy, Component, signal, viewChild } from '@angular/core';
import {
  ContentComponent,
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  LayerVectorComponent,
  LinkInteractionComponent,
  MapComponent,
  OverlayComponent,
  SourceGeoJSONComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';
import type { Coordinate } from 'ol/coordinate';
import type MapBrowserEvent from 'ol/MapBrowserEvent';
import Point from 'ol/geom/Point';
import { POPULATION_STYLE, townText } from './populated-places.shared';

@Component({
  selector: 'app-populated-places',
  imports: [
    ContentComponent,
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    LayerVectorComponent,
    LinkInteractionComponent,
    MapComponent,
    OverlayComponent,
    SourceGeoJSONComponent,
    SourceOsmComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  templateUrl: './populated-places.html',
})
export class PopulatedPlaces {
  protected readonly info = signal('');
  protected readonly position = signal<Coordinate>([0, 0]);
  protected readonly style = POPULATION_STYLE;
  private readonly map = viewChild.required(MapComponent);

  protected hover(event: MapBrowserEvent): void {
    if (event.dragging) this.hide();
    else this.show(event);
  }
  protected show(event: MapBrowserEvent): void {
    const target = event.originalEvent.target as Element | null;
    const feature = target?.closest('.ol-control')
      ? undefined
      : this.map().instance.forEachFeatureAtPixel(event.pixel, (hit) => hit);
    if (!feature) return this.hide();
    const geometry = feature.getGeometry();
    this.position.set(geometry instanceof Point ? geometry.getCoordinates() : event.coordinate);
    this.info.set(townText(feature));
  }
  protected hide(): void {
    this.info.set('');
  }
}

Architecture and OpenLayers mechanics

A local GeoJSON vector source loads town features over OpenStreetMap, styled via shared POPULATION_STYLE flat expressions. On pointer move (ignoring drags and .ol-control buttons) and clicks, forEachFeatureAtPixel resolves the hit town, anchors an aol-overlay to geometry.getCoordinates() when geometry instanceof Point, and renders townText(feature) as plain text.

How it works

  • Local GeoJSON town features render over OpenStreetMap using shared population-driven circle size and colour expressions.
  • Both Angular and React implementations reuse identical style rules and plain-text label formatting.
  • Pointer hover and clicks anchor an overlay badge onto the matched town point while drags and pointer-leave hide it.

Pitfalls

  • Bundled town populations represent a static demonstration snapshot rather than a live statistical GUS feed.
  • Rendering overlay text via plain text binding rather than unsanitized HTML prevents malformed feature names from injecting markup.

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.