我有一个包含大约300,000个向量的数据集,这些向量是根据纬度和经度随机分布在地球周围的。假设我现在位于北纬51.9167°,东经4.5000°附近,如何找到距离我100公里内的所有向量呢?最好使用简单的数学方法,如果需要可以使用Java或伪代码。
我有一个包含大约300,000个向量的数据集,这些向量是根据纬度和经度随机分布在地球周围的。假设我现在位于北纬51.9167°,东经4.5000°附近,如何找到距离我100公里内的所有向量呢?最好使用简单的数学方法,如果需要可以使用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;
}
cosφ·cosλ
v{x,y,z} = cosφ·sinλ
sinφ
请参考此页面