Vector data · Angular, React and OpenLayers

A Koch snowflake of up to 786,433 points

Render a recursive Koch snowflake up to 786,433 vertices over Kraków in OpenLayers to demonstrate automatic Douglas-Peucker simplification.

A blue Koch snowflake of depth 5 over OpenStreetMap of Kraków, with the depth slider at 5 and the count of 3073 points below the map.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Over OpenStreetMap of Kraków, a blue Koch snowflake fractal starts from an equilateral triangle and subdivides recursively as you release the Depth slider—multiplying segment count by 4 per step up to depth 9 (786,433 vertices) while keeping panning and zooming remarkably responsive.

When you need it

High-precision LiDAR contours, national river networks and un-generalised coastal polygons frequently contain hundreds of thousands of vertices inside a single LineString or Polygon.

Testing a deterministic 786k-vertex Koch curve illustrates how OpenLayers’ built-in per-resolution Douglas–Peucker simplification keeps canvas frame times fast.

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.

fractal.ts
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
import {
  CollectionCoordinatesComponent,
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  FeatureComponent,
  GeometryLinestringComponent,
  LayerTileComponent,
  LayerVectorComponent,
  MapComponent,
  SourceOsmComponent,
  SourceVectorComponent,
  ViewComponent,
} from 'cc-openlayers';
import type { Coordinate } from 'ol/coordinate';
import { fromLonLat } from 'ol/proj';

const [centerX, centerY] = fromLonLat([19.9368, 50.0614]);
const radius = 10e6 / 1024;
const cos30 = Math.cos(Math.PI / 6);
const sin30 = Math.sin(Math.PI / 6);
const triangle: Coordinate[] = [
  [centerX, centerY + radius],
  [centerX + radius * cos30, centerY - radius * sin30],
  [centerX - radius * cos30, centerY - radius * sin30],
  [centerX, centerY + radius],
];

/** A Koch snowflake of up to 786 thousand vertices over Kraków, regenerated when the depth slider is released. */
@Component({
  selector: 'app-fractal',
  imports: [
    CollectionCoordinatesComponent,
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    FeatureComponent,
    GeometryLinestringComponent,
    LayerTileComponent,
    LayerVectorComponent,
    MapComponent,
    SourceOsmComponent,
    SourceVectorComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="11"><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>
        <aol-source-vector>
          <aol-feature>
            <aol-geometry-linestring
              ><aol-collection-coordinates [coordinates]="coordinates()"
            /></aol-geometry-linestring>
          </aol-feature>
        </aol-source-vector>
      </aol-layer-vector>
    </aol-map>
    <div class="example-controls">
      <label>
        Depth <input type="range" min="0" max="9" step="1" [value]="depth()" (change)="depth.set(number($event))" />
        <output>{{ depth() }}</output>
        ({{ coordinates().length }} points)
      </label>
    </div>
  `,
})
export class Fractal {
  readonly depth = signal(5);
  protected readonly coordinates = computed(() => makeFractal(this.depth()));

  protected number(event: Event): number {
    return (event.target as HTMLInputElement).valueAsNumber;
  }
}

/** The triangle with the middle third of every segment replaced by a peak, `depth` times. */
function makeFractal(depth: number): Coordinate[] {
  let coordinates = triangle;
  for (let level = 0; level < depth; ++level) {
    const next: Coordinate[] = [coordinates[0]];
    for (let i = 1; i < coordinates.length; ++i) {
      next.push(...injectPoints(coordinates[i - 1], coordinates[i]));
    }
    coordinates = next;
  }
  return coordinates;
}

/** The points after `start` on a segment: one third along, the peak of the _/\_ shape, two thirds along and `end`. */
function injectPoints(start: Coordinate, end: Coordinate): Coordinate[] {
  const dx = end[0] - start[0];
  const dy = end[1] - start[1];
  const r = Math.sqrt(dx * dx + dy * dy) / (2 * cos30);
  const a = Math.atan2(dy, dx) + Math.PI / 6;
  return [
    [start[0] + dx / 3, start[1] + dy / 3],
    [start[0] + r * Math.cos(a), start[1] + r * Math.sin(a)],
    [end[0] - dx / 3, end[1] - dy / 3],
    end,
  ];
}

Architecture and OpenLayers mechanics

makeFractal starts from triangle (radius 10e6 / 1024 projection units around Kraków) and applies injectPoints depth times—splitting each segment at 1/3, an outward equilateral peak, and 2/3 to quadruple segment count per iteration.

A computed signal feeds the resulting coordinate array into aol-collection-coordinates, while binding the slider’s (change) event (on pointer release rather than continuous input) avoids allocating millions of intermediate arrays mid-drag.

How it works

  • makeFractal subdivides an initial triangle (radius 10e6 / 1024) recursively depth times around Kraków.
  • injectPoints replaces each segment with four sub-segments, reaching 3,073 points at depth 5 and 786,433 points at depth 9.
  • A computed signal derives the vertex array from depth and passes it to aol-collection-coordinates.
  • Listening to (change) instead of input recomputes the fractal once when the user releases the slider thumb.

Pitfalls

  • Before painting each frame, OpenLayers’ 2D Canvas renderer simplifies geometries to a half-pixel tolerance at the active view resolution using Douglas–Peucker—so zooming all the way in draws detailed sub-peaks while zoomed-out pans stroke only visible vertices.
  • Never run O(4^n) geometry generation on every continuous slider input tick when upper depths allocate three-quarters of a million coordinate tuples.

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.