Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lambda-tiler): correct wgs84 bounding box when layers are large BM-631 #2345

Merged
merged 2 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions packages/lambda-tiler/src/__tests__/wmts.capability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,83 @@ o.spec('WmtsCapabilities', () => {
const layersB = tags(rawB, 'Layer');
o(layersB.length).equals(1);
});

o('should cover the entire WebMercatorBounds', () => {
const halfSize = GoogleTms.extent.width / 2;
// Create two fake imagery sets one covers tile z1 x0 y0 another covers tile z1 x1 y1
// so the entire bounding box should be tile z0 x0 y0 or the full extent
const imagery = new Map();
const imageTopLeft = { ...Imagery3857, id: 'im_top_left', name: 'top_left' };
imageTopLeft.bounds = { x: -halfSize, y: 0, width: halfSize, height: halfSize };
imagery.set(imageTopLeft.id, imageTopLeft);

const imageBottomRight = { ...Imagery3857, id: 'im_bottom_right', name: 'bottom_right' };
imageBottomRight.bounds = { x: 0, y: -halfSize, width: halfSize, height: halfSize };
imagery.set(imageBottomRight.id, imageBottomRight);

const tileSet = { ...TileSetAerial };
tileSet.layers = [
{ 3857: imageTopLeft.id, name: 'a_top_left' },
{ 3857: imageBottomRight.id, name: 'b_bottom_right' },
];

const raw = new WmtsCapabilities({
httpBase: 'https://basemaps.test',
provider: Provider,
tileMatrix: [GoogleTms],
tileSet,
imagery,
formats: [ImageFormat.Png],
isIndividualLayers: true,
}).toVNode();

const boundingBox = tags(raw, 'ows:WGS84BoundingBox').map((c) =>
roundNumbersInString(c.toString(), 4)
.split('\n')
.map((c) => c.trim()),
);
o(boundingBox[0][1]).deepEquals('<ows:LowerCorner>-180 -85.0511</ows:LowerCorner>');
o(boundingBox[0][2]).equals('<ows:UpperCorner>180 85.0511</ows:UpperCorner>');

o(boundingBox[1][1]).deepEquals('<ows:LowerCorner>-180 0</ows:LowerCorner>');
o(boundingBox[1][2]).equals('<ows:UpperCorner>0 85.0511</ows:UpperCorner>');

o(boundingBox[2][1]).deepEquals('<ows:LowerCorner>0 -85.0511</ows:LowerCorner>');
o(boundingBox[2][2]).equals('<ows:UpperCorner>180 0</ows:UpperCorner>');
});

o('should work when crossing anti meridian', () => {
const halfSize = GoogleTms.extent.width / 2;

const imagery = new Map();
// This image covers z1 x1.5 y1 to z1 x0.5 y1
// which cross the AM and covers half the width of two tiles
const imageBottomRight = { ...Imagery3857, id: 'im_bottom_right', name: 'bottom_right' };
imageBottomRight.bounds = { x: halfSize / 2, y: -halfSize, width: halfSize, height: halfSize };
imagery.set(imageBottomRight.id, imageBottomRight);

const tileSet = { ...TileSetAerial };
tileSet.layers = [{ 3857: imageBottomRight.id, name: 'b_bottom_right' }];

const raw = new WmtsCapabilities({
httpBase: 'https://basemaps.test',
provider: Provider,
tileMatrix: [GoogleTms],
tileSet,
imagery,
formats: [ImageFormat.Png],
isIndividualLayers: true,
}).toVNode();

const boundingBox = tags(raw, 'ows:WGS84BoundingBox').map((c) =>
roundNumbersInString(c.toString(), 4)
.split('\n')
.map((c) => c.trim()),
);
o(boundingBox[0][1]).deepEquals('<ows:LowerCorner>-180 -85.0511</ows:LowerCorner>');
o(boundingBox[0][2]).equals('<ows:UpperCorner>180 85.0511</ows:UpperCorner>');

o(boundingBox[1][1]).deepEquals('<ows:LowerCorner>-180 -85.0511</ows:LowerCorner>');
o(boundingBox[1][2]).equals('<ows:UpperCorner>180 85.0511</ows:UpperCorner>');
});
});
23 changes: 13 additions & 10 deletions packages/lambda-tiler/src/wmts.capability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Bounds, GoogleTms, ImageFormat, TileMatrixSet, WmtsProvider } from '@ba
import { Projection, V, VNodeElement } from '@basemaps/shared';
import { ImageFormatOrder } from '@basemaps/tiler';
import { BoundingBox } from '@cogeotiff/core';
import { BBox, Wgs84 } from '@linzjs/geojson';
import { BBox } from '@linzjs/geojson';

const CapabilitiesAttrs = {
xmlns: 'http://www.opengis.net/wmts/1.0',
Expand Down Expand Up @@ -73,22 +73,25 @@ export class WmtsCapabilities {
buildWgs84BoundingBox(tms: TileMatrixSet, layers: Bounds[]): VNodeElement {
let bbox: BBox;
if (layers.length > 0) {
bbox = wgs84Extent(tms, layers[0]);
let bounds = layers[0];
for (let i = 1; i < layers.length; i++) {
bbox = Wgs84.union(bbox, wgs84Extent(tms, layers[i]));
bounds = bounds.union(layers[i]);
}
bbox = wgs84Extent(tms, bounds.toJson());
} else {
// No layers provided assume extent is the size of the tile matrix set :shrug: ?
bbox = wgs84Extent(tms, tms.extent);
}

return V(
'ows:WGS84BoundingBox',
{ crs: 'urn:ogc:def:crs:OGC:2:84' },
bbox[2] > 180
? [V('ows:LowerCorner', `-180 -90`), V('ows:UpperCorner', `180 90`)]
: [V('ows:LowerCorner', `${bbox[0]} ${bbox[1]}`), V('ows:UpperCorner', `${bbox[2]} ${bbox[3]}`)],
);
// Is East less than West? if so this has crossed the anti meridian
// Ignore the bounding box and just use the tile matrix extent
// TODO is this the correct behaviour
if (bbox[2] < bbox[0]) bbox = wgs84Extent(tms, tms.extent);

return V('ows:WGS84BoundingBox', { crs: 'urn:ogc:def:crs:OGC:2:84' }, [
V('ows:LowerCorner', `${bbox[0]} ${bbox[1]}`),
V('ows:UpperCorner', `${bbox[2]} ${bbox[3]}`),
]);
}

/** Combine all the bounds of the imagery inside the layers into a extent for the imagery set */
Expand Down