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

y3bcpkx1  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(373)

我需要在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()提供列的方式有关。

vptzau2j

vptzau2j1#

多亏了@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。
记住,在为多边形构造wkt时,点的顺序很重要 (POLYGON((left bottom,right bottom,right top,left top,left bottom))) ,并且第一个点和最后一个点必须相同,以便多边形闭合。

相关问题