Skip to content

Commit

Permalink
Add postgresql join example (#812)
Browse files Browse the repository at this point in the history
  • Loading branch information
julsbreakdown authored and bchapuis committed Dec 11, 2023
1 parent b56b325 commit 7e6528a
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
46 changes: 46 additions & 0 deletions examples/postgresql-join/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
CREATE EXTENSION IF NOT EXISTS postgis;

-- Drop and create schema
DROP SCHEMA IF EXISTS baremaps CASCADE;
CREATE SCHEMA baremaps;

-- Create table baremaps.tree_type
CREATE TABLE IF NOT EXISTS baremaps.tree_type (
id SERIAL PRIMARY KEY,
type VARCHAR(255) NOT NULL
);

-- Populate baremaps.tree_type with 10 different types of trees
INSERT INTO baremaps.tree_type (type) VALUES
('Oak'),
('Maple'),
('Pine'),
('Birch'),
('Spruce'),
('Willow'),
('Cherry'),
('Poplar'),
('Cypress'),
('Cedar');

-- Create table baremaps.point_trees
CREATE TABLE IF NOT EXISTS baremaps.point_trees (
id SERIAL PRIMARY KEY,
geom GEOMETRY(Point, 3857) NOT NULL,
size INTEGER,
tree_type_id INTEGER REFERENCES baremaps.tree_type(id)
);

-- Populate baremaps.point_trees with 1000 random points in France
INSERT INTO baremaps.point_trees (geom, size, tree_type_id)
SELECT
ST_Transform(
ST_SetSRID(ST_MakePoint(
random() * (5.5 - 0.5) + 0.5, -- Longitude range for France
random() * (51.1 - 41.1) + 41.1 -- Latitude range for France
), 4326)
,3857),
floor(random() * 100) + 1, -- Random size between 1 and 100
floor(random() * 10) + 1 -- Random tree_type_id between 1 and 10
FROM generate_series(1, 1000); -- Number of points to generate

41 changes: 41 additions & 0 deletions examples/postgresql-join/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": 8,
"sources": {
"baremaps": {
"type": "vector",
"url": "http://localhost:9000/tiles.json"
}
},
"center": [
2.6231,
48.8404
],
"zoom": 10,
"layers": [
{
"id": "trees",
"type": "circle",
"source": "baremaps",
"source-layer": "trees",
"minzoom": 0,
"maxzoom": 20,
"paint": {
"circle-radius": 6,
"circle-color": [
"match",
["get", "type"],
"Oak", "#33a02c",
"Maple", "#1f78b4",
"Pine", "#e31a1c",
"Birch", "#ff7f00",
"Spruce", "#6a3d9a",
"Poplar", "#fdbf6f",
"Cypress", "#cab2d6",
"Cedar", "#ffff99",
"#000000"
]
}
}
]
}

26 changes: 26 additions & 0 deletions examples/postgresql-join/tileset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"tilejson": "2.1.0",
"tiles": [
"http://localhost:9000/tiles/{z}/{x}/{y}.mvt"
],
"database": "jdbc:postgresql://localhost:5432/baremaps?&user=baremaps&password=baremaps",
"bounds": [
-180,
-90,
180,
90
],
"vector_layers": [
{
"id": "trees",
"queries": [
{
"minzoom": 0,
"maxzoom": 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"
}
]
}
]
}

15 changes: 15 additions & 0 deletions examples/postgresql-join/workflow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"steps": [
{
"id": "postgresql-join",
"needs": [],
"tasks": [
{
"type": "ExecuteSqlScript",
"file": "./init.sql",
"database": "jdbc:postgresql://localhost:5432/baremaps?&user=baremaps&password=baremaps"
}
]
}
]
}

0 comments on commit 7e6528a

Please sign in to comment.