Skip to content

Commit

Permalink
Support geometry collection in Quadint polyfill (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
vdelacruzb authored Aug 11, 2021
1 parent 0149fb9 commit 9576dc4
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 13 deletions.
5 changes: 5 additions & 0 deletions modules/quadkey/bigquery/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.3] - 2021-08-11

### Fixed
- Support GEOMETRYCOLLECTION from ST_ASQUADINT_POLYFILL.

## [1.0.2] - 2021-08-04

### Added
Expand Down
2 changes: 1 addition & 1 deletion modules/quadkey/bigquery/doc/VERSION.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ Returns the current version of the quadkey module.

```sql
SELECT carto-os.quadkey.VERSION();
-- 1.0.1
-- 1.0.3
```
2 changes: 1 addition & 1 deletion modules/quadkey/bigquery/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quadkey_bigquery",
"version": "1.0.1",
"version": "1.0.3",
"description": "Quadkey module for BigQuery",
"author": "CARTO",
"license": "BSD-3-Clause",
Expand Down
12 changes: 11 additions & 1 deletion modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ AS """
throw new Error('NULL argument passed to UDF');
}
const pol = JSON.parse(geojson);
const quadints = quadkeyLib.geojsonToQuadints(pol, {min_zoom: Number(resolution), max_zoom: Number(resolution)});
let quadints = [];
if (pol.type == 'GeometryCollection') {
pol.geometries.forEach(function (geom) {
quadints = quadints.concat(quadkeyLib.geojsonToQuadints(geom, {min_zoom: Number(resolution), max_zoom: Number(resolution)}));
});
quadints = Array.from(new Set(quadints));
}
else
{
quadints = quadkeyLib.geojsonToQuadints(pol, {min_zoom: Number(resolution), max_zoom: Number(resolution)});
}
return quadints.map(String);
""";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,81 @@ test('ST_ASQUADINT_POLYFILL should work', async () => {
expect(rows[0].polyfill14.sort()).toEqual(polyfillFixturesOut.polyfill2);
});

test('ST_ASQUADINT_POLYFILL should work with GEOMETRYCOLLECTION', async () => {
const feature = {
'type': 'GeometryCollection',
'geometries': [
{
'type': 'LineString',
'coordinates': [
[
-73.96697,
40.59585
],
[
-73.96697,
40.59586
]
]
},
{
'type': 'LineString',
'coordinates': [
[
-73.96697,
40.59585
],
[
-73.96697,
40.59586
]
]
},
{
'type': 'Polygon',
'coordinates': [
[
[
-73.96697,
40.59585
],
[
-73.96697,
40.59584
],
[
-73.96733,
40.5958
],
[
-73.96732,
40.59574
],
[
-73.96695,
40.59578
],
[
-73.96696,
40.5958
],
[
-73.96697,
40.59585
]
]
]
}
]
};
const featureJSON = JSON.stringify(feature);

const query = `SELECT \`@@BQ_PREFIX@@quadkey.ST_ASQUADINT_POLYFILL\`(ST_GEOGFROMGEOJSON('${featureJSON}'), 22) as polyfill22`;
const rows = await runQuery(query);
expect(rows.length).toEqual(1);
expect(rows[0].polyfill22.sort()).toEqual(polyfillFixturesOut.polyfill3);
});

test('ST_ASQUADINT_POLYFILL should fail if any NULL argument', async () => {
let query = 'SELECT `@@BQ_PREFIX@@quadkey.ST_ASQUADINT_POLYFILL`(NULL, 10);';
await expect(runQuery(query)).rejects.toThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ module.exports = {
3239308078, 3239832270,
3239832302, 3239832334,
3239832366, 3239832398
]
],
polyfill3: [
211899364619734, 211899498837334,
211899498837366, 211899498837398,
211899498837430, 211899498837462,
211899633055062, 211899633055094,
211899633055126, 211899633055158
]
}
5 changes: 5 additions & 0 deletions modules/quadkey/snowflake/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.1] - 2021-08-11

### Fixed
- Support GEOMETRYCOLLECTION from ST_ASQUADINT_POLYFILL.

## [1.0.0] - 2021-03-31

### Added
Expand Down
2 changes: 1 addition & 1 deletion modules/quadkey/snowflake/doc/VERSION.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ Returns the current version of the quadkey module.

```sql
SELECT sfcarto.quadkey.VERSION();
-- 1.0.0
-- 1.0.1
```
2 changes: 1 addition & 1 deletion modules/quadkey/snowflake/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quadkey_snowflake",
"version": "1.0.0",
"version": "1.0.1",
"description": "Quadkey module for Snowflake",
"author": "CARTO",
"license": "BSD-3-Clause",
Expand Down
12 changes: 11 additions & 1 deletion modules/quadkey/snowflake/sql/ST_ASQUADINT_POLYFILL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ AS $$
}

