【发布时间】:2018-09-22 17:12:20
【问题描述】:
我有两个地理点定义的矩形:
1 -> 54.2749558,18.4287748 (lat, lng)
2 -> 54.4472187,18.9512795 (lat, lng)
这两点之间的距离(对角线)为 39 公里(使用this 算法计算)。 现在我需要将我的对角线加倍:39km * 2 = 78km 并找到新扩展矩形的坐标(与第一个矩形的中间位置相同)。
有人可以帮我在 Java 中创建该算法吗?
编辑: 我的代码使用 Mbo 的 答案:
public static void main(String[] args) {
//example data
double lat1d = 54.2749558;
double lng1d = 18.4287748;
double lat2d = 54.4472187;
double lng2d = 18.9512795;
Coordinate lat1 = Coordinate.fromDegrees(lat1d);
Coordinate lng1 = Coordinate.fromDegrees(lng1d);
Point point1 = Point.at(lat1, lng1);
Coordinate lat2 = Coordinate.fromDegrees(lat2d);
Coordinate lng2 = Coordinate.fromDegrees(lng2d);
Point point2 = Point.at(lat2, lng2);
System.out.println("Point1: " + point1);
System.out.println("Point2: " + point2);
double distance = EarthCalc.gcdDistance(point1, point2); //in meters
System.out.println("Current distance between points 1 and 2: " + distance);
double newDistance = distance * 2;
System.out.println("Needed distance between points 3 and 4: " + newDistance);
double y = Math.sin(lng2d - lng1d) * Math.cos(lat2d);
double x = Math.cos(lat1d) * Math.sin(lat2d) - Math.sin(lat1d) * Math.cos(lat2d) * Math.cos(lng2d - lng1d);
double brng4 = Math.toDegrees(Math.atan2(y, x)); // bearing for calculating point 4
double earthRadiusInMeters = EarthCalc.EARTH_DIAMETER / 2;
double distanceByRadius4 = distance * 1.5 / (earthRadiusInMeters / 2);
double lat4d = Math.asin(Math.sin(lat1d) * Math.cos(distanceByRadius4) +
Math.cos(lat1d) * Math.sin(distanceByRadius4) * Math.cos(brng4));
double lng4d = lng1d + Math.atan2(Math.sin(brng4) * Math.sin(distanceByRadius4) * Math.cos(lat1d), Math.cos(distanceByRadius4) - Math.sin(lat1d) * Math.sin(lat2d));
Point point4 = Point.at(Coordinate.fromDegrees(lat4d), Coordinate.fromDegrees(lng4d));
double brng3 = brng4 + Math.PI; // bearing for calculating point 3
double distanceByRadius3 = distance * 0.5 / (earthRadiusInMeters / 2);
double lat3d = Math.asin(Math.sin(lat1d) * Math.cos(distanceByRadius3) +
Math.cos(lat1d) * Math.sin(distanceByRadius3) * Math.cos(brng3));
double lng3d = lng1d + Math.atan2(Math.sin(brng3) * Math.sin(distanceByRadius3) * Math.cos(lat1d),
Math.cos(distanceByRadius3) - Math.sin(lat1d) * Math.sin(lat2d));
Point point3 = Point.at(Coordinate.fromDegrees(lat3d), Coordinate.fromDegrees(lng3d));
System.out.println("Point3: " + point3);
System.out.println("Point4: " + point4);
double actualDistance = EarthCalc.gcdDistance(point3, point4); //in meters
System.out.println("Actual distance:" + actualDistance);
}
和控制台输出:
Point1: Point{latitude=54.2749558, longitude=18.4287748}
Point2: Point{latitude=54.4472187, longitude=18.9512795}
Current distance between points 1 and 2: 38896.62579783285
Needed distance between points 3 and 4: 77793.2515956657
Point3: Point{latitude=-0.8693568850955943, longitude=18.451667950625396}
Point4: Point{latitude=-0.8624187436224934, longitude=18.360085243458784}
Actual distance:10211.570252961072
【问题讨论】:
-
请注意,两点不定义矩形 iirc。但这并不重要,因为您基本上想延长两个点之间的距离,每个点都停留在连接原始点的线上,并且与另一个点的距离相同,对吧?您是否尝试过自己进行距离计算以了解几何的工作原理?
-
也许这个库有你需要的一切(在 EarthCalc 类中,使用“bearing”和“pointAt”github.com/grumlimited/geocalc
-
我没有考虑地球的曲率(面积太小,不重要),所以对我来说 2 个对角定义了一个矩形 :) 我试过了,但得到了一些奇怪的结果,所以决定请求帮忙。 @ThomasSallaberger 谢谢你,去试试那个库