From 3811ff9b1b2860f7c1660d7c90a4d53c68060acb Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Thu, 7 Jul 2022 12:36:16 +0800 Subject: [PATCH] js decoder 1.1.0 adds fflate dependency; maplibre decoder can read gzipped tiles [#41] --- app/src/MaplibreMap.tsx | 1 + js/index.ts | 8 +++++++- js/package-lock.json | 17 +++++++++++++++-- js/package.json | 13 ++++++++----- python/pmtiles/reader.py | 1 - 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/src/MaplibreMap.tsx b/app/src/MaplibreMap.tsx index 2f5ec02..137993b 100644 --- a/app/src/MaplibreMap.tsx +++ b/app/src/MaplibreMap.tsx @@ -93,6 +93,7 @@ function MaplibreMap(props: { file: PMTiles }) { useEffect(() => { let cache = new ProtocolCache(); maplibregl.addProtocol("pmtiles", cache.protocol); + cache.add(props.file); // this is necessary for non-HTTP sources map = new maplibregl.Map({ container: mapContainerRef.current!, diff --git a/js/index.ts b/js/index.ts index 575560a..2ff762e 100644 --- a/js/index.ts +++ b/js/index.ts @@ -1,5 +1,7 @@ declare const L: any; +import { decompressSync } from "fflate"; + export const shift = (n: number, shift: number) => { return n * Math.pow(2, shift); }; @@ -536,7 +538,11 @@ export class ProtocolCache { instance!.source .getBytes(val.offset, val.length) .then((arr) => { - callback(null, new Uint8Array(arr.buffer), null, null); + let data = new Uint8Array(arr.buffer); + if (data[0] == 0x1f && data[1] == 0x8b) { + data = decompressSync(data); + } + callback(null, data, null, null); }) .catch((e) => { callback(new Error("Canceled"), null, null, null); diff --git a/js/package-lock.json b/js/package-lock.json index ca1c1d1..bd55288 100644 --- a/js/package-lock.json +++ b/js/package-lock.json @@ -1,13 +1,16 @@ { "name": "pmtiles", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "pmtiles", - "version": "1.0.0", + "version": "1.1.0", "license": "BSD-3-Clause", + "dependencies": { + "fflate": "^0.7.3" + }, "devDependencies": { "esbuild": "^0.11.14", "esbuild-runner": "^2.2.1", @@ -47,6 +50,11 @@ "esbuild": "*" } }, + "node_modules/fflate": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.7.3.tgz", + "integrity": "sha512-0Zz1jOzJWERhyhsimS54VTqOteCNwRtIlh8isdL0AXLo0g7xNTfTL7oWrkmCnPhZGocKIkWHBistBrrpoNH3aw==" + }, "node_modules/source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", @@ -115,6 +123,11 @@ "tslib": "2.3.1" } }, + "fflate": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.7.3.tgz", + "integrity": "sha512-0Zz1jOzJWERhyhsimS54VTqOteCNwRtIlh8isdL0AXLo0g7xNTfTL7oWrkmCnPhZGocKIkWHBistBrrpoNH3aw==" + }, "source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", diff --git a/js/package.json b/js/package.json index 3a8e037..c08e8c1 100644 --- a/js/package.json +++ b/js/package.json @@ -1,19 +1,19 @@ { "name": "pmtiles", - "version": "1.0.4", + "version": "1.1.0", "description": "PMTiles archive decoder for browsers", "main": "dist/index.cjs", "module": "dist/index.mjs", - "types":"dist/index.d.ts", + "types": "dist/index.d.ts", "source": "index.ts", "files": [ "dist", "index.ts" ], "scripts": { - "build-iife": "esbuild index.ts --outfile=dist/index.js --target=es6 --global-name=pmtiles --format=iife", - "build-esm": "esbuild index.ts --outfile=dist/index.mjs --target=es6 --global-name=pmtiles --format=esm", - "build-cjs": "esbuild index.ts --outfile=dist/index.cjs --target=es6 --global-name=pmtiles --format=cjs", + "build-iife": "esbuild index.ts --outfile=dist/index.js --target=es6 --global-name=pmtiles --bundle --format=iife", + "build-esm": "esbuild index.ts --outfile=dist/index.mjs --target=es6 --global-name=pmtiles --bundle --format=esm", + "build-cjs": "esbuild index.ts --outfile=dist/index.cjs --target=es6 --global-name=pmtiles --bundle --format=cjs", "build-tsc": "tsc --declaration --emitDeclarationOnly --outdir dist", "build": "npm run build-iife && npm run build-esm && npm run build-cjs && npm run build-tsc", "test": "node -r esbuild-runner/register index.test.ts", @@ -28,5 +28,8 @@ "esbuild-runner": "^2.2.1", "typescript": "^4.5.5", "zora": "^5.0.2" + }, + "dependencies": { + "fflate": "^0.7.3" } } diff --git a/python/pmtiles/reader.py b/python/pmtiles/reader.py index 813b97a..712217d 100644 --- a/python/pmtiles/reader.py +++ b/python/pmtiles/reader.py @@ -18,7 +18,6 @@ def MemorySource(buf): return get_bytes - class Reader: def __init__(self, get_bytes): self.get_bytes = get_bytes