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.

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.tsimport { 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] });
}
} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
drag-and-drop-image-vector.react.tsximport { useRef, useState } from 'react';
import type { Extent } from 'ol/extent.js';
import type Feature from 'ol/Feature.js';
import type OLMap from 'ol/Map.js';
import type MapBrowserEvent from 'ol/MapBrowserEvent.js';
import { getTopLeft, getWidth } from 'ol/extent.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import GPX from 'ol/format/GPX.js';
import IGC from 'ol/format/IGC.js';
import KML from 'ol/format/KML.js';
import TopoJSON from 'ol/format/TopoJSON.js';
import type { DragAndDropEvent } from 'ol/interaction/DragAndDrop.js';
import VectorImageLayer from 'ol/layer/VectorImage.js';
import { EXTENT } from 'ol/proj/epsg3857.js';
import VectorSource from 'ol/source/Vector.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
DragAndDropInteraction,
Map,
TileLayer,
View,
WMTSSource,
useWMTSTileGrid,
} from 'cc-openlayers/react';
const size = getWidth(EXTENT) / 256;
const formats = [GPX, GeoJSON, IGC, KML, TopoJSON];
const gridOptions = {
origin: getTopLeft(EXTENT),
resolutions: Array.from({ length: 20 }, (_, z) => size / 2 ** z),
matrixIds: Array.from({ length: 20 }, (_, z) => `EPSG:3857:${z}`),
};
/** Dropped GPX, GeoJSON, IGC, KML and TopoJSON files become vector image layers, each rendered to an image on the client. */
export function DragAndDropImageVector() {
const [info, setInfo] = useState('');
const map = useRef<OLMap>(null);
const tileGrid = useWMTSTileGrid(gridOptions);
/** The names of the features under the pointer. */
const describe = (event: MapBrowserEvent): void => {
if (event.dragging) return;
const names: string[] = [];
map.current?.forEachFeatureAtPixel(event.pixel, (feature) => {
names.push(feature.get('name'));
});
setInfo(names.join(', '));
};
/** Shows the dropped features in a new vector image layer and fits the view to them, 40 pixels inside the map. */
const show = (event: DragAndDropEvent): void => {
const source = new VectorSource({ features: event.features as Feature[] });
const current = map.current;
if (!current) return;
current.addLayer(new VectorImageLayer({ source }));
current.getView().fit(source.getExtent() as Extent, { padding: [40, 40, 40, 40] });
};
return (
<>
<Map ref={map} height="480px" onPointerMove={describe} onClick={describe}>
<DefaultInteractions />
<DragAndDropInteraction formatConstructors={formats} onAddFeatures={show} />
<DefaultControls />
<View zoom={12}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<WMTSSource
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"
tileGrid={tileGrid}
/>
</TileLayer>
</Map>
<div className="example-controls">
<p className="example-hint">Drag and drop GPX, GeoJSON, IGC, KML or TopoJSON files on to the map.</p>
<output>{info}</output>
</div>
</>
);
}How the React version works
Dropped files become VectorImage layers, which render their features to an image on the client.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection.
From Angular to React
| Angular | React |
|---|---|
<aol-interaction-draganddrop [formatConstructors] (addFeatures)> | <DragAndDropInteraction formatConstructors onAddFeatures>; the format lists are module constants |
<aol-source-tilewmts> with <aol-tilegrid-wmts> | <WMTSSource> with a grid from useWMTSTileGrid |
(pointerMove) and viewChild of the map | onPointerMove (and onClick) on Map with useRef<OLMap>; the reading in useState |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular. The onAddFeatures handler adds the layers of dropped files to the map; they go away with the map on unmount.
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 aVectorImageLayerinstead 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.
formatssupplies 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.
Related examples
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.