|
| 1 | +import { |
| 2 | + createSolidColorImage, |
| 3 | + loadImage, |
| 4 | +} from "../layer-helpers/pattern-fill"; |
| 5 | +import { createPatternImage } from "@macrostrat/ui-components"; |
| 6 | + |
| 7 | +export interface StyleImageManagerOptions { |
| 8 | + baseURL?: string; |
| 9 | + pixelRatio?: number; |
| 10 | + resolvers?: Record<string, PatternResolverFunction>; |
| 11 | + throwOnMissing?: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +type PatternResolverFunction = ( |
| 15 | + key: string, |
| 16 | + args: string[], |
| 17 | + options: StyleImageManagerOptions, |
| 18 | +) => Promise<PatternResult | ImageResult>; |
| 19 | + |
| 20 | +type ImageResult = |
| 21 | + | HTMLImageElement |
| 22 | + | ImageData |
| 23 | + | { url: string; options?: AddImageOptions } |
| 24 | + | null; |
| 25 | + |
| 26 | +interface PatternResult { |
| 27 | + image: ImageResult; |
| 28 | + options?: AddImageOptions; |
| 29 | +} |
| 30 | + |
| 31 | +interface AddImageOptions { |
| 32 | + sdf?: boolean; |
| 33 | + pixelRatio?: number; |
| 34 | +} |
| 35 | + |
| 36 | +export function setupStyleImageManager( |
| 37 | + map: any, |
| 38 | + options: StyleImageManagerOptions = {}, |
| 39 | +): () => void { |
| 40 | + const { throwOnMissing = false } = options; |
| 41 | + const styleImageMissing = (e) => { |
| 42 | + loadStyleImage(map, e.id, options) |
| 43 | + .catch((err) => { |
| 44 | + if (throwOnMissing) { |
| 45 | + throw err; |
| 46 | + } |
| 47 | + console.error(`Failed to load pattern image for ${e.id}:`, err); |
| 48 | + }) |
| 49 | + .then(() => {}); |
| 50 | + }; |
| 51 | + |
| 52 | + // Register the event listener for missing images |
| 53 | + map.on("styleimagemissing", styleImageMissing); |
| 54 | + return () => { |
| 55 | + // Clean up the event listener when the component unmounts |
| 56 | + map.off("styleimagemissing", styleImageMissing); |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +async function loadStyleImage( |
| 61 | + map: mapboxgl.Map, |
| 62 | + id: string, |
| 63 | + options: StyleImageManagerOptions = {}, |
| 64 | +) { |
| 65 | + const { pixelRatio = 3 } = options; |
| 66 | + const [prefix, name, ...rest] = id.split(":"); |
| 67 | + |
| 68 | + const { resolvers = defaultResolvers } = options; |
| 69 | + |
| 70 | + // Match the prefix to a resolver function |
| 71 | + if (prefix in resolvers) { |
| 72 | + const resolver = resolvers[prefix]; |
| 73 | + const result = await resolver(prefix, [name, ...rest], options); |
| 74 | + |
| 75 | + let image: ImageResult = null; |
| 76 | + let addOptions: AddImageOptions = { pixelRatio }; |
| 77 | + |
| 78 | + if (result != null) { |
| 79 | + if ("options" in result) { |
| 80 | + addOptions = { ...addOptions, ...(result.options ?? {}) }; |
| 81 | + } |
| 82 | + if ("image" in result) { |
| 83 | + image = result.image; |
| 84 | + addOptions = { ...addOptions, ...(result.options ?? {}) }; |
| 85 | + } else { |
| 86 | + image = result; |
| 87 | + } |
| 88 | + } |
| 89 | + if (image == null) { |
| 90 | + throw new Error(`No image returned by resolver for pattern: ${id}`); |
| 91 | + } |
| 92 | + if (typeof image === "object" && "url" in image) { |
| 93 | + await addImageURLToMap(map, id, image.url, addOptions); |
| 94 | + } else { |
| 95 | + addImageToMap(map, id, image, addOptions); |
| 96 | + } |
| 97 | + return; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +enum SymbolImageFormat { |
| 102 | + PNG = "png", |
| 103 | + SVG = "svg", |
| 104 | +} |
| 105 | + |
| 106 | +function resolveLineSymbolImage( |
| 107 | + id: string, |
| 108 | + args: string[], |
| 109 | + options: StyleImageManagerOptions = {}, |
| 110 | +) { |
| 111 | + return resolveSymbolImage( |
| 112 | + "geologic-symbols/lines/dev", |
| 113 | + args, |
| 114 | + SymbolImageFormat.PNG, |
| 115 | + options, |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 119 | +function resolvePointSymbolImage( |
| 120 | + id: string, |
| 121 | + args: string[], |
| 122 | + options: StyleImageManagerOptions = {}, |
| 123 | +) { |
| 124 | + return resolveSymbolImage( |
| 125 | + "geologic-symbols/points/strabospot", |
| 126 | + args, |
| 127 | + SymbolImageFormat.PNG, |
| 128 | + options, |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +function resolveSymbolImage( |
| 133 | + set: string, |
| 134 | + args: string[], |
| 135 | + format: SymbolImageFormat = SymbolImageFormat.PNG, |
| 136 | + options: StyleImageManagerOptions = {}, |
| 137 | +) { |
| 138 | + const { baseURL = "https://dev.macrostrat.org/assets/web" } = options; |
| 139 | + const [name, ...rest] = args; |
| 140 | + const lineSymbolsURL = `${baseURL}/${set}/${format}`; |
| 141 | + return { url: lineSymbolsURL + `/${name}.${format}`, options: { sdf: true } }; |
| 142 | +} |
| 143 | + |
| 144 | +export function addImageToMap( |
| 145 | + map: mapboxgl.Map, |
| 146 | + id: string, |
| 147 | + image: HTMLImageElement | ImageData | null, |
| 148 | + options: AddImageOptions, |
| 149 | +) { |
| 150 | + if (map.hasImage(id) || image == null) return; |
| 151 | + map.addImage(id, image, options); |
| 152 | +} |
| 153 | + |
| 154 | +export async function addImageURLToMap( |
| 155 | + map: mapboxgl.Map, |
| 156 | + id: string, |
| 157 | + url: string, |
| 158 | + options: AddImageOptions, |
| 159 | +) { |
| 160 | + if (map.hasImage(id)) return; |
| 161 | + const image = await loadImage(url); |
| 162 | + addImageToMap(map, id, image, options); |
| 163 | +} |
| 164 | + |
| 165 | +async function resolveFGDCImage( |
| 166 | + key: string, |
| 167 | + args: string[], |
| 168 | + options: StyleImageManagerOptions, |
| 169 | +): Promise<PatternResult | null> { |
| 170 | + const { baseURL = "https://dev.macrostrat.org/assets/web" } = options; |
| 171 | + const [name, color, backgroundColor = "transparent"] = args; |
| 172 | + |
| 173 | + const num = parseInt(name); |
| 174 | + let patternName = name; |
| 175 | + if (num == NaN) { |
| 176 | + throw new Error(`Invalid FGDC pattern name: ${name}`); |
| 177 | + } |
| 178 | + if (num <= 599) { |
| 179 | + // FGDC 1-599 are fill patterns |
| 180 | + // Check if pattern ID has a suffix, or if not add one |
| 181 | + patternName = `${num}-K`; |
| 182 | + } |
| 183 | + |
| 184 | + const image = (await createPatternImage({ |
| 185 | + patternURL: `${baseURL}/geologic-patterns/png/${patternName}.png`, |
| 186 | + color: backgroundColor, |
| 187 | + patternColor: color, |
| 188 | + })) as ImageData; |
| 189 | + |
| 190 | + return image; |
| 191 | +} |
| 192 | + |
| 193 | +async function resolveSolidColorImage( |
| 194 | + key: string, |
| 195 | + args: string[], |
| 196 | + options: StyleImageManagerOptions, |
| 197 | +): Promise<ImageData> { |
| 198 | + return createSolidColorImage(args[0]); |
| 199 | +} |
| 200 | + |
| 201 | +export const defaultResolvers: Record<string, PatternResolverFunction> = { |
| 202 | + fgdc: resolveFGDCImage, |
| 203 | + color: resolveSolidColorImage, |
| 204 | + "line-symbol": resolveLineSymbolImage, |
| 205 | + point: resolvePointSymbolImage, |
| 206 | +}; |
0 commit comments