使用多边形或圆作为搜索来查找纬度/经度

发布于 2024-11-24 01:40:57 字数 409 浏览 0 评论 0原文

我不适合这里,所以如果我没有正确/清楚地问这个问题,请原谅我。

我有一张用户表,每个用户都有纬度/经度。

我从联邦机构收到一个查询,该查询提供了一个多边形或一个圆形,如下所示:

<polygon>38.47,-120.14 38.34,-119.95 38.52,-119.74 38.62,-119.89 38.47,-120.14</polygon>
<circle>32.9525,-115.5527 0</circle>

给定这两个输入中的任何一个,我需要返回多边形或圆形内的所有用户。

我已经查看了所有内容,阅读了所有 mysql 空间文档等,我要么错过了一个步骤,要么只是没有理解查询方法。

任何帮助将不胜感激!

I'm out of my element here so please forgive me if I dont ask this correctly/clearly.

I have a table of users with lat/long for each one.

I get a query from a federal agency that provides either a polygon or a circle like this:

<polygon>38.47,-120.14 38.34,-119.95 38.52,-119.74 38.62,-119.89 38.47,-120.14</polygon>
<circle>32.9525,-115.5527 0</circle>

Given either of these two inputs, I need to return all users within the polygon or circle.

I've looked all over, read all the mysql spatial docs etc. and I am either missing a step or just not grokking the query method.

ANY help would be appreciated!

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

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

发布评论

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

评论(3

吾性傲以野 2024-12-01 01:40:58

这是我想出的答案,似乎正在工作:

首先,我通过添加一个字段来保存从用户纬度/经度创建的空间点来更改表:

ALTER TABLE users ADD location POINT NOT NULL;
UPDATE users set location = PointFromText(CONCAT('POINT(',lat,' ',lon,')'))
CREATE SPATIAL INDEX location ON users(location);

从那里,我使用它来查询以查找多边形内的用户:

SET @bbox = 'POLYGON((38.47,-120.14 38.34,-119.95 38.52,-119.74 38.62,-119.89 38.47,-120.14))';
SELECT * , AsText( location ) 
FROM users
WHERE Intersects( location, GeomFromText( @bbox ) ) ;

对于新用户,我创建了一个触发器来根据纬度/经度更新位置(如果没有纬度\经度,则无法添加用户):

DROP TRIGGER IF EXISTS `users_insert_defaults`;
DELIMITER //
CREATE TRIGGER `users_insert_defaults` BEFORE INSERT ON `users`
 FOR EACH ROW SET NEW.location = PointFromText(CONCAT('POINT(',NEW.lat,' ',NEW.lon,')'))
//
DELIMITER ;

This is the answer that I came up with and appears to be working:

First, I altered the table by adding a field to hold the spatial points created from the users lat/lon:

ALTER TABLE users ADD location POINT NOT NULL;
UPDATE users set location = PointFromText(CONCAT('POINT(',lat,' ',lon,')'))
CREATE SPATIAL INDEX location ON users(location);

From there, I'm using this to query to find the users within the polygon:

SET @bbox = 'POLYGON((38.47,-120.14 38.34,-119.95 38.52,-119.74 38.62,-119.89 38.47,-120.14))';
SELECT * , AsText( location ) 
FROM users
WHERE Intersects( location, GeomFromText( @bbox ) ) ;

For new users I created a trigger to update the location from the lat/lon (user can't be added without a lat\lon):

DROP TRIGGER IF EXISTS `users_insert_defaults`;
DELIMITER //
CREATE TRIGGER `users_insert_defaults` BEFORE INSERT ON `users`
 FOR EACH ROW SET NEW.location = PointFromText(CONCAT('POINT(',NEW.lat,' ',NEW.lon,')'))
//
DELIMITER ;
谁的新欢旧爱 2024-12-01 01:40:58

对于圆,您需要计算从圆中心到人员列表中每个纬度/经度的距离。如果距离小于圆的半径,则该人在圆内。计算沿着“大圆”的 2 个纬度/经度之间的距离的典型公式称为半正矢公式。下面的网站用 JavaScript 显示了这个公式。

http://www.movable-type.co.uk/scripts/latlong.html

对于一般的多边形来说,这要困难得多。
对于多边形中的一般点,点包含测试似乎可以假设一个简单的等矩形投影(x = lon,y = lat)。

For a circle, you would need to compute the distance from the circle center to each of the lat/lon's in your persons list. If the distance is less than the radius of the circle, that person is inside the circle. Typical formula for computing distances between 2 lat/lon's along the 'great circle' is called the haversine formula. The website below shows this formula in javascript.

http://www.movable-type.co.uk/scripts/latlong.html

For a general polygon, this is much harder.
For general point in a polygon, the Point Inclusion Test seems to work assuming a trivial Equirectanglular projection (x = lon, y = lat).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文