]> git.openstreetmap.org Git - rails.git/blob - db/functions/functions.sql
Merge remote-tracking branch 'upstream/pull/2381'
[rails.git] / db / functions / functions.sql
1 --------------------------------------------------------------------------------
2 -- SQL versions of the C database functions.
3 --
4 -- Pure pl/pgsql versions are *slower* than the C versions, and not recommended
5 -- for production use. However, they are significantly easier to install, and
6 -- require fewer dependencies.
7 --------------------------------------------------------------------------------
8
9 -- tile_for_point function returns a Morton-encoded integer representing a z16
10 -- tile which contains the given (scaled_lon, scaled_lat) coordinate. Note that
11 -- these are passed into the function as (lat, lon) and should be scaled by
12 -- 10^7.
13 --
14 -- The Morton encoding packs two dimensions down to one with fairly good
15 -- spatial locality, and can be used to index points without the need for a
16 -- proper 2D index.
17 CREATE OR REPLACE FUNCTION tile_for_point(scaled_lat int4, scaled_lon int4)
18   RETURNS int8
19   AS $$
20 DECLARE
21   x int8; -- quantized x from lon,
22   y int8; -- quantized y from lat,
23 BEGIN
24   x := round(((scaled_lon / 10000000.0) + 180.0) * 65535.0 / 360.0);
25   y := round(((scaled_lat / 10000000.0) +  90.0) * 65535.0 / 180.0);
26
27   -- these bit-masks are special numbers used in the bit interleaving algorithm.
28   -- see https://graphics.stanford.edu/~seander/bithacks.html#InterleaveBMN
29   -- for the original algorithm and more details.
30   x := (x | (x << 8)) &   16711935; -- 0x00FF00FF
31   x := (x | (x << 4)) &  252645135; -- 0x0F0F0F0F
32   x := (x | (x << 2)) &  858993459; -- 0x33333333
33   x := (x | (x << 1)) & 1431655765; -- 0x55555555
34
35   y := (y | (y << 8)) &   16711935; -- 0x00FF00FF
36   y := (y | (y << 4)) &  252645135; -- 0x0F0F0F0F
37   y := (y | (y << 2)) &  858993459; -- 0x33333333
38   y := (y | (y << 1)) & 1431655765; -- 0x55555555
39
40   RETURN (x << 1) | y;
41 END;
42 $$ LANGUAGE plpgsql IMMUTABLE;
43
44
45 -- maptile_for_point returns an integer representing the tile at the given zoom
46 -- which contains the point (scaled_lon, scaled_lat). Note that the arguments
47 -- are in the order (lat, lon), and should be scaled by 10^7.
48 --
49 -- The maptile_for_point function is used only for grouping the results of the
50 -- (deprecated?) /changes API call. Please don't use it for anything else, as
51 -- it might go away in the future.
52 CREATE OR REPLACE FUNCTION maptile_for_point(scaled_lat int8, scaled_lon int8, zoom int4)
53   RETURNS int4
54   AS $$
55 DECLARE
56   lat CONSTANT DOUBLE PRECISION := scaled_lat / 10000000.0;
57   lon CONSTANT DOUBLE PRECISION := scaled_lon / 10000000.0;
58   zscale CONSTANT DOUBLE PRECISION := 2.0 ^ zoom;
59   pi CONSTANT DOUBLE PRECISION := 3.141592653589793;
60   r_per_d CONSTANT DOUBLE PRECISION := pi / 180.0;
61   x int4;
62   y int4;
63 BEGIN
64   -- straight port of the C code. see db/functions/maptile.c
65   x := floor((lon + 180.0) * zscale / 360.0);
66   y := floor((1.0 - ln(tan(lat * r_per_d) + 1.0 / cos(lat * r_per_d)) / pi) * zscale / 2.0);
67
68   RETURN (x << zoom) | y;
69 END;
70 $$ LANGUAGE plpgsql IMMUTABLE;
71
72 -- xid_to_int4 converts a PostgreSQL transaction ID (xid) to a 32-bit integer
73 -- which can then be used to efficiently find rows which have changed between
74 -- two given transactions. This is currently used by Osmosis to extract a
75 -- stream of edits for "diff replication" **HOWEVER** this is a pain point, as
76 -- (ab)using the xid in this way is _not_ supported or recommended by Postgres
77 -- devs. It is preventing us upgrading to PostgreSQL version 10+, and will
78 -- hopefully be replaced Real Soon Now.
79 --
80 -- From the Osmosis distribution by Brett Henderson:
81 -- https://github.com/openstreetmap/osmosis/blob/master/package/script/contrib/apidb_0.6_osmosis_xid_indexing.sql
82 CREATE OR REPLACE FUNCTION xid_to_int4(t xid)
83   RETURNS integer
84   AS
85 $$
86 DECLARE
87   tl bigint;
88   ti int;
89 BEGIN
90   tl := t;
91
92   IF tl >= 2147483648 THEN
93     tl := tl - 4294967296;
94   END IF;
95
96   ti := tl;
97
98   RETURN ti;
99 END;
100 $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT;