使用 MySQL 查找包含某个点的区域

发布于 2024-09-07 14:38:22 字数 150 浏览 1 评论 0原文

我正在使用 MySQL GIS 和 Spatial Extensions。

我有一个表格,其中包含由纬度、经度和半径描述的“圆形”区域。

我需要一个查询来获取其区域包含由纬度和经度定义的给定点的所有行。 “圆形”区域可以相交,因此该点可以落在多个区域中。

I'm using MySQL GIS and Spatial Extensions.

I've a table with "circular" regions described by latitude, longitude and radius.

I need a query to get all the rows whose region include a given point defined by a latitude and a longitude. The "circular" regions can intersect and therefore the point can fall in more than one region.

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

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

发布评论

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

评论(1

潦草背影 2024-09-14 14:38:22

我想出了一个解决方案,我想将其发布在这里只是为了对此发表意见。请注意,上面的表结构略有修改,添加了一个新列“location POINT”,该列是使用每次插入时的纬度和经度构建的。

CREATE PROCEDURE GetRequestsAroundLocation( IN lat DOUBLE, 
                                            IN lon DOUBLE )
BEGIN
  SET @answerLocation = GeomFromText( CONCAT( 'POINT( ', lat, ' ', lon, ' )' ) );

  SELECT id, token, latitude, longitude FROM Request 
  WHERE answerId = -1 AND 
        Intersects( @answerLocation, 
                    GeomFromText( CONCAT( 'POLYGON((', latitude - radius, ' ', longitude - radius, ',', 
                                                       latitude + radius, ' ', longitude - radius, ',', 
                                                       latitude + radius, ' ', longitude + radius, ',', 
                                                       latitude - radius, ' ', longitude + radius, ',', 
                                                       latitude - radius, ' ', longitude - radius, '))' ) ) ) AND 
        SQRT( POW( ABS( lat - latitude ), 2 ) + POW( ABS( lon - longitude ), 2 ) ) < radius;  
END //          

I came up with a solution and I would like to post it here just to have an opinion about it. Notice that the table structure above was slightly modified by adding a new column "location POINT" that is built using the latitude and the longitude at each insert.

CREATE PROCEDURE GetRequestsAroundLocation( IN lat DOUBLE, 
                                            IN lon DOUBLE )
BEGIN
  SET @answerLocation = GeomFromText( CONCAT( 'POINT( ', lat, ' ', lon, ' )' ) );

  SELECT id, token, latitude, longitude FROM Request 
  WHERE answerId = -1 AND 
        Intersects( @answerLocation, 
                    GeomFromText( CONCAT( 'POLYGON((', latitude - radius, ' ', longitude - radius, ',', 
                                                       latitude + radius, ' ', longitude - radius, ',', 
                                                       latitude + radius, ' ', longitude + radius, ',', 
                                                       latitude - radius, ' ', longitude + radius, ',', 
                                                       latitude - radius, ' ', longitude - radius, '))' ) ) ) AND 
        SQRT( POW( ABS( lat - latitude ), 2 ) + POW( ABS( lon - longitude ), 2 ) ) < radius;  
END //          
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文