Migrating a MariaDB table with geometric data to MySql, some data cannot be inserted, because they are not well-formed, even if it's not a issue for MariaDB.
This request works on MariaDB (10.2).
CREATE TABLE IF NOT EXISTS geo (
id INT AUTO_INCREMENT NOT NULL,
value GEOMETRY NOT NULL,
SPATIAL INDEX idx_value (value),
PRIMARY KEY(id)
) ENGINE = InnoDB;
INSERT INTO geo (value) SELECT ST_GeomFromText('LINESTRING(1 2)');
Not on MySql (5.7.20), where the error is:
3037 - Invalid GIS data provided to function st_geometryfromtext.
There are three functions to identify such geometries in MySql: ST_IsSimple()
, ST_IsValid()
, and ST_Validate()
but they don't work with badly formatted geometries:
SELECT ST_IsSimple(ST_GeomFromText('LINESTRING(1 2)'));
SELECT ST_IsValid(ST_GeomFromText('LINESTRING(1 2)'));
SELECT ST_AsText(ST_Validate(ST_GeomFromText('LINESTRING(1 1)')));
3055 - Geometry byte string must be little endian.
This example comes from https://dev.mysql.com/doc/refman/5.7/en/spatial-convenience-functions.html, but it doesn't work. So it's strange (the doc was not updated for 5.7). More details about validity on mysql: https://dev.mysql.com/doc/refman/5.7/en/geometry-well-formedness-validity.html (mysql accepts any syntactically well-formed input, but not the geometrically invalid).
Similar issues here:
- MySQL spatial geometry validate wkt, where the answer is : the functions provided by MySQL to test validity of geometries requires well formed geometry as input...
- MySQL 5.7: Invalid GIS data, where there is the idea to use a stored function and to create an exception handler, so a bit complicated.
- A bug report asking for the same issue, but without response: https://bugs.mysql.com/bug.php?id=76595
- https://github.com/creof/doctrine2-spatial/issues/155 (doctrine php orm), that says that the result is changing between versions of mysql.
But none of them answer to the issue: how to identify badly formatted geometries on mysql 5.7?