我有一条在地球上具有起点和终点坐标的线。
我正在尝试将垂直点放置在起点 length 距离的每一侧。
本来我以为可以
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);
}
我认为在经纬度上进行数学运算并考虑其范围是有问题的,这并不说明地球不平坦。
还有更好的方法吗?
答案 0 :(得分:1)
答案 1 :(得分:1)
您可能不应该尝试在球体上进行这种数学运算(虽然可以使它起作用,但又困难又缓慢)。
假设length
大约为10s-100s公里,则应将问题重新投影到以起点为中心的“平坦”表面,并在平面上使用欧几里得数学。
幸运的是,GeoTools仅针对此问题提供了方便的自动投影。这里的x
和y
是起点的坐标(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
因此要使其适应您的问题。
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
方法检查数学。