Skip to content

Commit

Permalink
Support OpenLayers 7, 8, 9
Browse files Browse the repository at this point in the history
  • Loading branch information
gberaudo committed Mar 15, 2024
1 parent 177b9a3 commit d7c79d4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
68 changes: 41 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"test": "jest"
},
"peerDependencies": {
"ol": "9"
"ol": "7 || 8 || 9"
},
"devDependencies": {
"@babel/preset-env": "7.24.0",
Expand Down
25 changes: 20 additions & 5 deletions src/MVTEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -168,7 +173,7 @@ export default class MVTEncoder {
tolerance,
resourceLoadedListener,
undefined,
declutter
declutterBuilderGroup
) || loading;
}
}
Expand Down Expand Up @@ -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,
Expand All @@ -218,7 +227,7 @@ export default class MVTEncoder {
);
declutterExecutorGroup.execute(
context,
[context.canvas.width, context.canvas.height],
scaledSize,
transform,
viewRotation,
snapToPixel,
Expand Down Expand Up @@ -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<any>(7)
: undefined) as unknown as boolean)
: !!layer.getDeclutter();

// render to these tiles;
const encodedLayers = renderTiles.map((rt) =>
Expand Down

0 comments on commit d7c79d4

Please sign in to comment.