gpt4 book ai didi

java - 使用地理数学查找半径内的所有值

转载 作者:行者123 更新时间:2023-12-01 18:15:28 29 4
gpt4 key购买 nike

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

这将返回指定地理位置 X 英里范围内的机构列表。

最佳答案

你已经有计算距离的方法了吗?只需循环遍历各个场所并检查它们是否在半径范围内即可。

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

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

关于java - 使用地理数学查找半径内的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29857340/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com