esri.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import Map from 'ol/Map';
  2. import { MapLayerRegistryItem, MapLayerOptions, GrafanaTheme2, RegistryItem, Registry } from '@grafana/data';
  3. import { xyzTiles, defaultXYZConfig, XYZConfig } from './generic';
  4. interface PublicServiceItem extends RegistryItem {
  5. slug: string;
  6. }
  7. const CUSTOM_SERVICE = 'custom';
  8. const DEFAULT_SERVICE = 'streets';
  9. export const publicServiceRegistry = new Registry<PublicServiceItem>(() => [
  10. {
  11. id: DEFAULT_SERVICE,
  12. name: 'World Street Map',
  13. slug: 'World_Street_Map',
  14. },
  15. {
  16. id: 'world-imagery',
  17. name: 'World Imagery',
  18. slug: 'World_Imagery',
  19. },
  20. {
  21. id: 'world-physical',
  22. name: 'World Physical',
  23. slug: 'World_Physical_Map',
  24. },
  25. {
  26. id: 'topo',
  27. name: 'Topographic',
  28. slug: 'World_Topo_Map',
  29. },
  30. {
  31. id: 'usa-topo',
  32. name: 'USA Topographic',
  33. slug: 'USA_Topo_Maps',
  34. },
  35. {
  36. id: 'ocean',
  37. name: 'World Ocean',
  38. slug: 'Ocean/World_Ocean_Base',
  39. },
  40. {
  41. id: CUSTOM_SERVICE,
  42. name: 'Custom MapServer',
  43. description: 'Use a custom MapServer with pre-cached values',
  44. slug: '',
  45. },
  46. ]);
  47. export interface ESRIXYZConfig extends XYZConfig {
  48. server: string;
  49. }
  50. export const esriXYZTiles: MapLayerRegistryItem<ESRIXYZConfig> = {
  51. id: 'esri-xyz',
  52. name: 'ArcGIS MapServer',
  53. isBaseMap: true,
  54. create: async (map: Map, options: MapLayerOptions<ESRIXYZConfig>, theme: GrafanaTheme2) => {
  55. const cfg = { ...options.config };
  56. const svc = publicServiceRegistry.getIfExists(cfg.server ?? DEFAULT_SERVICE)!;
  57. if (svc.id !== CUSTOM_SERVICE) {
  58. const base = 'https://services.arcgisonline.com/ArcGIS/rest/services/';
  59. cfg.url = `${base}${svc.slug}/MapServer/tile/{z}/{y}/{x}`;
  60. cfg.attribution = `Tiles © <a href="${base}${svc.slug}/MapServer">ArcGIS</a>`;
  61. }
  62. const opts = { ...options, config: cfg as XYZConfig };
  63. return xyzTiles.create(map, opts, theme).then((xyz) => {
  64. xyz.registerOptionsUI = (builder) => {
  65. builder
  66. .addSelect({
  67. path: 'config.server',
  68. name: 'Server instance',
  69. settings: {
  70. options: publicServiceRegistry.selectOptions().options,
  71. },
  72. })
  73. .addTextInput({
  74. path: 'config.url',
  75. name: 'URL template',
  76. description: 'Must include {x}, {y} or {-y}, and {z} placeholders',
  77. settings: {
  78. placeholder: defaultXYZConfig.url,
  79. },
  80. showIf: (cfg) => cfg.config?.server === CUSTOM_SERVICE,
  81. })
  82. .addTextInput({
  83. path: 'config.attribution',
  84. name: 'Attribution',
  85. settings: {
  86. placeholder: defaultXYZConfig.attribution,
  87. },
  88. showIf: (cfg) => cfg.config?.server === CUSTOM_SERVICE,
  89. });
  90. };
  91. return xyz;
  92. });
  93. },
  94. defaultOptions: {
  95. server: DEFAULT_SERVICE,
  96. },
  97. };
  98. export const esriLayers = [esriXYZTiles];