Skip to content

Commit 6e0b627

Browse files
committed
update examples
1 parent e81e371 commit 6e0b627

File tree

6 files changed

+275
-2
lines changed

6 files changed

+275
-2
lines changed

data/functions/hexagon.sql

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
-- Custom "hexagon" layer
2+
-- Adapted from https://github.com/CrunchyData/pg_tileserv
3+
--
4+
--
5+
-- Given an input ZXY tile coordinate, output a set of hexagons
6+
-- (and hexagon coordinates) in web mercator that cover that tile
7+
CREATE OR REPLACE FUNCTION tilehexagons(
8+
bounds geometry,
9+
step integer,
10+
epsg integer,
11+
OUT geom geometry(Polygon, 3857), OUT i integer, OUT j integer)
12+
RETURNS SETOF record AS $$
13+
DECLARE
14+
maxbounds geometry := ST_TileEnvelope(0, 0, 0);
15+
edge float8;
16+
BEGIN
17+
edge := (ST_XMax(bounds) - ST_XMin(bounds)) / pow(2, step);
18+
FOR geom, i, j IN
19+
SELECT ST_SetSRID(hexagon(h.i, h.j, edge), epsg), h.i, h.j
20+
FROM hexagoncoordinates(bounds, edge) h
21+
LOOP
22+
IF maxbounds ~ geom AND bounds && geom THEN
23+
RETURN NEXT;
24+
END IF;
25+
END LOOP;
26+
END;
27+
$$
28+
LANGUAGE 'plpgsql'
29+
IMMUTABLE
30+
STRICT
31+
PARALLEL SAFE;
32+
33+
-- Given coordinates in the hexagon tiling that has this
34+
-- edge size, return the built-out hexagon
35+
CREATE OR REPLACE FUNCTION hexagon(
36+
i integer,
37+
j integer,
38+
edge float8
39+
)
40+
RETURNS geometry AS $$
41+
DECLARE
42+
h float8 := edge*cos(pi()/6.0);
43+
cx float8 := 1.5*i*edge;
44+
cy float8 := h*(2*j+abs(i%2));
45+
BEGIN
46+
RETURN ST_MakePolygon(ST_MakeLine(ARRAY[
47+
ST_MakePoint(cx - 1.0*edge, cy + 0),
48+
ST_MakePoint(cx - 0.5*edge, cy + -1*h),
49+
ST_MakePoint(cx + 0.5*edge, cy + -1*h),
50+
ST_MakePoint(cx + 1.0*edge, cy + 0),
51+
ST_MakePoint(cx + 0.5*edge, cy + h),
52+
ST_MakePoint(cx - 0.5*edge, cy + h),
53+
ST_MakePoint(cx - 1.0*edge, cy + 0)
54+
]));
55+
END;
56+
$$
57+
LANGUAGE 'plpgsql'
58+
IMMUTABLE
59+
STRICT
60+
PARALLEL SAFE;
61+
62+
-- Given a square bounds, find all the hexagonal cells
63+
-- of a hex tiling (determined by edge size)
64+
-- that might cover that square (slightly over-determined)
65+
CREATE OR REPLACE FUNCTION hexagoncoordinates(
66+
bounds geometry,
67+
edge float8,
68+
OUT i integer,
69+
OUT j integer
70+
)
71+
RETURNS SETOF record AS $$
72+
DECLARE
73+
h float8 := edge*cos(pi()/6);
74+
mini integer := floor(st_xmin(bounds) / (1.5*edge));
75+
minj integer := floor(st_ymin(bounds) / (2*h));
76+
maxi integer := ceil(st_xmax(bounds) / (1.5*edge));
77+
maxj integer := ceil(st_ymax(bounds) / (2*h));
78+
BEGIN
79+
FOR i, j IN
80+
SELECT a, b
81+
FROM generate_series(mini, maxi) a,
82+
generate_series(minj, maxj) b
83+
LOOP
84+
RETURN NEXT;
85+
END LOOP;
86+
END;
87+
$$
88+
LANGUAGE 'plpgsql'
89+
IMMUTABLE
90+
STRICT
91+
PARALLEL SAFE;
92+
93+
-- Given an input tile, generate the covering hexagons Step parameter determines
94+
-- how many hexagons to generate per tile.
95+
CREATE OR REPLACE FUNCTION hexagon(
96+
-- mandatory parameters
97+
xmin float,
98+
ymin float,
99+
xmax float,
100+
ymax float,
101+
epsg integer,
102+
-- additional parameters
103+
query_params json
104+
)
105+
RETURNS bytea AS $$
106+
DECLARE
107+
result bytea;
108+
step integer;
109+
bounds geometry;
110+
BEGIN
111+
-- Find the bbox bounds
112+
bounds := ST_MakeEnvelope(xmin, ymin, xmax, ymax, epsg);
113+
114+
step := coalesce((query_params ->> 'step')::int, 4);
115+
116+
WITH
117+
rows AS (
118+
-- All the hexes that interact with this tile
119+
SELECT h.i, h.j, h.geom
120+
FROM TileHexagons(bounds, step, epsg) h
121+
),
122+
mvt AS (
123+
-- Usual tile processing, ST_AsMVTGeom simplifies, quantizes,
124+
-- and clips to tile boundary
125+
SELECT ST_AsMVTGeom(rows.geom, bounds) AS geom, rows.i, rows.j
126+
FROM rows
127+
)
128+
-- Generate MVT encoding of final input record
129+
SELECT ST_AsMVT(mvt, 'default')
130+
INTO result
131+
FROM mvt;
132+
133+
RETURN result;
134+
END;
135+
$$
136+
LANGUAGE 'plpgsql'
137+
STABLE
138+
STRICT
139+
PARALLEL SAFE;
140+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
CREATE OR REPLACE FUNCTION landsat_poly_centroid(
2+
-- mandatory parameters
3+
xmin float,
4+
ymin float,
5+
xmax float,
6+
ymax float,
7+
epsg integer,
8+
-- additional parameters
9+
query_params json
10+
)
11+
RETURNS bytea
12+
AS $$
13+
DECLARE
14+
bounds geometry;
15+
tablename text;
16+
result bytea;
17+
BEGIN
18+
WITH
19+
-- Create bbox enveloppe in given EPSG
20+
bounds AS (
21+
SELECT ST_MakeEnvelope(xmin, ymin, xmax, ymax, epsg) AS geom
22+
),
23+
selected_geom AS (
24+
SELECT t.*
25+
FROM public.landsat_wrs t, bounds
26+
WHERE ST_Intersects(t.geom, ST_Transform(bounds.geom, 4326))
27+
),
28+
mvtgeom AS (
29+
SELECT
30+
ST_AsMVTGeom(ST_Transform(ST_Centroid(t.geom), epsg), bounds.geom) AS geom, t.path, t.row
31+
FROM selected_geom t, bounds
32+
UNION
33+
SELECT ST_AsMVTGeom(ST_Transform(t.geom, epsg), bounds.geom) AS geom, t.path, t.row
34+
FROM selected_geom t, bounds
35+
)
36+
SELECT ST_AsMVT(mvtgeom.*, 'default')
37+
38+
-- Put the query result into the result variale.
39+
INTO result FROM mvtgeom;
40+
41+
-- Return the answer
42+
RETURN result;
43+
END;
44+
$$
45+
LANGUAGE 'plpgsql'
46+
IMMUTABLE -- Same inputs always give same outputs
47+
STRICT -- Null input gets null output
48+
PARALLEL SAFE;
49+
50+
COMMENT ON FUNCTION landsat_poly_centroid IS 'Return Combined Polygon/Centroid geometries from landsat table.';

