Skip to content

Commit

Permalink
fix local pmtiles viewing
Browse files Browse the repository at this point in the history
  • Loading branch information
bdon committed Feb 1, 2024
1 parent 9d5d286 commit 365b57b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 9 additions & 4 deletions app/src/MapViewComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ const FeaturesProperties = (props: { features: MapGeoJSONFeature[] }) => {
);
};

export const isValidTiles = (tiles?: string): boolean => {
if (!tiles) return false;
if (!tiles.startsWith("http") && tiles.endsWith(".pmtiles")) return true;
if (tiles.startsWith("http") && new URL(tiles).pathname.endsWith(".pmtiles"))
return true;
return false;
};

function getMaplibreStyle(
theme: string,
tiles?: string,
Expand All @@ -75,10 +83,7 @@ function getMaplibreStyle(
maxZoom?: number,
): StyleSpecification {
let tilesWithProtocol = tiles;
if (
tilesWithProtocol &&
new URL(tilesWithProtocol).pathname.endsWith(".pmtiles")
) {
if (isValidTiles(tiles)) {
tilesWithProtocol = `pmtiles://${tiles}`;
}
const style = {
Expand Down
10 changes: 10 additions & 0 deletions app/test/MapViewComponent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { isValidTiles } from "src/MapViewComponent";
import { expect, test } from "vitest";

test("checks valid tiles value", () => {
expect(isValidTiles("local.pmtiles")).toBe(true);
expect(isValidTiles("http://example.com/remote.pmtiles")).toBe(true);
expect(isValidTiles("http://example.com/remote.pmtiles?abc=def")).toBe(true);
expect(isValidTiles("invalid")).toBe(false);
expect(isValidTiles("invalid.pmtiles?abc=def")).toBe(false);
});

0 comments on commit 365b57b

Please sign in to comment.