|
| 1 | +/** |
| 2 | + * COG Layer Adapter for integrating deck.gl COG layers with maplibre-gl-layer-control. |
| 3 | + */ |
| 4 | + |
| 5 | +import type { CustomLayerAdapter, LayerState } from 'maplibre-gl-layer-control'; |
| 6 | +import type { Map as MapLibreMap } from 'maplibre-gl'; |
| 7 | +import type { Layer } from '@deck.gl/core'; |
| 8 | +import { getDeckLayers, setDeckLayerVisibility, setDeckLayerOpacity } from './deck-overlay'; |
| 9 | + |
| 10 | +interface DeckLayerEntry { |
| 11 | + id: string; |
| 12 | + layer: Layer; |
| 13 | + visible: boolean; |
| 14 | + opacity: number; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Adapter for COG (Cloud Optimized GeoTIFF) layers. |
| 19 | + * Allows the layer control to manage deck.gl COG layers. |
| 20 | + */ |
| 21 | +export class COGLayerAdapter implements CustomLayerAdapter { |
| 22 | + readonly type = 'cog'; |
| 23 | + |
| 24 | + private map: MapLibreMap; |
| 25 | + private changeCallbacks: Array<(event: 'add' | 'remove', layerId: string) => void> = []; |
| 26 | + |
| 27 | + constructor(map: MapLibreMap) { |
| 28 | + this.map = map; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Get all COG layer IDs. |
| 33 | + */ |
| 34 | + getLayerIds(): string[] { |
| 35 | + const layers = getDeckLayers(this.map); |
| 36 | + return Object.keys(layers); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Get the state of a COG layer. |
| 41 | + */ |
| 42 | + getLayerState(layerId: string): LayerState | null { |
| 43 | + const layers = getDeckLayers(this.map); |
| 44 | + const entry = layers[layerId] as DeckLayerEntry | undefined; |
| 45 | + if (!entry) return null; |
| 46 | + |
| 47 | + return { |
| 48 | + visible: entry.visible, |
| 49 | + opacity: entry.opacity, |
| 50 | + name: this.getName(layerId), |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Set visibility of a COG layer. |
| 56 | + */ |
| 57 | + setVisibility(layerId: string, visible: boolean): void { |
| 58 | + setDeckLayerVisibility(this.map, layerId, visible); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Set opacity of a COG layer. |
| 63 | + */ |
| 64 | + setOpacity(layerId: string, opacity: number): void { |
| 65 | + setDeckLayerOpacity(this.map, layerId, opacity); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get the display name for a COG layer. |
| 70 | + */ |
| 71 | + getName(layerId: string): string { |
| 72 | + // Convert layer ID to a friendly name |
| 73 | + return layerId |
| 74 | + .replace(/^(mgl-extend-)?gpu-cog[-_]?/i, '') |
| 75 | + .replace(/[-_]/g, ' ') |
| 76 | + .replace(/\b\w/g, c => c.toUpperCase()) || 'COG Layer'; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Get the symbol type for COG layers. |
| 81 | + */ |
| 82 | + getSymbolType(): string { |
| 83 | + return 'raster'; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Notify that a layer was added. |
| 88 | + * Call this when a COG layer is added. |
| 89 | + */ |
| 90 | + notifyLayerAdded(layerId: string): void { |
| 91 | + this.changeCallbacks.forEach(cb => cb('add', layerId)); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Notify that a layer was removed. |
| 96 | + * Call this when a COG layer is removed. |
| 97 | + */ |
| 98 | + notifyLayerRemoved(layerId: string): void { |
| 99 | + this.changeCallbacks.forEach(cb => cb('remove', layerId)); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Subscribe to layer changes. |
| 104 | + */ |
| 105 | + onLayerChange(callback: (event: 'add' | 'remove', layerId: string) => void): () => void { |
| 106 | + this.changeCallbacks.push(callback); |
| 107 | + return () => { |
| 108 | + const idx = this.changeCallbacks.indexOf(callback); |
| 109 | + if (idx >= 0) { |
| 110 | + this.changeCallbacks.splice(idx, 1); |
| 111 | + } |
| 112 | + }; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export default COGLayerAdapter; |
0 commit comments