worldmap.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { Point } from 'ol/geom';
  2. import { fromLonLat } from 'ol/proj';
  3. import { PlacenameInfo, Gazetteer } from './gazetteer';
  4. // https://github.com/grafana/worldmap-panel/blob/master/src/data/countries.json
  5. export interface WorldmapPoint {
  6. key?: string;
  7. keys?: string[]; // new in grafana 8.1+
  8. latitude: number;
  9. longitude: number;
  10. name?: string;
  11. }
  12. export function loadWorldmapPoints(path: string, data: WorldmapPoint[]): Gazetteer {
  13. let count = 0;
  14. const values = new Map<string, PlacenameInfo>();
  15. for (const v of data) {
  16. const point = new Point(fromLonLat([v.longitude, v.latitude]));
  17. const info: PlacenameInfo = {
  18. point: () => point,
  19. geometry: () => point,
  20. };
  21. if (v.name) {
  22. values.set(v.name, info);
  23. values.set(v.name.toUpperCase(), info);
  24. }
  25. if (v.key) {
  26. values.set(v.key, info);
  27. values.set(v.key.toUpperCase(), info);
  28. }
  29. if (v.keys) {
  30. for (const key of v.keys) {
  31. values.set(key, info);
  32. values.set(key.toUpperCase(), info);
  33. }
  34. }
  35. count++;
  36. }
  37. return {
  38. path,
  39. find: (k) => {
  40. let v = values.get(k);
  41. if (!v && typeof k === 'string') {
  42. v = values.get(k.toUpperCase());
  43. }
  44. return v;
  45. },
  46. count,
  47. examples: (count) => {
  48. const first: string[] = [];
  49. if (values.size < 1) {
  50. first.push('no values found');
  51. } else {
  52. for (const key of values.keys()) {
  53. first.push(key);
  54. if (first.length >= count) {
  55. break;
  56. }
  57. }
  58. }
  59. return first;
  60. },
  61. };
  62. }