From 32efe375a443012522d199d3626e58b61db541f7 Mon Sep 17 00:00:00 2001 From: vdelacruzb Date: Tue, 10 Aug 2021 12:03:34 +0200 Subject: [PATCH 1/7] test geometry collection --- modules/quadkey/bigquery/lib/index.js | 3 +++ modules/quadkey/bigquery/package.json | 3 ++- .../quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/modules/quadkey/bigquery/lib/index.js b/modules/quadkey/bigquery/lib/index.js index 4b74eb492..c52f2ed19 100644 --- a/modules/quadkey/bigquery/lib/index.js +++ b/modules/quadkey/bigquery/lib/index.js @@ -15,6 +15,8 @@ import { ZXYFromQuadint } from './quadkey'; +import { geometryCollection } from '@turf/helpers'; + export default { bbox, kring, @@ -29,5 +31,6 @@ export default { quadintFromZXY, geojsonToQuadints, ZXYFromQuadint, + geometryCollection, version }; \ No newline at end of file diff --git a/modules/quadkey/bigquery/package.json b/modules/quadkey/bigquery/package.json index 61e669efa..2eb7be567 100644 --- a/modules/quadkey/bigquery/package.json +++ b/modules/quadkey/bigquery/package.json @@ -7,6 +7,7 @@ "private": true, "dependencies": { "@mapbox/tile-cover": "3.0.2", - "@mapbox/tilebelt": "1.0.2" + "@mapbox/tilebelt": "1.0.2", + "@turf/helpers": "^6.5.0" } } diff --git a/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql b/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql index 1eb8287c0..43d89a8a4 100644 --- a/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql +++ b/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql @@ -17,6 +17,18 @@ AS """ return quadints.map(String); """; +CREATE OR REPLACE FUNCTION `@@BQ_PREFIX@@quadkey.__geometrycollection` +(geojson STRING) +RETURNS STRING +DETERMINISTIC +LANGUAGE js +OPTIONS (library=["@@BQ_LIBRARY_BUCKET@@"]) +AS """ + const pol = JSON.parse(geojson); + return JSON.stringify(quadkeyLib.geometrycollection(pol)) +"""; + + CREATE OR REPLACE FUNCTION `@@BQ_PREFIX@@quadkey.ST_ASQUADINT_POLYFILL` (geog GEOGRAPHY, resolution INT64) RETURNS ARRAY From 3973c913cb7e109a520d9687b2472544c043cf7e Mon Sep 17 00:00:00 2001 From: vdelacruzb Date: Tue, 10 Aug 2021 12:04:43 +0200 Subject: [PATCH 2/7] add comments --- modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql b/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql index 43d89a8a4..889ad2c1d 100644 --- a/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql +++ b/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql @@ -26,6 +26,10 @@ OPTIONS (library=["@@BQ_LIBRARY_BUCKET@@"]) AS """ const pol = JSON.parse(geojson); return JSON.stringify(quadkeyLib.geometrycollection(pol)) + // Iterate through collection features + // Concat quadints arrays + // Add all of them into a SET + // return the SET casted to array """; From 5d4d958d3d99b56c533e60b67a82ef6eb28fbedd Mon Sep 17 00:00:00 2001 From: vdelacruzb Date: Tue, 10 Aug 2021 16:17:09 +0200 Subject: [PATCH 3/7] update polyfill function --- modules/quadkey/bigquery/lib/index.js | 3 -- modules/quadkey/bigquery/package.json | 3 +- .../bigquery/sql/ST_ASQUADINT_POLYFILL.sql | 28 ++++++++----------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/modules/quadkey/bigquery/lib/index.js b/modules/quadkey/bigquery/lib/index.js index c52f2ed19..4b74eb492 100644 --- a/modules/quadkey/bigquery/lib/index.js +++ b/modules/quadkey/bigquery/lib/index.js @@ -15,8 +15,6 @@ import { ZXYFromQuadint } from './quadkey'; -import { geometryCollection } from '@turf/helpers'; - export default { bbox, kring, @@ -31,6 +29,5 @@ export default { quadintFromZXY, geojsonToQuadints, ZXYFromQuadint, - geometryCollection, version }; \ No newline at end of file diff --git a/modules/quadkey/bigquery/package.json b/modules/quadkey/bigquery/package.json index 2eb7be567..61e669efa 100644 --- a/modules/quadkey/bigquery/package.json +++ b/modules/quadkey/bigquery/package.json @@ -7,7 +7,6 @@ "private": true, "dependencies": { "@mapbox/tile-cover": "3.0.2", - "@mapbox/tilebelt": "1.0.2", - "@turf/helpers": "^6.5.0" + "@mapbox/tilebelt": "1.0.2" } } diff --git a/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql b/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql index 889ad2c1d..aade4a251 100644 --- a/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql +++ b/modules/quadkey/bigquery/sql/ST_ASQUADINT_POLYFILL.sql @@ -13,26 +13,20 @@ 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); """; -CREATE OR REPLACE FUNCTION `@@BQ_PREFIX@@quadkey.__geometrycollection` -(geojson STRING) -RETURNS STRING -DETERMINISTIC -LANGUAGE js -OPTIONS (library=["@@BQ_LIBRARY_BUCKET@@"]) -AS """ - const pol = JSON.parse(geojson); - return JSON.stringify(quadkeyLib.geometrycollection(pol)) - // Iterate through collection features - // Concat quadints arrays - // Add all of them into a SET - // return the SET casted to array -"""; - - CREATE OR REPLACE FUNCTION `@@BQ_PREFIX@@quadkey.ST_ASQUADINT_POLYFILL` (geog GEOGRAPHY, resolution INT64) RETURNS ARRAY From cb0a717a9e1e3a08fa4cd88d306f70f4e4b523f6 Mon Sep 17 00:00:00 2001 From: vdelacruzb Date: Tue, 10 Aug 2021 16:53:55 +0200 Subject: [PATCH 4/7] add bigquery test --- .../integration/ST_ASQUADINT_POLYFILL.test.js | 62 +++++++++++++++++++ .../out/polyfill.js | 9 ++- 2 files changed, 70 insertions(+), 1 deletion(-) 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..fe58a285c 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,68 @@ 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': '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 From da8a05306248a14444dd8d09a78c6100bcab4843 Mon Sep 17 00:00:00 2001 From: vdelacruzb Date: Tue, 10 Aug 2021 16:54:10 +0200 Subject: [PATCH 5/7] fix the same issue in snowflake --- .../snowflake/sql/ST_ASQUADINT_POLYFILL.sql | 12 +++- .../integration/ST_ASQUADINT_POLYFILL.test.js | 72 +++++++++++++++++-- .../out/polyfill.js | 9 ++- 3 files changed, 86 insertions(+), 7 deletions(-) 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..617cc8771 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,70 @@ 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': '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 +150,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 From d8533a9c58f1552f861070e577e197495bf5256b Mon Sep 17 00:00:00 2001 From: vdelacruzb Date: Tue, 10 Aug 2021 17:07:26 +0200 Subject: [PATCH 6/7] add duplicated feature to tests --- .../integration/ST_ASQUADINT_POLYFILL.test.js | 15 ++++++++++++++- .../integration/ST_ASQUADINT_POLYFILL.test.js | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) 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 fe58a285c..66c1fa598 100644 --- a/modules/quadkey/bigquery/test/integration/ST_ASQUADINT_POLYFILL.test.js +++ b/modules/quadkey/bigquery/test/integration/ST_ASQUADINT_POLYFILL.test.js @@ -82,7 +82,20 @@ test('ST_ASQUADINT_POLYFILL should work with GEOMETRYCOLLECTION', async () => { 40.59586 ] ] - }, + }, + { + 'type': 'LineString', + 'coordinates': [ + [ + -73.96697, + 40.59585 + ], + [ + -73.96697, + 40.59586 + ] + ] + }, { 'type': 'Polygon', 'coordinates': [ 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 617cc8771..822a49a6e 100644 --- a/modules/quadkey/snowflake/test/integration/ST_ASQUADINT_POLYFILL.test.js +++ b/modules/quadkey/snowflake/test/integration/ST_ASQUADINT_POLYFILL.test.js @@ -83,7 +83,20 @@ test('ST_ASQUADINT_POLYFILL should work with GEOMETRYCOLLECTION', async () => { 40.59586 ] ] - }, + }, + { + 'type': 'LineString', + 'coordinates': [ + [ + -73.96697, + 40.59585 + ], + [ + -73.96697, + 40.59586 + ] + ] + }, { 'type': 'Polygon', 'coordinates': [ From 6ffd4c369997bea463a79b0e79fb30b6626aa49d Mon Sep 17 00:00:00 2001 From: vdelacruzb Date: Wed, 11 Aug 2021 09:57:25 +0200 Subject: [PATCH 7/7] update changelogs --- modules/quadkey/bigquery/CHANGELOG.md | 5 +++++ modules/quadkey/bigquery/doc/VERSION.md | 2 +- modules/quadkey/bigquery/package.json | 2 +- modules/quadkey/snowflake/CHANGELOG.md | 5 +++++ modules/quadkey/snowflake/doc/VERSION.md | 2 +- modules/quadkey/snowflake/package.json | 2 +- 6 files changed, 14 insertions(+), 4 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/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",