data/functions/squares.sql

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
CREATE OR REPLACE FUNCTION squares(
2+
-- mandatory parameters
3+
xmin float,
4+
ymin float,
5+
xmax float,
6+
ymax float,
7+
epsg integer,
8+
-- additional parameters
9+
query_params json
10+
)
11+
RETURNS bytea AS $$
12+
DECLARE
13+
result bytea;
14+
sq_width float;
15+
bbox_xmin float;
16+
bbox_ymin float;
17+
bounds geometry;
18+
depth integer;
19+
BEGIN
20+
-- Find the bbox bounds
21+
bounds := ST_MakeEnvelope(xmin, ymin, xmax, ymax, epsg);
22+
23+
-- Find the bottom corner of the bounds
24+
bbox_xmin := ST_XMin(bounds);
25+
bbox_ymin := ST_YMin(bounds);
26+
27+
-- We want bbox divided up into depth*depth squares per bbox,
28+
-- so what is the width of a square?
29+
depth := coalesce((query_params ->> 'depth')::int, 2);
30+
31+
sq_width := (ST_XMax(bounds) - ST_XMin(bounds)) / depth;
32+
33+
WITH mvtgeom AS (
34+
SELECT
35+
-- Fill in the bbox with all the squares
36+
ST_AsMVTGeom(
37+
ST_SetSRID(
38+
ST_MakeEnvelope(
39+
bbox_xmin + sq_width * (a - 1),
40+
bbox_ymin + sq_width * (b - 1),
41+
bbox_xmin + sq_width * a,
42+
bbox_ymin + sq_width * b
43+
),
44+
epsg
45+
),
46+
bounds
47+
)
48+
49+
-- Drive the square generator with a two-dimensional
50+
-- generate_series setup
51+
FROM generate_series(1, depth) a, generate_series(1, depth) b
52+
)
53+
SELECT ST_AsMVT(mvtgeom.*, 'default')
54+
55+
-- Put the query result into the result variale.
56+
INTO result FROM mvtgeom;
57+
58+
-- Return the answer
59+
RETURN result;
60+
END;
61+
$$
62+
LANGUAGE 'plpgsql'
63+
IMMUTABLE -- Same inputs always give same outputs
64+
STRICT -- Null input gets null output
65+
PARALLEL SAFE;

