java - 查找以公里为半径的地理坐标

标签 java math

我有一个包含大约 300.000 个 vector 的数据集,使用纬度和经度随机放置在地球周围。 假设我位于北纬 51.9167°,东经 4.5000°,我如何找到我周围半径为 100 公里的所有 vector ? 简单的数学是首选。 Java 和伪代码也可以。

最佳答案

假设您有一个 Location带有纬度/经度和 Collection<Location> 的类你想处理,你可以这样做:

Collection<Location> locations; // filled somewhere
final Location here;

List<Location> within100km = locations.stream()
    .filter(l -> haversine(l.getLatitude(), l.getLongitude(),
      here.getLatitude(), here.getLongitude()) <= 100)
    .collect(Collectors.toList());

public static double haversine(
        double lat1, double lng1, double lat2, double lng2) {
    int r = 6371; // average radius of the earth in km
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lng2 - lng1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
       Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) 
      * Math.sin(dLon / 2) * Math.sin(dLon / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double d = r * c;
    return d;
}

关于java - 查找以公里为半径的地理坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32881932/

相关文章:

java - JPanel 背景图像不适用于其中的 JPanel

java - GCS断点续传速度

java - Java中的getView和Beacon(Estimote)问题

java - Apache Camel - 如何在 java 中使用属性配置端点

swift - Swift 中负数的算术运算符

math - 对于 Lisp 初学者来说,有什么好的数学书籍?

java - 在 Saucelabs 中使用 Selenium 远程 Firefox Webdriver 安装扩展时出现问题

algorithm - 如何转换两组离散点(向量)以帮助以共同的比例绘制它们

php - 将一个数字分成随机不相等的部分

c# - 将 Excel 列字母转换为数字的算法是什么?