-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathadapters.ts
More file actions
275 lines (250 loc) · 7.78 KB
/
adapters.ts
File metadata and controls
275 lines (250 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// biome-ignore lint: needed for Leaflet + IIFE to work
declare const L: any;
// biome-ignore lint: needed for window.URL to disambiguate from cloudflare workers
declare const window: any;
declare const document: DocumentLike;
import type { Coords } from "leaflet";
import { PMTiles, TileType } from "./index";
interface DocumentLike {
// biome-ignore lint: we don't want to bring in the entire document type
createElement: (s: string) => any;
}
// biome-ignore lint: we don't want to bring in the entire document type
type DoneCallback = (error?: Error, tile?: any) => void;
/**
* Add a raster PMTiles as a layer to a Leaflet map.
*
* For vector tiles see https://github.com/protomaps/protomaps-leaflet
*/
export const leafletRasterLayer = (source: PMTiles, options: unknown) => {
let loaded = false;
let mimeType = "";
const cls = L.GridLayer.extend({
createTile: (coord: Coords, done: DoneCallback) => {
const el = document.createElement("img");
const controller = new AbortController();
const signal = controller.signal;
el.cancel = () => {
controller.abort();
};
if (!loaded) {
source.getHeader().then((header) => {
if (header.tileType === TileType.Mvt) {
console.error(
"Error: archive contains MVT vector tiles, but leafletRasterLayer is for displaying raster tiles. See https://github.com/protomaps/PMTiles/tree/main/js for details."
);
} else if (header.tileType === 2) {
mimeType = "image/png";
} else if (header.tileType === 3) {
mimeType = "image/jpeg";
} else if (header.tileType === 4) {
mimeType = "image/webp";
} else if (header.tileType === 5) {
mimeType = "image/avif";
}
});
loaded = true;
}
source
.getZxy(coord.z, coord.x, coord.y, signal)
.then((arr) => {
if (arr) {
const blob = new Blob([arr.data], { type: mimeType });
const imageUrl = window.URL.createObjectURL(blob);
el.src = imageUrl;
el.cancel = undefined;
done(undefined, el);
}
})
.catch((e) => {
if (e.name !== "AbortError") {
throw e;
}
});
return el;
},
_removeTile: function (key: string) {
const tile = this._tiles[key];
if (!tile) {
return;
}
if (tile.el.cancel) tile.el.cancel();
tile.el.width = 0;
tile.el.height = 0;
tile.el.deleted = true;
L.DomUtil.remove(tile.el);
delete this._tiles[key];
this.fire("tileunload", {
tile: tile.el,
coords: this._keyToTileCoords(key),
});
},
});
return new cls(options);
};
type GetResourceResponse<T> = ExpiryData & {
data: T;
};
type AddProtocolAction = (
requestParameters: RequestParameters,
abortController: AbortController
) => Promise<GetResourceResponse<unknown>>;
type ExpiryData = {
cacheControl?: string | null;
expires?: string | null; // MapLibre can be a Date object
};
// copied from MapLibre /util/ajax.ts
type RequestParameters = {
url: string;
headers?: unknown;
method?: "GET" | "POST" | "PUT";
body?: string;
type?: "string" | "json" | "arrayBuffer" | "image";
credentials?: "same-origin" | "include";
collectResourceTiming?: boolean;
};
// for legacy maplibre-3 interop
type ResponseCallbackV3 = (
error?: Error | undefined,
data?: unknown | undefined,
cacheControl?: string | undefined,
expires?: string | undefined
) => void;
type V3OrV4Protocol = <
T extends AbortController | ResponseCallbackV3,
R = T extends AbortController
? Promise<GetResourceResponse<unknown>>
: { cancel: () => void },
>(
requestParameters: RequestParameters,
arg2: T
) => R;
const v3compat =
(v4: AddProtocolAction): V3OrV4Protocol =>
(requestParameters, arg2) => {
if (arg2 instanceof AbortController) {
// biome-ignore lint: overloading return type not handled by compiler
return v4(requestParameters, arg2) as any;
}
const abortController = new AbortController();
v4(requestParameters, abortController)
.then(
(result) => {
return arg2(
undefined,
result.data,
result.cacheControl || "",
result.expires || ""
);
},
(err) => {
return arg2(err);
}
)
.catch((e) => {
return arg2(e);
});
return { cancel: () => abortController.abort() };
};
/**
* MapLibre GL JS protocol. Must be added once globally.
*/
export class Protocol {
/** @hidden */
tiles: Map<string, PMTiles>;
metadata: boolean;
errorOnMissingTile: boolean;
/**
* Initialize the MapLibre PMTiles protocol.
*
* * metadata: also load the metadata section of the PMTiles. required for some "inspect" functionality
* and to automatically populate the map attribution. Requires an extra HTTP request.
* * errorOnMissingTile: When a vector MVT tile is missing from the archive, raise an error instead of
* returning the empty array. Not recommended. This is only to reproduce the behavior of ZXY tile APIs
* which some applications depend on when overzooming.
*/
constructor(options?: { metadata?: boolean; errorOnMissingTile?: boolean }) {
this.tiles = new Map<string, PMTiles>();
this.metadata = options?.metadata || false;
this.errorOnMissingTile = options?.errorOnMissingTile || false;
}
/**
* Add a {@link PMTiles} instance to the global protocol instance.
*
* For remote fetch sources, references in MapLibre styles like pmtiles://http://...
* will resolve to the same instance if the URLs match.
*/
add(p: PMTiles) {
this.tiles.set(p.source.getKey(), p);
}
/**
* Fetch a {@link PMTiles} instance by URL, for remote PMTiles instances.
*/
get(url: string) {
return this.tiles.get(url);
}
/** @hidden */
tilev4 = async (
params: RequestParameters,
abortController: AbortController
) => {
if (params.type === "json") {
const pmtilesUrl = params.url.substr(10);
let instance = this.tiles.get(pmtilesUrl);
if (!instance) {
instance = new PMTiles(pmtilesUrl);
this.tiles.set(pmtilesUrl, instance);
}
if (this.metadata) {
return {
data: await instance.getTileJson(params.url),
};
}
const h = await instance.getHeader();
if (h.minLon >= h.maxLon || h.minLat >= h.maxLat) {
console.error(
`Bounds of PMTiles archive ${h.minLon},${h.minLat},${h.maxLon},${h.maxLat} are not valid.`
);
}
return {
data: {
tiles: [`${params.url}/{z}/{x}/{y}`],
minzoom: h.minZoom,
maxzoom: h.maxZoom,
bounds: [h.minLon, h.minLat, h.maxLon, h.maxLat],
},
};
}
const re = new RegExp(/pmtiles:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)/);
const result = params.url.match(re);
if (!result) {
throw new Error("Invalid PMTiles protocol URL");
}
const pmtilesUrl = result[1];
let instance = this.tiles.get(pmtilesUrl);
if (!instance) {
instance = new PMTiles(pmtilesUrl);
this.tiles.set(pmtilesUrl, instance);
}
const z = result[2];
const x = result[3];
const y = result[4];
const header = await instance.getHeader();
const resp = await instance?.getZxy(+z, +x, +y, abortController.signal);
if (resp) {
return {
data: new Uint8Array(resp.data),
cacheControl: resp.cacheControl,
expires: resp.expires,
};
}
if (header.tileType === TileType.Mvt) {
if (this.errorOnMissingTile) {
throw new Error("Tile not found.");
}
return { data: new Uint8Array() };
}
return { data: null };
};
tile = v3compat(this.tilev4);
}