demo/leaflet/index_3035.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
}
3232
);
3333

34-
var mapboxUrl = "http://0.0.0.0:8081/tiles/EuropeanETRS89_LAEAQuad/public.countries/{z}/{x}/{y}";
34+
var mapboxUrl = "http://127.0.0.1:8000/tiles/EuropeanETRS89_LAEAQuad/public.countries/{z}/{x}/{y}";
3535

3636
var mapboxVectorTileOptions = {
3737
rendererFactory: L.canvas.tile,

demo/leaflet/index_4326.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
continuousWorld: true,
2727
}).addTo(map)
2828

29-
var mapboxUrl = "http://0.0.0.0:8081/tiles/WGS1984Quad/public.countries/{z}/{x}/{y}.pbf";
29+
var mapboxUrl = "http://127.0.0.1:8000/tiles/WGS1984Quad/public.countries/{z}/{x}/{y}";
3030

3131
var mapboxVectorTileOptions = {
3232
rendererFactory: L.canvas.tile,

docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,21 @@ services:
3737
command: postgres -N 500
3838
volumes:
3939
- ./.pgdata:/var/lib/postgresql/data
40+
41+
# pg_tileserv:
42+
# image: pramsey/pg_tileserv:latest
43+
# environment:
44+
# - DATABASE_URL=postgresql://username:password@database:5432/postgis
45+
# ports:
46+
# - "7800:7800"
47+
# depends_on:
48+
# - database
49+
50+
# martin:
51+
# image: maplibre/martin
52+
# environment:
53+
# - DATABASE_URL=postgresql://username:password@database:5432/postgis
54+
# ports:
55+
# - "3000:3000"
56+
# depends_on:
57+
# - database

0 commit comments

Comments
 (0)