[postgis-users] Cleaning non valid multipolygon

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


> -- 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;
> 

Hi Kevin,

I found out that about 20 features out of the 150 that I have a non
valid geometry for get a null geometry as a result of your proposed
solution.  I broke down the parts and found that in the cases where
things do not match buildarea fails on a multilinestring that does not
close.  Here is the breakdown for feature id = 122 with the culprit
lines in **bold**:

orignal geometry : 

'MULTIPOLYGON(((
375838.7246768 8718403.0280759,
375844.0704528 8718410.3968027,
375904.0280814 8718366.8994515,
375898.8035535 8718359.4427696,
**375838.8459248 8718402.9401143,**
375898.8035535 8718359.4427696,
375904.0280814 8718366.8994515,
375844.0704528 8718410.3968027,
375838.7246768 8718403.0280759)))'

122 startpoint

"MULTIPOINT(375838.7246768 8718403.0280759)"

122 boundary

"MULTILINESTRING((
375838.7246768 8718403.0280759,
375844.0704528 8718410.3968027,
375904.0280814 8718366.8994515,
375898.8035535 8718359.4427696,
375838.8459248 8718402.9401143,
375898.8035535 8718359.4427696,
375904.0280814 8718366.8994515,
375844.0704528 8718410.3968027,
375838.7246768 8718403.0280759))"

122 Union:

'MULTILINESTRING(
(375838.7246768 8718403.0280759,375844.0704528 8718410.3968027),
(375844.0704528 8718410.3968027,375904.0280814 8718366.8994515),
(375904.0280814 8718366.8994515,375898.8035535 8718359.4427696),
(375898.8035535 8718359.4427696,**375838.8459248 8718402.9401143)**
)'

You can see the union closes on a point very close to the origin point,
but not quite.  Can I either set some tolerance on the buildarea so it
snaps to the closest point in the case the multilinestring does not
close or enforce a constraint on the union so it produces a closed
linestring ?

If not, I'll correct them by hand.  You've already helped quite a bit !

TIA,

Yves





More information about the postgis-users mailing list