getFeatures.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { FeatureLike } from 'ol/Feature';
  2. import { SelectableValue } from '@grafana/data';
  3. import { GeometryTypeId } from '../style/types';
  4. export interface LayerContentInfo {
  5. geometryType: GeometryTypeId;
  6. propertes: Array<SelectableValue<string>>;
  7. }
  8. export function getLayerPropertyInfo(features: FeatureLike[]): LayerContentInfo {
  9. const types = new Set<string>();
  10. const props = new Set<string>();
  11. features.some((feature, idx) => {
  12. for (const key of Object.keys(feature.getProperties())) {
  13. if (key === 'geometry') {
  14. continue;
  15. }
  16. props.add(key);
  17. const g = feature.getGeometry();
  18. if (g) {
  19. types.add(g.getType());
  20. }
  21. }
  22. return idx > 10; // first 10 items
  23. });
  24. let geometryType = GeometryTypeId.Any;
  25. if (types.size === 1) {
  26. switch (types.values().next().value) {
  27. case 'Point':
  28. case 'MultiPoint':
  29. geometryType = GeometryTypeId.Point;
  30. break;
  31. case 'Line':
  32. case 'MultiLine':
  33. geometryType = GeometryTypeId.Line;
  34. break;
  35. case 'Polygon':
  36. geometryType = GeometryTypeId.Polygon;
  37. }
  38. }
  39. return {
  40. geometryType,
  41. propertes: Array.from(props.keys()).map((v) => ({ label: v, value: v })),
  42. };
  43. }
  44. export function getUniqueFeatureValues(features: FeatureLike[], key: string): string[] {
  45. const unique = new Set<string>();
  46. for (const feature of features) {
  47. const v = feature.get(key);
  48. if (v != null) {
  49. unique.add(`${v}`); // always string
  50. }
  51. }
  52. const buffer = Array.from(unique);
  53. buffer.sort();
  54. return buffer;
  55. }