我该怎么办有效范围搜索+同纬度/经度数据计算?经度、纬度、我该、范围

2023-09-11 00:29:25 作者:一次硬半年

我正在与一大组重新由纬度/经度对psented $ P $点(这些点不一定是唯一的,有可能是几个点在该组是在相同的位置)。该点被存储在数据库中。

I'm working with a large set of points represented by latitude/longitude pairs (the points are not necessarily unique, there could be several points in the set that are at the same location). The points are stored in a database.

我需要做的是找出一种方法来有效地执行搜索来获得骗任意点的给定半径(例如,25英里)内的点数。 计并不需要100%的准确 - 更重要的是,它只是要快,并且相当接近正确的计数。与SQL这样做是可能的,通过使用查询与WHERE子句中一些三角通过它们的参考点的距离来过滤分。不幸的是,这个查询是非常,非常昂贵,缓存可能不会提供太多的帮助,因为该位置将是Very S修$ P $垫了。

What I need to do is figure out a way to efficiently perform a search to get the number of points that lie within a given radius (say, 25 miles) of an arbitrary point. The count does not need to be 100% accurate - more importantly, it just has to be fast, and reasonably close to the correct count. Doing this with SQL is possible, by using a query with some trigonometry in the WHERE clause to filter points by their distance to a reference point. Unfortunately, this query is very, very expensive and caching is not likely to provide much help because the locations will be very spread out.

我最终寻求建立某种形式的内存结构,将能够有效地处理这类操作 - 交易掉一些数据(也许重建它只是每天一次)中的准确度和活跃度的回报速度。我一直在做的KD树一些研究,但我还不清楚如何好这一可以应用到纬度/经度数据(如在一个二维平面相对于X,Y数据)。

I'm ultimately looking to build some kind of in-memory structure that will be able to handle this kind of operation efficiently - trading off some of the accuracy and liveness of the data (maybe rebuilding it only once a day) in return for speed. I've been doing some research on kd-trees, but i'm not clear yet on how well this can be applied to latitude/longitude data (as opposed to x,y data in a 2d plane).

如果任何人有任何意见或解决方案我应该看看,我真的AP preciate它 - 所以在此先感谢

If anybody has any ideas or solutions I should look into, I'd really appreciate it - so thanks in advance.

推荐答案

我不认为你应该使用这个解决方案。前几天经随机想过这个问题,我认为,测量距离的特定点的方格的位置将基于圈子,而不是一个穿制服的网格。进一步远离0,0你是不太准确,这将是!的

I don't think you should use this solution. Having randomly thought about it a few days ago, I think that measuring the distance from a specific point the grid squares' locations will be based on circles rather than a uniformed grid. The further away from 0,0 you are the less accurate this will be!

我所做的就是在我的邮政code类2个额外的价值。每当我更新龙/纬度上的邮政code我算一个X,从长0是距离,纬度0。

What I did was to have 2 additional values on my PostalCode class. Whenever I update the Long/Lat on a PostalCode I calculate an X,Y distance from Long 0, Lat 0.

public static class MathExtender
{
    public static double GetDistanceBetweenPoints(double sourceLatitude, double sourceLongitude, double destLatitude, double destLongitude)
    {
        double theta = sourceLongitude - destLongitude;
        double distance =
            Math.Sin(DegToRad(sourceLatitude))
            * Math.Sin(DegToRad(destLatitude))
            + Math.Cos(DegToRad(sourceLatitude))
            * Math.Cos(DegToRad(destLatitude))
            * Math.Cos(DegToRad(theta));
        distance = Math.Acos(distance);
        distance = RadToDeg(distance);
        distance = distance * 60 * 1.1515;
        return (distance);
    }


    public static double DegToRad(double degrees)
    {
        return (degrees * Math.PI / 180.0);
    }

    public static double RadToDeg(double radians)
    {
        return (radians / Math.PI * 180.0);
    }
}

然后更新我的类像这样:

Then I update my class like so:

private void CalculateGridReference()
{
    GridReferenceX = MathExtender.GetDistanceBetweenPoints(0, 0, 0, Longitude);
    GridReferenceY = MathExtender.GetDistanceBetweenPoints(0, 0, Latitude, 0);
}

所以,现在我已经从栅格参考0,0在我的数据库每行一个x,y网格距离(英里)。如果我想找到5英里长/纬度我首先得到了X,Y座标的所有地方(比如25,75),那么我会寻找20..30,70..80在DB,并进一步使用过滤结果在内存

So now I have an x,y grid distance (in miles) from grid reference 0,0 for each row in my DB. If I want to find all places with 5 miles of a long/lat I would first get the X,Y grid reference (say 25,75) then I would search 20..30, 70..80 in the DB, and further filter the results in memory using

MathExtensder.GetDistanceBetweenPoints(candidate.Lat, candidate.Long, search.Lat, search.Long) < TheRadiusOfInterest

在数据库中的一部分是超快,并在内存中的一部分工作在一个更小的组,使之超准确。

The in DB part is ultra fast, and the in-memory part works on a smaller set to make it ultra accurate.