Skip to content

Commit 9a46190

Browse files
committed
* 'main' of https://github.com/UW-Macrostrat/macrostrat: Format code and sort imports added tile_utils inside the rockd migration
2 parents 63ce9a8 + 80914a3 commit 9a46190

File tree

3 files changed

+174
-1
lines changed

3 files changed

+174
-1
lines changed

cli/macrostrat/cli/database/rockd/db_subsystem.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ def get_db(self):
1515

1616
def initialize(self):
1717
selected_fixtures = [
18-
migrations_dir / "0010_rockd_user_privileges.sql", # grants/defaults (safe)
18+
migrations_dir
19+
/ "0010_tile_utils.sql", # creates util functions in the rockd db for the tileserver api
20+
migrations_dir / "0011_rockd_user_privileges.sql", # grants/defaults (safe)
1921
migrations_dir / "0030_rockd-coords.sql", # standalone table
2022
migrations_dir
2123
/ "0040_model-feedback-schema.sql", # references checkins/observations
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
CREATE EXTENSION IF NOT EXISTS postgis;
2+
CREATE SCHEMA IF NOT EXISTS tile_utils;
3+
4+
CREATE TABLE IF NOT EXISTS tile_utils.tms_definition (
5+
name text PRIMARY KEY,
6+
bounds geometry(Polygon) NOT NULL,
7+
geographic_srid integer NOT NULL DEFAULT 4326 REFERENCES spatial_ref_sys(srid)
8+
);
9+
10+
INSERT INTO tile_utils.tms_definition (name, bounds, geographic_srid) VALUES (
11+
'web_mercator',
12+
ST_TileEnvelope(0, 0, 0),
13+
4326
14+
) ON CONFLICT DO NOTHING;
15+
16+
CREATE OR REPLACE FUNCTION tile_utils.default_tms()
17+
RETURNS text AS $$ SELECT 'web_mercator'; $$
18+
LANGUAGE SQL;
19+
20+
CREATE OR REPLACE FUNCTION tile_utils.tms_data(tms text = null)
21+
RETURNS tile_utils.tms_definition
22+
AS $$
23+
SELECT *
24+
FROM tile_utils.tms_definition
25+
WHERE name = coalesce(
26+
tms,
27+
tile_utils.default_tms()
28+
);
29+
$$ LANGUAGE SQL IMMUTABLE;
30+
31+
CREATE OR REPLACE FUNCTION tile_utils.tms_bounds(
32+
tms text = null
33+
) RETURNS geometry(Polygon)
34+
AS $$
35+
SELECT (tile_utils.tms_data(tms)).bounds
36+
$$
37+
LANGUAGE SQL IMMUTABLE;
38+
39+
CREATE OR REPLACE FUNCTION tile_utils.tms_srid(tms text = null)
40+
RETURNS integer
41+
AS $$
42+
SELECT ST_SRID(tile_utils.tms_bounds(tms));
43+
$$ LANGUAGE SQL IMMUTABLE;
44+
45+
CREATE OR REPLACE FUNCTION tile_utils.tms_geographic_srid(tms text = null)
46+
RETURNS integer
47+
AS $$
48+
SELECT coalesce((tile_utils.tms_data(tms)).geographic_srid, 4326);
49+
$$ LANGUAGE SQL IMMUTABLE;
50+
51+
52+
CREATE OR REPLACE FUNCTION tile_utils.envelope(
53+
_x integer,
54+
_y integer,
55+
_z integer,
56+
_tms text = null
57+
) RETURNS geometry(Polygon)
58+
AS $$
59+
SELECT ST_TileEnvelope(
60+
_z, _x, _y,
61+
tile_utils.tms_bounds(_tms)
62+
);
63+
$$ LANGUAGE SQL IMMUTABLE;
64+
65+
CREATE OR REPLACE FUNCTION tile_utils.tile_width(_z integer, _tms text = null)
66+
/** Tile width in projected coordinates (currently only works for square TMS)*/
67+
RETURNS numeric AS $$
68+
DECLARE
69+
_tms_bounds geometry;
70+
_tms_size numeric;
71+
_tile_size numeric;
72+
BEGIN
73+
_tms_bounds := tile_utils.tms_bounds(_tms);
74+
_tms_size := ST_XMax(_tms_bounds) - ST_XMin(_tms_bounds);
75+
RETURN _tms_size/(2^_z);
76+
END;
77+
$$ LANGUAGE PLPGSQL IMMUTABLE;
78+
79+
80+
-- This currently only works for square tiles.
81+
CREATE OR REPLACE FUNCTION tile_utils.tile_index(
82+
coord numeric,
83+
z integer,
84+
_tms text = null
85+
) RETURNS integer AS $$
86+
SELECT floor(coord/tile_utils.tile_width(z, _tms))::integer;
87+
$$ LANGUAGE SQL STABLE;
88+
89+
90+
CREATE OR REPLACE FUNCTION tile_utils.containing_tiles(
91+
_geom geometry,
92+
_tms text = null
93+
) RETURNS TABLE (
94+
x integer,
95+
y integer,
96+
z integer
97+
) AS $$
98+
DECLARE
99+
_tms_bounds geometry;
100+
_geom_bbox box2d;
101+
BEGIN
102+
_tms_bounds := tile_utils.tms_bounds(_tms);
103+
104+
IF ST_Within(_geom, ST_Transform(_tms_bounds, ST_SRID(_geom))) THEN
105+
_geom_bbox := ST_Transform(_geom, ST_SRID(_tms_bounds))::box2d;
106+
ELSE
107+
RETURN;
108+
END IF;
109+
110+
RETURN QUERY
111+
WITH tile_sizes AS (
112+
SELECT
113+
a.zoom
114+
FROM generate_series(0, 24) AS a(zoom)
115+
), tilebounds AS (
116+
SELECT t.zoom,
117+
tile_utils.tile_index((ST_XMin(_geom_bbox)-ST_XMin(_tms_bounds))::numeric, t.zoom) xmin,
118+
tile_utils.tile_index((ST_YMax(_tms_bounds)-ST_YMin(_geom_bbox))::numeric, t.zoom) ymin,
119+
tile_utils.tile_index((ST_XMax(_geom_bbox)-ST_XMin(_tms_bounds))::numeric, t.zoom) xmax,
120+
tile_utils.tile_index((ST_YMax(_tms_bounds)-ST_YMax(_geom_bbox))::numeric, t.zoom) ymax
121+
FROM tile_sizes t
122+
)
123+
SELECT
124+
t.xmin::integer x,
125+
t.ymin::integer y,
126+
t.zoom::integer z
127+
FROM tilebounds t
128+
WHERE t.xmin = t.xmax
129+
AND t.ymin = t.ymax
130+
ORDER BY z DESC;
131+
END;
132+
$$ LANGUAGE plpgsql STABLE;
133+
134+
135+
CREATE OR REPLACE FUNCTION tile_utils.parent_tile(
136+
_geom geometry,
137+
_tms text = null
138+
) RETURNS TABLE (
139+
x integer,
140+
y integer,
141+
z integer
142+
) AS $$
143+
SELECT x, y, z FROM tile_utils.containing_tiles(_geom, _tms) LIMIT 1;
144+
$$ LANGUAGE sql STABLE;
145+
146+
147+
CREATE OR REPLACE FUNCTION tile_utils.cluster_expansion_zoom(
148+
geom geometry,
149+
zoom integer,
150+
expanded_pixel_width integer DEFAULT 256
151+
)
152+
/** Returns the zoom level at which the given tile-relative geometry will be expanded to the given pixel width.
153+
The input geometry must already be in tile pixel coordinates created by ST_AsMVTGeom.
154+
This is useful for calculating expansion zooms for clustered points.
155+
*/
156+
RETURNS integer AS $$
157+
DECLARE
158+
bbox_size double precision;
159+
zoom_delta double precision;
160+
BEGIN
161+
SELECT greatest(ST_XMax(geom) - ST_XMin(geom), ST_YMax(geom)-ST_YMin(geom)) INTO bbox_size;
162+
163+
IF bbox_size = 0 THEN
164+
RETURN null;
165+
END IF;
166+
167+
zoom_delta := sqrt(expanded_pixel_width/bbox_size);
168+
169+
RETURN round(zoom + zoom_delta);
170+
END;
171+
$$ LANGUAGE plpgsql IMMUTABLE;

cli/macrostrat/cli/database/rockd/migrations/0010_rockd_user_privileges.sql renamed to cli/macrostrat/cli/database/rockd/migrations/0011_rockd_user_privileges.sql

File renamed without changes.

0 commit comments

Comments
 (0)