[postgis-users] Cleaning non valid multipolygon

Yves Moisan yves.moisan at boreal-is.com
Tue Feb 3 06:09:50 PST 2009


> This may work, but be careful!  ST_Union is designed to work on *valid* 
> geometries. 

Right.  I specifically owrk with the non valid geometries to begin
with. 

> -- Here's how I would rebuild your geometry
> SELECT ST_AsText(
>    -- properly deals with polygon holes
>    ST_BuildArea(
>       -- generate properly noded linework
>       ST_Union(
>          ST_Boundary(column1),
>          ST_Startpoint(ST_Boundary(column1))
>       )
>    )
> )
> FROM ( VALUES ('MULTIPOLYGON(((
> 376249.7830234 8717655.6050357,
> 376268.0818048 8717666.0116082,
> 376265.2666038 8717670.5117466,
> 376247.812391 8717660.3864341,
> 376249.7830234 8717655.6050357,
> 376247.812391 8717660.3864341,
> 376265.2666038 8717670.5117466,
> 376268.0818048 8717666.0116082,
> 376249.7830234 8717655.6050357)))'::geometry)) AS foo;
> 

Thanx Kevin.  That worked superbly!  I had to add multi() to the whole
bit because I had a constraint on multipolygons, so here is how I
cleaned my non valid geometries, for the record :

UPDATE MyTable g set the_geom = 
multi(
  ST_BuildArea(
      -- generate properly noded linework
      ST_Union(
         ST_Boundary(foo.the_geom),
         ST_Startpoint(ST_Boundary(foo.the_geom))
      )
   )
) 
  
FROM (SELECT the_geom, gid
FROM MyTable
WHERE NOT ST_IsValid(the_geom)
) As foo
WHERE g.gid = foo.gid

Thanx again and thanx community (Simon and Düster in particular).

Yves




More information about the postgis-users mailing list