Skip to content

Instantly share code, notes, and snippets.

@pigreco
Created May 15, 2019 11:58
Show Gist options
  • Save pigreco/0baedb2f7249ba8df8bc16b417880637 to your computer and use it in GitHub Desktop.
Save pigreco/0baedb2f7249ba8df8bc16b417880637 to your computer and use it in GitHub Desktop.
Genera 1000 punti random all'interno di un BoundingBox di un poligono
-- creo una tabella vuota che conterrà i random point
CREATE TABLE point_bb
(id INTEGER PRIMARY KEY AUTOINCREMENT,cir_id INTEGER);
-- popolo ID tabella
INSERT INTO point_bb
WITH RECURSIVE
cnt(id) AS ( SELECT 1 UNION ALL SELECT id + 1 FROM cnt LIMIT 1000)
SELECT id,0 FROM cnt;
-- aggiorno tabella con i punti casuali usando bounding box del poligono
SELECT AddGeometryColumn ('point_bb','geom',4326,'POINT','XY');
UPDATE point_bb SET
geom = (SELECT makepoint (
CAST ((select (0.5 - RANDOM()/CAST(-9223372036854775808 AS REAL)/ 2)*(extent_max_x - extent_min_x) + extent_min_x
FROM "vector_layers_statistics" WHERE id=point_bb.id AND table_name = 'poly') AS REAL),
CAST ((select (0.5 - RANDOM()/CAST(-9223372036854775808 AS REAL)/ 2)*(extent_max_y - extent_min_y) + extent_min_y
FROM "vector_layers_statistics" WHERE id=point_bb.id AND table_name = 'poly') AS REAL), 4326) WHERE id=point_bb.id);
@pigreco
Copy link
Author

pigreco commented May 15, 2019

Ecco output in QGIS:

image

qui database sqlite con poligono:
https://bit.ly/2HlvAoQ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment