Skip to content

Instantly share code, notes, and snippets.

@ian29
Created March 5, 2013 16:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ian29/5091516 to your computer and use it in GitHub Desktop.
Save ian29/5091516 to your computer and use it in GitHub Desktop.
-- 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