[postgis-users] what to do about TopologyExceptions

620 views
Skip to first unread message

David Kaplan

unread,
Dec 20, 2010, 3:53:53 AM12/20/10
to postgi...@postgis.refractions.net
Hi,

I am working with a GIS table that is derived from importing a fairly
complex shapefile. I want to do a number of union and difference
operations, but I am encountering TopologyException problems and am
wondering how much attention I need to pay to these exceptions and what
is the best way to deal with these problems.

For example, upon doing a simple union operation, I have:

CREATE TABLE by_country_iucn AS
SELECT min(ogc_fid) AS ogc_fid, country, iucn_cat,
ST_Multi( ST_Union( wkb_geometry ) ) AS wkb_geometry
FROM pol_2010
GROUP BY country, iucn_cat
ORDER BY country, iucn_cat;

I get the following exception at the end:

NOTICE: TopologyException: side location conflict at 29.1972 0.251105

However, I am not sure what to do with this for two reasons:

1) The point indicated in the exception (presuming this is a 2D point
location) is located in the middle of nowhere and none of my polygons
come anywhere near it (the nearest is >1000 km away).

2) Looking at the result of the union, I don't see any obvious problems.
Do these exceptions mean I am missing some part of the desired union?

For the union, this returns a result no problem, but for the difference
operations that follow it, the query fails with no result after a
similar exception.

I saw in a previous thread that I should use ST_IsValid to test for
problems. I applied this to my shapefile and it found lots of results.
Looking at the "bad" polygons, many look fine to the naked eye, but some
do have strange things like internal lines. Is there a good way to fix
these? ST_SimplifyPreserveTopology? Will doing this help the other
union and difference manipulations?

Thanks for the help.

Cheers,
David

--
**********************************
David M. Kaplan
Charge de Recherche 1

Institut de Recherche pour le Developpement
Centre de Recherche Halieutique Mediterraneenne et Tropicale
av. Jean Monnet
B.P. 171
34203 Sete cedex
France

Phone: +33 (0)4 99 57 32 27
Fax: +33 (0)4 99 57 32 95

http://www.ur097.ird.fr/team/dkaplan/index.html
http://www.amped.ird.fr/
**********************************


_______________________________________________
postgis-users mailing list
postgi...@postgis.refractions.net
http://postgis.refractions.net/mailman/listinfo/postgis-users

strk

unread,
Dec 20, 2010, 5:54:06 AM12/20/10
to PostGIS Users Discussion
On Mon, Dec 20, 2010 at 09:53:53AM +0100, David Kaplan wrote:
> Hi,
>
> I am working with a GIS table that is derived from importing a fairly
> complex shapefile. I want to do a number of union and difference
> operations, but I am encountering TopologyException problems and am
> wondering how much attention I need to pay to these exceptions and what
> is the best way to deal with these problems.
>
> For example, upon doing a simple union operation, I have:
>
> CREATE TABLE by_country_iucn AS
> SELECT min(ogc_fid) AS ogc_fid, country, iucn_cat,
> ST_Multi( ST_Union( wkb_geometry ) ) AS wkb_geometry
> FROM pol_2010
> GROUP BY country, iucn_cat
> ORDER BY country, iucn_cat;
>
> I get the following exception at the end:
>
> NOTICE: TopologyException: side location conflict at 29.1972 0.251105
>
> However, I am not sure what to do with this for two reasons:
>
> 1) The point indicated in the exception (presuming this is a 2D point
> location) is located in the middle of nowhere and none of my polygons
> come anywhere near it (the nearest is >1000 km away).

I can see this is annoying. In fact that point only exists in a version
of the geometries which are internally translated closest to the origin
to increase precision. In turn this "precision imrovement" was triggered
by _another_ topology exception, which isn't printed unless whatever
is attempted later fails.

> 2) Looking at the result of the union, I don't see any obvious problems.
> Do these exceptions mean I am missing some part of the desired union?

It means the operation with the original geometries failed and was instead
conducted on a sligtly modified versions of them.

> For the union, this returns a result no problem, but for the difference
> operations that follow it, the query fails with no result after a
> similar exception.

That might mean nothing worked (precision reduction, snapping etc..)

> I saw in a previous thread that I should use ST_IsValid to test for
> problems. I applied this to my shapefile and it found lots of results.
> Looking at the "bad" polygons, many look fine to the naked eye, but some
> do have strange things like internal lines. Is there a good way to fix
> these? ST_SimplifyPreserveTopology? Will doing this help the other
> union and difference manipulations?

Often ST_Buffer(0) fixes it. Try it. If it doesn't, considere ST_MakeValid
(requires PostGIS svn).

--strk;

() Free GIS & Flash consultant/developer
/\ http://strk.keybit.net/services.html

Peter Hopfgartner

