Bug #64649 ST_Difference() returns invalid polygons with self tangency
Submitted: 14 Mar 2012 15:30 Modified: 5 Oct 2012 17:11
Reporter: Benjamin Morel Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: GIS Severity:S3 (Non-critical)
Version:5.6.4-m7, 5.6.5-m8 OS:Any
Assigned to:
Tags: Geometry, gis, polygon, spatial, ST_Difference
Triage: Needs Triage: D2 (Serious)

[14 Mar 2012 15:30] Benjamin Morel
Description:
ST_Difference() wrongly keeps the rings of the polygons involved in the calculation.

In the example below, we're computing the difference of a polygon with itself; this should return an empty Geometry (behavior verified on PostGIS).

Instead, the geometry returned is composed of invalid polygons with zero area.

How to repeat:
SET @a = ST_GeomFromText('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))', 4326);
SELECT ST_AsText(ST_Difference(@a, @a));

Expected result:
GEOMETRYCOLLECTION EMPTY

Actual result:
MULTIPOLYGON(((0 0,0 1,0 0)),((0 0,1 0,1 1,0 1,1 1,1 0,0 0)))
[14 Mar 2012 15:49] Valerii Kravchuk
Thank you for the bug report. Same with 5.6.5:

macbook-pro:trunk openxs$ bin/mysql -uroot test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.5-m8-debug Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SET @a = ST_GeomFromText('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))', 4326);
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT ST_AsText(ST_Difference(@a, @a));
+---------------------------------------------------------------+
| ST_AsText(ST_Difference(@a, @a))                              |
+---------------------------------------------------------------+
| MULTIPOLYGON(((0 0,0 1,0 0)),((0 0,1 0,1 1,0 1,1 1,1 0,0 0))) |
+---------------------------------------------------------------+
1 row in set (0.00 sec)
[5 Oct 2012 17:11] Paul Dubois
Noted in 5.6.8, 5.7.0 changelogs.

ST_Difference() could incorrectly produce empty polygons in the
result.