-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsicob_forceclosed.sql
More file actions
24 lines (24 loc) · 757 Bytes
/
Copy pathsicob_forceclosed.sql
File metadata and controls
24 lines (24 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
SET CLIENT_ENCODING TO 'utf8';
CREATE OR REPLACE FUNCTION public.sicob_forceclosed(geom geometry)
RETURNS geometry
LANGUAGE plpgsql
IMMUTABLE COST 42
AS $function$BEGIN
IF ST_IsClosed(geom) THEN
RETURN geom;
ELSIF GeometryType(geom) = 'LINESTRING' THEN
SELECT ST_AddPoint(geom, ST_StartPoint(geom)) INTO geom;
ELSIF GeometryType(geom) ~ '(MULTI|COLLECTION)' THEN
-- Recursively deconstruct parts
WITH parts AS (
SELECT ST_ForceClosed(gd.geom) AS closed_geom FROM ST_Dump(geom) AS gd
) -- Reconstitute parts
SELECT ST_Collect(closed_geom) INTO geom
FROM parts;
END IF;
IF NOT ST_IsClosed(geom) THEN
RAISE EXCEPTION 'Could not close geometry';
END IF;
RETURN geom;
END;$function$