Skip to content
Snippets Groups Projects
Commit 285b189d authored by Laurens Oostwegel's avatar Laurens Oostwegel
Browse files

Remove rules

parent 0706e4c1
No related branches found
No related tags found
Loading
Pipeline #71723 passed
CREATE OR REPLACE FUNCTION tile_xy_to_quadkey(
IN tile_x INT,
IN tile_y INT,
IN levelOfDetail INT
)
RETURNS TEXT
LANGUAGE plpgsql
AS $$
DECLARE
_quadkey TEXT;
mask INT;
digit INT;
BEGIN
_quadkey := '';
FOR i IN REVERSE levelOfDetail..1 LOOP
digit := 0;
mask := 1 << (i - 1);
CASE (tile_x & mask)
WHEN 0 THEN digit = digit;
ELSE digit = digit + 1;
END CASE;
CASE (tile_y & mask)
WHEN 0 THEN digit = digit;
ELSE digit = digit + 2;
END CASE;
_quadkey = CONCAT(_quadkey, digit);
END LOOP;
RETURN _quadkey;
END;
$$;
CREATE OR REPLACE FUNCTION latlon_to_pixel_xy(
IN latitude DOUBLE PRECISION,
IN longitude DOUBLE PRECISION,
IN levelOfDetail INT,
OUT pixel_x INT,
OUT pixel_y INT)
LANGUAGE plpgsql
AS $$
DECLARE
x DOUBLE PRECISION;
y DOUBLE PRECISION;
sinLatitude DOUBLE PRECISION;
mapSize INT;
BEGIN
latitude := Clip(latitude, -85.05112878, 85.05112878);
longitude := Clip(longitude, -180, 180);
x := (longitude + 180) / 360;
sinLatitude := SIN(latitude * PI() / 180);
y := 0.5 - LN((1 + sinLatitude) / (1 - sinLatitude)) / (4 * PI());
mapSize := map_size(levelOfDetail);
pixel_x := FLOOR(Clip(x * mapSize + 0.5, 0, mapSize - 1));
pixel_y := FLOOR(Clip(y * mapSize + 0.5, 0, mapSize - 1));
END;
$$;
CREATE OR REPLACE FUNCTION clip(n FLOAT, minValue FLOAT, maxValue FLOAT)
RETURNS DOUBLE PRECISION
LANGUAGE plpgsql
AS $$
BEGIN
RETURN LEAST(GREATEST(n, minValue), maxValue);
END;
$$;
CREATE OR REPLACE FUNCTION pixel_xy_to_tile_xy(
IN pixel_x INT,
IN pixel_y INT,
OUT tile_x INT,
OUT tile_y INT
)
LANGUAGE plpgsql
AS $$
BEGIN
tile_x := pixel_x / 256;
tile_y := pixel_y / 256;
END;
$$;
CREATE OR REPLACE FUNCTION bbox_to_quadkeys(
IN min_lat DOUBLE PRECISION,
IN min_lon DOUBLE PRECISION,
IN max_lat DOUBLE PRECISION,
IN max_lon DOUBLE PRECISION,
IN levelOfDetail INT
)
RETURNS TABLE (quadkey TEXT)
LANGUAGE plpgsql
AS $$
DECLARE
min_pixel_xy RECORD;
max_pixel_xy RECORD;
min_tile_xy RECORD;
max_tile_xy RECORD;
BEGIN
min_pixel_xy = latlon_to_pixel_xy(max_lat, min_lon, levelOfDetail);
max_pixel_xy = latlon_to_pixel_xy(min_lat, max_lon, levelOfDetail);
min_tile_xy = pixel_xy_to_tile_xy(min_pixel_xy.pixel_x, min_pixel_xy.pixel_y);
max_tile_xy = pixel_xy_to_tile_xy(max_pixel_xy.pixel_x, max_pixel_xy.pixel_y);
FOR i IN min_tile_xy.tile_x..max_tile_xy.tile_x LOOP
FOR j IN min_tile_xy.tile_y..max_tile_xy.tile_y LOOP
quadkey := tile_xy_to_quadkey(i, j, levelOfDetail);
RETURN next;
END LOOP;
END LOOP;
END;
$$;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment