C#實現墨卡托投影坐標系經緯度與米制單位之間的互轉


using System;
using GeoJSON.Net.Geometry;
 
namespace GISWebService.Common
{
    /// <summary>
    /// 墨卡托坐標轉換(經緯度與米之間互轉)
    /// </summary>
    public class MercatorCoordinatesConverter
    {
        /// <summary>
        /// 經緯度轉Web墨卡托(單位:米)
        /// </summary>
        /// <param name="longitude">經度</param>
        /// <param name="latitude">緯度</param>
        /// <returns>轉換后的位置</returns>
        public static Position Degree2WebMercatorMeter(double longitude, double latitude)
        {
            var xValue = longitude * 20037508.34 / 180;
            var y = Math.Log(Math.Tan((90 + latitude) * Math.PI / 360)) / (Math.PI / 180);
            var yValue = y * 20037508.34 / 180;
            return new Position(xValue, yValue);
        }
 
        /// <summary>
        /// 經緯度轉World墨卡托(單位:米)
        /// </summary>
        /// <param name="longitude">經度</param>
        /// <param name="latitude">緯度</param>
        /// <returns>轉換后的位置</returns>
        public static Position Degree2WorldMercatorMeter(double longitude, double latitude)
        {
            const int radius = 6378137;
            const double minorRadius = 6356752.314245179;
 
            const double d = Math.PI / 180;
            const double r = radius;
            var y = latitude * d;
            const double tmp = minorRadius / r;
            double e = Math.Sqrt(1 - tmp * tmp),
                con = e * Math.Sin(y);
 
            var ts = Math.Tan(Math.PI / 4 - y / 2) / Math.Pow((1 - con) / (1 + con), e / 2);
            y = -r * Math.Log(Math.Max(ts, 1E-10));
 
            var xValue = longitude * d * r;
            var yValue = y;
 
            return new Position(xValue, yValue);
        }
 
        /// <summary>
        /// Web墨卡托轉經緯度
        /// </summary>
        /// <param name="x">X坐標值(單位:米)</param>
        /// <param name="y">Y坐標值(單位:米)</param>
        /// <returns>轉換后的位置</returns>
        public static Position WebMercatorMeter2Degree(double x, double y)
        {
            var xValue = x / 20037508.34 * 180;
            var yValue = y / 20037508.34 * 180;
            yValue = 180 / Math.PI * (2 * Math.Atan(Math.Exp(yValue * Math.PI / 180)) - Math.PI / 2);
            var longitude = xValue;
            var latitude = yValue;
            return new Position(longitude, latitude);
        }
    }
}

代碼原文連接 https://blog.csdn.net/a_dev/article/details/80990492


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM