Skip to content

Commit c0b1257

Browse files
authored
Support tilejson (#364)
* refactor to support basemaps TileJSON in addition to PMTiles. * Support TileJSON in addition to PMTiles for basemaps.
1 parent 97e4e4f commit c0b1257

File tree

1 file changed

+59
-45
lines changed

1 file changed

+59
-45
lines changed

app/src/MapView.tsx

+59-45
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
setRTLTextPlugin,
1717
} from "maplibre-gl";
1818
import type {
19+
LngLatBoundsLike,
1920
MapGeoJSONFeature,
2021
MapTouchEvent,
2122
StyleSpecification,
@@ -299,19 +300,17 @@ function MapLibreView(props: {
299300
map.on("idle", () => {
300301
setZoom(map.getZoom());
301302
setError(undefined);
302-
memoizedArchive()
303-
?.getMetadata()
304-
.then((metadata) => {
305-
if (metadata) {
306-
const m = metadata as {
307-
version?: string;
308-
"planetiler:osm:osmosisreplicationtime"?: string;
309-
};
310-
setTimelinessInfo(
311-
`tiles@${m.version} ${m["planetiler:osm:osmosisreplicationtime"]?.substr(0, 10)}`,
312-
);
313-
}
314-
});
303+
archiveInfo().then((i) => {
304+
if (i?.metadata) {
305+
const m = i.metadata as {
306+
version?: string;
307+
"planetiler:osm:osmosisreplicationtime"?: string;
308+
};
309+
setTimelinessInfo(
310+
`tiles@${m.version} ${m["planetiler:osm:osmosisreplicationtime"]?.substr(0, 10)}`,
311+
);
312+
}
313+
});
315314
});
316315

317316
const showContextMenu = (e: MapTouchEvent) => {
@@ -365,34 +364,51 @@ function MapLibreView(props: {
365364
});
366365

367366
// ensure the dropped archive is first added to the protocol
368-
const memoizedArchive = () => {
367+
const archiveInfo = async (): Promise<
368+
{ metadata: unknown; bounds: LngLatBoundsLike } | undefined
369+
> => {
370+
if (!props.droppedArchive && props.tiles?.endsWith(".json")) {
371+
const resp = await fetch(props.tiles);
372+
const tileJson = await resp.json();
373+
return {
374+
metadata: tileJson,
375+
bounds: [
376+
[tileJson.bounds[0], tileJson.bounds[1]],
377+
[tileJson.bounds[2], tileJson.bounds[3]],
378+
],
379+
};
380+
}
381+
369382
const p = protocolRef();
370383
if (p) {
371-
if (props.droppedArchive) {
372-
p.add(props.droppedArchive);
373-
return props.droppedArchive;
374-
}
375-
if (props.tiles) {
376-
let archive = p.tiles.get(props.tiles);
384+
let archive = props.droppedArchive;
385+
if (archive) {
386+
p.add(archive);
387+
} else if (props.tiles) {
388+
archive = p.tiles.get(props.tiles);
377389
if (!archive) {
378390
archive = new PMTiles(props.tiles);
379391
p.add(archive);
380392
}
381-
return archive;
393+
}
394+
if (archive) {
395+
const metadata = await archive.getMetadata();
396+
const header = await archive.getHeader();
397+
return {
398+
metadata: metadata,
399+
bounds: [
400+
[header.minLon, header.minLat],
401+
[header.maxLon, header.maxLat],
402+
],
403+
};
382404
}
383405
}
384406
};
385407

386408
const fit = async () => {
387-
const header = await memoizedArchive()?.getHeader();
388-
if (header) {
389-
mapRef?.fitBounds(
390-
[
391-
[header.minLon, header.minLat],
392-
[header.maxLon, header.maxLat],
393-
],
394-
{ animate: false },
395-
);
409+
const bounds = (await archiveInfo())?.bounds;
410+
if (bounds) {
411+
mapRef?.fitBounds(bounds, { animate: false });
396412
}
397413
};
398414

@@ -411,22 +427,20 @@ function MapLibreView(props: {
411427
const styleMajorVersion = props.npmVersion
412428
? +props.npmVersion.split(".")[0]
413429
: STYLE_MAJOR_VERSION;
414-
memoizedArchive()
415-
?.getMetadata()
416-
.then((m) => {
417-
if (m instanceof Object && "version" in m) {
418-
const tilesetVersion = +(m.version as string).split(".")[0];
419-
if (
420-
VERSION_COMPATIBILITY[tilesetVersion].indexOf(styleMajorVersion) < 0
421-
) {
422-
setMismatch(
423-
`style v${styleMajorVersion} may not be compatible with tileset v${tilesetVersion}. `,
424-
);
425-
} else {
426-
setMismatch("");
427-
}
430+
archiveInfo().then((i) => {
431+
if (i && i.metadata instanceof Object && "version" in i.metadata) {
432+
const tilesetVersion = +(i.metadata.version as string).split(".")[0];
433+
if (
434+
VERSION_COMPATIBILITY[tilesetVersion].indexOf(styleMajorVersion) < 0
435+
) {
436+
setMismatch(
437+
`style v${styleMajorVersion} may not be compatible with tileset v${tilesetVersion}. `,
438+
);
439+
} else {
440+
setMismatch("");
428441
}
429-
});
442+
}
443+
});
430444
});
431445

432446
createEffect(() => {

0 commit comments

Comments
 (0)