Skip to content

Commit 4f3708e

Browse files
committed
Add functions.
1 parent 33bab85 commit 4f3708e

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/pubLib05pgis-misc.sql

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,54 @@
55

66
CREATE extension IF NOT EXISTS postgis;
77

8+
-- -- -- -- -- -- -- -- -- --
9+
-- -- -- Transform and simplify functions:
10+
11+
-- https://github.com/osm-codes/WS/issues/11
12+
CREATE or replace FUNCTION ST_CharactDiam(g geometry) RETURNS float
13+
AS $f$
14+
SELECT CASE
15+
WHEN tp IS NULL OR tp IN ('POINT','MULTIPOINT') THEN 0.0 -- or use convexHull for MULTIPOINT
16+
WHEN is_poly AND poly_p<2*poly_a THEN (poly_a+poly_p)/2.0 -- normal perimeter
17+
WHEN is_poly THEN (2*poly_a+SQRT(poly_p))/3.0 -- fractal perimeter
18+
ELSE ST_Length(g)/2.0 -- or use buffer or convexHull
19+
END
20+
FROM (
21+
SELECT tp, is_poly,
22+
CASE WHEN is_poly THEN SQRT(ST_Area(g)) ELSE 0 END AS poly_a,
23+
CASE WHEN is_poly THEN ST_Perimeter(g)/3.5 ELSE 0 END AS poly_p
24+
FROM (SELECT GeometryType(g)) t(tp),
25+
LATERAL (SELECT CASE WHEN tp IN ('POLYGON','MULTIPOLYGON') THEN true ELSE false END) t2(is_poly)
26+
) t3
27+
$f$ LANGUAGE SQL IMMUTABLE;
28+
COMMENT ON FUNCTION ST_CharactDiam
29+
IS 'Characteristic-diameter (zero for point or null geometry). A reference for ST_Segmentize, etc. for complex geometries as countries. Not depends on SRID.'
30+
;
31+
-- test with countries:
32+
-- select isolabel_ext, st_srid(geom) as srid, round(ST_CharactDiam(geom),3) as size_latlon FROM optim.jurisdiction_geom where isolabel_ext ~ '^..$' order by 3 desc,1;
33+
34+
CREATE or replace FUNCTION ST_Transform_resilient(
35+
g geometry,
36+
srid integer,
37+
size_fraction float DEFAULT 0.05
38+
) RETURNS geometry AS $f$
39+
-- discuss ideal at https://gis.stackexchange.com/q/444441/7505
40+
SELECT CASE
41+
WHEN size>0.0 THEN ST_Transform( ST_Segmentize(g,size) , srid )
42+
ELSE ST_Transform(g,srid)
43+
END
44+
FROM (
45+
SELECT CASE
46+
WHEN size_fraction IS NULL THEN 0.0
47+
WHEN size_fraction<0 THEN -size_fraction
48+
ELSE ST_CharactDiam(g) * size_fraction
49+
END
50+
) t1(size)
51+
$f$ LANGUAGE SQL IMMUTABLE;
52+
COMMENT ON FUNCTION ST_Transform_resilient
53+
IS 'Computes ST_Transform with previous ST_Segmentize when is not a point and size_fraction is not null. Negative size_fraction is used as characteristic-diameter avoiding CPU cost.'
54+
;
55+
856
-- -- -- -- -- -- -- -- -- --
957
-- -- -- URI Functions:
1058

0 commit comments

Comments
 (0)