获取多边形点mysql

时间:2011-02-24 13:02:55

标签: php mysql polygon

我在mysql中创建了一个表来存储多边形数据:

CREATE TABLE geom (g GEOMETRY);

我使用以下语法将多边形插入其中:

INSERT INTO geom (g)
VALUES(PolygonFromText('POLYGON((
9.190586853 45.464518970,
9.190602686 45.463993916,
9.191572471 45.464001929,
9.191613325 45.463884676,
9.192136130 45.463880767,
9.192111509 45.464095594,
9.192427961 45.464117804,
9.192417811 45.464112862,
9.192509035 45.464225851,
9.192493139 45.464371079,
9.192448471 45.464439002,
9.192387444 45.464477861,
9.192051402 45.464483037,
9.192012814 45.464643592,
9.191640825 45.464647090,
9.191622331 45.464506215,
9.190586853 45.464518970))')
);

现在我怎么能在mysql中找回这个多边形的顶点(点)?为什么我要问手段,后来我想找出一个点是否在多边形内。为了实现这一点,我希望我需要多边形顶点。

3 个答案:

答案 0 :(得分:10)

如果你想要WKT回复:SELECT AsText(g) FROM geom;

答案 1 :(得分:2)

如果要查找某个点是否在多边形中,则不需要派生各个顶点来执行此操作。 MySQL(5.6+)中有一个函数:

SELECT ST_Contains(PolygonFromText('POLYGON((
9.190586853 45.464518970,
9.190602686 45.463993916,
9.191572471 45.464001929,
9.191613325 45.463884676,
9.192136130 45.463880767,
9.192111509 45.464095594,
9.192427961 45.464117804,
9.192417811 45.464112862,
9.192509035 45.464225851,
9.192493139 45.464371079,
9.192448471 45.464439002,
9.192387444 45.464477861,
9.192051402 45.464483037,
9.192012814 45.464643592,
9.191640825 45.464647090,
9.191622331 45.464506215,
9.190586853 45.464518970))'), PointFromText("POINT(10 42)")
);

答案 2 :(得分:0)

要回答您的问题,一个不错的选择是输出GeoJSON格式。 在此处查看更多信息:(https://dev.mysql.com/doc/refman/5.7/en/spatial-geojson-functions.html

select ST_AsGeoJSON(g) from geom;

为解决在边界多边形中选择点的实际问题:@jcorry使用ST_Contains

有很好的解决方案

要进一步扩展以从Polygon封装的point表中选择所有coordinates,可以执行以下操作:

select
ST_X(point) as x,
ST_Y(point) as y   
from coordinates
where ST_Contains(
    (select g from geom limit 1), 
    point
)