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

feat(lambda-tiler): Move the union out of the calculation loop to improve the atrribution api performance #2732

Merged
merged 18 commits into from
Mar 15, 2023
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
143 changes: 142 additions & 1 deletion packages/lambda-tiler/src/__tests__/config.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,148 @@ export const Imagery3857: ConfigImagery = {
width: 51977.179234057665,
height: 30574.81131407339,
},
files: [],
files: [
{
x: 19461478.89763212,
y: -4591419.415033963,
width: 305.7481131407044,
height: 305.7481131407044,
name: '17-129188-80552',
},
{
x: 19502754.89290612,
y: -4603343.591446448,
width: 305.7481131407044,
height: 305.7481131407044,
name: '17-129323-80591',
},
{
x: 19504283.633471873,
y: -4608847.057483015,
width: 611.4962262814096,
height: 611.4962262814096,
name: '16-64664-40304',
},
{
x: 19509175.60328212,
y: -4605178.080125325,
width: 611.4962262814096,
height: 611.4962262814096,
name: '16-64672-40298',
},
{
x: 19509175.60328212,
y: -4604566.583899044,
width: 611.4962262814096,
height: 611.4962262814096,
name: '16-64672-40297',
},
{
x: 19459032.912726905,
y: -4582552.719752827,
width: 1222.9924525628148,
height: 1222.9924525628148,
name: '15-32295-20130',
},
{
x: 19460255.905179467,
y: -4592336.659373329,
width: 1222.9924525628148,
height: 1222.9924525628148,
name: '15-32296-20138',
},
{
x: 19501837.648566607,
y: -4609458.55370921,
width: 1222.9924525628148,
height: 1222.9924525628148,
name: '15-32330-20152',
},
{
x: 19503060.64101917,
y: -4609458.55370921,
width: 1222.9924525628148,
height: 1222.9924525628148,
name: '15-32331-20152',
},
{
x: 19503060.64101917,
y: -4603343.591446393,
width: 1222.9924525628148,
height: 1222.9924525628148,
name: '15-32331-20147',
},
{
x: 19457809.920274343,
y: -4593559.651825892,
width: 2445.9849051256297,
height: 2445.9849051256297,
name: '14-16147-10069',
},
{
x: 19457809.920274343,
y: -4591113.666920764,
width: 2445.9849051256297,
height: 2445.9849051256297,
name: '14-16147-10068',
},
{
x: 19457809.920274343,
y: -4588667.682015641,
width: 2445.9849051256297,
height: 2445.9849051256297,
name: '14-16147-10067',
},
{
x: 19457809.920274343,
y: -4581329.727300262,
width: 2445.9849051256297,
height: 2445.9849051256297,
name: '14-16147-10064',
},
{
x: 19460255.905179467,
y: -4591113.666920764,
width: 2445.9849051256297,
height: 2445.9849051256297,
name: '14-16148-10068',
},
{
x: 19460255.905179586,
y: -4588667.682015713,
width: 4891.969810251274,
height: 4891.969810251274,
name: '13-8074-5033',
},
{
x: 19460255.905179586,
y: -4583775.712205462,
width: 4891.969810251274,
height: 4891.969810251274,
name: '13-8074-5032',
},
{
x: 19499391.663661595,
y: -4608235.561256718,
width: 4891.969810251274,
height: 4891.969810251274,
name: '13-8082-5037',
},
{
x: 19504283.63347185,
y: -4608235.561256718,
width: 4891.969810251274,
height: 4891.969810251274,
name: '13-8083-5037',
},
{
x: 19504283.63347185,
y: -4603343.591446467,
width: 4891.969810251274,
height: 4891.969810251274,
name: '13-8083-5036',
},
],
};