const pol = JSON.parse(GEOJSON);
const quadints = quadkeyLib.geojsonToQuadints(pol, {min_zoom: RESOLUTION, max_zoom: RESOLUTION});
let quadints = [];
if (pol.type == 'GeometryCollection') {
pol.geometries.forEach(function (geom) {
quadints = quadints.concat(quadkeyLib.geojsonToQuadints(geom, {min_zoom: RESOLUTION, max_zoom: RESOLUTION}));
});
quadints = Array.from(new Set(quadints));
}
else
{
quadints = quadkeyLib.geojsonToQuadints(pol, {min_zoom: RESOLUTION, max_zoom: RESOLUTION});
}
return quadints.map(String);
$$;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ test('ST_ASQUADINT_POLYFILL should work', async () => {
};
const featureJSON = JSON.stringify(feature);

const query = `SELECT @@SF_PREFIX@@quadkey._POLYFILL_FROMGEOJSON('${featureJSON}', 10) as polyfill10,
@@SF_PREFIX@@quadkey._POLYFILL_FROMGEOJSON('${featureJSON}', 14) as polyfill14`;
const query = `SELECT @@SF_PREFIX@@quadkey.ST_ASQUADINT_POLYFILL(TO_GEOGRAPHY('${featureJSON}'), 10) as polyfill10,
@@SF_PREFIX@@quadkey.ST_ASQUADINT_POLYFILL(TO_GEOGRAPHY('${featureJSON}'), 14) as polyfill14`;
const rows = await runQuery(query);
expect(rows.length).toEqual(1);
expect(rows[0]['POLYFILL10'].sort()).toEqual(['12631722', '12664490']);
Expand All @@ -67,8 +67,83 @@ test('ST_ASQUADINT_POLYFILL should work', async () => {
expect(rows[0]['POLYFILL14'].sort()).toEqual(polyfillFixturesOut.polyfill2);
});

test('__POLYFILL_FROMGEOJSON should fail if any NULL argument', async () => {
let query = 'SELECT @@SF_PREFIX@@quadkey._POLYFILL_FROMGEOJSON(NULL, 10);';
test('ST_ASQUADINT_POLYFILL should work with GEOMETRYCOLLECTION', async () => {
const feature = {
'type': 'GeometryCollection',
'geometries': [
{
'type': 'LineString',
'coordinates': [
[
-73.96697,
40.59585
],
[
-73.96697,
40.59586
]
]
},
{
'type': 'LineString',
'coordinates': [
[
-73.96697,
40.59585
],
[
-73.96697,
40.59586
]
]
},
{
'type': 'Polygon',
'coordinates': [
[
[
-73.96697,
40.59585
],
[
-73.96697,
40.59584
],
[
-73.96733,
40.5958
],
[
-73.96732,
40.59574
],
[
-73.96695,
40.59578
],
[
-73.96696,
40.5958
],
[
-73.96697,
40.59585
]
]
]
}
]
};
const featureJSON = JSON.stringify(feature);

const query = `SELECT @@SF_PREFIX@@quadkey.ST_ASQUADINT_POLYFILL(TO_GEOGRAPHY('${featureJSON}'), 22) as polyfill22`;
const rows = await runQuery(query);
expect(rows.length).toEqual(1);
expect(rows[0]['POLYFILL22'].sort()).toEqual(polyfillFixturesOut.polyfill3);
});

test('ST_ASQUADINT_POLYFILL should fail if any NULL argument', async () => {
let query = 'SELECT @@SF_PREFIX@@quadkey.ST_ASQUADINT_POLYFILL(NULL, 10);';
await expect(runQuery(query)).rejects.toThrow();

const feature = {
Expand All @@ -88,6 +163,6 @@ test('__POLYFILL_FROMGEOJSON should fail if any NULL argument', async () => {
};
const featureJSON = JSON.stringify(feature);

query = `SELECT @@SF_PREFIX@@quadkey._POLYFILL_FROMGEOJSON('${featureJSON}', NULL)`;
query = `SELECT @@SF_PREFIX@@quadkey.ST_ASQUADINT_POLYFILL(TO_GEOGRAPHY('${featureJSON}'), NULL)`;
await expect(runQuery(query)).rejects.toThrow();
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,12 @@ module.exports = {
'3239308078', '3239832270',
'3239832302', '3239832334',
'3239832366', '3239832398'
]
],
polyfill3: [
'211899364619734', '211899498837334',
'211899498837366', '211899498837398',
'211899498837430', '211899498837462',
'211899633055062', '211899633055094',
'211899633055126', '211899633055158'
]
}

0 comments on commit 9576dc4

Please sign in to comment.