unread,
Dec 20, 2010, 11:49:43 AM12/20/10
to David Kaplan, postgi...@postgis.refractions.net
AFAIK there are slightly different definitions of valid polygon between shape files and PostGIS/Simple Features.
Paul Ramsey had a talk on a number of tricks to correct those problems (and some more):
http://2010.foss4g.org/presentations_show.php?id=3369.

Peter Hopfgartner

R3 GIS Srl - GmbH
http://www.r3-gis.com


--------David Kaplan <david....@ird.fr> wrote--------
Subject: [postgis-users] what to do about TopologyExceptions
Date: 20.12.2010 09:54

>Hi,
>
>I am working with a GIS table that is derived from importing a fairly
>complex shapefile. I want to do a number of union and difference
>operations, but I am encountering TopologyException problems and am
>wondering how much attention I need to pay to these exceptions and what
>is the best way to deal with these problems.
>
>For example, upon doing a simple union operation, I have:
>
>CREATE TABLE by_country_iucn AS
>SELECT min(ogc_fid) AS ogc_fid, country, iucn_cat,
> ST_Multi( ST_Union( wkb_geometry ) ) AS wkb_geometry
>FROM pol_2010
>GROUP BY country, iucn_cat
>ORDER BY country, iucn_cat;
>
>I get the following exception at the end:
>
>NOTICE: TopologyException: side location conflict at 29.1972 0.251105
>
>However, I am not sure what to do with this for two reasons:
>
>1) The point indicated in the exception (presuming this is a 2D point
>location) is located in the middle of nowhere and none of my polygons
>come anywhere near it (the nearest is >1000 km away).
>

>2) Looking at the result of the union, I don't see any obvious problems.
>Do these exceptions mean I am missing some part of the desired union?
>

>For the union, this returns a result no problem, but for the difference
>operations that follow it, the query fails with no result after a
>similar exception.
>

>I saw in a previous thread that I should use ST_IsValid to test for
>problems. I applied this to my shapefile and it found lots of results.
>Looking at the "bad" polygons, many look fine to the naked eye, but some
>do have strange things like internal lines. Is there a good way to fix
>these? ST_SimplifyPreserveTopology? Will doing this help the other
>union and difference manipulations?
>

>Thanks for the help.
>
>Cheers,
>David
>
>--
>**********************************
>David M. Kaplan
>Charge de Recherche 1
>
>Institut de Recherche pour le Developpement
>Centre de Recherche Halieutique Mediterraneenne et Tropicale
>av. Jean Monnet
>B.P. 171
>34203 Sete cedex
>France
>
>Phone: +33 (0)4 99 57 32 27
>Fax: +33 (0)4 99 57 32 95
>
>http://www.ur097.ird.fr/team/dkaplan/index.html
>http://www.amped.ird.fr/
>**********************************
>
>

David Kaplan

unread,
Dec 23, 2010, 7:26:13 AM12/23/10
to postgi...@postgis.refractions.net
Hi,

I thought I had everything figured out with respect to my problems
unioning and differencing polygons, but I am running into more
TopologyExceptions that have me confused.

After looking at Paul Ramsey's FOSS4G presentation, I realized that what
I really wanted to do was an "ArcGIS Union Operation" - i.e. reducing a
set of overlapping polygons into all possible combinations of overlaps.
My table consists of a set of MULTIPOLYGONs that sometimes overlap. I
used ST_Buffer(0) to force them to be valid polygons (and checked this
worked correctly). Then I did the following to reduce every polygon to
just the LINESTRINGs that define them:

SELECT ST_ExteriorRing((ST_DumpRings((ST_Dump(wkb_geometry)).geom)).geom) AS geom
INTO TEMP TABLE ht1 FROM my_geo_table;

Note that I added ST_ExteriorRing to ST_DumpRings to reduce polygons to
linestrings. Is this correct?

Then I wanted to union these linestrings to assure that they have all
the nodes at overlaps, but I keep getting a TopologyException:

SELECT ST_Union(geom) AS geom INTO TEMP TABLE ht2 FROM ht1;
NOTICE: TopologyException: found non-noded intersection between LINESTRING (-165.443 23.1836, -165.443 23.1836) and LINESTRING (-165.443 23.1836, -165.464 23.2079) at -165.443 23.1836
ERROR: GEOS union() threw an error!

Presuming the coordinates given are actual geographic coordinates, it
appears that this problem occurs at a location that two different
linestrings just barely overlap (see attached image). Is this a
postgis / GEOS problem or is there something that I am doing that is
conceptually wrong? In either case, is there a way to move forward?

The next steps in the process would be to use ST_Polygonize to get back
a bunch of polygons, ST_PointOnSurface to find interior points and
ST_Intersects to find the original polygons these points belonged to.

Thanks again for the help.

Cheers,
David

linestring.union.prob.png
Reply all
Reply to author
Forward
0 new messages