Skip to content

Instantly share code, notes, and snippets.

@maxcollombin
Last active August 2, 2023 11:57
Show Gist options
  • Save maxcollombin/6ef7e65a7ccbc1bc94fe60cf34e54724 to your computer and use it in GitHub Desktop.
Save maxcollombin/6ef7e65a7ccbc1bc94fe60cf34e54724 to your computer and use it in GitHub Desktop.
Create random geometries
-- create points1 table
CREATE TABLE points1 (
id SERIAL PRIMARY KEY,
geom GEOMETRY(Point, 4326),
attr1 TEXT,
attr2 INTEGER
);
-- insert random data into points1 table
INSERT INTO points1 (geom, attr1, attr2)
SELECT ST_SetSRID(ST_MakePoint(random()*360-180, random()*180-90), 4326),
md5(random()::text),
floor(random()*100)
FROM generate_series(1, 100);
-- create points2 table with foreign key to points1 table
CREATE TABLE points2 (
id SERIAL PRIMARY KEY,
geom GEOMETRY(Point, 4326),
attr1 TEXT,
attr2 INTEGER,
fk_id1 INTEGER REFERENCES points1(id)
);
-- insert random data into points2 table
INSERT INTO points2 (geom, attr1, attr2, fk_id1)
SELECT ST_SetSRID(ST_MakePoint(random()*360-180, random()*180-90), 4326),
md5(random()::text),
floor(random()*100),
floor(random()*99)+1
FROM generate_series(1, 100);
-- create joined_points view
CREATE VIEW joined_points AS
SELECT points1.id as id1, points2.id as id2, points1.geom as geom1, points2.geom as geom2,
points1.attr1 as attr1_1, points2.attr1 as attr1_2, points1.attr2 as attr2_1, points2.attr2 as attr2_2
FROM points1
INNER JOIN points2 ON points1.id = points2.fk_id1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment