9

我正在尝试将多边形的地理坐标插入我的 MySQL 数据库。我有一个名为 polygon 类型的字段,我尝试运行所有这些查询,但继续收到 SQL 语法错误:

SET @g = 'POLYGON((-74.13591384887695 40.93750722242824,-74.13522720336914 40.929726129575016,-74.15102005004883 40.9329683629703,-74.14329528808594 40.94256444133327))';
INSERT INTO 'zones' ('polygon') VALUES (PolyFromText(@g));

INSERT INTO 'zones' ('polygon') VALUES (PolyFromText('POLYGON((-74.13591384887695 40.93750722242824,-74.13522720336914 40.929726129575016,-74.15102005004883 40.9329683629703,-74.14329528808594 40.94256444133327))'));

INSERT INTO 'zones' ('polygon') VALUES (PolyFromText('POLYGON((-74.13591384887695 40.93750722242824,-74.13522720336914 40.929726129575016,-74.15102005004883 40.9329683629703,-74.14329528808594 40.94256444133327))', 0));

最后一个查询是使用 phpmyadmins 自己的地理空间工具生成的,并返回“列'多边形'不能为空”。任何帮助表示赞赏!

4

2 回答 2

31

原因是因为 null 退出最后一点不等于第一点,是满足 OSGeo 标准 WKT 的条件,在某些实现中这是允许的,但 mysql 对此要求严格,在 SQL Server 文档中更好地解释条件。

看到这个

SELECT  Dimension(GeomFromText('POLYGON((-74.13591384887695 40.93750722242824,-74.13522720336914 40.929726129575016,-74.15102005004883 40.9329683629703,-74.14329528808594 40.94256444133327,-74.13591384887695 40.93750722242824)))'));

mysql的文档

http://dev.mysql.com/doc/refman/5.0/en/gis-class-polygon.html

sqlserver的文档

http://msdn.microsoft.com/en-us/library/bb964739(v=sql.105).aspx

于 2013-03-16T19:07:15.753 回答
-1

截至2020年...

您的查询应更改为

INSERT INTO 'zones' ('polygon') VALUES (ST_PolygonFromText('POLYGON((-74.13591384887695 40.93750722242824,-74.13522720336914 40.929726129575016,-74.15102005004883 40.9329683629703,-74.14329528808594 40.94256444133327, -74.13591384887695 40.93750722242824))'));

注意事项:

  1. 使用正确的多边形转换函数 ST_PolygonFromText
  2. 正确使用嵌套括号来注意内部多边形
  3. 最后一个点等于第一个点 - 所以,多边形是闭合的
于 2020-09-08T11:09:28.327 回答