C# 使用NetTopologySuite计算点之间的地理距离
我正在尝试使用NetTopologySuite计算两点之间的距离。由于我参考了Microsoft文档,因此我提出了以下GeometryExtension和GeometryHelper类:C# 使用NetTopologySuite计算点之间的地理距离,c#,.net,.net-core,gis,nettopologysuite,C#,.net,.net Core,Gis,Nettopologysuite,我正在尝试使用NetTopologySuite计算两点之间的距离。由于我参考了Microsoft文档,因此我提出了以下GeometryExtension和GeometryHelper类: public static class GeometryExtensions { private static readonly CoordinateSystemServices _coordinateSystemServices = new CoordinateSyst
public static class GeometryExtensions
{
private static readonly CoordinateSystemServices _coordinateSystemServices = new CoordinateSystemServices(new Dictionary<int, string>
{
// Coordinate systems:
[4326] = GeographicCoordinateSystem.WGS84.WKT,
// CRS for Denmark ESPG 25832. Source: https://epsg.io/25832 and https://sdfe.dk/
[25832] = @"PROJCS[""ETRS89 / UTM zone 32N"",
GEOGCS[""ETRS89"",
DATUM[""European_Terrestrial_Reference_System_1989"",
SPHEROID[""GRS 1980"",6378137,298.257222101,
AUTHORITY[""EPSG"",""7019""]],
TOWGS84[0,0,0,0,0,0,0],
AUTHORITY[""EPSG"",""6258""]],
PRIMEM[""Greenwich"",0,
AUTHORITY[""EPSG"",""8901""]],
UNIT[""degree"",0.0174532925199433,
AUTHORITY[""EPSG"",""9122""]],
AUTHORITY[""EPSG"",""4258""]],
PROJECTION[""Transverse_Mercator""],
PARAMETER[""latitude_of_origin"",0],
PARAMETER[""central_meridian"",9],
PARAMETER[""scale_factor"",0.9996],
PARAMETER[""false_easting"",500000],
PARAMETER[""false_northing"",0],
UNIT[""metre"",1,
AUTHORITY[""EPSG"",""9001""]],
AXIS[""Easting"",EAST],
AXIS[""Northing"",NORTH],
AUTHORITY[""EPSG"",""25832""]]"
}
);
/// <summary>
/// Projects a geometry to another SRID
/// </summary>
/// <param name="geometry"></param>
/// <param name="targetSrid"></param>
/// <param name="defaultSourceSrid">If the geometry SRID has not been specified (i.e. equals 0) defaultSourceSrid is used</param>
/// <returns></returns>
public static Geometry ProjectTo(this Geometry geometry, int targetSrid, int? defaultSourceSrid = null)
{
if (geometry == null)
throw new Exception("Geometry is null, cannot project");
var sourceSrid = geometry.SRID == 0 && defaultSourceSrid.HasValue ? defaultSourceSrid.Value : geometry.SRID;
var transformation = _coordinateSystemServices.CreateTransformation(sourceSrid, targetSrid);
var result = geometry.Copy();
result.Apply(new MathTransformFilter(transformation.MathTransform));
return result;
}
}
public class GeometryHelper
{
private static readonly int _longLatSRID = 4326;
private static readonly int _targetSRID = 25832;
/// <summary>
/// In order to get the distance in meters, we need to project to an appropriate
/// coordinate system. If no SRID is provided 25832, which covers Denmark is used.
/// If the provided Points have no SRID, 4326 (longitude/latitude) is assumed.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="targetSrid"></param>
/// <returns></returns>
public static double DistanceInMeters(Point a, Point b, int? targetSrid = null)
{
targetSrid ??= _targetSRID;
try
{
//If SRID is not set, assume long/lat, ie. 4326
return a.ProjectTo(targetSrid.Value, _longLatSRID).Distance(b.ProjectTo(targetSrid.Value, _longLatSRID));
}
catch (Exception e)
{
throw new Exception("Failed to calculate distance", e);
}
}
}
您需要计算
大圆距离
。
NetTopologySuitePoint.Distance
方法返回笛卡尔距离
请尝试以下操作:
public static double Radians(double x)
{
return x * Math.PI / 180;
}
public static double GreatCircleDistance(double lon1, double lat1, double lon2, double lat2)
{
double R = 6371e3; // m
double sLat1 = Math.Sin(Radians(lat1));
double sLat2 = Math.Sin(Radians(lat2));
double cLat1 = Math.Cos(Radians(lat1));
double cLat2 = Math.Cos(Radians(lat2));
double cLon = Math.Cos(Radians(lon1) - Radians(lon2));
double cosD = sLat1*sLat2 + cLat1*cLat2*cLon;
double d = Math.Acos(cosD);
double dist = R * d;
return dist;
}
您也可以使用内置的,它实现了小距离时更精确的公式。尽管saeedkazemi的答案是正确的,结果更接近真实值,但我的代码给出的结果也非常接近,但我发现我必须翻转Google maps给出的坐标。所以如果给我55.6765,12.5672,我需要给公式12.5672,55.6765。尽管如此,saeedkazemi的回答提供了更接近实际价值的结果
public static double Radians(double x)
{
return x * Math.PI / 180;
}
public static double GreatCircleDistance(double lon1, double lat1, double lon2, double lat2)
{
double R = 6371e3; // m
double sLat1 = Math.Sin(Radians(lat1));
double sLat2 = Math.Sin(Radians(lat2));
double cLat1 = Math.Cos(Radians(lat1));
double cLat2 = Math.Cos(Radians(lat2));
double cLon = Math.Cos(Radians(lon1) - Radians(lon2));
double cosD = sLat1*sLat2 + cLat1*cLat2*cLon;
double d = Math.Acos(cosD);
double dist = R * d;
return dist;
}