2025-07-16 08:04:41 +08:00
|
|
|
import Feature from 'ol/Feature';
|
|
|
|
import { Geometry } 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
|
|
|
|
2025-07-16 08:04:41 +08:00
|
|
|
import { DataFrame } 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 {}
|
|
|
|
|
2025-07-16 08:04:41 +08:00
|
|
|
// Helper function to create properly typed Features
|
|
|
|
function createFeature<T extends Geometry>(properties: {
|
|
|
|
frame: DataFrame;
|
|
|
|
rowIndex: number;
|
|
|
|
geometry: T;
|
|
|
|
}): Feature<T> {
|
|
|
|
const feature = new Feature(properties);
|
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class FrameVectorSource<T extends Geometry = Geometry> extends VectorSource<Feature<T>> {
|
2023-07-27 02:58:55 +08:00
|
|
|
constructor(public location: LocationFieldMatchers) {
|
2022-01-14 09:15:31 +08:00
|
|
|
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++) {
|
2025-07-16 08:04:41 +08:00
|
|
|
const geometry = info.field.values[i] as T;
|
2022-01-14 09:15:31 +08:00
|
|
|
this.addFeatureInternal(
|
2025-07-16 08:04:41 +08:00
|
|
|
createFeature({
|
2022-01-14 09:15:31 +08:00
|
|
|
frame,
|
|
|
|
rowIndex: i,
|
2025-07-16 08:04:41 +08:00
|
|
|
geometry,
|
2022-01-14 09:15:31 +08:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// only call this at the end
|
|
|
|
this.changed();
|
|
|
|
}
|
|
|
|
}
|