-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsatapi.sql
More file actions
101 lines (93 loc) · 2.41 KB
/
satapi.sql
File metadata and controls
101 lines (93 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
CREATE OR REPLACE VIEW collectionitems AS
SELECT
c.properties as collectionproperties,
i.collection as collection,
i.id as id,
i.geometry as geom,
i.bbox as bbox,
i.type,
i.assets,
data.ST_AsGeoJSON(i.geometry) :: json as geometry,
i.properties as properties,
i.datetime as datetime,
i.links,
i.stac_version,
i.tiebreak
FROM data.itemsLinks i
RIGHT JOIN
data.collections c ON i.collection = c.id;
ALTER VIEW collectionitems owner to api;
CREATE OR REPLACE FUNCTION api.search(
bbox numeric[] default NULL,
intersects json default NULL,
include text[] default NULL,
andquery text default NULL,
sort text default 'ORDER BY c.datetime',
lim int default 50,
next text default '0'
) RETURNS setof api.collectionitems AS $$
DECLARE
res_headers text;
prefer text;
intersects_geometry data.geometry;
BEGIN
-- prefer := current_setting('request.header.prefer');
IF bbox IS NOT NULL THEN
intersects_geometry = data.ST_MakeEnvelope(
bbox[1],
bbox[2],
bbox[3],
bbox[4],
4326
);
ELSIF intersects IS NOT NULL THEN
intersects_geometry = data.st_SetSRID(data.ST_GeomFromGeoJSON(intersects), 4326);
END IF;
RETURN QUERY EXECUTE
FORMAT(
'SELECT
collectionproperties,
collection,
id,
c.geom,
c.bbox,
type,
assets,
geometry,
CASE WHEN $2 IS NULL THEN properties
ELSE (
SELECT jsonb_object_agg(e.key, e.value)
FROM jsonb_each(properties) e
WHERE e.key = ANY ($2)
)
END as properties,
datetime,
links,
stac_version,
tiebreak
FROM api.collectionitems c
WHERE (
$1 IS NULL OR
data.ST_Intersects($1, c.geom)
) %1s %2s LIMIT %3s OFFSET %4s;
', COALESCE(andQuery, ''), sort, lim, next)
USING intersects_geometry, include;
res_headers := format('[{"Func-Range": "%s-%s/*"}]', next, (next::int + lim::int) - 1);
PERFORM set_config('response.headers', res_headers, true);
END;
$$ LANGUAGE PLPGSQL IMMUTABLE;
CREATE OR REPLACE VIEW items AS
SELECT * FROM data.items_string_geometry;
ALTER VIEW items owner to api;
CREATE OR REPLACE VIEW collections AS
SELECT * FROM data.collectionsLinks;
ALTER VIEW collections owner to api;
CREATE OR REPLACE VIEW rootcollections AS
SELECT * FROM data.collectionsobject;
ALTER VIEW rootcollections owner to api;
CREATE OR REPLACE VIEW root AS
SELECT * FROM data.rootLinks;
ALTER VIEW root owner to api;
CREATE OR REPLACE VIEW stac AS
SELECT * FROM data.stacLinks;
ALTER VIEW stac owner to api;