From d7c79d4b569e8b6b70adb479a28bc021b5dbd4b4 Mon Sep 17 00:00:00 2001 From: Guillaume Beraudo Date: Fri, 15 Mar 2024 14:27:39 +0100 Subject: [PATCH] Support OpenLayers 7, 8, 9 --- package-lock.json | 68 ++++++++++++++++++++++++++++------------------- package.json | 2 +- src/MVTEncoder.ts | 25 +++++++++++++---- 3 files changed, 62 insertions(+), 33 deletions(-) diff --git a/package-lock.json b/package-lock.json index ebff152..4b3647f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34,7 +34,7 @@ "vite": "5.1.4" }, "peerDependencies": { - "ol": "9" + "ol": "7 || 8 || 9" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -3263,9 +3263,9 @@ } }, "node_modules/@petamoriken/float16": { - "version": "3.8.3", - "resolved": "https://registry.npmjs.org/@petamoriken/float16/-/float16-3.8.3.tgz", - "integrity": "sha512-an2OZ7/6er9Jja8EDUvU/tmtGIutdlb6LwXOwgjzoCjDRAsUd8sRZMBjoPEy78Xa9iOp+Kglk2CHgVwZuZbWbw==", + "version": "3.8.6", + "resolved": "https://registry.npmjs.org/@petamoriken/float16/-/float16-3.8.6.tgz", + "integrity": "sha512-GNJhABTtcmt9al/nqdJPycwFD46ww2+q2zwZzTjY0dFFwUAFRw9zszvEr9osyJRd9krRGy6hUDopWUg9fX7VVw==", "dev": true }, "node_modules/@rollup/rollup-android-arm-eabi": { @@ -4616,9 +4616,9 @@ "dev": true }, "node_modules/color-parse": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/color-parse/-/color-parse-2.0.0.tgz", - "integrity": "sha512-g2Z+QnWsdHLppAbrpcFWo629kLOnOPtpxYV69GCqm92gqSgyXbzlfyN3MXs0412fPBkFmiuS+rXposgBgBa6Kg==", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-parse/-/color-parse-2.0.1.tgz", + "integrity": "sha512-lJJCDBXlTHDI+iO1VIbrMPR8OqxR3kZoyiLDqPh+FOtXfzjL45q8QNHjf98KaZ5Ki+WPPcdWmbUibMD15spbUg==", "dev": true, "dependencies": { "color-name": "^1.0.0" @@ -6109,9 +6109,9 @@ } }, "node_modules/geotiff": { - "version": "2.0.7", - "resolved": "https://registry.npmjs.org/geotiff/-/geotiff-2.0.7.tgz", - "integrity": "sha512-FKvFTNowMU5K6lHYY2f83d4lS2rsCNdpUC28AX61x9ZzzqPNaWFElWv93xj0eJFaNyOYA63ic5OzJ88dHpoA5Q==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/geotiff/-/geotiff-2.1.3.tgz", + "integrity": "sha512-PT6uoF5a1+kbC3tHmZSUsLHBp2QJlHasxxxxPW47QIY1VBKpFB+FcDvX+MxER6UzgLQZ0xDzJ9s48B9JbOCTqA==", "dev": true, "dependencies": { "@petamoriken/float16": "^3.4.7", @@ -6120,7 +6120,8 @@ "parse-headers": "^2.0.2", "quick-lru": "^6.1.1", "web-worker": "^1.2.0", - "xml-utils": "^1.0.2" + "xml-utils": "^1.0.2", + "zstddec": "^0.1.0" }, "engines": { "node": ">=10.19" @@ -10744,9 +10745,9 @@ } }, "node_modules/web-worker": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/web-worker/-/web-worker-1.2.0.tgz", - "integrity": "sha512-PgF341avzqyx60neE9DD+XS26MMNMoUQRz9NOZwW32nPQrF6p77f1htcnjBSEV8BGMKZ16choqUG4hyI0Hx7mA==", + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/web-worker/-/web-worker-1.3.0.tgz", + "integrity": "sha512-BSR9wyRsy/KOValMgd5kMyr3JzpdeoR9KVId8u5GVlTTAtNChlsE4yTxeY7zMdNSyOmoKBv8NH2qeRY9Tg+IaA==", "dev": true }, "node_modules/webidl-conversions": { @@ -11075,6 +11076,12 @@ "funding": { "url": "https://github.com/sponsors/sindresorhus" } + }, + "node_modules/zstddec": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/zstddec/-/zstddec-0.1.0.tgz", + "integrity": "sha512-w2NTI8+3l3eeltKAdK8QpiLo/flRAr2p8AGeakfMZOXBxOg9HIu4LVDxBi81sYgVhFhdJjv1OrB5ssI8uFPoLg==", + "dev": true } }, "dependencies": { @@ -13289,9 +13296,9 @@ } }, "@petamoriken/float16": { - "version": "3.8.3", - "resolved": "https://registry.npmjs.org/@petamoriken/float16/-/float16-3.8.3.tgz", - "integrity": "sha512-an2OZ7/6er9Jja8EDUvU/tmtGIutdlb6LwXOwgjzoCjDRAsUd8sRZMBjoPEy78Xa9iOp+Kglk2CHgVwZuZbWbw==", + "version": "3.8.6", + "resolved": "https://registry.npmjs.org/@petamoriken/float16/-/float16-3.8.6.tgz", + "integrity": "sha512-GNJhABTtcmt9al/nqdJPycwFD46ww2+q2zwZzTjY0dFFwUAFRw9zszvEr9osyJRd9krRGy6hUDopWUg9fX7VVw==", "dev": true }, "@rollup/rollup-android-arm-eabi": { @@ -14273,9 +14280,9 @@ "dev": true }, "color-parse": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/color-parse/-/color-parse-2.0.0.tgz", - "integrity": "sha512-g2Z+QnWsdHLppAbrpcFWo629kLOnOPtpxYV69GCqm92gqSgyXbzlfyN3MXs0412fPBkFmiuS+rXposgBgBa6Kg==", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-parse/-/color-parse-2.0.1.tgz", + "integrity": "sha512-lJJCDBXlTHDI+iO1VIbrMPR8OqxR3kZoyiLDqPh+FOtXfzjL45q8QNHjf98KaZ5Ki+WPPcdWmbUibMD15spbUg==", "dev": true, "requires": { "color-name": "^1.0.0" @@ -15389,9 +15396,9 @@ "dev": true }, "geotiff": { - "version": "2.0.7", - "resolved": "https://registry.npmjs.org/geotiff/-/geotiff-2.0.7.tgz", - "integrity": "sha512-FKvFTNowMU5K6lHYY2f83d4lS2rsCNdpUC28AX61x9ZzzqPNaWFElWv93xj0eJFaNyOYA63ic5OzJ88dHpoA5Q==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/geotiff/-/geotiff-2.1.3.tgz", + "integrity": "sha512-PT6uoF5a1+kbC3tHmZSUsLHBp2QJlHasxxxxPW47QIY1VBKpFB+FcDvX+MxER6UzgLQZ0xDzJ9s48B9JbOCTqA==", "dev": true, "requires": { "@petamoriken/float16": "^3.4.7", @@ -15400,7 +15407,8 @@ "parse-headers": "^2.0.2", "quick-lru": "^6.1.1", "web-worker": "^1.2.0", - "xml-utils": "^1.0.2" + "xml-utils": "^1.0.2", + "zstddec": "^0.1.0" } }, "get-caller-file": { @@ -18740,9 +18748,9 @@ "dev": true }, "web-worker": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/web-worker/-/web-worker-1.2.0.tgz", - "integrity": "sha512-PgF341avzqyx60neE9DD+XS26MMNMoUQRz9NOZwW32nPQrF6p77f1htcnjBSEV8BGMKZ16choqUG4hyI0Hx7mA==", + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/web-worker/-/web-worker-1.3.0.tgz", + "integrity": "sha512-BSR9wyRsy/KOValMgd5kMyr3JzpdeoR9KVId8u5GVlTTAtNChlsE4yTxeY7zMdNSyOmoKBv8NH2qeRY9Tg+IaA==", "dev": true }, "webidl-conversions": { @@ -18983,6 +18991,12 @@ "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", "dev": true + }, + "zstddec": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/zstddec/-/zstddec-0.1.0.tgz", + "integrity": "sha512-w2NTI8+3l3eeltKAdK8QpiLo/flRAr2p8AGeakfMZOXBxOg9HIu4LVDxBi81sYgVhFhdJjv1OrB5ssI8uFPoLg==", + "dev": true } } } diff --git a/package.json b/package.json index 5b301f2..fce144c 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "test": "jest" }, "peerDependencies": { - "ol": "9" + "ol": "7 || 8 || 9" }, "devDependencies": { "@babel/preset-env": "7.24.0", diff --git a/src/MVTEncoder.ts b/src/MVTEncoder.ts index 85a2b32..3c1245e 100644 --- a/src/MVTEncoder.ts +++ b/src/MVTEncoder.ts @@ -24,7 +24,12 @@ import {transform2D} from 'ol/geom/flat/transform.js'; import CanvasBuilderGroup from 'ol/render/canvas/BuilderGroup.js'; import CanvasExecutorGroup from 'ol/render/canvas/ExecutorGroup.js'; -import TileGrid from 'ol/tilegrid/TileGrid'; +import RBush from 'rbush'; +import TileGrid from 'ol/tilegrid/TileGrid.js'; +import {VERSION} from 'ol'; +import type {Size} from 'ol/size.js'; + +const olMajorVersion = Number.parseInt(VERSION.split('.')[0]); /** * Simple proxy to the fetch function for now. @@ -168,7 +173,7 @@ export default class MVTEncoder { tolerance, resourceLoadedListener, undefined, - declutter + declutterBuilderGroup ) || loading; } } @@ -197,10 +202,14 @@ export default class MVTEncoder { const transform = coordinateToPixelTransform; const viewRotation = 0; const snapToPixel = true; + const scaledSize = + olMajorVersion < 9 + ? (1 as unknown as Size) + : [context.canvas.width, context.canvas.height]; renderingExecutorGroup.execute( context, - [context.canvas.width, context.canvas.height], + scaledSize, transform, viewRotation, snapToPixel, @@ -218,7 +227,7 @@ export default class MVTEncoder { ); declutterExecutorGroup.execute( context, - [context.canvas.width, context.canvas.height], + scaledSize, transform, viewRotation, snapToPixel, @@ -431,7 +440,13 @@ export default class MVTEncoder { const styleResolution = options.styleResolution || tileResolution; const layerStyleFunction = layer.getStyleFunction()!; // there is always a default one const layerOpacity = layer.get('opacity'); - const declutter = !!layer.getDeclutter(); + // declutter is a boolean in OpenLayers 9 but anq RBush in earlier versions + const declutter: boolean = + olMajorVersion < 9 + ? ((layer.getDeclutter() + ? new RBush(7) + : undefined) as unknown as boolean) + : !!layer.getDeclutter(); // render to these tiles; const encodedLayers = renderTiles.map((rt) =>