查找另一个地理点范围内的地理点 - SQL Server

发布于 2024-12-19 11:33:17 字数 333 浏览 1 评论 0原文

我在 SQL Server 中有一个表,其中包含地理数据类型列。我想查询此表以查找位于另一个给定地理点附近(在一定范围内)的行。有谁对执行此操作的最佳方法有任何想法吗? 我有 2 个选项可以选择在哪里执行此类查询。我可以将其作为存储过程编写在 sql 服务器上,也可以在 C# 代码中进行,因为我使用实体框架进行数据访问。

我将有一个查询,其中包含一个范围(例如 100m)和一个传递给它的地理点。 伪代码将是这样的......

选择行 where rows.geo 在给定地理点的范围内

我在网络上的 SQL Server 中查找地理查询的示例时遇到了一些麻烦。

任何帮助将不胜感激。

I have a table in SQL Server that has geography datatype column in it. I want to query this table to find rows that fall near (within a range) of another given geography point. Does anyone have any ideas as to the best way to do this?
I have 2 options of where to do this type of query. I can write it on the sql server as a stored proc or I can do it in c# code as I am using the Entity Framework for data access.

I would have a query that has a range (eg 100m) and a geography point passed in to it.
The pseudo code would be something like this...

select rows
where rows.geo within range of given geography point

I'm having a bit of trouble finding examples of geography queries in SQL Server on the web.

Any help would be appreciated.

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

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

发布评论

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

评论(2

对风讲故事 2024-12-26 11:33:17

您已经在使用 SQL Server Spatial 和 geography 列,因此您只需使用以下命令即可获取结果。有两种方法:

使用 STDistance():

-- Get the center point
DECLARE @g geography
SELECT @g = geo FROM yourTable WHERE PointId = something

-- Get the results, radius 100m
SELECT * FROM yourTable WHERE @g.STDistance(geo) <= 100

使用 STBuffer() 和 STIntersects

-- Get the center buffer, 100m radius
DECLARE @g geography
SELECT @g = geo.STBuffer(100) FROM yourTable WHERE PointId = something

-- Get the results within the buffer
SELECT * FROM yourTable WHERE @g.STIntersects(geo) = 1

根据我的经验,两种方法的性能随数据分布和空间索引网格大小而变化,因此请根据您自己的数据进行测试来决定使用哪一种。请记住在地理列上创建空间索引。

You are already using SQL Server Spatial and geography columns, so you can just use the following to get the result. There are two ways:

Using STDistance():

-- Get the center point
DECLARE @g geography
SELECT @g = geo FROM yourTable WHERE PointId = something

-- Get the results, radius 100m
SELECT * FROM yourTable WHERE @g.STDistance(geo) <= 100

Using STBuffer() and STIntersects

-- Get the center buffer, 100m radius
DECLARE @g geography
SELECT @g = geo.STBuffer(100) FROM yourTable WHERE PointId = something

-- Get the results within the buffer
SELECT * FROM yourTable WHERE @g.STIntersects(geo) = 1

From my experience the performance of two methods varies with data distribution and spatial index grid size, so test on your own data to decide which one to use. Remember to have Spatial Index created on the geo column.

街角卖回忆 2024-12-26 11:33:17

假设您有数据库中点的纬度和经度值。

select * from yourtable where SQRT 
( POWER((yourtable.lat - reflat) * COS(reflat/180) * 40000 / 360, 2) 
+ POWER((yourtable.long - reflong) * 40000 / 360, 2)) < radiusofinterest

reflat 和 reflong 是您想要了解附近地点的点。
radiusofinterest 是距该点的距离。
40000是地球的周长。你可以使用更准确的数字。

我还没有检查 SQLServer 的语法...所以那里可能有一些错误。

cos(reflat) 根据您所在的纬度校正周长。对于较小的距离,它应该可以正常工作。

Assuming you have lat and long values of the points in the db.

select * from yourtable where SQRT 
( POWER((yourtable.lat - reflat) * COS(reflat/180) * 40000 / 360, 2) 
+ POWER((yourtable.long - reflong) * 40000 / 360, 2)) < radiusofinterest

reflat and reflong is the point from which you want to know the places close to.
radiusofinterest is the distance from this point.
40000 is the circumference of the earth. you could use more accurate figures.

i havent checked the syntax with SQLServer though.... so there may be some errors there.

the cos(reflat) corrects the circumference based on the lat you are in. It should work ok for smaller distances.

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