4

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

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

PostGIS 似乎可以通过ST_Buffer函数执行此操作。MySQL GIS 是否提供类似的功能?

4

3 回答 3

3

据我所知,缓冲函数尚未在 MySQL 中实现:

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 查询和欧几里得距离

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

希望这可以帮助。


编辑: 性能肯定是这个查询的一个问题。

至于 MySQL 中的空间函数,似乎最新的快照包括了 Buffer 或 Distance 等新函数。您可能想尝试一下:

于 2010-01-16T16:10:09.657 回答
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 的计算。

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

于 2010-01-26T11:55:32.590 回答
0

从 MySQL 5.7.6 开始。
ST_Distance_sphere(g1, g2[, 半径])

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

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

编辑:为什么投反对票?

SELECT *
FROM gistable g
WHERE ST_Distance_Sphere(g.point, circleCenterPT) <= radius
于 2017-01-05T22:18:06.037 回答