Skip to content

Commit 0dd52e9

Browse files
julsbreakdownbchapuis
authored andcommitted
Add postgresql join example (#812)
1 parent b56b325 commit 0dd52e9

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

examples/postgresql-join/init.sql

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
CREATE EXTENSION IF NOT EXISTS postgis;
2+
3+
-- Drop and create schema
4+
DROP SCHEMA IF EXISTS baremaps CASCADE;
5+
CREATE SCHEMA baremaps;
6+
7+
-- Create table baremaps.tree_type
8+
CREATE TABLE IF NOT EXISTS baremaps.tree_type (
9+
id SERIAL PRIMARY KEY,
10+
type VARCHAR(255) NOT NULL
11+
);
12+
13+
-- Populate baremaps.tree_type with 10 different types of trees
14+
INSERT INTO baremaps.tree_type (type) VALUES
15+
('Oak'),
16+
('Maple'),
17+
('Pine'),
18+
('Birch'),
19+
('Spruce'),
20+
('Willow'),
21+
('Cherry'),
22+
('Poplar'),
23+
('Cypress'),
24+
('Cedar');
25+
26+
-- Create table baremaps.point_trees
27+
CREATE TABLE IF NOT EXISTS baremaps.point_trees (
28+
id SERIAL PRIMARY KEY,
29+
geom GEOMETRY(Point, 3857) NOT NULL,
30+
size INTEGER,
31+
tree_type_id INTEGER REFERENCES baremaps.tree_type(id)
32+
);
33+
34+
-- Populate baremaps.point_trees with 1000 random points in France
35+
INSERT INTO baremaps.point_trees (geom, size, tree_type_id)
36+
SELECT
37+
ST_Transform(
38+
ST_SetSRID(ST_MakePoint(
39+
random() * (5.5 - 0.5) + 0.5, -- Longitude range for France
40+
random() * (51.1 - 41.1) + 41.1 -- Latitude range for France
41+
), 4326)
42+
,3857),
43+
floor(random() * 100) + 1, -- Random size between 1 and 100
44+
floor(random() * 10) + 1 -- Random tree_type_id between 1 and 10
45+
FROM generate_series(1, 1000); -- Number of points to generate
46+

examples/postgresql-join/style.json

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": 8,
3+
"sources": {
4+
"baremaps": {
5+
"type": "vector",
6+
"url": "http://localhost:9000/tiles.json"
7+
}
8+
},
9+
"center": [
10+
2.6231,
11+
48.8404
12+
],
13+
"zoom": 10,
14+
"layers": [
15+
{
16+
"id": "trees",
17+
"type": "circle",
18+
"source": "baremaps",
19+
"source-layer": "trees",
20+
"minzoom": 0,
21+
"maxzoom": 20,
22+
"paint": {
23+
"circle-radius": 6,
24+
"circle-color": [
25+
"match",
26+
["get", "type"],
27+
"Oak", "#33a02c",
28+
"Maple", "#1f78b4",
29+
"Pine", "#e31a1c",
30+
"Birch", "#ff7f00",
31+
"Spruce", "#6a3d9a",
32+
"Poplar", "#fdbf6f",
33+
"Cypress", "#cab2d6",
34+
"Cedar", "#ffff99",
35+
"#000000"
36+
]
37+
}
38+
}
39+
]
40+
}
41+

examples/postgresql-join/tileset.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"tilejson": "2.1.0",
3+
"tiles": [
4+
"http://localhost:9000/tiles/{z}/{x}/{y}.mvt"
5+
],
6+
"database": "jdbc:postgresql://localhost:5432/baremaps?&user=baremaps&password=baremaps",
7+
"bounds": [
8+
-180,
9+
-90,
10+
180,
11+
90
12+
],
13+
"vector_layers": [
14+
{
15+
"id": "trees",
16+
"queries": [
17+
{
18+
"minzoom": 0,
19+
"maxzoom": 20,
20+
"sql": "SELECT pt.id::integer as id, jsonb_build_object('type', tt.type, 'size', pt.size) as tags, pt.geom as geom FROM baremaps.point_trees pt JOIN baremaps.tree_type tt ON pt.tree_type_id = tt.id"
21+
}
22+
]
23+
}
24+
]
25+
}
26+
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"steps": [
3+
{
4+
"id": "postgresql-join",
5+
"needs": [],
6+
"tasks": [
7+
{
8+
"type": "ExecuteSqlScript",
9+
"file": "./init.sql",
10+
"database": "jdbc:postgresql://localhost:5432/baremaps?&user=baremaps&password=baremaps"
11+
}
12+
]
13+
}
14+
]
15+
}

0 commit comments

Comments
 (0)