ST_Buffer相当于MySQL中基于Circle的搜索?

时间:2009-12-28 20:56:10

标签: mysql gis postgis

我需要使用MySQL GIS搜索具有指定圆圈内的点的行。伪代码示例查询是:

select * from gistable g where isInCircle(g.point, circleCenterPT, radius)

PostGIS似乎可以通过ST_Buffer功能完成此操作。 MySQL GIS是否提供类似的功能?

3 个答案:

答案 0 :(得分:3)

据我所知,MySQL中的缓冲区函数为not yet implemented

  

这些功能不是   在MySQL中实现。他们可能会   出现在未来的版本中。

* Buffer(g,d)

  Returns a geometry that represents all points whose distance from the geometry value g is less than or equal to a distance of d.

如果我理解你的问题,你可能甚至不需要空间函数来执行此查询,你可以使用“常规”SQL查询和Euclidean distance

select * 
from gistable g 
where SQRT(POW(circleCenterPT.x - point.x,2) + POW(circleCenterPT.y - point.y,2)) < radius

希望这有帮助。


修改 此查询肯定会导致性能问题。

对于MySQL中的空间函数,似乎最新的快照包括缓冲区或距离等新功能。 你可能想尝试一下:

答案 1 :(得分:1)

即使您使用PostGIS,也不需要使用ST_Buffer函数,但ST_Expand执行与此相同的操作(伪代码):

-- expand bounding box with 'units' in each direction
envelope.xmin -= units;
envelope.ymin -= units;
envelope.xmax += units;
envelope.ymax += units;
-- also Z coordinate can be expanded this way

在PostGIS语法中,SQL查询通常如下所示:

SELECT AsText(geom) FROM mypoints
WHERE
  -- operator && triggers use of spatial index, for better performance
  geom && ST_Expand(ST_GeometryFromText('POINT(10 20)', 1234), 5) 
AND
  -- and here is the actual filter condition
  Distance(geom, ST_GeometryFromText('POINT(10 20)', 1234)) < 5

在postgis-users邮件列表中查找Buffer vs Expand解释。

因此,理想情况下是用MySQL复制类似的行为。我根本不是MySQL专家,但我认为即使没有ST_Expand函数也是可行的。

以下是模仿ST_Expand函数的方法:

CONCAT('POLYGON((',
    X(GeomFromText('POINT(10 20)')) - 5, ' ', Y(GeomFromText('POINT(10 20)')) - 5, ',',
    X(GeomFromText('POINT(10 20)')) + 5, ' ', Y(GeomFromText('POINT(10 20)')) - 5, ',',
    X(GeomFromText('POINT(10 20)')) + 5, ' ', Y(GeomFromText('POINT(10 20)')) + 5, ',',
    X(GeomFromText('POINT(10 20)')) - 5, ' ', Y(GeomFromText('POINT(10 20)')) + 5, ',',
    X(GeomFromText('POINT(10 20)')) - 5, ' ', Y(GeomFromText('POINT(10 20)')) - 5, '))'
);

然后将此结果与此类查询结合起来:

SELECT AsText(geom) FROM mypoints
WHERE
  -- AFAIK, this should trigger use of spatial index in MySQL
  -- replace XXX with the of expanded point as result of CONCAT above
  Intersects(geom, GeomFromText( XXX ) )
AND 
  -- test condition
  Distance(geom, GeomFromText('POINT(10 20)')) < 5

如果您使用距离功能不可用的旧MySQL版本,那么您可以使用amercader使用基于SQRT的计算。

我希望它会给你一些想法。

答案 2 :(得分:0)

自MySQL 5.7.6起。
ST_Distance_sphere(g1, g2[, radius])

  

返回球体上两点和/或多点之间的最小球面距离(米),如果任何几何参数为NULL或为空,则返回NULL

     

计算使用球形地球和可配置的半径。可选的radius参数应以米为单位。如果省略,则默认半径为6,370,986米。如果存在radius参数但不是正数

,则会发生ER_WRONG_ARGUMENTS错误