ly/Common/CoordConvert.cs

58 lines
2.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using NetTopologySuite.Geometries;
namespace LY.App.Common
{
public static class CoordConvert
{
/// <summary>
/// 经纬度转Web墨卡托单位
/// </summary>
/// <param name="longitude">经度</param>
/// <param name="latitude">纬度</param>
/// <returns>转换后的位置</returns>
public static CoordPoint WGS84ToMercator(double lon, double lat)
{
var x = lon * Math.PI / 180 * Parameters.LongRadius;
var param = lat * Math.PI / 180;
var y = Parameters.LongRadius / 2 * Math.Log((1.0 + Math.Sin(param)) / (1.0 - Math.Sin(param)));
return new CoordPoint(x, y);
}
/// <summary>
/// 经纬度转Web墨卡托单位
/// </summary>
/// <param name="longitude">经度</param>
/// <param name="latitude">纬度</param>
/// <returns>转换后的位置</returns>
public static CoordPoint WGS84ToMercator(Coordinate coord)
{
return WGS84ToMercator(coord.Lon, coord.Lat);
}
/// <summary>
/// Web墨卡托转经纬度
/// </summary>
/// <param name="x">X坐标值单位</param>
/// <param name="y">Y坐标值单位</param>
/// <returns>转换后的位置</returns>
public static Coordinate MercatorToWGS84(double x, double y)
{
var lon = x / Parameters.MercatorLength * 180;
var lat = y / Parameters.MercatorLength * 180;
lat = 180 / Math.PI * (2 * Math.Atan(Math.Exp(lat * Math.PI / 180)) - Math.PI / 2);
return new Coordinate(lon, lat);
}
/// <summary>
/// Web墨卡托转经纬度
/// </summary>
/// <param name="x">X坐标值单位</param>
/// <param name="y">Y坐标值单位</param>
/// <returns>转换后的位置</returns>
public static Coordinate MercatorToWGS84(CoordPoint point)
{
return MercatorToWGS84(point.X, point.Y);
}
}
}