使用地理数学查找半径内的所有值

Finding all values within a radius using geographical math

如何使用 Latitude/Longitude 坐标找到 (X) 英里半径范围内的所有值(在本例中为机构)?

代码:

public class GeoGen {

    static final GeoPosition USER_POSITION = new GeoPosition(39.410868, -107.102182);

    public static void main(String[] args) {
        new Establishment("Fries Electronics", randomLocation(USER_POSITION, 40)).print();
        new Establishment("Walmart Supercenter", randomLocation(USER_POSITION, 40)).print();
        new Establishment("Target", randomLocation(USER_POSITION, 40)).print();
        new Establishment("Krogers", randomLocation(USER_POSITION, 40)).print();
        new Establishment("McDonalds", randomLocation(USER_POSITION, 40)).print();
    }

    public static GeoPosition randomLocation(GeoPosition location, double radius) {
        Random random = new Random();

        // Convert radius from miles to meters
        double meters = radius * 1609.34;

        // Convert radius from meters to degrees
        double radiusInDegrees = meters / 111000f;

        double u = random.nextDouble();
        double v = random.nextDouble();
        double w = radiusInDegrees * Math.sqrt(u);
        double t = 2 * Math.PI * v;
        double x = w * Math.cos(t);
        double y = w * Math.sin(t);

        // Adjust the x-coordinate for the shrinking of the east-west distances
        double new_x = x / Math.cos(location.latitude());

        double foundLongitude = new_x + location.longitude();
        double foundLatitude = y + location.latitude();
        return new GeoPosition(foundLongitude, foundLatitude);
    }

    public static double distanceBetween(GeoPosition a, GeoPosition b) {
        double longDif = a.longitude() - b.longitude();

        double distance = 
                Math.sin(deg2rad(a.latitude()))
                *
                Math.sin(deg2rad(b.latitude()))
                +
                Math.cos(deg2rad(a.latitude()))
                *
                Math.cos(deg2rad(b.latitude()))
                *
                Math.cos(deg2rad(longDif));
        distance = Math.acos(distance);
        distance = rad2deg(distance);
        distance = distance * 60 * 1.1515; // Convert to meters
        distance = distance * 0.8684; // Convert to miles.
        return distance;
    }

    private static double rad2deg(double rad) {
        return (rad * 180.0 / Math.PI);
    }

    private static double deg2rad(double deg) {
        return (deg * Math.PI / 180.0);
    }

}

/**
 * A class representing an establishment in the world.
 * 
 * @author Christian
 */
class Establishment {

    public static Map<GeoPosition, String> establishments = new HashMap<>();

    private final String name;
    private final GeoPosition geoPosition;

    public Establishment(String name, GeoPosition geoPosition) {
        this.name = name;
        this.geoPosition = geoPosition;
        establishments.put(geoPosition, name);
    }

    public void print() {
        System.out.print("Establishment("+name+") was created approx ");
        System.out.printf("%.2f", GeoGen.distanceBetween(geoPosition, GeoGen.USER_POSITION));
        System.out.print(" miles from specified Lat/Long \n");
    }

    public final String name() { return name; }
    public final GeoPosition position() { return geoPosition; }
}

/**
 * A class representing a geographical location using latitude/longitude.
 * 
 * @author Christian
 */
class GeoPosition {
    private final double longitude;
    private final double latitude;

    public GeoPosition(double longitude, double latitude) {
        this.longitude = longitude;
        this.latitude = latitude;
    }

    public double longitude() { return longitude; }
    public double latitude() { return latitude; }
}

运行 此应用程序每次都会产生不同的结果,因为它是随机的,但是这里是使用 GeoGen#distanceBetween(GeoPosition, GeoPosition) 方法的示例。

Establishment(Fries Electronics) was created approx 19.26 miles from specified Lat/Long 
Establishment(Walmart Supercenter) was created approx 9.79 miles from specified Lat/Long 
Establishment(Target) was created approx 28.83 miles from specified Lat/Long 
Establishment(Krogers) was created approx 10.61 miles from specified Lat/Long 
Establishment(McDonalds) was created approx 3.37 miles from specified Lat/Long 

但是,我想弄清楚的是如何让所有机构都在 X 英里以内。例如像这样的

GeoGen#FindEstablishmentsWithinRadius(GeoPosition, Double) returns List<Establishment>

这将 return 指定 GeoPosition X 英里范围内的机构列表。

你已经有计算距离的方法了吗?只需遍历这些场所并检查它们是否在半径内。

将所有机构添加到列表中,然后您可以使用流,例如:

public List<Establishment> findEstablishmentsWithinRadius(List<Establishment> list, GeoPosition position, double radius) {
    return list.stream().filter(e -> distanceBetween(position, e.position()) <= radius).collect(Collectors.toList());
}

嗯,对我来说似乎很微不足道。假设您有一份您创建的所有机构的列表,并且您已经有一个距离函数。剩下的只是一个函数,它在给定中心和半径的情况下遍历所有机构,计算每个机构与中心之间的距离,并收集落在半径以下的机构作为结果。完毕。我想这回答了你的问题。

现在,如果性能是您所关注的,而此方法不能说服您(例如,您的机构太多,无法 运行 对所有机构进行线性传递),您可能想要使用首先进行一些近似并预过滤一个合理的集合,然后选择这是否足够或者您仍然想计算该子集中的距离。为此,您可能需要阅读 Geohashing, Quad-Trees, R-Trees and/or KD-trees ... 或它们的一些变体 :-)