垂直点放置

时间:2020-03-09 15:31:53

标签: java coordinates latitude-longitude geo geotools

我有一条在地球上具有起点和终点坐标的线。
我正在尝试将垂直点放置在起点 length 距离的每一侧。 enter image description here

本来我以为可以

  • 获取直线的斜率
  • 确定起点处垂直线的斜率
  • 求解x和y
Coordinate p1 = Ppoint(start, end, length); 
Coordinate p2 = Ppoint(start, end, -(length)); 

public static Coordinate Ppoint(Coordinate start, Coordinate end, double length){
   double slope = getSlope(start, end);
   double pSlope;  
   if(slope != 0)
   {
      pSlope = -(1/slope); 
   } 
   else 
   { 
      pSlope = 0; 
   }

   double b = start.y + (-(pSlope * start.x)); 

   double x = (start.x + length);
   double y = (pSlope * x) + b; 

   Return new Coordinate(x,y); 
}

我认为在经纬度上进行数学运算并考虑其范围是有问题的,这并不说明地球不平坦。
还有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

地球不平坦吗?

好的,有this个网站会比我更好地解释如何使用球体。您正在寻找的是:给定起点,距离和方位的目的地

您还可以将坐标系更改为平面坐标系,这并不可耻。 https://epsg.io/

答案 1 :(得分:1)

您可能不应该尝试在球体上进行这种数学运算(虽然可以使它起作用,但又困难又缓慢)。

假设length大约为10s-100s公里,则应将问题重新投影到以起点为中心的“平坦”表面,并在平面上使用欧几里得数学。

幸运的是,GeoTools仅针对此问题提供了方便的自动投影。这里的xy是起点的坐标(lon == x,lat == y):

String code = "AUTO:42001," + y + "," + x;
// System.out.println(code);
CoordinateReferenceSystem auto = CRS.decode(code);
// System.out.println(auto);
MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84,
    auto);
MathTransform rTransform = CRS.findMathTransform(auto, DefaultGeographicCRS.WGS84);

然后可以使用transform对象将点转换为新的投影:

Geometry g3 = JTS.transform(g1, transform);

进行所需的任何数学运算,然后使用rTransform

转换回lat

因此要使其适应您的问题。

Coordinate start = new Coordinate(1.0, 51.0);
Coordinate end = new Coordinate(2.0, 52.0);
double length = 10000;
GeometryFactory gf = new GeometryFactory();

double x = start.getX();
double y = start.getY();
String code;
if(CRS.getAxisOrder(DefaultGeographicCRS.WGS84).equals(AxisOrder.EAST_NORTH)) {
  code = "AUTO:42001," + x + "," + y;
} else {
  code = "AUTO:42001," + y + "," + x;
}
CoordinateReferenceSystem auto = CRS.decode(code);
MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, auto);
MathTransform rTransform = CRS.findMathTransform(auto, DefaultGeographicCRS.WGS84);

Point pStart = gf.createPoint(start);
Point pEnd = gf.createPoint(end);

Point ptStart = (Point) JTS.transform(pStart, transform);
Point ptEnd = (Point) JTS.transform(pEnd, transform);

Coordinate p1 = pPoint(ptStart.getCoordinate(), ptEnd.getCoordinate(), length);

Point tPoint = gf.createPoint(p1);
Point p = (Point) JTS.transform(tPoint, rTransform);
System.out.println(p);

这给了我POINT (1.2643 47.6531),这对我来说似乎是错的!您可能需要使用pPoint方法检查数学。