Skip to content

Commit

Permalink
Remove z_order_curve index
Browse files Browse the repository at this point in the history
  • Loading branch information
frodrigo committed Dec 25, 2023
1 parent b4668e0 commit 4aadbdd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 105 deletions.
34 changes: 10 additions & 24 deletions modules/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,33 +160,19 @@ def _build_param(
params.append(classs)
where.append(f"markers.class = ANY (${len(params)})")

if tilex and tiley and zoom:
minlon, minlat = tiles.tile2lonlat(tilex, tiley, zoom)
maxlon, maxlat = tiles.tile2lonlat(tilex + 1, tiley + 1, zoom)
bbox = [minlon, minlat, maxlon, maxlat]

if bbox:
params += [bbox[1], bbox[3], bbox[0], bbox[2]]
params += bbox
where.append(
f"""
markers.lat BETWEEN
${len(params)-3} AND
${len(params)-2} AND
markers.lon BETWEEN
((${len(params)-1})::numeric + 180) % 360 - 180 AND
((${len(params)})::numeric + 180) % 360 - 180"""
)
if item is None:
# Compute a tile to use index
tilex, tiley, zoom = tiles.bbox2tile(*bbox)
if zoom < 8:
zoom = 8
tilex, tiley = tiles.lonlat2tile(
(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2, zoom
)

if tilex and tiley and zoom:
params += [tilex, tiley, zoom]
where.append(
f"""lonlat2z_order_curve(lon, lat) BETWEEN
zoc18min(z_order_curve(${len(params)-2}, ${len(params)-1}), ${len(params)}) AND
zoc18max(z_order_curve(${len(params)-2}, ${len(params)-1}), ${len(params)}) AND
lat > -90"""
point(markers.lon, markers.lat) <@ box(
point(${len(params)-3}, ((${len(params)-2})::numeric + 180) % 360 - 180),
point(${len(params)-1}, ((${len(params)})::numeric + 180) % 360 - 180)
)"""
)

if country is not None:
Expand Down
8 changes: 8 additions & 0 deletions tools/database/48_drop_z_order_curve.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DROP FUNCTION lon2tile(lon double precision, zoom integer);
DROP FUNCTION lat2tile(lat double precision, zoom integer);
DROP FUNCTION z_order_curve(xx integer, yy integer);
DROP FUNCTION lonlat2z_order_curve(lon double precision, lat double precision);
DROP FUNCTION zoc18min(zoc bigint, z integer);
DROP FUNCTION zoc18max(zoc bigint, z integer);
DROP INDEX IF EXISTS idx_marker_source_class_z_order_curve;
DROP INDEX IF EXISTS idx_marker_z_order_curve_item;
81 changes: 0 additions & 81 deletions tools/database/schema.sql
Original file line number Diff line number Diff line change
@@ -1,70 +1,3 @@
CREATE OR REPLACE FUNCTION public.lon2tile(lon double precision, zoom integer)
RETURNS integer
LANGUAGE sql
IMMUTABLE
AS $function$
SELECT FLOOR( (lon + 180) / 360 * (1 << zoom) )::integer;
$function$;

CREATE OR REPLACE FUNCTION public.lat2tile(lat double precision, zoom integer)
RETURNS integer
LANGUAGE sql
IMMUTABLE
AS $function$
SELECT floor( (1.0 - ln(tan(radians(lat)) + 1.0 / cos(radians(lat))) / pi()) / 2.0 * (1 << zoom) )::integer;
$function$;

CREATE OR REPLACE FUNCTION public.z_order_curve(xx integer, yy integer)
RETURNS bigint
LANGUAGE plpgsql
IMMUTABLE
AS $function$
DECLARE
B bigint[] := ARRAY[x'5555555555555555'::bigint, x'3333333333333333'::bigint, x'0F0F0F0F0F0F0F0F'::bigint, x'00FF00FF00FF00FF'::bigint, x'0000FFFF0000FFFF'::bigint];
S integer[] := ARRAY[1, 2, 4, 8, 16];
x bigint := xx;
y bigint := yy;
z bigint;
BEGIN
x := (x | (x << S[5])) & B[5];
x := (x | (x << S[4])) & B[4];
x := (x | (x << S[3])) & B[3];
x := (x | (x << S[2])) & B[2];
x := (x | (x << S[1])) & B[1];
y := (y | (y << S[5])) & B[5];
y := (y | (y << S[4])) & B[4];
y := (y | (y << S[3])) & B[3];
y := (y | (y << S[2])) & B[2];
y := (y | (y << S[1])) & B[1];
z := x | (y << 1);
return z::bigint;
END;
$function$;

CREATE OR REPLACE FUNCTION public.lonlat2z_order_curve(lon double precision, lat double precision)
RETURNS bigint
LANGUAGE sql
IMMUTABLE
AS $function$
SELECT public.z_order_curve(public.lon2tile(lon, 18), public.lat2tile(lat, 18));
$function$;

CREATE OR REPLACE FUNCTION public.zoc18min(zoc bigint, z integer)
RETURNS bigint
LANGUAGE sql
IMMUTABLE
AS $function$
SELECT zoc << (2 * (18 - z));
$function$;

CREATE OR REPLACE FUNCTION public.zoc18max(zoc bigint, z integer)
RETURNS bigint
LANGUAGE sql
IMMUTABLE
AS $function$
SELECT public.zoc18min(zoc, z) + power(2, 2 * (18 - z))::bigint - 1;
$function$;

CREATE OR REPLACE FUNCTION public.marker_elem_ids(elems jsonb[])
RETURNS bigint[]
LANGUAGE sql
Expand Down Expand Up @@ -478,27 +411,13 @@ CREATE INDEX idx_marker_item_lat_lon ON public.markers USING btree (item, lat, l
CREATE INDEX idx_marker_source_class ON public.markers USING btree (source_id, class);


--
-- Name: idx_marker_source_class_z_order_curve; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_marker_source_class_z_order_curve ON public.markers USING btree (source_id, class, public.lonlat2z_order_curve((lon)::double precision, (lat)::double precision)) WHERE (lat > ('-90'::integer)::numeric);


--
-- Name: idx_marker_usernames; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_marker_usernames ON public.markers USING gin (public.marker_usernames(elems));


--
-- Name: idx_marker_z_order_curve_item; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_marker_z_order_curve_item ON public.markers USING btree (public.lonlat2z_order_curve((lon)::double precision, (lat)::double precision), item) WHERE (lat > ('-90'::integer)::numeric);


--
-- Name: idx_markers_counts_item_class; Type: INDEX; Schema: public; Owner: -
--
Expand Down

0 comments on commit 4aadbdd

Please sign in to comment.