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

问题描述 投票:0回答:1

是GeoTools和GIS的新手,我正在尝试使用GeoTools库计算孟买和德班之间的距离。我正在接近较小距离的准确结果,但是当我寻找较大距离的结果时,距离2000 km的计算过于偏离,我不完全了解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);
gis geotools
1个回答
0
投票

这是当您从stackexchange questions盲目复制代码而没有阅读question的原因时会发生的情况。

我一直在回答这个问题(和posted code like that),发问者都在尝试使用经度/纬度坐标来测量以米为单位的短距离。您问题中显示的技巧将以“ AUTO:42001”位(在您的情况下为52N 13E)之后指定的位置为中心创建一个自动UTM投影-该位置必须是您感兴趣的区域的中心,因此在如果这些值可能仍然是错误的。

但是您对一个小区域不感兴趣,从孟买到德班是绕地球的重要途径,因此您需要考虑地球表面的曲率。同样,您也不会尝试将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
© www.soinside.com 2019 - 2024. All rights reserved.