9

Many a time and oft I've spent hours struggling with bizarre bugs resulting from invalid geometries. The symptoms vary greatly, including:

  • Database slowdowns
  • Incorrect\Null query results
  • Log files overloaded with errors and warnings
  • Inconsistent result with geometric operators (e.g. intersection)

I would like to categorize the types of invalid geometries, in order to help everybody here sanitize their data.

Please answer with your favorite type of invalid geometry (one type per answer). Screenshots, descriptions and solutions are welcome - but try to keep your answer short.

Adam Matan
  • 6,838
  • 7
  • 37
  • 50

3 Answers3

6

An 8 year old gold mine of invalid geometries is provided by the PostGIS isvalid regress tests. Many of these regression tests have been moved to GEOS, for example here and here, as well as other pockets in the tests tree. (Sorry, too many to highlight. Pick a few and paste the WKT into JTS Test builder to visualize them.)

Mike T
  • 42,095
  • 10
  • 126
  • 187
3

Zero-length segments

Example:

SELECT geomFromEWKT('LINESTRING(1 1, 1 1)');
                                geomfromewkt                                    

0102000000020000000000000000000840000000000000104000000000000008400000000000001040

Problems: Storing zero length segment can cause bugs when calculating length ratios (division by zero) or when trying to calculate azimuths and trigonometric functions.

Detection: in PostGIS, zero length segments can be detected using isValid().

Solutions: Try to keep zero length segments as points.

Adam Matan
  • 6,838
  • 7
  • 37
  • 50
3

Self-Intersecting polygons

enter image description here

Example:

SELECT isValid(geomFromEWKT('POLYGON((0 0, 1 1, 1 0, 0 1, 0 0))'));
NOTICE:  Self-intersection at or near point 0.5 0.5
 isvalid 
---------
 f
(1 row)

Problems: Wrong calculations, For example, area():

SELECT area(geomFromEWKT('POLYGON((0 0, 1 1, 1 0, 0 1, 0 0))'));
 area 
------
    0
(1 row)

This can cause zero division bugs with calling functions.

Detection: In PostGIS, self intersecting segments can be detected using isValid().

Solution: Conversion to MULTIPOLYGON (see comment).

Adam Matan
  • 6,838
  • 7
  • 37
  • 50