MySQL 的半正矢公式的逆运算?

发布于 2024-07-25 10:09:29 字数 453 浏览 6 评论 0原文

在我的数据库中,我存储一个中心点以及半径(以米为单位)。

我希望传递 lat/lng,然后让我存储的 mysql 值创建一个圆圈,告诉我我传入的点是否在该圆圈内。 有没有什么东西可以让我这样做,类似于半正弦论坛(假设我的观点已经在数据库中)。

半正矢公式: ( 3959 * acos( cos( 弧度(40) ) * cos( 弧度( 纬度 ) ) * cos( 弧度( ) - 弧度(-110) ) + sin( 弧度(40) ) * sin( radians( long ) ) )

db:

circleLatCenter,circleLngCenter,

传入的半径> select id from foo where lat,lng in (制作圆函数:circleLat、circleLng、radius)

In my DB i store a center point, along with a radius (in meters).

I'm looking to pass in a lat/lng, and then have the mysql values i've stored create a circle to tell me if my point i passed in is within that circle. Is there something that would allow me to do this, similar to the haversine forumla (which would assume that my point was already in the db).

Haversine Formula:
( 3959 * acos( cos( radians(40) ) * cos( radians( lat ) ) * cos( radians( long ) - radians(-110) ) + sin( radians(40) ) * sin( radians( long ) ) )

db:

circleLatCenter, circleLngCenter, Radius

passing in>
select id from foo where lat,lng in (make circle function: circleLat, circleLng, radius)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怼怹恏 2024-08-01 10:09:29

MySQL 拥有一整套空间数据函数:

MySQL 的空间扩展

我认为关于测量几何图形之间关系的部分就是您所追求的:

几何图形之间的关系

MySQL has a whole host of spatial data functions:

Spatial Extensions to MySQL

I think the section on measuring the relationships between geometries is what you're after:

Relationships Between Geometries

栖迟 2024-08-01 10:09:29

我通过大圆距离计算边界框并查询数据库来完成类似的地理搜索。 您仍然需要在应用程序中进行另一次处理,以从边界框到圆圈“圆角”。

因此,给定一个点数据库、一个搜索点 (X,Y) 和一个距离 D,找到 (X,Y) D 内的所有点:

  1. 计算 deltaX,即沿 Y 轴移动距离 D 时的点。
  2. 计算 deltaY,即沿 X 轴移动距离 D 的点。
  3. 计算边界框:(X-deltaX,Y-deltaY),(X+deltaX,Y+deltaY)
  4. 使用 SQL BETWEEN 运算符查询点数据库:SELECT * FROM TABLE WHERE X BETWEEN X-deltaX AND X+deltaX AND Y BETWEEN Y-deltaY 和 Y+deltaY
  5. 对返回的点列表进行后处理,计算实际的大圆距离,以删除不在距离圆内的正方形角点处的点。

作为捷径,我通常计算纬度和经度的每英里度数(在赤道,因为经度的每英里度数在两极不同),并将 deltaX 和 deltaY 导出为 (D * 度数) -纬度每英里)或度数经度每英里。 赤道与极点的差异并不重要,因为我已经在 SQL 查询后计算实际距离。

仅供参考 - 每英里 0.167469 至 0.014564 度经度,以及每英里 0.014483 度纬度

I've done similar geographical searches by computing the bounding box via great circle distance and querying the database for that. You still need another pass in your application to "round the corners" from bounding box to circle.

So, given a database of points, a search point (X,Y) and a distance D, find all points within D of (X,Y):

  1. Compute deltaX, which is the point if you moved distance D along the Y axis.
  2. Compute deltaY, which is the point if you moved distance D along the X axis.
  3. Compute your bounding box: (X-deltaX,Y-deltaY),(X+deltaX,Y+deltaY)
  4. Query database of points use SQL BETWEEN operator: SELECT * FROM TABLE WHERE X BETWEEN X-deltaX AND X+deltaX AND Y BETWEEN Y-deltaY AND Y+deltaY
  5. Post-process the list of points returned, computing the actual great circle distance, to remove the points at the corners of the square that are not within your distance circle.

As a short-cut, I typically calculate degrees-per-mile for both lat and lon (at the equator, since the degrees-per-mile is different at the poles for lon), and derive deltaX and deltaY as (D * degrees-lat-per-mile) or degrees-lon-per-mile. The difference at the equator vs pole doesn't matter much, since I'm already computing actual distance after the SQL query.

FYI - 0.167469 to 0.014564 degrees-lon-per-mile, and 0.014483 degrees-lat-per-mile

老子叫无熙 2024-08-01 10:09:29

我知道这是一篇早已死亡的帖子,但是,如果有人遇到过这种情况,您根本不需要创建“反向半正弦公式”。 半正矢公式给出了 a 点和 b 点之间的距离。 您需要 b 点和 a 点之间的距离进行计算。 这些是相同的值。

SELECT *, 
( 3959 * acos( cos( radians(40) ) * cos( radians( `circleLatCenter` ) ) * cos( radians( `circleLngCenter` ) - radians(-110) ) + sin( radians(40) ) * sin( radians( `circleLngCenter` ) ) ) as `haversine` 
FROM `table` WHERE 1=1 
HAVING `haversine` < `Radius`

I know this is a long-dead post, but, in case anyone ever comes across this, you don't need to create a "reverse haversine formula" at all. The Haversine formula gives the distance between point a and point b. You need the distance between point b and point a, for your calculation. These are the same value.

SELECT *, 
( 3959 * acos( cos( radians(40) ) * cos( radians( `circleLatCenter` ) ) * cos( radians( `circleLngCenter` ) - radians(-110) ) + sin( radians(40) ) * sin( radians( `circleLngCenter` ) ) ) as `haversine` 
FROM `table` WHERE 1=1 
HAVING `haversine` < `Radius`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文