使用 GeoTools 计算两点之间的大距离

Calculate large distance between two points using GeoTools

GeoTools 和 GIS 的新手,我正在尝试使用 GeoTools 库计算孟买和德班之间的距离。我越来越接近小距离的准确结果,但是当我选择更大的距离时,计算距离 2000 公里太远了,我不完全理解 CRS 系统。下面是我计算孟买和德班之间距离的代码

    Coordinate source = new Coordinate(19.0760, 72.8777);   ///Mumbai Lat Long
    Coordinate destination1 = new Coordinate(-29.883333, 31.049999); //Durban Lat Long

    GeometryFactory geometryFactory = new GeometryFactory();
    Geometry point1 = geometryFactory.createPoint(source);
    Geometry point2 = geometryFactory.createPoint(destination1);

    CoordinateReferenceSystem auto = auto = CRS.decode("AUTO:42001,13.45,52.3");
    MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, auto);

    Geometry g3 = JTS.transform(point1, transform);
    Geometry g4 = JTS.transform(point2, transform);

    double distance = g3.distance(g4);

当你从 stackexchange 中盲目复制代码时会发生这种情况 questions without reading the question 它基于它解释了原因。

我一直在回答这个问题(和 posted code like that),提问者试图使用 lat/lon 度数坐标来测量以米为单位的短距离。您问题中显示的技巧会创建一个自动 UTM 投影,该投影以 "AUTO:42001," 位之后指定的位置为中心(在您的情况下为 52N 13E)-这需要是您感兴趣的区域的中心,因此在您的情况下无论如何,这些值可能是错误的。

但是您对从孟买到德班的小区域不感兴趣,这是绕地球一周的重要途径,因此您需要考虑到地球表面的曲率。此外,您并没有尝试做一些困难的事情,因为 JTS 是唯一的进程来源(例如缓冲)。在这种情况下,您应该使用 GeodeticCalculator,它使用 C. F. F. Karney 的库将地球的形状考虑在内,测地线算法,J. Geodesy 87, 43–55 (2013)。

无论如何,足够的解释,以后没有人会读,这里是代码:

  public static void main(String[] args) {
    DefaultGeographicCRS crs = DefaultGeographicCRS.WGS84;
    if (args.length != 4) {
      System.err.println("Need 4 numbers lat_1 lon_1 lat_2 lon_2");
      return;
    }
    GeometryFactory geomFactory = new GeometryFactory();
    Point[] points = new Point[2];
    for (int i = 0, k = 0; i < 2; i++, k += 2) {
      double x = Double.valueOf(args[k]);
      double y = Double.valueOf(args[k + 1]);
      if (CRS.getAxisOrder(crs).equals(AxisOrder.NORTH_EAST)) {
        System.out.println("working with a lat/lon crs");
        points[i] = geomFactory.createPoint(new Coordinate(x, y));
      } else {
        System.out.println("working with a lon/lat crs");
        points[i] = geomFactory.createPoint(new Coordinate(y, x));
      }

    }

    double distance = 0.0;

    GeodeticCalculator calc = new GeodeticCalculator(crs);
    calc.setStartingGeographicPoint(points[0].getX(), points[0].getY());
    calc.setDestinationGeographicPoint(points[1].getX(), points[1].getY());

    distance = calc.getOrthodromicDistance();
    double bearing = calc.getAzimuth();

    Quantity<Length> dist = Quantities.getQuantity(distance, SI.METRE);
    System.out.println(dist.to(MetricPrefix.KILO(SI.METRE)).getValue() + " Km");
    System.out.println(dist.to(USCustomary.MILE).getValue() + " miles");
    System.out.println("Bearing " + bearing + " degrees");
  }

给予:

working with a lon/lat crs
POINT (72.8777 19.076)
POINT (31.049999 -29.883333)
7032.866960793305 Km
4370.020928274692 miles
Bearing -139.53428618565218 degrees