-
Notifications
You must be signed in to change notification settings - Fork 63
/
Sieve.sql
53 lines (46 loc) · 1.89 KB
/
Sieve.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/******************************************************************************
### Sieve ###
Filters small rings (both inner and outer) from a multipolygon based on area.
__Parameters:__
- `geometry` g - A multipolygon
- `float` area_threshold - the minimum ring area to keep.
__Returns:__ `geometry` - a polygon or multipolygon
******************************************************************************/
create or replace function Sieve (g geometry, area_threshold float)
returns geometry
language sql immutable as
$func$
with exploded as (
-- First use ST_Dump to explode the input multipolygon
-- to individual polygons.
select (ST_Dump(g)).geom
), rings as (
-- Next use ST_DumpRings to turn all of the inner and outer rings
-- into their own separate polygons.
select (ST_DumpRings(geom)).geom from exploded
) select
-- Finally, build the multipolygon back up using only the rings
-- that are larger than the specified threshold area.
ST_SetSRID(ST_BuildArea(ST_Collect(geom)), ST_SRID(g))
from rings
where ST_Area(geom) > area_threshold;
$func$;
create or replace function Sieve (g geometry, area_threshold integer)
returns geometry
language sql immutable as
$func$
with exploded as (
-- First use ST_Dump to explode the input multipolygon
-- to individual polygons.
select (ST_Dump(g)).geom
), rings as (
-- Next use ST_DumpRings to turn all of the inner and outer rings
-- into their own separate polygons.
select (ST_DumpRings(geom)).geom from exploded
) select
-- Finally, build the multipolygon back up using only the rings
-- that are larger than the specified threshold area.
ST_SetSRID(ST_BuildArea(ST_Collect(geom)), ST_SRID(g))
from rings
where ST_Area(geom) > area_threshold;
$func$;