-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add h3.KRING_INDEXED and quadkey.KRING_INDEXED (#144)
* add h3.KRING_INDEXED and quadkey.KRING_INDEXED * Quadkey kring indexed (#145) * Add h3.KRING_INDEXED and quadkey.KRING_INDEXED * remove kring_hollow * add KRING_INDEXED test * add KRING_INDEXED docs and update tests * remove trailing comma * fix tabs * fix tabs * delete s2 * fix camelCase * fix erroring * update changelogs
- Loading branch information
1 parent
8cdba5a
commit 5f369d7
Showing
11 changed files
with
379 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
### KRING_INDEXED | ||
|
||
{{% bannerNote type="code" %}} | ||
h3.KRING_INDEXED(index, distance) | ||
{{%/ bannerNote %}} | ||
|
||
**Description** | ||
|
||
Returns an array with the indexes and their `distance` in term of cell to the given input hexagon of all hexagons within `distance`. The order of the hexagons is undefined. Returns `null` on invalid input. | ||
|
||
* `index`: `STRING` The H3 cell index. | ||
* `distance`: `INT64` distance (in number of cells) to the source. | ||
|
||
**Return type** | ||
|
||
`ARRAY<STRUCT<idx STRING, distance INT64>>` | ||
|
||
{{% customSelector %}} | ||
**Example** | ||
{{%/ customSelector %}} | ||
|
||
```sql | ||
SELECT carto-os.h3.KRING_INDEXED('837b59fffffffff', 1) mykring_indexed; | ||
-- "mykring_indexed": [ | ||
{ | ||
"idx": "837b59fffffffff", | ||
"distance": "0" | ||
}, | ||
{ | ||
"idx": "837b5dfffffffff", | ||
"distance": "1" | ||
}, | ||
{ | ||
"idx": "837b58fffffffff", | ||
"distance": "1" | ||
}, | ||
{ | ||
"idx": "837b5bfffffffff", | ||
"distance": "1" | ||
}, | ||
{ | ||
"idx": "837a66fffffffff", | ||
"distance": "1" | ||
}, | ||
{ | ||
"idx": "837a64fffffffff", | ||
"distance": "1" | ||
}, | ||
{ | ||
"idx": "837b4afffffffff", | ||
"distance": "1" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
---------------------------- | ||
-- Copyright (C) 2021 CARTO | ||
---------------------------- | ||
|
||
CREATE OR REPLACE FUNCTION `@@BQ_PREFIX@@h3.KRING_INDEXED` | ||
(idx STRING, distance INT64) | ||
RETURNS ARRAY<STRUCT<idx STRING, distance INT64>> | ||
DETERMINISTIC | ||
LANGUAGE js | ||
OPTIONS (library=["@@BQ_LIBRARY_BUCKET@@"]) | ||
AS """ | ||
if (!idx || distance == null || distance < 0) { | ||
return null; | ||
} | ||
if (!h3Lib.h3IsValid(idx)) { | ||
return null; | ||
} | ||
return Array.from(Array(parseInt(distance)+1).keys()).map(x => h3Lib.hexRing(idx, x).map(idx => ({idx:idx, distance:x}))).flat(); | ||
"""; |
46 changes: 46 additions & 0 deletions
46
modules/h3/bigquery/test/integration/KRING_INDEXED.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const { runQuery } = require('../../../../../common/bigquery/test-utils'); | ||
|
||
|
||
test('KRING_INDEXED should work', async () => { | ||
const query = ` | ||
WITH kring_data AS | ||
( SELECT myrow, | ||
\`@@BQ_PREFIX@@h3.KRING_INDEXED\`(idx, distance) as kring_elem, | ||
FROM UNNEST([ | ||
STRUCT(1 as myrow, "invalid_index" as idx, 1 as distance), | ||
STRUCT(2, "8928308280fffff", NULL), | ||
STRUCT(3, "8928308280fffff", 1), | ||
STRUCT(4, "8928308280fffff", 3) | ||
])) | ||
SELECT | ||
myrow, | ||
-- use STRING_AGG to deal with null | ||
STRING_AGG(CAST(ke.distance as STRING) ORDER BY ke.idx) as kring_distance, | ||
STRING_AGG(CAST(ke.idx as STRING) ORDER BY ke.idx) as kring_idx | ||
FROM kring_data left join UNNEST(kring_elem) as ke | ||
GROUP BY myrow | ||
`; | ||
const myrows = await runQuery(query); | ||
expect(myrows.map(r => r.kring_distance)).toEqual( | ||
[ | ||
null, | ||
null, | ||
'1,1,1,0,1,1,1', | ||
'1,1,1,0,2,2,2,2,3,2,3,2,3,1,3,2,3,3,2,2,2,3,3,1,1,2,3,3,3,2,3,3,3,3,3,3,3' | ||
]); | ||
|
||
expect(myrows.map(r => r.kring_idx)).toEqual( | ||
[ | ||
null, | ||
null, | ||
'89283082803ffff,89283082807ffff,8928308280bffff,8928308280fffff,8928308283bffff,89283082873ffff,89283082877ffff', | ||
'89283082803ffff,89283082807ffff,8928308280bffff,8928308280fffff,89283082813ffff,89283082817ffff,8928308281bffff,89283082823ffff,89283082827ffff,8928308282bffff,8928308282fffff,89283082833ffff,89283082837ffff,8928308283bffff,89283082843ffff,89283082847ffff,8928308284fffff,89283082853ffff,89283082857ffff,89283082863ffff,89283082867ffff,8928308286bffff,8928308286fffff,89283082873ffff,89283082877ffff,8928308287bffff,8928308288bffff,8928308288fffff,892830828a3ffff,892830828abffff,892830828afffff,892830828bbffff,892830828c7ffff,892830828cfffff,89283082ab7ffff,89283082b93ffff,89283082b9bffff' | ||
]); | ||
}); | ||
|
||
test('KRING_INDEXED should fail with NULL argument', async () => { | ||
const query = ` | ||
SELECT \`@@BQ_PREFIX@@h3.KRING_INDEXED\`(NULL) | ||
`; | ||
await expect(runQuery(query)).rejects.toThrow(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
### KRING_INDEXED | ||
|
||
{{% bannerNote type="code" %}} | ||
quadkey.KRING_INDEXED(quadint, distance) | ||
{{%/ bannerNote %}} | ||
|
||
**Description** | ||
|
||
Returns an array containing all the quadints and their relative position to the given quadint in term of x and y. Quadints returned are directly next to the given quadint at the same level of zoom. Diagonal, horizontal and vertical nearby quadints plus the current quadint are considered, so KRING_INDEXED always returns `(distance*2 + 1)^2` quadints. | ||
|
||
* `quadint`: `INT64` quadint to get the KRING_INDEXED from. | ||
* `distance`: `INT64` distance (in cells) to the source. | ||
|
||
**Return type** | ||
|
||
`ARRAY<STRUCT<x INT64, y INT64, idx INT64>>` | ||
|
||
|
||
{{% customSelector %}} | ||
**Example** | ||
{{%/ customSelector %}} | ||
|
||
```sql | ||
SELECT carto-os.quadkey.KRING_INDEXED(4388, 1) mykring_indexed; | ||
-- "mykring_indexed": [ | ||
{ | ||
"x": "-1", | ||
"y": "-1", | ||
"idx": "3844" | ||
}, | ||
{ | ||
"x": "0", | ||
"y": "-1", | ||
"idx": "3876" | ||
}, | ||
{ | ||
"x": "1", | ||
"y": "-1", | ||
"idx": "3908" | ||
}, | ||
{ | ||
"x": "-1", | ||
"y": "0", | ||
"idx": "4356" | ||
}, | ||
{ | ||
"x": "0", | ||
"y": "0", | ||
"idx": "4388" | ||
}, | ||
{ | ||
"x": "1", | ||
"y": "0", | ||
"idx": "4420" | ||
}, | ||
{ | ||
"x": "-1", | ||
"y": "1", | ||
"idx": "4868" | ||
}, | ||
{ | ||
"x": "0", | ||
"y": "1", | ||
"idx": "4900" | ||
}, | ||
{ | ||
"x": "1", | ||
"y": "1", | ||
"idx": "4932" | ||
} | ||
] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.