NetTopologySuite返回的单位是哪种单位,如何将其转换为英里/公里?

时间:2019-03-04 20:40:23

标签: c# geometry nettopologysuite

每当我使用FreeMapTools计算自己和朋友邮政编码之间的距离时,都会得到以下信息:

  • 300.788英里
  • 484.072公里

Screenshot from FreeMapTools showing 300.788 Miles Screenshot from FreeMapTools showing 484.072 KM

使用NetTopologySuite时,返回的值为 5.2174236612815

  • 5.2174236612815 的60倍次数是 313.04541967689
  • 5.2174236612815 次乘以100是 521.74236612815

这些值与FreeMapTools上显示的距离不太远,但是距离仍然很远。

我的代码如下:

using System;
using GeoAPI.Geometries;
using NetTopologySuite;

namespace TestingDistances
{
    class Program
    {
        static void Main(string[] args)
        {
            var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);

            // BT2 8HB
            var myPostcode = geometryFactory.CreatePoint(new Coordinate(-5.926223, 54.592395));

            // DT11 0DB
            var myMatesPostcode = geometryFactory.CreatePoint(new Coordinate(-2.314507, 50.827157));

            var distance = myPostcode.Distance(myMatesPostcode);
            Console.WriteLine(distance); // returns 5.2174236612815

            Console.WriteLine(distance * 60); //similar to miles (313.04541967689)
            Console.WriteLine(distance * 100); //similar to km (521.74236612815)

            Console.ReadLine();
        }
    }
}

如何将NetTopologySuite返回的值准确地转换为英里/距离?这是我不知道的某种形式的GPS测距单元吗?

谢谢

2 个答案:

答案 0 :(得分:1)

数字似乎只是一个简单的笛卡尔坐标系。例如:

var point1 = geometryFactory.CreatePoint(new Coordinate(0, 0));
var point2 = geometryFactory.CreatePoint(new Coordinate(0, 270));

var distance = point1.Distance(point2);

此处distance为270。如果我们使用0, 030, 40,则距离为50。那只是毕达哥拉斯的简单计算(即30^2 + 40^2 = 50^2

答案 1 :(得分:1)

正如DavidG正确提及的那样,NetTopologySuite假定笛卡尔坐标。您的座标是地理(纬度/经度)。因此,您得到的结果是没有用的,无法转换为米或英里。

您必须在调用distance方法之前执行坐标转换,例如使用ProjNet:

var csWgs84 = ProjNet.CoordinateSystems.GeographicCoordinateSystems.WGS84;
const string epsg27700 = "..."; // see http://epsg.io/27700
var cs27700 = ProjNet.Converters.WellKnownText.CoordinateSystemWktReader.Parse(epsg27700);
var ctFactory = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
var ct = ctFactory.CreateFromCoordinateSystems(csWgs84, cs27700);
var mt = ct.MathTransform;

var gf = new NetTopologySuite.Geometries.GeometryFactory(27700);

// BT2 8HB
var myPostcode = gf.CreatePoint(mt.Transform(new Coordinate(-5.926223, 54.592395)));
// DT11 0DB
var myMatesPostcode = gf.CreatePoint(mt.Transform(new Coordinate(-2.314507, 50.827157)));

double distance = myPostcode.Distance(myMatesPostcode);