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

后端 未结 2 1936
-上瘾入骨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:23

    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.

提交回复
热议问题