【问题标题】:Finding all values within a radius using geographical math使用地理数学查找半径内的所有值
【发布时间】:2015-04-24 20:58:04
【问题描述】:

如何使用纬度/经度坐标找到半径 (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>

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

【问题讨论】:

    标签: java latitude-longitude


    【解决方案1】:

    你已经有了计算距离的方法?只需遍历设施并检查它们是否在半径范围内。

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

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

    【讨论】:

    • 我真的不敢相信我这么愚蠢。感谢您指出这一点。
    • 原来如此简单!我在猜测这个问题:-)
    • @DanielL。 -- 我用固定的位置进行测试,但在应用程序中位置总是在变化,我只是想了很多。
    • 发生了,不用担心。如果您对地理计算感兴趣,请查看下面的数据结构,它们非常酷。
    【解决方案2】:

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

    现在,如果您正在查看性能并且此方法无法说服您(例如,您有太多的场所无法对所有场所进行线性遍历),您可能需要先使用一些近似值并预先过滤一个合理的集合,然后选择这是否足够,或者您仍想计算该子集中的距离。为此,您可能需要阅读GeohashingQuad-TreesR-Trees 和/或KD-trees ...或它们的一些变体:-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 2012-05-04
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      相关资源
      最近更新 更多