从边界坐标列创建mysql多边形列

时间:2018-07-18 12:52:56

标签: mysql sql geospatial

我需要在mysql数据库(MySQL 5.5.60-0ubuntu0.14.04.1)中创建一列,该列的特征是从数据库中包含顶部,底部,左侧和右侧边界坐标的其他表创建的边界框。

我的查询看起来像这样,但是由于语法错误而无法执行。尽管当我创建一个点并只取两个坐标时,它仍然可以正常工作。

SELECT 
name.`field_site_sitelong_value` AS name,
basetable.`uuid`,
basetable.`nid`,
msl.`field_elevation_average_value`,
Polygon(
    coordinates.`field_geo_bounding_box_left` coordinates.`field_geo_bounding_box_bottom`,
    coordinates.`field_geo_bounding_box_right` coordinates.`field_geo_bounding_box_bottom`,
    coordinates.`field_geo_bounding_box_right` coordinates.`field_geo_bounding_box_top`,
    coordinates.`field_geo_bounding_box_left` coordinates.`field_geo_bounding_box_top`,
    coordinates.`field_geo_bounding_box_left` coordinates.`field_geo_bounding_box_bottom`
) AS geom

FROM `node` basetable
INNER JOIN `field_data_field_geo_bounding_box` coordinates
ON coordinates.`entity_id` = basetable.`nid`

INNER JOIN `field_data_field_site_sitelong` name
ON name.`entity_id` = basetable.`nid`

LEFT JOIN `field_data_field_elevation_average` msl
ON msl.`entity_id` = basetable.`nid`

WHERE basetable.`status` = 1 AND `field_geo_bounding_box_geo_type` = 'polygon'

我认为问题与我向Polygon()提供列的方式有所不同。

1 个答案:

答案 0 :(得分:1)

由于@Jorge Campos的非常有用的评论,我得以提出一个有效的查询:

SELECT 
name.`field_site_sitelong_value` AS name,
basetable.`uuid`,
basetable.`nid`,
msl.`field_elevation_average_value`,

POLYFROMTEXT(concat(
        'Polygon((', 
            coordinates.`field_geo_bounding_box_left`   , ' ', coordinates.`field_geo_bounding_box_bottom`  , ', ',     
            coordinates.`field_geo_bounding_box_right`  , ' ', coordinates.`field_geo_bounding_box_bottom`  , ', ', 
            coordinates.`field_geo_bounding_box_right`  , ' ', coordinates.`field_geo_bounding_box_top`     , ', ',
            coordinates.`field_geo_bounding_box_left`   , ' ', coordinates.`field_geo_bounding_box_top`     , ', ', 
            coordinates.`field_geo_bounding_box_left`   , ' ', coordinates.`field_geo_bounding_box_bottom`  , 
        '))'
    ))
 AS GEOM

FROM `node` basetable
INNER JOIN `field_data_field_geo_bounding_box` coordinates
ON coordinates.`entity_id` = basetable.`nid`

INNER JOIN `field_data_field_site_sitelong` name
ON name.`entity_id` = basetable.`nid`

LEFT JOIN `field_data_field_elevation_average` msl
ON msl.`entity_id` = basetable.`nid`

WHERE basetable.`status` = 1 
  AND `field_geo_bounding_box_geo_type` = 'polygon'

POLYFROMTEXT函数将来自concat函数结果的完整多边形字符串转换为实际的多边形空间数据类型。如果省略POLYFROMTEXT,您将获得每个多边形的人类可读WKT

请记住,在为多边形(POLYGON((left bottom,right bottom,right top,left top,left bottom)))构造WKT时,点的顺序很重要,而且第一个点和最后一个点也必须相同,以便闭合多边形。