fix dynamic tile arguments and implement in both layers

This commit is contained in:
Paul Bienkowski 2022-05-02 21:14:32 +02:00
parent 598ba8d187
commit 5beb5ac0d3
4 changed files with 21 additions and 10 deletions

View file

@ -39,10 +39,12 @@ async def tiles(req, zoom: int, x: int, y: str):
tile = get_tile(req.app.config.TILES_FILE, int(zoom), int(x), int(y))
else:
user_id = req.ctx.get_single_arg("user_id", convert=int, default=None)
if user_id is not None:
if req.ctx.user is None or req.ctx.user.id != user_id:
user_id = None
username = req.ctx.get_single_arg("user", default=None)
if username is not None:
if req.ctx.user is None or req.ctx.user.username != username:
raise Forbidden()
user_id = req.ctx.user.id
tile = await req.ctx.db.scalar(
text(

View file

@ -48,7 +48,7 @@ DROP FUNCTION IF EXISTS {fname}(integer, integer, integer{extra_args_types});
CREATE FUNCTION {fname}(zoom integer, x integer, y integer{extra_args_definitions})
RETURNS {'TABLE(mvt bytea, key text)' if self.key_column else 'bytea'} AS $$
{self.generate_sql()};
$$ LANGUAGE SQL STABLE RETURNS NULL ON NULL INPUT;"""
$$ LANGUAGE SQL STABLE CALLED ON NULL INPUT;"""
def parse_pg_url(url=app.config.POSTGRES_URL):

View file

@ -1,6 +1,7 @@
DROP FUNCTION IF EXISTS layer_obs_roads(geometry, int);
DROP FUNCTION IF EXISTS layer_obs_roads(geometry, int, integer, timestamp, timestamp);
CREATE OR REPLACE FUNCTION layer_obs_roads(bbox geometry, zoom_level int)
CREATE OR REPLACE FUNCTION layer_obs_roads(bbox geometry, zoom_level int, user_id integer, min_time timestamp, max_time timestamp)
RETURNS TABLE(
way_id bigint,
geometry geometry,
@ -25,16 +26,24 @@ RETURNS TABLE(
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY distance_overtaker) as distance_overtaker_median,
array_agg(distance_overtaker) as distance_overtaker_array,
count(overtaking_event.id)::int as distance_overtaker_count,
(select count(id) from road_usage where road_usage.way_id = road.way_id and
(road.directionality != 0 or road_usage.direction_reversed = r.rev)) as usage_count,
(
SELECT count(road_usage.id) from road_usage
JOIN track ON track.id = road_usage.track_id
WHERE road_usage.way_id = road.way_id
AND (road.directionality != 0 or road_usage.direction_reversed = r.rev)
AND (user_id is NULL or user_id = track.author_id)
AND road_usage.time BETWEEN COALESCE(min_time, '1900-01-01'::timestamp) AND COALESCE(max_time, '2100-01-01'::timestamp)
) as usage_count,
r.dir as direction,
road.zone::zone_type as zone,
case when road.directionality = 0 then r.dir else 0 end as offset_direction
FROM road
LEFT JOIN (VALUES (-1, TRUE), (1, FALSE), (0, FALSE)) AS r(dir, rev) ON (abs(r.dir) != road.directionality)
FULL OUTER JOIN overtaking_event ON (road.way_id = overtaking_event.way_id and (road.directionality != 0 or overtaking_event.direction_reversed = r.rev))
-- WHERE road.name = 'Merzhauser Straße'
JOIN track ON overtaking_event.track_id = track.id
WHERE road.geometry && bbox
GROUP BY road.name, road.way_id, road.geometry, road.directionality, r.dir, r.rev, road.zone;
AND (user_id is NULL or user_id = track.author_id)
AND overtaking_event.time BETWEEN COALESCE(min_time, '1900-01-01'::timestamp) AND COALESCE(max_time, '2100-01-01'::timestamp)
GROUP BY road.name, road.way_id, road.geometry, road.directionality, road.zone, r.dir, r.rev;
$$ LANGUAGE SQL IMMUTABLE;

View file

@ -48,7 +48,7 @@ layer:
direction,
zone,
offset_direction
FROM layer_obs_roads(!bbox!, z(!scale_denominator!))
FROM layer_obs_roads(!bbox!, z(!scale_denominator!), user_id, min_time, max_time)
) AS t
schema: