# Invalid geometry zoo

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.

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.)

## 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.

# Self-Intersecting polygons 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).

• That same shape is equivalent the valid: `MULTIPOLYGON(((0 0, 0 1, 0.5 0.5, 0 0)),((1 1, 1 0, 0.5 0.5, 1 1)))` – Mike T Jun 19 '11 at 6:01
• @Mike Toews: Correct, converting to MULTIPOLYGON is a good solution. – Adam Matan Jun 19 '11 at 7:21