Algorithm for finding nearby points?

前端 未结 7 909
独厮守ぢ
独厮守ぢ 2021-01-30 18:19

Given a set of several million points with x,y coordinates, what is the algorithm of choice for quickly finding the top 1000 nearest points from a location? \"Quickly\" here mea

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-30 18:37

    i know its been said as not being the fastest if you want REALLY REALLY fast results by seeing i found this post from google i thought i'd add my SQL solution that i used a while ago in the form of a stored proc. It looks for locations close by the a coord and returns them by distance.

    I hope it helps someone :)

    CREATE PROCEDURE [dbo].[getstores] @lat float,  @lng float AS
    DECLARE @radius float, @DegToRad float
    SET @DegToRad = 57.29577951
    SET @radius = 25000
    SELECT TOP 10
        name
        ,sto_lat
        ,sto_lng
        ,postcode
        ,ROUND((ACOS((SIN(@lat/57.2958) * SIN(sto_lat/@DegToRad)) +(COS(@lat/@DegToRad) * COS(sto_lat/@DegToRad) *COS(sto_lng/@DegToRad - @lng/@DegToRad))))* 6387.7, 2) AS distance
    FROM store
    WHERE (sto_lat >= @lat - (@radius/111))
    And (sto_lat <= @lat + (@radius/111))
    AND (sto_lng >= @lng - (@radius/111))
    AND (sto_lng <= @lng + (@radius/111))
    AND (
         ISNUMERIC(sto_lat) = 1
        AND
        ISNUMERIC(sto_lat) = 1
    )
    ORDER BY distance
    

    NOTE: i have already stated that this is not the best solution for this question simply maybe for someone who found this on google like me

提交回复
热议问题