Skip to content

Commit

Permalink
Simplify functions.sql
Browse files Browse the repository at this point in the history
Reduce number of functions
Tidy comments
  • Loading branch information
dch0ph committed Jun 15, 2024
1 parent 987258c commit a1dfd94
Showing 1 changed file with 19 additions and 38 deletions.
57 changes: 19 additions & 38 deletions functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
'no' is returned if the rendering for highway category does not support 'restricted'.
NULL is functionally equivalent to 'yes', but indicates the absence of a restriction
rather than a positive access = yes. 'unknown' corresponds to an uninterpretable
access restriction e.g. access=unknown or motorcar=occasionally */
CREATE OR REPLACE FUNCTION carto_int_access(int_highway text, accesstag text)
access restriction e.g. access=unknown or motorcar=occasionally */
CREATE OR REPLACE FUNCTION carto_int_access(accesstag text, allow_restricted boolean)
RETURNS text
LANGUAGE SQL
IMMUTABLE PARALLEL SAFE
Expand All @@ -16,7 +16,7 @@ SELECT
CASE
WHEN accesstag IN ('yes', 'designated', 'permissive') THEN 'yes'
WHEN accesstag IN ('destination', 'delivery', 'customers') THEN
CASE WHEN int_highway IN ('road', 'pedestrian') THEN 'restricted' ELSE 'yes' END
CASE WHEN allow_restricted = TRUE THEN 'restricted' ELSE 'yes' END
WHEN accesstag IN ('no', 'permit', 'private', 'agricultural', 'forestry', 'agricultural;forestry') THEN 'no'
WHEN accesstag IS NULL THEN NULL
ELSE 'unknown'
Expand All @@ -38,49 +38,30 @@ SELECT
END
$$;

/* Coalesce highways that will be treated in the same way, e.g. all roads become 'road'
Note that bicycle, horse arguments are only relevant if considering highway=path */
CREATE OR REPLACE FUNCTION carto_highway_int_highway(highway text, bicycle text, horse text)
RETURNS text
LANGUAGE SQL
IMMUTABLE PARALLEL SAFE
AS $$
SELECT
CASE
WHEN highway IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'primary', 'primary_link', 'secondary',
'secondary_link', 'tertiary', 'tertiary_link', 'residential', 'unclassified', 'living_street', 'service', 'road') THEN 'road'
WHEN highway IN ('footway', 'steps') THEN 'footway'
WHEN highway = 'path' THEN carto_path_type(bicycle, horse)
ELSE highway
END
$$;

/* Return int_access value which will be used to determine access marking.
Only a restricted number of types can be returned, with NULL corresponding to no access restriction */
/* Return int_access value which will be used to determine access marking */
CREATE OR REPLACE FUNCTION carto_highway_int_access(highway text, "access" text, foot text, bicycle text, horse text, motorcar text, motor_vehicle text, vehicle text)
RETURNS text
LANGUAGE SQL
IMMUTABLE PARALLEL SAFE
AS $$
SELECT
CASE carto_highway_int_highway(highway, bicycle, horse)
WHEN 'road' THEN
carto_int_access('road', CASE
CASE
WHEN highway IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'primary', 'primary_link', 'secondary',
'secondary_link', 'tertiary', 'tertiary_link', 'residential', 'unclassified', 'living_street', 'service', 'road') THEN
carto_int_access(CASE
WHEN motorcar <> 'unknown' THEN motorcar
WHEN motor_vehicle <> 'unknown' THEN motor_vehicle
WHEN vehicle <> 'unknown' THEN vehicle
ELSE "access" END)
WHEN 'pedestrian' THEN carto_int_access('pedestrian', CASE WHEN foot <> 'unknown' THEN foot ELSE "access" END)
WHEN 'footway' THEN carto_int_access('footway', CASE WHEN foot <> 'unknown' THEN foot ELSE "access" END)
WHEN 'path' THEN carto_int_access('path', CASE WHEN foot <> 'unknown' THEN foot ELSE "access" END)
WHEN 'cycleway' THEN carto_int_access('cycleway', CASE WHEN bicycle <> 'unknown' THEN bicycle ELSE "access" END)
WHEN 'bridleway' THEN carto_int_access('bridleway', CASE WHEN horse <> 'unknown' THEN horse ELSE "access" END)
ELSE carto_int_access(NULL, "access")
ELSE "access" END, TRUE)
WHEN highway = 'path' THEN
CASE carto_path_type(bicycle, horse)
WHEN 'cycleway' THEN carto_int_access(CASE WHEN bicycle <> 'unknown' THEN bicycle ELSE "access" END, FALSE)
WHEN 'bridleway' THEN carto_int_access(CASE WHEN horse <> 'unknown' THEN horse ELSE "access" END, FALSE)
ELSE carto_int_access(CASE WHEN foot <> 'unknown' THEN foot ELSE "access" END, FALSE)
END
WHEN highway IN ('pedestrian', 'footway', 'steps') THEN carto_int_access(CASE WHEN foot <> 'unknown' THEN foot ELSE "access" END, FALSE)
WHEN highway = 'cycleway' THEN carto_int_access(CASE WHEN bicycle <> 'unknown' THEN bicycle ELSE "access" END, FALSE)
WHEN highway = 'bridleway' THEN carto_int_access(CASE WHEN horse <> 'unknown' THEN horse ELSE "access" END, FALSE)
ELSE carto_int_access("access", TRUE)
END
$$;

/* Uncomment lines below to create generated column for int_access
ALTER TABLE planet_osm_line DROP COLUMN IF EXISTS int_access;
ALTER TABLE planet_osm_line
ADD int_access text GENERATED ALWAYS AS (CASE WHEN highway IS NOT NULL THEN carto_highway_int_access(highway, "access", foot, bicycle, horse, tags->'motorcar', tags->'motor_vehicle', tags->'vehicle') ELSE NULL END) STORED;
*/

0 comments on commit a1dfd94

Please sign in to comment.