Finding a geography point within a range of another - SQL Server

后端 未结 2 1928
-上瘾入骨i
-上瘾入骨i 2020-12-30 16:43

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 poi

2条回答
  •  执笔经年
    2020-12-30 17:33

    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.

提交回复
热议问题