Skip to content

Commit 1b4d064

Browse files
committed
Move function isValid to core
The function isValid should be moved to the core TileKey class as a method. Copied to the TileKey and deprecated the original one. Relates-To: OLPEDGE-2466 Signed-off-by: Oleksii Zubko <[email protected]>
1 parent ac45f5b commit 1b4d064

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

@here/olp-sdk-core/lib/utils/TileKey.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 HERE Europe B.V.
2+
* Copyright (C) 2020-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -238,6 +238,30 @@ export class TileKey implements QuadKey {
238238
return Math.floor(mortonCode / 4);
239239
}
240240

241+
/**
242+
* Checks if a quadkey is valid.
243+
*
244+
* The number of rows and columns must not be greater than the maximum number of rows and columns in the given level.
245+
* The maximum number of rows and columns in a level is computed as 2 to the power of the level.
246+
*
247+
* @note As the JavaScript number type can hold 53 bits in its mantissa, only levels up to 26 can be
248+
* represented in the number representation returned by [[mortonCodeFromQuadKey]].
249+
* A level must be positive and can't be greater than 26.
250+
*
251+
* @param key The current quadkey.
252+
* @return True if the quadkey is valid, false otherwise.
253+
*/
254+
static isValid(key: QuadKey): boolean {
255+
// tslint:disable-next-line:no-magic-numbers
256+
if (key.level < 0 || key.level > 26) {
257+
return false;
258+
}
259+
const dimensionAtLevel = Math.pow(2, key.level);
260+
const rowValid = key.row >= 0 && key.row < dimensionAtLevel;
261+
const columnValid = key.column >= 0 && key.column < dimensionAtLevel;
262+
return rowValid && columnValid;
263+
}
264+
241265
/**
242266
* Returns a tile key representing the parent of the tile addressed by this tile key.
243267
*

@here/olp-sdk-core/test/unit/TileKey.test.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 HERE Europe B.V.
2+
* Copyright (C) 2020-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -231,4 +231,26 @@ describe("TileKey", function() {
231231
);
232232
}
233233
});
234+
235+
it("Tile is not valid if the row/column is out of bounds", function() {
236+
const invalid_tile_1 = { row: 5, column: 1, level: 1 };
237+
assert.isFalse(TileKey.isValid(invalid_tile_1));
238+
239+
const invalid_tile_2 = { row: -5, column: 1, level: 1 };
240+
assert.isFalse(TileKey.isValid(invalid_tile_2));
241+
242+
const invalid_tile_3 = { row: 1, column: 5, level: 1 };
243+
assert.isFalse(TileKey.isValid(invalid_tile_3));
244+
245+
const invalid_tile_4 = { row: 1, column: -5, level: 1 };
246+
assert.isFalse(TileKey.isValid(invalid_tile_4));
247+
});
248+
249+
it("Tile is not valid if the level is out of bounds", function() {
250+
const invalid_tile_1 = { row: 0, column: 0, level: -1 };
251+
assert.isFalse(TileKey.isValid(invalid_tile_1));
252+
253+
const invalid_tile_2 = { row: 0, column: 0, level: 100 };
254+
assert.isFalse(TileKey.isValid(invalid_tile_2));
255+
});
234256
});

@here/olp-sdk-dataservice-read/lib/client/QueryClient.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2020 HERE Europe B.V.
2+
* Copyright (C) 2019-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,15 +21,15 @@ import {
2121
FetchOptions,
2222
HRN,
2323
OlpClientSettings,
24-
RequestFactory
24+
RequestFactory,
25+
TileKey
2526
} from "@here/olp-sdk-core";
2627
import {
2728
AdditionalFields,
2829
MetadataApi,
2930
QueryApi
3031
} from "@here/olp-sdk-dataservice-api";
3132
import {
32-
isValid,
3333
MetadataCacheRepository,
3434
mortonCodeFromQuadKey,
3535
PartitionsRequest,
@@ -70,7 +70,7 @@ export class QueryClient {
7070
const quadKey = request.getQuadKey();
7171
const layerId = request.getLayerId();
7272

73-
if (!quadKey || !isValid(quadKey)) {
73+
if (!quadKey || !TileKey.isValid(quadKey)) {
7474
return Promise.reject("Please provide correct QuadKey");
7575
}
7676

@here/olp-sdk-dataservice-read/lib/utils/QuadKeyUtils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const powerOfTwo = [
101101
// tslint:enable:no-magic-numbers
102102

103103
/**
104-
* @deprecated This function will be removed by 02.2021.
104+
* @deprecated This function will be removed by 08.2021.
105105
* Please use the same method from [[TileKey]] class, imported from `@here/olp-sdk-core` package.
106106
*
107107
* Creates a quadkey from a numeric or string Morton code representation.

0 commit comments

Comments
 (0)