8

Here's my code:

SET @poly =
    'Polygon((-98.07697478272888 30.123832577126326,
              -98.07697478272888 30.535734310413392,
              -97.48302581787107 30.535734310413392,
              -97.48302581787107 30.123832577126326))';

SELECT name
FROM county_shapes
WHERE MBRContains(ST_GeomFromText(@poly), SHAPE);

Whenever I run that I get a "MySQL: Invalid GIS data provided to function st_geometryfromtext" error.

This returns the same error:

SELECT name
FROM county_shapes
WHERE MBRContains(ST_GeomFromText('Polygon((-98.07697478272888 30.123832577126326,
              -98.07697478272888 30.535734310413392,
              -97.48302581787107 30.535734310413392,
              -97.48302581787107 30.123832577126326))'), SHAPE);

Any ideas?

neubert
  • 15,947
  • 24
  • 120
  • 212
  • 2
    Exactly which version of MySQL 5.7? Prior to [5.7.5](https://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-5.html#mysqld-5-7-5-spatial-support), rings had to be explicitly closed by defining the first and last points in their sequences to be the same. – eggyal Dec 30 '15 at 07:46
  • 5.7.10. But defining the first and last points to be the same did fix the issue. Thanks! – neubert Dec 30 '15 at 13:46
  • This thread lead me to create [this one](http://stackoverflow.com/q/44050320/1219280). – Veverke May 18 '17 at 14:19

2 Answers2

20

You need to specify the first and last point as same.

Try this.

SET @poly =
    'Polygon((-98.07697478272888 30.123832577126326,
              -98.07697478272888 30.535734310413392,
              -97.48302581787107 30.535734310413392,
              -97.48302581787107 30.123832577126326,
              -98.07697478272888 30.123832577126326,))';

SELECT name
FROM county_shapes
WHERE MBRContains(ST_GeomFromText(@poly), SHAPE);

AND

SELECT name
FROM county_shapes
WHERE MBRContains(ST_GeomFromText('Polygon((
              -98.07697478272888 30.123832577126326,
              -98.07697478272888 30.535734310413392,
              -97.48302581787107 30.535734310413392,
              -97.48302581787107 30.123832577126326,
              -98.07697478272888 30.123832577126326))'), SHAPE);
codejunkie
  • 908
  • 2
  • 21
  • 34
  • 2
    wow, this makes sense!!! (from a geometry point of view, a polygon is a closed plane figure, hence the starting and ending points must equal...) Great one! – Veverke May 18 '17 at 13:22
  • Hello! I have another question here: https://stackoverflow.com/questions/45808396/mysql-5-7-19-invalid-gis-data-provided-to-function-st-geometryfromtext, can you please check it out? Thank you – andres.gtz Aug 22 '17 at 14:22
0

This was my query

SELECT * FROM my_table WHERE ST_Within(point(`lon`, `lat`),ST_GeomFromText('MultiPolygon(((-58.5832214 -34.5365410,-58.5832214 -34.5365410,-58.5832214 -34.5365410,-58.5832214 -34.5365410,-58.5832214 -34.5365410)),((-58.5887184 -34.6642799,-58.2893410 -34.6642799,-58.2893410 -34.5280609,-58.5887184 -34.5280609,-58.5887184 -34.6642799,-58.5887184 -34.6642799),(-58.4351311 -34.5975914,-58.4347000 -34.5976410,-58.4346085 -34.5977516,-58.4348602 -34.5979195,-58.4351311 -34.5975914,-58.4351311 -34.5975914)))'))>'0'

If you see the first polygon's points, all are same, its not a valid polygon

mysql 5.7 was throwing invalid GIS error, but mysql 8 was not throwing error

removing the first polygon solved my problem

Dharman
  • 30,962
  • 25
  • 85
  • 135
Akhil
  • 69
  • 1
  • 5