grafana/public/app/features/geo/utils/frameVectorSource.ts
Ashley Harrison ff0642bf6e
Chore: Type improvements 🧹 (#75271)
* fix some e2e flows types

* some type fixes

* must... fix... more...

* MOAR

* MOREMOREMORE

* 1 more
2023-09-22 15:06:49 +01:00

61 lines
1.4 KiB
TypeScript

import { Feature } from 'ol';
import { Geometry, LineString, Point } from 'ol/geom';
import VectorSource from 'ol/source/Vector';
import { DataFrame, Field } from '@grafana/data';
import { getGeometryField, LocationFieldMatchers } from './location';
export interface FrameVectorSourceOptions {}
export class FrameVectorSource<T extends Geometry = Geometry> extends VectorSource<T> {
constructor(public location: LocationFieldMatchers) {
super({});
}
update(frame: DataFrame) {
this.clear(true);
const info = getGeometryField(frame, this.location);
if (!info.field) {
this.changed();
return;
}
for (let i = 0; i < frame.length; i++) {
this.addFeatureInternal(
new Feature({
frame,
rowIndex: i,
geometry: info.field.values[i] as T,
})
);
}
// only call this at the end
this.changed();
}
updateLineString(frame: DataFrame) {
this.clear(true);
const info = getGeometryField(frame, this.location);
if (!info.field) {
this.changed();
return;
}
//eslint-disable-next-line
const field = info.field as unknown as Field<Point>;
const geometry: Geometry = new LineString(field.values.map((p) => p.getCoordinates()));
this.addFeatureInternal(
new Feature({
frame,
rowIndex: 0,
geometry: geometry as T,
})
);
// only call this at the end
this.changed();
}
}