Raster, GeoTIFF and WebGL · Angular, React and OpenLayers

Point styles edited as JSON

Render 1,027 Polish towns on the GPU in OpenLayers and edit live JSON flat point styles—circles, icons and time-animated bars—with hover state.

OpenStreetMap of southern Poland with the towns as blue circles sized by population, Kraków and Wrocław the largest, and the Circles, size related to population style and its JSON below.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Across Poland over OpenStreetMap, 1,027 towns render on the GPU through seven selectable preset styles—from population-scaled circles and custom icons to continuously rotating bars driven by ['time']—paired with an editable <textarea> where typing valid JSON updates the live WebGL shader immediately and hovering highlights towns in red in the circle presets.

When you need it

No-code GIS style editors, IoT sensor dashboards and massive point clouds require GPU point throughput combined with plain JSON style definitions that users or CMS backends can edit at runtime.

Testing seven distinct flat-style idioms side by side demonstrates static sizing, attribute interpolation, sprite icons and time-based GPU animation.

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.

webgl-points-layer.shared.ts
import type { FlatStyleLike } from 'ol/style/flat.js';

/**
 * The predefined styles of the original, for Poland's towns: the population stops are the original's, the latitude
 * stops span Poland from south to north, the labels read a town's `name`, and the icon is the gallery's own pin.
 */
export const STYLES = {
  icons: {
    'icon-src': 'assets/krakow/pin.svg',
    'icon-width': 18,
    'icon-height': 28,
    'icon-color': 'lightyellow',
    'icon-rotate-with-view': false,
    'icon-displacement': [0, 9],
  },
  triangles: {
    'shape-points': 3,
    'shape-radius': 9,
    'shape-fill-color': ['interpolate', ['linear'], ['get', 'population'], 20000, '#5aca5b', 300000, '#ff6a19'],
    'shape-rotate-with-view': true,
  },
  'triangles-latitude': {
    'shape-points': 3,
    'shape-radius': ['interpolate', ['linear'], ['get', 'population'], 40000, 6, 2000000, 12],
    'shape-fill-color': [
      'interpolate',
      ['linear'],
      ['get', 'latitude'],
      49,
      '#ff14c3',
      51,
      '#ff621d',
      53,
      '#ffed02',
      55,
      '#00ff67',
    ],
    'shape-opacity': 0.95,
  },
  circles: {
    'circle-radius': ['interpolate', ['linear'], ['get', 'population'], 40000, 4, 2000000, 14],
    'circle-fill-color': ['match', ['get', 'hover'], 1, '#ff3f3f', '#006688'],
    'circle-rotate-with-view': false,
    'circle-displacement': [0, 0],
    'circle-opacity': ['interpolate', ['linear'], ['get', 'population'], 40000, 0.6, 2000000, 0.92],
  },
  'circles-zoom': {
    // by using an exponential interpolation with a base of 2 we can make it so that circles will have a fixed size
    // in world coordinates between zoom level 5 and 15
    'circle-radius': ['interpolate', ['exponential', 2], ['zoom'], 5, 1.5, 15, 1.5 * Math.pow(2, 10)],
    'circle-fill-color': ['match', ['get', 'hover'], 1, '#ff3f3f', '#006688'],
    'circle-displacement': [0, 0],
    'circle-opacity': 0.95,
  },
  'circles-text': {
    'text-value': ['get', 'name'],
    'text-font': 'bold 11px "Open Sans", "Arial Unicode MS", sans-serif',
    'text-fill-color': '#334',
    'text-stroke-color': 'rgba(255,255,255,0.8)',
    'text-stroke-width': 2,
    'text-offset-y': -12,
    'circle-radius': 4,
    'circle-fill-color': ['match', ['get', 'hover'], 1, '#ff3f3f', '#006688'],
    'circle-rotate-with-view': false,
  },
  'rotating-bars': {
    'shape-rotation': ['*', ['time'], 0.13],
    'shape-points': 4,
    'shape-radius': 4,
    'shape-radius2': 4 * Math.sqrt(2),
    'shape-scale': ['array', 1, ['interpolate', ['linear'], ['get', 'population'], 20000, 1, 300000, 7]],
    'shape-fill-color': ['interpolate', ['linear'], ['get', 'population'], 20000, '#ffdc00', 300000, '#ff5b19'],
    'shape-displacement': ['array', 0, ['interpolate', ['linear'], ['get', 'population'], 20000, 2, 300000, 14]],
  },
} satisfies Record<string, FlatStyleLike>;

/** The name of a predefined style. */
export type StyleName = keyof typeof STYLES;

/** The predefined styles in the order the list offers them, with their labels. */
export const CHOICES: { value: StyleName; label: string }[] = [
  { value: 'icons', label: 'Icons' },
  { value: 'triangles', label: 'Triangles, color related to population' },
  { value: 'triangles-latitude', label: 'Triangles, color related to latitude' },
  { value: 'circles', label: 'Circles, size related to population' },
  { value: 'circles-zoom', label: 'Circles, size related to zoom' },
  { value: 'circles-text', label: 'Circles with label' },
  { value: 'rotating-bars', label: 'Rotating bars' },
];
webgl-points-layer.ts
import {
  afterNextRender,
  ChangeDetectionStrategy,
  Component,
  DestroyRef,
  inject,
  signal,
  viewChild,
} from '@angular/core';
import type Feature from 'ol/Feature';
import GeoJSON from 'ol/format/GeoJSON';
import type MapBrowserEvent from 'ol/MapBrowserEvent';
import type { FlatStyleLike } from 'ol/style/flat';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  SourceVectorComponent,
  ViewComponent,
} from 'cc-openlayers';
import { LayerWebGLVectorComponent } from 'cc-openlayers/webgl';
import { CHOICES, STYLES, type StyleName } from './webgl-points-layer.shared';