export const Provider: ConfigProvider = {
Expand Down
66 changes: 64 additions & 2 deletions packages/lambda-tiler/src/routes/__tests__/attribution.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Attribution } from '@basemaps/attribution';
import { ConfigProviderMemory } from '@basemaps/config';
import { Nztm2000QuadTms } from '@basemaps/geo';
import { LogConfig } from '@basemaps/shared';
import { GoogleTms, Nztm2000QuadTms } from '@basemaps/geo';
import { LogConfig, Projection } from '@basemaps/shared';
import { BBox } from '@linzjs/geojson';
import { HttpHeader } from '@linzjs/lambda';
import o from 'ospec';
import sinon from 'sinon';
import { handler } from '../../index.js';
import { ConfigLoader } from '../../util/config.loader.js';
import { FakeData, Imagery2193, Imagery3857, Provider, TileSetAerial } from '../../__tests__/config.data.js';
import { mockUrlRequest } from '../../__tests__/xyz.util.js';
import { createCoordinates } from '../attribution.js';

// const ExpectedJson = {
// id: 'aerial_WebMercatorQuad',
Expand Down Expand Up @@ -387,4 +389,64 @@ o.spec('/v1/attribution', () => {
o(output.collections[0].summaries['linz:zoom']).deepEquals({ min: 0, max: Nztm2000QuadTms.maxZoom });
});
});

o('should create valid coordinates', async () => {
//bbox: BBox, files: NamedBounds[], proj: Projection
const bbox = [174.79248047, -38.21228805, 175.25939941, -37.99616268] as BBox;
const proj = Projection.get(GoogleTms);
const coordinates = createCoordinates(bbox, Imagery3857.files, proj);
console.log(JSON.stringify(coordinates));
o(coordinates).deepEquals([
[
[
[174.79247149, -38.09998972],
[174.81446211, -38.09998972],
[174.81446211, -38.09134368],
[174.82544844, -38.09134368],
[174.82544844, -38.08485848],
[174.82819502, -38.08485848],
[174.82819502, -38.08269662],
[174.83643476, -38.08269662],
[174.83643476, -38.06539942],
[174.85840742, -38.06539942],
[174.85840742, -37.9961556],
[174.81446211, -37.9961556],
[174.81446211, -37.9961556],
[174.79247149, -37.9961556],
[174.79247149, -38.01348331],
[174.80345781, -38.01348331],
[174.80345781, -38.02213855],
[174.81444414, -38.02213855],
[174.81444414, -38.04808399],
[174.79247149, -38.04808399],
[174.79247149, -38.09998972],
],
],
[
[
[175.16600664, -38.20366238],
[175.1879793, -38.20366238],
[175.1879793, -38.21229511],
[175.20996992, -38.21229511],
[175.20996992, -38.20797887],
[175.21546308, -38.20797887],
[175.21546308, -38.20366238],
[175.25391523, -38.20366238],
[175.25391523, -38.18207606],
[175.2594084, -38.18207606],
[175.2594084, -38.17342562],
[175.25391523, -38.17342562],
[175.25391523, -38.13454951],
[175.20995195, -38.13454951],
[175.20995195, -38.16046922],
[175.19896563, -38.16046922],
[175.19896563, -38.16694771],
[175.19621904, -38.16694771],
[175.19621904, -38.16910707],
[175.16600664, -38.16910707],
[175.16600664, -38.20366238],
],
],
]);
});
});
12 changes: 4 additions & 8 deletions packages/lambda-tiler/src/routes/attribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,19 @@ function roundPair(p: Pair): Pair {
* @param files in target projection
* @return MultiPolygon in WGS84
*/
function createCoordinates(bbox: BBox, files: NamedBounds[], proj: Projection): MultiPolygon {
export function createCoordinates(bbox: BBox, files: NamedBounds[], proj: Projection): MultiPolygon {
if (Wgs84.delta(bbox[0], bbox[2]) <= 0) {
// This bounds spans more than half the globe which multiPolygonToWgs84 can't handle; just
// return bbox as polygon
return Wgs84.bboxToMultiPolygon(bbox);
}

let coordinates: MultiPolygon = [];

const polygons: MultiPolygon = [];
// merge imagery bounds
for (const image of files) {
const poly = [Bounds.fromJson(image).pad(SmoothPadding).toPolygon()] as MultiPolygon;
coordinates = union(coordinates, poly);
}
for (const image of files) polygons.push(Bounds.fromJson(image).pad(SmoothPadding).toPolygon());
const coordinates = union(polygons);

const roundToWgs84 = (p: number[]): number[] => roundPair(proj.toWgs84(p) as Pair);

return multiPolygonToWgs84(coordinates, roundToWgs84);
}

Expand Down