Last active
August 29, 2015 14:13
-
-
Save bjornharrtell/52056d421c33b6a6ed77 to your computer and use it in GitHub Desktop.
cleanpolys
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
-- Function: gkdb.repairgeom(geometry) | |
-- DROP FUNCTION gkdb.repairgeom(geometry); | |
CREATE OR REPLACE FUNCTION gkdb.repairgeom(polygon geometry) | |
RETURNS geometry AS | |
$BODY$ | |
DECLARE | |
linework geometry; | |
cleanpolygon geometry; | |
BEGIN | |
cleanpolygon := polygon; | |
LOOP | |
linework := ST_SnapToGrid( | |
ST_Collect( | |
array(select ST_ExteriorRing((ST_DumpRings((ST_Dump(cleanpolygon)).geom)).geom) | |
) | |
), 0.001); | |
IF linework IS NULL THEN RETURN NULL; END IF; | |
cleanpolygon := ST_SnapToGrid(ST_BuildArea(ST_Node(lines)), 0.001); | |
IF cleanpolygon IS NULL THEN RETURN NULL; END IF; | |
EXIT WHEN ST_IsValid(cleanpolygon) IS TRUE; | |
END LOOP; | |
RETURN cleanpolygon; | |
--EXCEPTION | |
-- WHEN OTHERS THEN | |
-- RAISE NOTICE 'Error poly: %', ST_AsText(polygon); | |
-- RETURN null; | |
END; | |
$BODY$ | |
LANGUAGE plpgsql VOLATILE | |
COST 100; | |
ALTER FUNCTION gkdb.repairgeom(geometry) | |
OWNER TO postgres; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment