forked from opensciencemap/VectorTileMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getTile.sql
314 lines (269 loc) · 11.1 KB
/
getTile.sql
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
-- example functions for PostGIS backend of VectorTileMap
-- TODO make default work with standard psm2psql database
CREATE OR REPLACE FUNCTION __get_tile(IN tilex bigint, IN tiley bigint, IN tilez integer)
RETURNS TABLE(id bigint, tags hstore, geom bytea) AS
$BODY$
DECLARE
bbox geometry;
min float;
BEGIN
bbox := __tileToBox3D(tileX, tileY, tileZ);
-- pixel size at zoomlevel
min := 20037508.342789244 / 256 / (2 ^ tileZ);
IF (tileZ > 14) THEN
RETURN QUERY SELECT * FROM __get_tile15(bbox, min)
UNION ALL
SELECT * FROM __get_tile7(bbox, min);
ELSIF (tileZ > 13) THEN
RETURN QUERY SELECT * FROM __get_tile14(bbox)
UNION ALL
SELECT * FROM __get_tile7(bbox, min);
ELSIF (tileZ > 11) THEN
RETURN QUERY SELECT * FROM __get_tile12(bbox, min)
UNION ALL
SELECT * FROM __get_tile7(bbox, min);
ELSIF (tileZ > 8) THEN
RETURN QUERY SELECT * FROM __get_tile7(bbox, min)
UNION ALL
SELECT * FROM __get_tile_simp_ways2(bbox, min)
UNION ALL
SELECT * FROM __get_tile_admin_0(bbox, min);
ELSIF (tileZ > 6) THEN
RETURN QUERY SELECT * FROM __get_tile4(bbox, min)
UNION ALL
SELECT * FROM __get_tile_simp_ways3(bbox, min)
UNION ALL
SELECT * FROM __get_tile_admin_0(bbox, min);
ELSIF (tileZ > 3) THEN
RETURN QUERY SELECT * FROM __get_tile3(bbox, min)
UNION ALL
SELECT * FROM __get_tile_admin_0(bbox, min);
ELSE
RETURN QUERY SELECT * FROM __get_tile0(bbox, min)
UNION ALL
SELECT * FROM __get_tile_admin_0(bbox, min);
END IF;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
CREATE OR REPLACE FUNCTION __tiletobox3d(tilex bigint, tiley bigint, tilez integer)
RETURNS geometry AS
$BODY$
DECLARE
scaleFactor double precision;
minLon double precision;
maxLon double precision;
minLat double precision;
maxLat double precision;
tileSize bigint;
center double precision;
pixel integer;
BEGIN
pixel := 10;
scaleFactor := 20037508.342789244;
tileSize := 256;
center := (tileSize << tileZ) >> 1;
minLat := ((center - (tileY + tileSize + pixel)) / center) * scaleFactor;
maxLat := ((center - (tileY - pixel)) / center) * scaleFactor;
minLon := (((tileX - pixel) - center) / center) * scaleFactor;
maxLon := (((tileX + tileSize + pixel) - center) / center) * scaleFactor;
if (minLat < -scaleFactor) then
minLat := -scaleFactor;
elsif (minLat > scaleFactor) then
minLat := scaleFactor;
end if;
if (minLon < -scaleFactor) then
minLon := -scaleFactor;
elsif (minLon > scaleFactor) then
minLon := scaleFactor;
end if;
if (maxLat < -scaleFactor) then
maxLat := -scaleFactor;
elsif (maxLat > scaleFactor) then
maxLat := scaleFactor;
end if;
if (maxLon < -scaleFactor) then
maxLon := -scaleFactor;
elsif (maxLon > scaleFactor) then
maxLon := scaleFactor;
end if;
-- RAISE NOTICE 'c %, %, %, %,%', center, minLon, minLat, maxLon, maxLat;
RETURN ST_SetSRID(concat('BOX3D(',minLon,' ', minLat, ',', maxLon, ' ', maxLat, ')')::box3d , 900913);
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
CREATE OR REPLACE FUNCTION __get_tile0(IN bbox geometry, IN pixel float)
RETURNS TABLE(id bigint, tags hstore, geom bytea) AS
$BODY$
SELECT 1::bigint as id, ('natural' => 'water') as tags,
ST_AsEWKB(ST_Collect(ST_SimplifyPreserveTopology(geom, $2))) FROM
(SELECT (ST_Dump(ST_Intersection(geomp, $1))).geom AS geom FROM
ne_110m_ocean
WHERE geomp && $1) p WHERE geometrytype(geom) = 'POLYGON';
$BODY$
LANGUAGE sql VOLATILE;
CREATE OR REPLACE FUNCTION __get_tile3(IN bbox geometry, IN pixel float)
RETURNS TABLE(id bigint, tags hstore, geom bytea) AS
$BODY$
SELECT 1::bigint as id, ('natural' => 'water') as tags,
ST_AsEWKB(ST_Collect(ST_SimplifyPreserveTopology(geom, 2*$2))) FROM
(SELECT (ST_Dump(ST_Intersection(geomp, $1))).geom AS geom FROM
ne_50m_ocean
WHERE geomp && $1) p WHERE geometrytype(geom) = 'POLYGON';
$BODY$
LANGUAGE sql VOLATILE;
CREATE OR REPLACE FUNCTION __get_tile4(IN bbox geometry, IN pixel float)
RETURNS TABLE(id bigint, tags hstore, geom bytea) AS
$BODY$
SELECT 1::bigint as id, ('natural' => 'water') as tags,
COALESCE (ST_AsEWKB(ST_SymDifference(ST_Buffer(ST_Collect(ST_SimplifyPreserveTopology(geom, 2*$2)),-5*$2,0), $1)), ST_AsEWKB($1)) FROM
(SELECT (ST_Dump(ST_Buffer(ST_Intersection($1, st_buffer(the_geom,0)),5*$2,0))).geom AS geom FROM
shorelines
WHERE the_geom && $1) p WHERE geometrytype(geom) = 'POLYGON' AND ST_Area(geom) > (500*$2^2);
$BODY$
LANGUAGE sql VOLATILE;
CREATE OR REPLACE FUNCTION __get_tile7(IN bbox geometry, IN pixel float)
RETURNS TABLE(id bigint, tags hstore, geom bytea) AS
$BODY$
SELECT 1::bigint as id, ('natural' => 'water') as tags,
COALESCE (ST_AsEWKB(ST_SymDifference(ST_Buffer(ST_Collect(ST_SimplifyPreserveTopology(geom, 2*$2)), -5*$2,0), $1)), ST_AsEWKB($1)) FROM
(SELECT (ST_Dump(ST_Buffer(ST_Intersection($1, the_geom),5*$2,0))).geom AS geom FROM
coastlines
WHERE the_geom && $1) p WHERE geometrytype(geom) = 'POLYGON' AND ST_Area(geom) > (500*$2^2);
$BODY$
LANGUAGE sql VOLATILE;
CREATE OR REPLACE FUNCTION __get_tile_simp_ways3(IN bbox geometry, IN pixel float)
RETURNS TABLE(id bigint, tags hstore, geom bytea) AS
$BODY$
SELECT 1::bigint as id, ('highway' => way_type) as tags,
ST_AsEWKB(ST_SimplifyPreserveTopology(geom, 2*$2)) FROM
(SELECT (ST_Dump(ST_Intersection($1, geom))).geom AS geom, way_type FROM
ways_simp
WHERE geom && $1) p WHERE geometrytype(geom) in ('LINESTRING');
$BODY$
LANGUAGE sql VOLATILE;
CREATE OR REPLACE FUNCTION __get_tile_simp_ways(IN bbox geometry, IN pixel float)
RETURNS TABLE(id bigint, tags hstore, geom bytea) AS
$BODY$
SELECT 1::bigint as id, ('highway' => way_type) as tags,
ST_AsEWKB(ST_SimplifyPreserveTopology(geom, 2*$2)) FROM
(SELECT (ST_Dump(ST_Intersection($1, the_geom))).geom AS geom, way_type FROM
simple_ways2
WHERE the_geom && $1 AND way_type in ('motorway', 'motorrway_link', 'primary', 'primary_link', 'trunk', 'trunk_link')) p WHERE geometrytype(geom) in ('LINESTRING');
$BODY$
LANGUAGE sql VOLATILE;
CREATE OR REPLACE FUNCTION __get_tile_simp_ways2(IN bbox geometry, IN pixel float)
RETURNS TABLE(id bigint, tags hstore, geom bytea) AS
$BODY$
SELECT 1::bigint as id, ('highway' => way_type) as tags,
ST_AsEWKB(ST_SimplifyPreserveTopology(geom, 2*$2)) FROM
(SELECT (ST_Dump(ST_Intersection($1, the_geom))).geom AS geom, way_type FROM
simple_ways2
WHERE the_geom && $1) p WHERE geometrytype(geom) in ('LINESTRING');
$BODY$
LANGUAGE sql VOLATILE;
CREATE OR REPLACE FUNCTION __get_tile_admin_0(IN bbox geometry, IN pixel float)
RETURNS TABLE(id bigint, tags hstore, geom bytea) AS
$BODY$
SELECT 1::bigint as id, ('admin_level' => '2') as tags,
ST_AsEWKB(ST_SimplifyPreserveTopology(geom, 2*$2)) FROM
(SELECT (ST_Dump(ST_Intersection($1, geom))).geom AS geom FROM
ne_50m_admin_0_boundary_lines_land
WHERE geom && $1) p;
$BODY$
LANGUAGE sql VOLATILE;
CREATE OR REPLACE FUNCTION __get_tile12(IN bbox geometry, IN pixel float)
RETURNS TABLE(id bigint, tags hstore, geom bytea) AS
$BODY$
WITH polygons AS
(SELECT way,
CASE WHEN (landuse is not null) THEN ('landuse' => landuse)
WHEN (waterway is not null) THEN ('waterway' => waterway)
-- WHEN (building is not null) THEN ('building' => building)
WHEN ("natural" is not null) THEN ('natural' => "natural")
WHEN (leisure is not null) THEN ('leisure' => leisure)
END AS tags
FROM planet_polygon
WHERE way && $1
AND ((landuse is not null)
OR (waterway is not null)
--OR (building is not null)
OR ("natural" is not null)
OR (leisure is not null))
AND way_area > 400 * ($2^2))
SELECT 1::bigint AS id, ('highway' => highway)::hstore, ST_AsEWKB((ST_Dump(ST_Multi(ST_SimplifyPreserveTopology(ST_LineMerge(way), $2)))).geom) AS geom FROM
(SELECT ST_Intersection(ST_Collect(way), $1) as way, highway FROM
planet_line WHERE
(highway IS NOT NULL) AND
way && $1 AND (highway in ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary'))
group by highway) p
UNION ALL
SELECT 1::bigint AS id, tags, ST_AsEWKB(ST_Collect(geom)) AS geom FROM
(SELECT tags, (ST_Dump(ST_Buffer(ST_SimplifyPreserveTopology(way, 2*$2), - $2*4,0))).geom as geom FROM
-- (SELECT ST_Intersection(ST_Union(ST_SimplifyPreserveTopology(ST_Buffer(way,$2,0), 2*$2)), $1) as way, tags FROM
(SELECT ST_Intersection(ST_Union(ST_Buffer(way,$2*4,0)), $1) as way, tags FROM
polygons group by tags ) p ) p WHERE geometrytype(geom) = 'POLYGON' group by tags
$BODY$
LANGUAGE sql VOLATILE;
CREATE OR REPLACE FUNCTION __get_tile14(IN bbox geometry, IN pixel float)
RETURNS TABLE(id bigint, tags hstore, geom bytea) AS
$BODY$
WITH polygons AS
(SELECT way,
CASE WHEN (landuse is not null) THEN ('landuse' => landuse)
WHEN (waterway is not null) THEN ('waterway' => waterway)
-- WHEN (building is not null) THEN ('building' => building)
WHEN ("natural" is not null) THEN ('natural' => "natural")
WHEN (leisure is not null) THEN ('leisure' => leisure)
END AS tags
FROM planet_polygon
WHERE way && $1
AND ((landuse is not null)
OR (waterway is not null)
--OR (building is not null)
OR ("natural" is not null)
OR leisure is not null))
SELECT 1::bigint AS id, ('highway' => highway)::hstore, ST_AsEWKB((ST_Dump(ST_Multi(ST_SimplifyPreserveTopology(ST_LineMerge(way), $2)))).geom) AS geom FROM
(SELECT ST_Intersection(ST_Union(way), $1) as way, highway FROM
planet_line WHERE
(highway IS NOT NULL) AND
way && $1 AND (highway in ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'residential'))
group by highway) p
UNION ALL
SELECT 1::bigint AS id, tags, ST_AsEWKB(ST_Collect(geom)) AS geom FROM
(SELECT tags, (ST_Dump(way)).geom as geom FROM
(SELECT ST_Intersection(ST_Union(ST_Buffer(way,0)), $1) as way, tags FROM
polygons group by tags ) p ) p WHERE geometrytype(geom) = 'POLYGON' group by tags
$BODY$
LANGUAGE sql VOLATILE;
CREATE OR REPLACE FUNCTION __get_tile15(IN bbox geometry, IN pixel float)
RETURNS TABLE(id bigint, tags hstore, geom bytea) AS
$BODY$
WITH polygons AS
(SELECT way,
CASE WHEN (landuse is not null) THEN ('landuse' => landuse)
WHEN (waterway is not null) THEN ('waterway' => waterway)
WHEN (building is not null) THEN ('building' => building)
WHEN ("natural" is not null) THEN ('natural' => "natural")
WHEN (leisure is not null) THEN ('leisure' => leisure)
END AS tags
FROM planet_polygon
WHERE way && $1
AND ((landuse is not null)
OR (waterway is not null)
OR (building is not null)
OR ("natural" is not null)
OR leisure is not null))
SELECT 1::bigint AS id, ('highway' => highway)::hstore, ST_AsEWKB((ST_Dump(ST_Multi(ST_SimplifyPreserveTopology(ST_LineMerge(way), $2)))).geom) AS geom FROM
(SELECT ST_Intersection(ST_Union(way), $1) as way, highway FROM
planet_line WHERE
(highway IS NOT NULL) AND
way && $1
group by highway) p
UNION ALL
SELECT 1::bigint AS id, tags, ST_AsEWKB(ST_Collect(geom)) AS geom FROM
(SELECT tags, (ST_Dump(way)).geom as geom FROM
(SELECT ST_Intersection(ST_Union(ST_Buffer(way,$2*10,0)), $1) as way, tags FROM
polygons group by tags ) p ) p WHERE geometrytype(geom) = 'POLYGON' group by tags
$BODY$
LANGUAGE sql VOLATILE;