使用GeoTools计算两点之间的大距离 [英] Calculate large distance between two points using GeoTools

查看:27
本文介绍了使用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);

推荐答案

如果您盲目地从StackExchangequestions复制代码,而没有阅读question,就会发生这种情况。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

这篇关于使用GeoTools计算两点之间的大距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