/**
 * Poland's towns drawn with WebGL in a style chosen from a list or edited as JSON; the town under the pointer is
 * marked `hover`, which the circle styles colour red, and the map renders every frame for the time-based style.
 */
@Component({
  selector: 'app-webgl-points-layer',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    LayerWebGLVectorComponent,
    MapComponent,
    SourceOsmComponent,
    SourceVectorComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px" (pointerMove)="hover($event)">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="7">
        <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
      </aol-view>
      <aol-layer-tile><aol-source-osm /></aol-layer-tile>
      <aol-layer-webglvector [style]="style()">
        <aol-source-vector
          url="assets/poland/towns.geojson"
          [format]="format"
          [wrapX]="true"
          attributions='Towns © <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
        />
      </aol-layer-webglvector>
    </aol-map>
    <div class="example-controls">
      <p class="example-hint">Choose a predefined style from the list below or edit it as JSON manually.</p>
      <select aria-label="Predefined style" (change)="choose($event)">
        @for (choice of choices; track choice.value) {
          <option [value]="choice.value" [selected]="choice.value === 'circles'">{{ choice.label }}</option>
        }
      </select>
      <textarea aria-label="Style as JSON" [value]="text()" (input)="edit($event)"></textarea>
      <small class="style-status">
        @if (status() === 'valid') {
          <span class="style-valid">✓ style is valid</span>
        } @else if (status()) {
          <span class="style-invalid">✗ {{ status() }}</span>
        }
      </small>
    </div>
  `,
})
export class WebglPointsLayer {
  protected readonly choices = CHOICES;
  protected readonly format = new GeoJSON();
  protected readonly style = signal<FlatStyleLike>(STYLES.circles);
  protected readonly text = signal(JSON.stringify(STYLES.circles, null, 2));
  /** 'valid' once an edited style parses, the parser's message while it does not, empty for a predefined style. */
  protected readonly status = signal('');
  private readonly map = viewChild.required(MapComponent);
  private selected: Feature | null = null;

  constructor() {
    // animate the map
    let frame = 0;
    const animate = () => {
      this.map().instance.render();
      frame = window.requestAnimationFrame(animate);
    };
    afterNextRender(animate);
    inject(DestroyRef).onDestroy(() => window.cancelAnimationFrame(frame));
  }

  protected choose(event: Event): void {
    const style = STYLES[(event.target as HTMLSelectElement).value as StyleName];
    this.text.set(JSON.stringify(style, null, 2));
    this.style.set(style);
    this.status.set('');
  }

  protected edit(event: Event): void {
    const text = (event.target as HTMLTextAreaElement).value;
    try {
      const style = JSON.parse(text) as FlatStyleLike;
      if (JSON.stringify(style) !== JSON.stringify(this.style())) this.style.set(style);
      this.status.set('valid');
    } catch (error) {
      this.status.set((error as Error).message);
    }
  }

  protected hover(event: MapBrowserEvent): void {
    if (this.selected !== null) {
      this.selected.set('hover', 0);
      this.selected = null;
    }
    this.map().instance.forEachFeatureAtPixel(event.pixel, (feature) => {
      this.selected = feature as Feature;
      this.selected.set('hover', 1);
      return true;
    });
  }
}

Architecture and OpenLayers mechanics

STYLES provides seven flat point style presets (icons through rotating-bars) selectable from a dropdown and mirrored as formatted JSON inside a <textarea>. Handler edit parses user edits via JSON.parse(text) and updates [style]="style()" strictly when valid JSON differs from the active style.

circles scales radius by population (4 px at 40,000 to 14 px at 2,000,000), hover toggles hover (1 vs 0) matched via ['match', ['get', 'hover'], 1, '#ff3f3f', '#006688'], and animate schedules continuous frames for ['time']-driven styles.

How it works

  • STYLES supplies seven presets from icons to rotating-bars, displayed and editable live inside a JSON <textarea>.
  • edit validates edits with JSON.parse(text) and updates [style]="style()" only when the parsed JSON actually changed.
  • hover sets feature property hover (1 or 0), styled via ['match', ['get', 'hover'], 1, '#ff3f3f', '#006688'].
  • circles interpolates size by population, while rotating-bars reads ['time'] driven by an animate render loop.

Pitfalls

  • Mutating a feature’s hover attribute (feature.set for the hover attribute) triggers WebGL vertex-buffer regeneration across all 1,027 points—fine at 1,000 points, whereas for 100,000+ points prefer matching the feature id against a style variable as shown in webgl-vector-layer.
  • Every [style] replacement invokes setStyle and compiles new WebGL shaders—guard against no-op edits and syntax errors before assigning.
  • Styles referencing ['time'] only advance visually when a requestAnimationFrame loop calls map.render().

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.