Vector data · Angular, React and OpenLayers

Drop files onto vector image layers

Drop large GPX, GeoJSON, IGC, KML or TopoJSON files onto an OpenLayers map and render each dataset smoothly through a fast VectorImageLayer.

The orthophoto of western Kraków from Kościuszko Mound to the Main Market Square with the dropped GeoJSON landmarks as small blue points and the square as a polygon, Wawel Castle named below the map.
A screenshot of the example. Orthophotomap © GUGiK. Map data © OpenStreetMap contributors.

What this example shows

Spanning western Kraków from Kościuszko Mound to the Main Market Square and Wawel Castle over aerial orthophotography, dropping local GIS files renders imported polygons, tracks and dense landmark sets into off-screen VectorImageLayer bitmaps—keeping panning fast while still reporting feature names under the pointer.

When you need it

When users drop multi-megabyte municipal boundaries, dense contour lines or tens of thousands of GPS points onto a map, standard per-frame vector re-stroking can drop frame rates during pans and zooms.

Routing dropped features into a VectorImageLayer rasterises each imported file once per settled view while preserving full vector hit detection under the cursor.

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.

drag-and-drop-image-vector.ts
import { ChangeDetectionStrategy, Component, signal, viewChild } from '@angular/core';
import type Feature from 'ol/Feature';
import type MapBrowserEvent from 'ol/MapBrowserEvent';
import { getTopLeft, getWidth } from 'ol/extent';
import GeoJSON from 'ol/format/GeoJSON';
import GPX from 'ol/format/GPX';
import IGC from 'ol/format/IGC';
import KML from 'ol/format/KML';
import TopoJSON from 'ol/format/TopoJSON';
import type { DragAndDropEvent } from 'ol/interaction/DragAndDrop';
import VectorImageLayer from 'ol/layer/VectorImage';
import { EXTENT } from 'ol/proj/epsg3857';
import VectorSource from 'ol/source/Vector';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  DragAndDropInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceTileWMTSComponent,
  TileGridWMTSComponent,
  ViewComponent,
} from 'cc-openlayers';

const size = getWidth(EXTENT) / 256;

/** Dropped GPX, GeoJSON, IGC, KML and TopoJSON files become vector image layers, each rendered to an image on the client. */
@Component({
  selector: 'app-drag-and-drop-image-vector',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    DragAndDropInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceTileWMTSComponent,
    TileGridWMTSComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px" (pointerMove)="describe($event)" (olClick)="describe($event)">
      <aol-interaction-default />
      <aol-interaction-draganddrop [formatConstructors]="formats" (addFeatures)="show($event)" />
      <aol-control-defaults />
      <aol-view [zoom]="12">
        <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
      </aol-view>
      <aol-layer-tile>
        <aol-source-tilewmts
          attributions='Tiles © <a href="https://www.geoportal.gov.pl/" target="_blank">GUGiK</a>'
          url="https://mapy.geoportal.gov.pl/wss/service/PZGIK/ORTO/WMTS/StandardResolution"
          layer="ORTOFOTOMAPA"
          matrixSet="EPSG:3857"
          format="image/jpeg"
          projection="EPSG:3857"
          [style]="'default'"
        >
          <aol-tilegrid-wmts [origin]="origin" [resolutions]="resolutions" [matrixIds]="matrixIds" />
        </aol-source-tilewmts>
      </aol-layer-tile>
    </aol-map>
    <div class="example-controls">
      <p class="example-hint">Drag and drop GPX, GeoJSON, IGC, KML or TopoJSON files on to the map.</p>
      <output>{{ info() }}</output>
    </div>
  `,
})
export class DragAndDropImageVector {
  readonly info = signal('');
  protected readonly formats = [GPX, GeoJSON, IGC, KML, TopoJSON];
  protected readonly origin = getTopLeft(EXTENT);
  protected readonly resolutions = Array.from({ length: 20 }, (_, z) => size / 2 ** z);
  protected readonly matrixIds = Array.from({ length: 20 }, (_, z) => `EPSG:3857:${z}`);
  private readonly map = viewChild.required(MapComponent);

  /** The names of the features under the pointer. */
  protected describe(event: MapBrowserEvent): void {
    if (event.dragging) return;
    const names: string[] = [];
    this.map().instance.forEachFeatureAtPixel(event.pixel, (feature) => {
      names.push(feature.get('name'));
    });
    this.info.set(names.join(', '));
  }

  /** Shows the dropped features in a new vector image layer and fits the view to them, 40 pixels inside the map. */
  protected show(event: DragAndDropEvent): void {
    const source = new VectorSource({ features: event.features as Feature[] });
    const map = this.map().instance;
    map.addLayer(new VectorImageLayer({ source }));
    map.getView().fit(source.getExtent(), { padding: [40, 40, 40, 40] });
  }
}

Architecture and OpenLayers mechanics

The DragAndDrop interaction declares its supported parser classes via formats (preserving KML’s default embedded styles) and hands decoded features in the view projection to show, which instantiates a VectorImageLayer rather than a standard VectorLayer.

During active camera drags or zooms OpenLayers simply translates and scales that cached viewport bitmap, redrawing crisp vector strokes once movement settles and resolving hovered feature names from the underlying vector source.

How it works

  • In show, dropped features mount inside a VectorImageLayer instead of an ordinary per-frame vector layer.
  • Each layer rasterises its features into a viewport-sized image and re-renders vector geometry only after panning or zooming stops.
  • Pointer hit detection remains fully active against the underlying features to display names below the map.
  • formats supplies the GPX, GeoJSON, IGC, KML and TopoJSON format classes with default KML style support.

Pitfalls

  • With default image ratio, the cached bitmap covers only the current viewport—panning reveals unpainted margins until the gesture ends and triggers a fresh rasterisation (raise imageRatio if wider pan margins are needed).
  • While zooming in or out, strokes and point icons scale as pixels until movement stops and redraws them at crisp target resolution.

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.