简体   繁体   English

如何快速计算在给定距离内哪些点

[英]How to quickly calculate what points are within a given distance of my point

I have a large number of Longitude and Latitudes and I want to quickly find out which ones are within say a 5km radius of a certain Longitude Latitude. 我有大量的经度和纬度,我想快速找出哪些位于某个经度纬度的5公里半径内。

Instead of using a datastructure (Which would be overkill), I should be able to perform n do products very quickly. 而不是使用数据结构(这可能会过大),我应该能够非常快速地执行n种产品。 I've just done something wrong and can't seem to see what. 我做错了什么,似乎看不到什么。

I have been trying to implement this in Java: 我一直试图在Java中实现这一点:

        final List<CoOrds> coOrds = Create20x20Grid();

        // Determine point X (centre of earth)
        final Vector2 X = new Vector2(0,0);

        // My CoOrd I want to check
        final double srclon = coOrds.get(0).getLongitude();
        final double srclat = coOrds.get(0).getLatitude();

        final Vector2 A = new Vector2(srclon, srclat, true);

        final double brng = 0;
        final double d = 5;
        final double R = 6371.1;
        double dist = 0;

        dist = d / R; // convert dist to angular distance in radians

        final double lat1 = Math.toRadians(srclat);
        final double lon1 = Math.toRadians(srclon);

        final double lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist)+ Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
        double lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) * Math.cos(lat1),Math.cos(dist) - Math.sin(lat1) * Math.sin(lat2));

        // normalise to -180..+180º
        lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;


        //Create another point which is the distance is d away from your point
        final Vector2 B = new Vector2(Math.toDegrees(lon2),Math.toDegrees(lat2), true);

        // Create a vector from X->A
        final Vector2 X_A = new Vector2((A.getX() - X.getX()),(A.getY() - X.getY()));
        // Create a vector from X->B
        final Vector2 X_B = new Vector2((B.getX() - X.getX()),(B.getY() - X.getY()));

        // Normalize XA
        final Vector2 nX_A = X_A.normalize();
        // Normalize XB
        final Vector2 nX_B = X_B.normalize();

        // Calculate the Dot Product
        final Double Alpha = nX_A.dot(nX_B);

        int count = 0;
        for (final CoOrds c : coOrds) {

            final Vector2 P = c.getPosition();
            final Vector2 X_P = new Vector2((P.getX() - X.getX()),(P.getY() - X.getY()));

            final Vector2 nX_P = X_P.normalize());
            final Double Beta = nX_A.dot(nX_P);

            if (Beta < Alpha) {
                System.out.println(count + " -- " + Beta + " : " + Alpha);
                count++;
            }

        }


        System.out.println("Number of CoOrds within Distance : " + count);

The new point P is correct as I've loaded it into Google Maps, but I am not entirely sure if I have the calculations correct. 当我将新点P加载到Google Maps中时,它是正确的,但是我不能完全确定我的计算是否正确。

I have created a custom Vector2 class, which stores the Longitude and Latitude. 我创建了一个自定义Vector2类,该类存储经度和纬度。 It also coverts them to Cartesian: 它还将它们隐蔽为笛卡尔坐标:

    private void convertSphericalToCartesian(final double latitude, final double longitude) {

    x = (earthRadius * Math.cos(latitude) * Math.cos(longitude)) ;
    y = (earthRadius * Math.cos(latitude) * Math.sin(longitude)) ;
}

The Dot Product: 点产品:

    public double dot(final Vector2 v2) {

    return ((getX() * v2.getX()) + (getY() * v2.getY()));

}

The Normalize: 规范化:

    public Vector2 normalize() {

    final double num2 = (getX() * getX()) + (getY() * getY());
    final double num = 1d / Math.sqrt(num2);

    double a = x;
    double b = y;

    a *= num;
    b *= num;

    return new Vector2(a, b);
}

Any help with this would be really appreciated 任何帮助,将不胜感激

I used this website: http://www.movable-type.co.uk/scripts/latlong.html To help me calculate point B. 我使用了以下网站: http : //www.movable-type.co.uk/scripts/latlong.html为了帮助我计算B点。

I used this website : http://rbrundritt.wordpress.com/2008/10/14/conversion-between-spherical-and-cartesian-coordinates-systems/ To help me transform Spherical CoOrdinates to Cartesian CoOrdinates. 我使用了以下网站: http ://rbrundritt.wordpress.com/2008/10/14/conversion-between-spherical-and-cartesian-coordinates-systems/帮助我将球坐标系转换为笛卡尔坐标系。

Thanks 谢谢

[EDIT] [编辑]

The test case I am currently running is: 我当前正在运行的测试用例是:

0-0-0 0-0-0

2-2-0 2-2-0

1-2-0 1-2-0

Where the above is a grid of 9 points. 上面是9点的网格。 The point I am checking is "1". 我要检查的点是“ 1”。 I expect it to return all the points "2". 我希望它返回所有点“ 2”。 But it is returning all the points in the grid. 但是它正在返回网格中的所有点。 I have manually checked the distances on Google Maps and it should only be returning the points "2". 我已经手动检查了Google Maps上的距离,它应该只返回点“ 2”。

Thanks 谢谢

From the comments above, you're not sure if your formulae are accurate. 从上面的注释中,您不确定公式是否正确。

The first step is to figure that out. 第一步是弄清楚这一点。

Select a few well-known points on the globe (North Pole, South Pole, Greenwich UK, etc), and create a test case that invokes convertSphericaltoCartesian() with these coordinates. 选择地球上一些著名的点(北极,南极,英国格林威治等),然后创建一个测试用例,用这些坐标调用convertSphericaltoCartesian()。 Check the results and see if they make any sense. 检查结果,看它们是否有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM