Created
March 5, 2013 16:22
-
-
Save ian29/5091516 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- from http://www.spatialdbadvisor.com/postgis_tips_tricks/92/filtering-rings-in-polygon-postgis | |
CREATE OR REPLACE FUNCTION filter_rings(geometry, DOUBLE PRECISION) | |
RETURNS geometry AS | |
$BODY$ | |
SELECT ST_BuildArea(ST_Collect(b.final_geom)) AS filtered_geom | |
FROM (SELECT ST_MakePolygon((/* Get outer ring of polygon */ | |
SELECT ST_ExteriorRing(a.the_geom) AS outer_ring /* ie the outer ring */ | |
), ARRAY(/* Get all inner rings > a particular area */ | |
SELECT ST_ExteriorRing(b.geom) AS inner_ring | |
FROM (SELECT (ST_DumpRings(a.the_geom)).*) b | |
WHERE b.path[1] > 0 /* ie not the outer ring */ | |
AND ST_Area(b.geom) > $2 | |
) ) AS final_geom | |
FROM (SELECT ST_GeometryN(ST_Multi($1),/*ST_Multi converts any Single Polygons to MultiPolygons */ | |
generate_series(1,ST_NumGeometries(ST_Multi($1))) | |
) AS the_geom | |
) a | |
) b | |
$BODY$ | |
LANGUAGE 'sql' IMMUTABLE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment