Calculating distances between two tables full of GEOGRAPHY points

蓝咒 提交于 2019-12-13 16:52:20

问题


I'm using SQL Server.

I have two tables like this:

Table1:

Column1, Column2, Column3, GeoLoc
-----------------------------------
a        b        c        0xE61...

Table2:

Column1, Column2, Column3, GeoLoc 
-----------------------------------
a        b        c        0xE62...

I am looking to get an output table which will compare all of the points in both tables, and show me where table1 has a GeoLoc which is within X distance of GeoLoc in table2.

Does anyone know how to go about this? One table has around 800 rows, and the other has about 300,000 rows. I'm stumped as to even where to start...


回答1:


Assuming your GeoLoc column is of the 'Geography' data type in SQL server, you should be able to use something like this:

select
  t1.*,
  t2.*,
  t1.GeoLoc.STDistance(t2.GeoLoc) as DistanceApart
from table1 t1
join table2 t2
on (t1.GeoLoc.STDistance(t2.GeoLoc) <= @distanceX)

with the 'DistanceApart' and 'distanceX' values being in meters



来源:https://stackoverflow.com/questions/13747500/calculating-distances-between-two-tables-full-of-geography-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!