1
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

80 lines
2.0 KiB

import { DataFrame, SelectableValue } from '@grafana/data';
import { Feature } from 'ol';
import { FeatureLike } from 'ol/Feature';
import { Point } from 'ol/geom';
import { GeometryTypeId } from '../style/types';
import { LocationInfo } from './location';
export const getFeatures = (frame: DataFrame, info: LocationInfo): Array<Feature<Point>> | undefined => {
const features: Array<Feature<Point>> = [];
// Map each data value into new points
for (let i = 0; i < frame.length; i++) {
features.push(
new Feature({
frame,
rowIndex: i,
geometry: info.points[i],
})
);
}
return features;
};
export interface LayerContentInfo {
geometryType: GeometryTypeId;
propertes: Array<SelectableValue<string>>;
}
export function getLayerPropertyInfo(features: FeatureLike[]): LayerContentInfo {
const types = new Set<string>();
const props = new Set<string>();
features.some((feature, idx) => {
for (const key of Object.keys(feature.getProperties())) {
if (key === 'geometry') {
continue;
}
props.add(key);
const g = feature.getGeometry();
if (g) {
types.add(g.getType());
}
}
return idx > 10; // first 10 items
});
let geometryType = GeometryTypeId.Any;
if (types.size === 1) {
switch (types.values().next().value) {
case 'Point':
case 'MultiPoint':
geometryType = GeometryTypeId.Point;
break;
case 'Line':
case 'MultiLine':
geometryType = GeometryTypeId.Line;
break;
case 'Polygon':
geometryType = GeometryTypeId.Polygon;
}
}
return {
geometryType,
propertes: Array.from(props.keys()).map((v) => ({ label: v, value: v })),
};
}
export function getUniqueFeatureValues(features: FeatureLike[], key: string): string[] {
const unique = new Set<string>();
for (const feature of features) {
const v = feature.get(key);
if (v != null) {
unique.add(`${v}`); // always string
}
}
const buffer = Array.from(unique);
buffer.sort();
return buffer;
}