我有一个大约 300.000 个向量的数据集,使用纬度和经度随机放置在地球周围。\n假设我位于 51.9167\xc2\xb0 N, 4.5000\xc2\xb0 E,我如何在一个向量中找到我周围的所有向量例如,半径是 100 公里?\n最好是简单的数学。Java 和伪代码也很好。
\n假设你有一个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;
}
Run Code Online (Sandbox Code Playgroud)