我有一个包含大约 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/