如何使用NetTopologySuite获取以米为单位的两点几何的距离?

时间:2019-07-01 05:54:55

标签: c# geometry nettopologysuite

如何使用NetTopologySuite获取以米为单位的两点几何的距离。

我使用过Distance()函数,但是得到了一些值,但无法确定这些值的单位。对于c#应用程序来说,忽略20米的公差值是可以忽略的。

using NetTopologySuite.Geometries;
using GeoAPI.Geometries;

private static double findistance()
{
   var geomFactory = new GeometryFactory(new PrecisionModel(), 4326);
   IGeometry geometry1 = geomFactory.CreatePoint(new Coordinate(12.977299, 77.571075));
   IGeometry geometry2 = geomFactory.CreatePoint(new Coordinate(12.977277, 77.571258));
   var distance = geometry1.Distance(geometry2);
   return distance;
}

我需要获取以米为单位的距离。

2 个答案:

答案 0 :(得分:0)

您可以使用毕达哥拉斯定理来计算距离。

以下是步骤:

  1. 获取增量
    • double int deltaX = Math.Abs​​(point1.X-point2.X);
    • double int deltaY = Math.Abs​​(point1.Y-point2.Y);
  2. 毕达哥拉斯公式
    • 双倍距离= Math.Sqrt((deltaX * deltaX)+(deltaY * deltaY));

如果要计算3D点,除计算DeltaZ外,其步骤相同,则公式为distance = X ^ 2 + Y ^ 2 + Z ^ 2

答案 1 :(得分:0)

对于每个docs,SRID 4326代表以下定义

GEOGCS["WGS 84",
  DATUM["WGS_1984",
    SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],
    AUTHORITY["EPSG","6326"]],
  PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],
  UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],
  AUTHORITY["EPSG","4326"]]

UNIT["degree"...告诉我们,您得到的结果具有度值。如果将这些点转换为投影坐标系,则可以以米为单位计算距离。 ProjNET4GeoAPI软件包可以为我们做到这一点。

private static double findDistance()
{
    CoordinateTransformationFactory ctfac = new CoordinateTransformationFactory();

    var from = GeographicCoordinateSystem.WGS84;
    var to = ProjectedCoordinateSystem.WebMercator;

    var trans = ctfac.CreateFromCoordinateSystems(from, to);
    var mathTransform = trans.MathTransform;

    var p1Coordinate = new GeoAPI.Geometries.Coordinate(12.977299, 77.571075);
    var p2Coordinate = new GeoAPI.Geometries.Coordinate(12.977277, 77.571258);

    p1Coordinate = mathTransform.Transform(p1Coordinate);
    p2Coordinate = mathTransform.Transform(p2Coordinate);

    return p1Coordinate.Distance(p2Coordinate);
}