-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery_function.sql
More file actions
177 lines (163 loc) · 4.96 KB
/
query_function.sql
File metadata and controls
177 lines (163 loc) · 4.96 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
DROP VIEW IF EXISTS view_osm_objects;
CREATE OR REPLACE VIEW
view_osm_objects AS
SELECT
CASE
WHEN g.osm_type = 'N' then 'node'
WHEN g.osm_type = 'W' then 'way'
WHEN g.osm_type = 'R' then 'relation'
ELSE NULL
END as osm_type,
g.osm_id,
CASE
WHEN g.osm_type = 'N' then n.tags
WHEN g.osm_type = 'W' then w.tags
WHEN g.osm_type = 'R' then r.tags
ELSE NULL
END as tags,
g.count_vertices,
g.geom_type,
g.geog AS geog
FROM
geometries AS g
LEFT JOIN raw_nodes AS n ON g.osm_id = n.id
AND g.osm_type = 'N'
LEFT JOIN raw_ways AS w ON g.osm_id = w.id
AND g.osm_type = 'W'
LEFT JOIN raw_rels AS r ON g.osm_id = r.id
AND g.osm_type = 'R';
DROP FUNCTION IF EXISTS postgisftw.osm_website_objects_around;
CREATE
OR REPLACE FUNCTION postgisftw.osm_website_objects_around (
latitude float,
longitude float,
radius int,
min_lat float,
min_lon float,
max_lat float,
max_lon float
) RETURNS TABLE (
osm_type text,
osm_id bigint,
tags jsonb,
geometry_type text,
geom geometry
) AS $$
SELECT
osm_type,
osm_id,
tags,
GeometryType(geog::geometry) as geometry_type,
-- only return geometry if it is in viewport
CASE
WHEN NOT ST_Covers (
ST_MakeEnvelope (min_lon, min_lat, max_lon, max_lat, 4326)::geography,
geog
) THEN NULL::geometry
ELSE geog::geometry
END AS geog
FROM view_osm_objects
WHERE ST_DWithin(geog, ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)::geography, radius)
$$ LANGUAGE sql STABLE PARALLEL SAFE;
DROP FUNCTION IF EXISTS postgisftw.osm_website_objects_enclosing_small;
CREATE
OR REPLACE FUNCTION postgisftw.osm_website_objects_enclosing_small (
latitude float,
longitude float,
min_lat float,
min_lon float,
max_lat float,
max_lon float
) RETURNS TABLE (
osm_type text,
osm_id bigint,
tags jsonb,
geometry_type text,
geom geometry
) AS $$
SELECT
osm_type,
osm_id,
tags,
GeometryType(geog::geometry) as geometry_type,
-- only return geometry if it is in viewport
CASE
WHEN NOT ST_Covers (
ST_MakeEnvelope (min_lon, min_lat, max_lon, max_lat, 4326)::geography,
geog
) THEN NULL::geometry
ELSE geog::geometry
END AS geog
FROM view_osm_objects
WHERE ST_Intersects(geog, ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)::geography)
AND count_vertices <= max_vertices()
$$ LANGUAGE sql STABLE PARALLEL SAFE;
DROP FUNCTION IF EXISTS postgisftw.osm_website_objects_enclosing_large;
CREATE
OR REPLACE FUNCTION postgisftw.osm_website_objects_enclosing_large (
latitude float,
longitude float,
min_lat float,
min_lon float,
max_lat float,
max_lon float
) RETURNS TABLE (
osm_type text,
osm_id bigint,
tags jsonb,
geometry_type text,
geom geometry
) AS $$
SELECT
p.osm_type,
p.osm_id,
CASE
WHEN p.osm_type = 'N' then n.tags
WHEN p.osm_type = 'W' then w.tags
WHEN p.osm_type = 'R' then r.tags
ELSE NULL
END as tags,
GeometryType(geog::geometry) as geometry_type,
-- only return geometry if it is in viewport
CASE
WHEN NOT ST_Covers (
ST_MakeEnvelope (min_lon, min_lat, max_lon, max_lat, 4326)::geography,
v.geog
) THEN NULL::geometry
ELSE v.geog::geometry
END AS geog
FROM polygons_subdivided as p
LEFT JOIN view_osm_objects AS v ON p.osm_id = v.osm_id
-- TODO: maybe we do not need to join nodes, because they are never a polygon. But it should not harm either ...
LEFT JOIN raw_nodes AS n ON p.osm_id = n.id
AND p.osm_type = 'N'
LEFT JOIN raw_ways AS w ON p.osm_id = w.id
AND p.osm_type = 'W'
LEFT JOIN raw_rels AS r ON p.osm_id = r.id
AND p.osm_type = 'R'
WHERE ST_Intersects(geom, ST_SetSRID(ST_MakePoint(longitude, latitude), 4326));
$$ LANGUAGE sql STABLE PARALLEL SAFE;
DROP FUNCTION IF EXISTS postgisftw.osm_website_combi;
CREATE
OR REPLACE FUNCTION postgisftw.osm_website_combi (
latitude float,
longitude float,
radius int,
min_lat float,
min_lon float,
max_lat float,
max_lon float
) RETURNS TABLE (
osm_type text,
osm_id bigint,
query_type text,
tags jsonb,
geometry_type text,
geom geometry
) AS $$
SELECT osm_type, osm_id, 'around' as query_type, tags, geometry_type, geom FROM postgisftw.osm_website_objects_around( latitude, longitude, radius, min_lat, min_lon, max_lat, max_lon )
UNION
SELECT osm_type, osm_id, 'enclosing' as query_type, tags, geometry_type, geom FROM postgisftw.osm_website_objects_enclosing_small( latitude, longitude, min_lat, min_lon, max_lat, max_lon )
UNION
SELECT osm_type, osm_id, 'enclosing' as query_type, tags, geometry_type, geom FROM postgisftw.osm_website_objects_enclosing_large( latitude, longitude, min_lat, min_lon, max_lat, max_lon )
$$ LANGUAGE sql STABLE PARALLEL SAFE;