-
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.
Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Javier Goizueta <[email protected]> Co-authored-by: Alberto Hernández <[email protected]> Co-authored-by: Jesús Arroyo Torrens <[email protected]> Co-authored-by: Pedro-Juan Ferrer <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
3f7804b
commit 532fc22
Showing
25 changed files
with
98 additions
and
577 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
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 |
---|---|---|
@@ -1,5 +1,24 @@ | ||
import { bbox } from '@turf/turf'; | ||
import { bbox, booleanPointInPolygon, randomPoint } from '@turf/turf'; | ||
|
||
function generateRandomPointsInPolygon (polygon, numPoints) { | ||
const randomPoints = []; | ||
while (randomPoints.length < numPoints) { | ||
const point = randomPoint(1, { bbox: bbox(polygon) }).features[0]; | ||
if (booleanPointInPolygon(point, polygon)) { | ||
randomPoints.push(JSON.stringify(point.geometry)); | ||
} | ||
} | ||
return randomPoints; | ||
} | ||
|
||
function generateRandomPointInPolygon (polygon) { | ||
let point | ||
do { | ||
point = randomPoint(1, { bbox: bbox(polygon) }).features[0]; | ||
} while (!booleanPointInPolygon(point, polygon)) | ||
return JSON.stringify(point.geometry); | ||
} | ||
|
||
export default { | ||
bbox | ||
generateRandomPointsInPolygon | ||
}; |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
const lib = require('../build/index'); | ||
|
||
test('random library defined', () => { | ||
expect(lib.random.bbox).toBeDefined(); | ||
expect(lib.random.generateRandomPointsInPolygon).toBeDefined(); | ||
}); |
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
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 |
---|---|---|
@@ -1,74 +1,23 @@ | ||
---------------------------- | ||
-- Copyright (C) 2021 CARTO | ||
---------------------------- | ||
-------------------------------- | ||
-- Copyright (C) 2021-2024 CARTO | ||
-------------------------------- | ||
|
||
CREATE OR REPLACE FUNCTION `@@BQ_DATASET@@.__ST_GENERATEPOINTS` | ||
(geojson STRING, npoints INT64) | ||
RETURNS ARRAY<STRING> | ||
DETERMINISTIC | ||
LANGUAGE js | ||
OPTIONS (library = ["@@BQ_LIBRARY_BUCKET@@"]) | ||
AS """ | ||
return lib.random.generateRandomPointsInPolygon(JSON.parse(geojson), npoints); | ||
"""; | ||
|
||
CREATE OR REPLACE FUNCTION `@@BQ_DATASET@@.ST_GENERATEPOINTS` | ||
(geog GEOGRAPHY, npoints INT64) | ||
RETURNS ARRAY<GEOGRAPHY> | ||
AS ( | ||
( | ||
WITH bbox AS ( | ||
-- compute the bounding box of the polygon | ||
SELECT `@@BQ_DATASET@@.__BBOX_FROM_GEOJSON`(ST_ASGEOJSON(geog)) AS box | ||
), | ||
|
||
bbox_coords AS ( | ||
-- break down the bbox array into minx, miny, maxx, maxy | ||
SELECT | ||
box[ORDINAL(1)] AS minx, | ||
box[ORDINAL(2)] AS miny, | ||
box[ORDINAL(3)] AS maxx, | ||
box[ORDINAL(4)] AS maxy | ||
FROM bbox | ||
), | ||
|
||
bbox_data AS ( | ||
-- compute area of bbox and put some handy values here too | ||
SELECT | ||
minx, | ||
miny, | ||
maxx, | ||
maxy, | ||
1.2 AS k, -- security factor to make it more likely that at least npoints fall within the polygon | ||
ST_AREA( | ||
ST_MAKEPOLYGON(ST_MAKELINE([ | ||
ST_GEOGPOINT(minx, miny), | ||
ST_GEOGPOINT(minx, maxy), | ||
ST_GEOGPOINT(maxx, maxy), | ||
ST_GEOGPOINT(maxx, miny), | ||
ST_GEOGPOINT(minx, miny) | ||
])) | ||
) AS bbox_area | ||
FROM bbox_coords | ||
), | ||
|
||
point_seeds AS ( | ||
-- generate enough values so that we will hopefully have at least npoints of them randomly placed inside geog | ||
SELECT | ||
GENERATE_ARRAY(1, CEIL(k * npoints * bbox_area / ST_AREA(geog))) AS i, | ||
SIN(miny * ACOS(-1) / 180.0) AS minsin, | ||
SIN(maxy * ACOS(-1) / 180.0) AS maxsin, | ||
180.0 / ACOS(-1) AS radtodeg | ||
FROM bbox_data | ||
), | ||
|
||
bbox_points AS ( | ||
-- compute the random points uniformly in the bbox; | ||
SELECT ST_GEOGPOINT(minx + RAND() * (maxx - minx), radtodeg * ASIN(minsin + RAND() * (maxsin - minsin))) AS point | ||
FROM bbox_coords, point_seeds, UNNEST(i) | ||
), | ||
|
||
poly_points AS ( | ||
-- now we need to select the points inside the polygon and number them, so we can limit | ||
-- the end result to npoints (note that we can't have a dynamic LIMIT) | ||
SELECT | ||
point, | ||
ROW_NUMBER() OVER () AS rn | ||
FROM bbox_points | ||
WHERE ST_WITHIN(point, geog) | ||
) | ||
|
||
-- finally select at most npoints and return them in an array | ||
SELECT ARRAY(SELECT point FROM poly_points WHERE rn <= npoints) | ||
SELECT ARRAY_AGG(ST_GEOGFROMGEOJSON(point)) | ||
FROM UNNEST(`@@BQ_DATASET@@.__ST_GENERATEPOINTS`(ST_ASGEOJSON(geog), npoints)) AS point | ||
) | ||
); |
13 changes: 0 additions & 13 deletions
13
clouds/bigquery/modules/sql/random/__BBOX_FROM_GEOJSON.sql
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1 @@ | ||
1.2.0 | ||
1.2.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 |
---|---|---|
@@ -1 +1 @@ | ||
Jinja2==3.1.2 | ||
Jinja2==3.1.3 |
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
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
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
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.