我想将Javascript对象存储为Mysql数据库中的多边形。我可以定义x和y的起点和终点来给我四个角:
var polygon = [
xstart +' '+ ystart,
xend +' '+ ystart,
xend +' '+ yend,
xstart +' '+ yend,
xstart +' '+ ystart
];
我可以将此对象格式化为字符串
polygon = "POLYGON'("+ polygon.toString() +")',0";
当我将其插入到Mysql数据库中时
INSERT INTO `caption` (`caption_id`, `caption_area`) VALUES (NULL, 'POLYGON\'(0.28540775806607094 0.3356063323928521,0.35407728596306665 0.3356063323928521,0.35407728596306665 0.45764498813705906,0.28540775806607094 0.45764498813705906,0.28540775806607094 0.3356063323928521)\',0')
查询失败
#1416 - Cannot get geometry object from data you send to the GEOMETRY field
如何格式化Javascript对象或查询,以将多边形正确插入数据库中?
在mysql中,您必须使用ST_GeomFromText
polygon = "ST_GeomFromText('POLYGON("+ polygon.toString() +")')";
我不确定您到底需要什么数字0。
POLYGON是来自Open Geospatial Consortium OpenGIS中定义的MySQL空间扩展的空间数据类型。您可以在MySQL中为四个多边形定义空间数据类型,例如:
LINESTRING(0.28540775806607094 0.3356063323928521, 0.35407728596306665 0.3356063323928521, 0.35407728596306665 0.45764498813705906, 0.28540775806607094 0.45764498813705906, 0.28540775806607094 0.3356063323928521)
因此,它使用以下命令将caption_area存储为TEXT:
SET @g = 'LINESTRING(0.28540775806607094 0.3356063323928521, 0.35407728596306665 0.3356063323928521, 0.35407728596306665 0.45764498813705906, 0.28540775806607094 0.45764498813705906, 0.28540775806607094 0.3356063323928521)';
INTO `caption` (`caption_id`, `caption_area`) VALUES (NULL,ST_AsText(ST_GeomFromText(@g)));