Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support geometry collection in Quadint polyfill #150

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
]
}
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'
]
}