Styling and labels · Angular, React and OpenLayers

Wind arrows from regular shapes

Visualize live Open-Meteo wind speed and direction across 16 Małopolska towns in OpenLayers by scaling and rotating two shared RegularShapes.

OpenStreetMap of Małopolska at zoom 8 with small black wind arrows at sixteen towns, most pointing east in a light westerly wind at the time of the screenshot.
A screenshot of the example. Weather data by Open-Meteo.com (CC BY 4.0). Map data © OpenStreetMap contributors.

What this example shows

Across Małopolska from Kraków and Tarnów to Zakopane at zoom 8 over OpenStreetMap, live surface wind observations fetched in a single batch call from Open-Meteo render at sixteen towns as oriented black wind arrows whose shaft length matches wind speed in m/s and whose arrowhead points downwind.

When you need it

Meteorological dashboards, air-quality dispersion monitors, maritime/ocean-current charts and drone weather briefings display vector fields where both bearing and magnitude must be read at a glance.

Constructing variable-length arrows from two shared RegularShape instances avoids shipping dozens of pre-drawn wind-barb images and updates dynamically with live API numbers.

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.

wind-arrows.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  LayerVectorComponent,
  MapComponent,
  SourceOsmComponent,
  SourceVectorComponent,
  ViewComponent,
} from 'cc-openlayers';
import Feature, { type FeatureLike } from 'ol/Feature';
import Point from 'ol/geom/Point';
import { fromLonLat } from 'ol/proj';
import Fill from 'ol/style/Fill';
import RegularShape from 'ol/style/RegularShape';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';

/** The towns of Małopolska whose current wind the example shows, with their longitude and latitude. */
const CITIES = [
  { name: 'Kraków', lon: 19.9368, lat: 50.0614 },
  { name: 'Tarnów', lon: 20.9886, lat: 50.0121 },
  { name: 'Nowy Sącz', lon: 20.6886, lat: 49.6249 },
  { name: 'Oświęcim', lon: 19.2097, lat: 50.0344 },
  { name: 'Chrzanów', lon: 19.4026, lat: 50.1355 },
  { name: 'Olkusz', lon: 19.5652, lat: 50.2813 },
  { name: 'Myślenice', lon: 19.9385, lat: 49.8336 },
  { name: 'Wieliczka', lon: 20.0645, lat: 49.9871 },
  { name: 'Bochnia', lon: 20.431, lat: 49.969 },
  { name: 'Wadowice', lon: 19.493, lat: 49.8833 },
  { name: 'Zakopane', lon: 19.9496, lat: 49.2992 },
  { name: 'Nowy Targ', lon: 20.032, lat: 49.4776 },
  { name: 'Gorlice', lon: 21.16, lat: 49.6547 },
  { name: 'Miechów', lon: 20.0098, lat: 50.357 },
  { name: 'Limanowa', lon: 20.422, lat: 49.713 },
  { name: 'Brzesko', lon: 20.607, lat: 49.969 },
];

const WIND_URL =
  'https://api.open-meteo.com/v1/forecast?current=wind_speed_10m,wind_direction_10m&wind_speed_unit=ms' +
  `&latitude=${CITIES.map((city) => city.lat).join(',')}&longitude=${CITIES.map((city) => city.lon).join(',')}`;

interface WindReport {
  current: { wind_speed_10m: number; wind_direction_10m: number };
}

/**
 * Loads the current wind of every city: one point per city with a `wind` property. The source adds the features the
 * promise resolves to, counts the request as loading meanwhile and fires `featuresloadend`.
 */
function loadWind(): Promise<Feature[]> {
  return fetch(WIND_URL)
    .then((response) => response.json() as Promise<WindReport[]>)
    .then((reports) => reports.map(windFeature));
}

/** The feature of one town with its current wind. */
function windFeature(report: WindReport, index: number): Feature {
  const { name, lon, lat } = CITIES[index];
  const feature = new Feature(new Point(fromLonLat([lon, lat])));
  feature.setProperties({
    name,
    wind: { speed: report.current.wind_speed_10m, deg: report.current.wind_direction_10m },
  });
  return feature;
}

const shaft = new RegularShape({
  points: 2,
  radius: 5,
  stroke: new Stroke({ width: 2, color: 'black' }),
  rotateWithView: true,
});

const head = new RegularShape({
  points: 3,
  radius: 5,
  fill: new Fill({ color: 'black' }),
  rotateWithView: true,
});

const styles = [new Style({ image: shaft }), new Style({ image: head })];

/** An arrow whose length follows the wind speed (m/s) and which points away from the direction it blows from. */
function windArrow(feature: FeatureLike): Style[] {
  const wind = feature.get('wind');
  const angle = ((wind.deg - 180) * Math.PI) / 180;
  const scale = wind.speed / 10;
  shaft.setScale([1, scale]);
  shaft.setRotation(angle);
  head.setDisplacement([0, head.getRadius() / 2 + shaft.getRadius() * scale]);
  head.setRotation(angle);
  return styles;
}

/** Arrows drawn by a style function at sixteen towns of Małopolska, sized and turned by the wind the Open-Meteo service reports. */
@Component({
  selector: 'app-wind-arrows',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    LayerVectorComponent,
    MapComponent,
    SourceOsmComponent,
    SourceVectorComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="8"><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-vector
          [loader]="loader"
          attributions='Weather data by <a href="https://open-meteo.com/">Open-Meteo</a>'
        />
      </aol-layer-vector>
    </aol-map>
  `,
})
export class WindArrows {
  protected readonly style = windArrow;
  protected readonly loader = loadWind;
}

Architecture and OpenLayers mechanics

A promise-returning vector loader fetches live wind speed (m/s) and meteorological wind-from bearing for all 16 coordinates in one Open-Meteo HTTP request, ensuring the source tracks active loading state and emits featuresloadend cleanly.

Inside the style function, just two shared RegularShape instances service all 16 towns: a 2-point 10-pixel shaft stretched longitudinally via setScale([1, scale]) (1 px per m/s) and a triangular arrowhead pushed to the shaft tip via setDisplacement—rotated by (direction - 180°) in radians so meteorological “from” bearings point in the downwind direction of travel.

How it works

  • A single batched Open-Meteo request fetches live wind speed (m/s) and direction across all sixteen Małopolska towns.
  • Returning the feature promise from the loader keeps source loading state accurate and fires featuresloadend on completion.
  • setScale([1, scale]) stretches the shared 2-point shaft shape proportionally to wind speed while setDisplacement positions the triangular head at its tip.
  • Subtracting 180° converts meteorological “wind coming from” degrees into downwind arrow direction.
  • Mutating two shared RegularShape styles per draw call renders all sixteen arrows with minimal memory overhead.

Pitfalls

  • Custom vector loaders must either return a Promise or invoke the success/failure callbacks—a fire-and-forget void loader leaves OpenLayers thinking the source is already idle before HTTP data arrives.
  • During calm weather (1–2 m/s), a 1:1 pixel-per-m/s scale produces shafts only 1–2 px long—apply a minimum base length or multiplier if arrows must remain prominent on calm days.

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.