From 9576dc47a4045ba6c7256736984addc3af5a2af8 Mon Sep 17 00:00:00 2001 From: Valentin de la Cruz Barquero <6054336+vdelacruzb@users.noreply.github.com> Date: Wed, 11 Aug 2021 10:00:38 +0200 Subject: [PATCH] Support geometry collection in Quadint polyfill (#150) --- modules/quadkey/bigquery/CHANGELOG.md | 5 ++ modules/quadkey/bigquery/doc/VERSION.md | 2 +- modules/quadkey/bigquery/package.json | 2 +- .../bigquery/sql/ST_ASQUADINT_POLYFILL.sql | 12 ++- .../integration/ST_ASQUADINT_POLYFILL.test.js | 75 ++++++++++++++++ .../out/polyfill.js | 9 +- modules/quadkey/snowflake/CHANGELOG.md | 5 ++ modules/quadkey/snowflake/doc/VERSION.md | 2 +- modules/quadkey/snowflake/package.json | 2 +- .../snowflake/sql/ST_ASQUADINT_POLYFILL.sql | 12 ++- .../integration/ST_ASQUADINT_POLYFILL.test.js | 85 +++++++++++++++++-- .../out/polyfill.js | 9 +- 12 files changed, 207 insertions(+), 13 deletions(-) diff --git a/modules/quadkey/bigquery/CHANGELOG.md b/modules/quadkey/bigquery/CHANGELOG.md index a93b40641..dace831ef 100644 --- a/modules/quadkey/bigquery/CHANGELOG.md +++ b/modules/quadkey/bigquery/CHANGELOG.md @@ -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 diff --git a/modules/quadkey/bigquery/doc/VERSION.md b/modules/quadkey/bigquery/doc/VERSION.md index a71d0fad2..6508aa567 100644 --- a/modules/quadkey/bigquery/doc/VERSION.md +++ b/modules/quadkey/bigquery/doc/VERSION.md @@ -18,5 +18,5 @@ Returns the current version of the quadkey module. ```sql SELECT carto-os.quadkey.VERSION(); --- 1.0.1 +-- 1.0.3 ``` \ No newline at end of file diff --git a/modules/quadkey/bigquery/package.json b/modules/quadkey/bigquery/package.json index 61e669efa..3cb3f67f2 100644 --- a/modules/quadkey/bigquery/package.json +++ b/modules/quadkey/bigquery/package.json @@ -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", diff --git a/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql b/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql index 1eb8287c0..aade4a251 100644 --- a/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql +++ b/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql @@ -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); """; diff --git a/modules/quadkey/bigquery/test/integration/ST_ASQUADINT_POLYFILL.test.js b/modules/quadkey/bigquery/test/integration/ST_ASQUADINT_POLYFILL.test.js index 76185db28..66c1fa598 100644 --- a/modules/quadkey/bigquery/test/integration/ST_ASQUADINT_POLYFILL.test.js +++ b/modules/quadkey/bigquery/test/integration/ST_ASQUADINT_POLYFILL.test.js @@ -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(); diff --git a/modules/quadkey/bigquery/test/integration/st_asquadint_polyfill_fixtures/out/polyfill.js b/modules/quadkey/bigquery/test/integration/st_asquadint_polyfill_fixtures/out/polyfill.js index 72209d982..6d0a65ff8 100644 --- a/modules/quadkey/bigquery/test/integration/st_asquadint_polyfill_fixtures/out/polyfill.js +++ b/modules/quadkey/bigquery/test/integration/st_asquadint_polyfill_fixtures/out/polyfill.js @@ -13,5 +13,12 @@ module.exports = { 3239308078, 3239832270, 3239832302, 3239832334, 3239832366, 3239832398 - ] + ], + polyfill3: [ + 211899364619734, 211899498837334, + 211899498837366, 211899498837398, + 211899498837430, 211899498837462, + 211899633055062, 211899633055094, + 211899633055126, 211899633055158 + ] } \ No newline at end of file diff --git a/modules/quadkey/snowflake/CHANGELOG.md b/modules/quadkey/snowflake/CHANGELOG.md index 1645e6160..ada717ac7 100644 --- a/modules/quadkey/snowflake/CHANGELOG.md +++ b/modules/quadkey/snowflake/CHANGELOG.md @@ -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 diff --git a/modules/quadkey/snowflake/doc/VERSION.md b/modules/quadkey/snowflake/doc/VERSION.md index a89e938a2..3b3231953 100644 --- a/modules/quadkey/snowflake/doc/VERSION.md +++ b/modules/quadkey/snowflake/doc/VERSION.md @@ -16,5 +16,5 @@ Returns the current version of the quadkey module. ```sql SELECT sfcarto.quadkey.VERSION(); --- 1.0.0 +-- 1.0.1 ``` \ No newline at end of file diff --git a/modules/quadkey/snowflake/package.json b/modules/quadkey/snowflake/package.json index fa6ddf564..9a1b98720 100644 --- a/modules/quadkey/snowflake/package.json +++ b/modules/quadkey/snowflake/package.json @@ -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", diff --git a/modules/quadkey/snowflake/sql/ST_ASQUADINT_POLYFILL.sql b/modules/quadkey/snowflake/sql/ST_ASQUADINT_POLYFILL.sql index e78f83385..e9208f48f 100644 --- a/modules/quadkey/snowflake/sql/ST_ASQUADINT_POLYFILL.sql +++ b/modules/quadkey/snowflake/sql/ST_ASQUADINT_POLYFILL.sql @@ -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); $$; diff --git a/modules/quadkey/snowflake/test/integration/ST_ASQUADINT_POLYFILL.test.js b/modules/quadkey/snowflake/test/integration/ST_ASQUADINT_POLYFILL.test.js index 4ddda36a9..822a49a6e 100644 --- a/modules/quadkey/snowflake/test/integration/ST_ASQUADINT_POLYFILL.test.js +++ b/modules/quadkey/snowflake/test/integration/ST_ASQUADINT_POLYFILL.test.js @@ -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']); @@ -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 = { @@ -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(); }); \ No newline at end of file diff --git a/modules/quadkey/snowflake/test/integration/st_asquadint_polyfill_fixtures/out/polyfill.js b/modules/quadkey/snowflake/test/integration/st_asquadint_polyfill_fixtures/out/polyfill.js index 9a95bb3ab..2831ffb6e 100644 --- a/modules/quadkey/snowflake/test/integration/st_asquadint_polyfill_fixtures/out/polyfill.js +++ b/modules/quadkey/snowflake/test/integration/st_asquadint_polyfill_fixtures/out/polyfill.js @@ -11,5 +11,12 @@ module.exports = { '3239308078', '3239832270', '3239832302', '3239832334', '3239832366', '3239832398' - ] + ], + polyfill3: [ + '211899364619734', '211899498837334', + '211899498837366', '211899498837398', + '211899498837430', '211899498837462', + '211899633055062', '211899633055094', + '211899633055126', '211899633055158' + ] } \ No newline at end of file