2022-01-14 09:15:31 +08:00
|
|
|
import { Feature } from 'ol';
|
2022-06-28 00:45:09 +08:00
|
|
|
import { Geometry, LineString, Point } from 'ol/geom';
|
2022-01-14 09:15:31 +08:00
|
|
|
import VectorSource from 'ol/source/Vector';
|
2022-04-22 21:33:13 +08:00
|
|
|
|
2022-06-28 00:45:09 +08:00
|
|
|
import { DataFrame, Field } from '@grafana/data';
|
2022-04-22 21:33:13 +08:00
|
|
|
|
2022-01-14 09:15:31 +08:00
|
|
|
import { getGeometryField, LocationFieldMatchers } from './location';
|
|
|
|
|
|
|
|
export interface FrameVectorSourceOptions {}
|
|
|
|
|
2022-03-03 16:45:33 +08:00
|
|
|
export class FrameVectorSource<T extends Geometry = Geometry> extends VectorSource<T> {
|
2022-01-14 09:15:31 +08:00
|
|
|
constructor(private 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,
|
2023-04-18 05:46:29 +08:00
|
|
|
geometry: info.field.values[i] as T,
|
2022-01-14 09:15:31 +08:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// only call this at the end
|
|
|
|
this.changed();
|
|
|
|
}
|
2022-06-28 00:45:09 +08:00
|
|
|
|
|
|
|
updateLineString(frame: DataFrame) {
|
|
|
|
this.clear(true);
|
|
|
|
const info = getGeometryField(frame, this.location);
|
|
|
|
if (!info.field) {
|
|
|
|
this.changed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-28 07:13:09 +08:00
|
|
|
//eslint-disable-next-line
|
2023-04-14 20:36:53 +08:00
|
|
|
const field = info.field as unknown as Field<Point>;
|
2023-04-18 05:46:29 +08:00
|
|
|
const geometry = new LineString(field.values.map((p) => p.getCoordinates())) as Geometry;
|
2022-06-28 00:45:09 +08:00
|
|
|
this.addFeatureInternal(
|
|
|
|
new Feature({
|
|
|
|
frame,
|
|
|
|
rowIndex: 0,
|
|
|
|
geometry: geometry as T,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// only call this at the end
|
|
|
|
this.changed();
|
|
|
|
}
|
2022-01-14 09:15:31 +08:00
|
|
|
}
|