diff --git a/api/tools/prepare_sql_tiles.py b/api/tools/prepare_sql_tiles.py index 2833fb5..599a7f5 100755 --- a/api/tools/prepare_sql_tiles.py +++ b/api/tools/prepare_sql_tiles.py @@ -32,6 +32,32 @@ EXTRA_ARGS = [ ] +class CustomMvtGenerator(MvtGenerator): + def generate_sqltomvt_func(self, fname, extra_args: List[Tuple[str, str]]) -> str: + """ + Creates a SQL function that returns a single bytea value or null. This + method is overridden to allow for custom arguments in the created function + """ + extra_args_types = "".join([f", {a[1]}" for a in extra_args]) + extra_args_definitions = "".join( + [f", {a[0]} {a[1]} DEFAULT {a[2]}" for a in extra_args] + ) + + return f"""\ +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;""" + +EXTRA_ARGS = [ + # name, type, default + ("user_id", "integer", "NULL"), + ("min_time", "timestamp", "NULL"), + ("max_time", "timestamp", "NULL"), +] + + class CustomMvtGenerator(MvtGenerator): def generate_sqltomvt_func(self, fname, extra_args: List[Tuple[str, str]]) -> str: """ diff --git a/tile-generator/layers/obs_events/layer.sql b/tile-generator/layers/obs_events/layer.sql index 6c4c5cd..ec6c9af 100644 --- a/tile-generator/layers/obs_events/layer.sql +++ b/tile-generator/layers/obs_events/layer.sql @@ -12,11 +12,11 @@ RETURNS TABLE(event_id bigint, geometry geometry, distance_overtaker float, dist CASE WHEN road.zone IS NULL THEN 'urban' else road.zone END as zone, overtaking_event.way_id::bigint as way_id FROM overtaking_event - FULL OUTER JOIN road ON road.way_id = overtaking_event.way_id + FULL OUTER JOIN road ON (road.way_id = overtaking_event.way_id) JOIN track on track.id = overtaking_event.track_id WHERE zoom_level >= 10 AND - ST_Transform(overtaking_event.geometry, 3857) && bbox + ST_Transform(overtaking_event.geometry, 3857) && bbox; AND (user_id is NULL OR user_id = track.author_id) AND time BETWEEN COALESCE(min_time, '1900-01-01'::timestamp) AND COALESCE(max_time, '2100-01-01'::timestamp);