1.前言
剛剛想從百度坐标拾取工具裡面找到一些地點的經緯度,存儲到系統中使用,由于百度拾取系統給到的是百度(BD-09)坐标系統,系統統一用到的是WGS-84,是以需要進行一次轉換,本來想從網上下載下傳一個,結果花了僅剩不多的49積分,下載下傳到了一個用起來很不友善,而且還報錯的東西下來,真TM好氣!是以無奈自己編寫一個提供給大家使用!
2.實作
2.1.語言及架構說明
我這邊是基于C#語言開發的winform桌面應用程式,使用的是.NET Framework 4.6.1
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiNx8FesU2cfdGLwczX0xiRGZkRGZ0Xy9GbvNGLwIzXlpXazxSP9Ekd412T1N1VtglUycTNVdUS2AXb0UTQClGVF5UMR9Fd4VGdsATNfd3bkFGazxycykFaKdkYzZUbapXNXlleSdVY2pESa9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLzUTN4EDM1QTM2ITOwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2.2.經緯度坐标系統轉換類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CoordinateConversionForm
{
public class CoordinateUtil
{
//WGS-84坐标系:全球定位系統使用,GPS、北鬥等
//GCJ-02坐标系:中國地區使用,由WGS-84偏移而來
//BD-09坐标系:百度專用,由GCJ-02偏移而來
private static readonly double pi = 3.14159265358979324;
private static readonly double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
//克拉索天斯基橢球體參數值
private static readonly double a = 6378245.0;
//第一偏心率
private static readonly double ee = 0.00669342162296594323;
/// <summary>
/// BD-09轉換GCJ-02
/// </summary>
/// <param name="bd_lat">緯度</param>
/// <param name="bd_lon">經度</param>
/// <returns></returns>
public static GPSPoint BD09ToGCJ02(double bd_lat, double bd_lon)
{
GPSPoint point = new GPSPoint();
double x = bd_lon - 0.0065, y = bd_lat - 0.006;
double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi);
double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi);
double gg_lon = z * Math.Cos(theta);
double gg_lat = z * Math.Sin(theta);
point.lat = gg_lat;
point.lon = gg_lon;
return point;
}
/// <summary>
/// GCJ-02轉WGS84
/// </summary>
/// <param name="gcj_lat"></param>
/// <param name="gcj_lon"></param>
/// <returns></returns>
public static GPSPoint GCJ02ToWGS84(double gcj_lat,double gcj_lon)
{
GPSPoint point = new GPSPoint();
if (OutOfChina(gcj_lat, gcj_lon))
{
point.lon = gcj_lon;
point.lat = gcj_lat;
}
else
{
double dlat = TransformLat(gcj_lon - 105.0, gcj_lat - 35.0);
double dlon = TransformLon(gcj_lon - 105.0, gcj_lat - 35.0);
double radlat = gcj_lat / 180.0 * pi;
double magic = Math.Sin(radlat);
magic = 1 - ee * magic * magic;
double sqrtmagic = Math.Sqrt(magic);
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
dlon = (dlon * 180.0) / (a / sqrtmagic * Math.Cos(radlat) * pi);
double mglat = gcj_lat + dlat;
double mglon = gcj_lon + dlon;
point.lon = gcj_lon * 2 - mglon;
point.lat = gcj_lat * 2 - mglat;
}
return point;
}
/// <summary>
/// BD09轉WGS84
/// </summary>
/// <param name="bd_lat"></param>
/// <param name="bd_lon"></param>
/// <returns></returns>
public static GPSPoint BD09ToWGS84(double bd_lat, double bd_lon)
{
GPSPoint point = BD09ToGCJ02(bd_lat, bd_lon);
return GCJ02ToWGS84(point.lat, point.lon);
}
/// <summary>
/// GS-84轉換BD09
/// </summary>
/// <param name="wgLat"></param>
/// <param name="wgLon"></param>
/// <returns></returns>
public static GPSPoint WGS84ToBD09(double wgLat, double wgLon)
{
GPSPoint point = WGS84ToGCJ02(wgLat, wgLon);
return GCJ02ToBD09(point.lat, point.lon);
}
/// <summary>
/// WGS-84轉換GCJ-02
/// </summary>
/// <param name="wgLat">緯度</param>
/// <param name="wgLon">經度</param>
/// <returns></returns>
public static GPSPoint WGS84ToGCJ02(double wgLat, double wgLon)
{
GPSPoint point = new GPSPoint();
if (OutOfChina(wgLat, wgLon))
{
point.lat = wgLat;
point.lon = wgLon;
return point;
}
double dLat = TransformLat(wgLon - 105.0, wgLat - 35.0);
double dLon = TransformLon(wgLon - 105.0, wgLat - 35.0);
double radLat = wgLat / 180.0 * pi;
double magic = Math.Sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.Sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
double lat = wgLat + dLat;
double lon = wgLon + dLon;
point.lat=lat;
point.lon=lon;
return point;
}
/// <summary>
/// GCJ-02轉換BD-09
/// </summary>
/// <param name="gg_lat">緯度</param>
/// <param name="gg_lon">經度</param>
/// <returns></returns>
public static GPSPoint GCJ02ToBD09(double gg_lat, double gg_lon)
{
GPSPoint point = new GPSPoint();
double x = gg_lon, y = gg_lat;
double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);
double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);
double bd_lon = z * Math.Cos(theta) + 0.0065;
double bd_lat = z * Math.Sin(theta) + 0.006;
point.lat = bd_lat;
point.lon = bd_lon;
return point;
}
/// <summary>
/// 經緯度點是否不再國内(這個方法精确度太差,可以使用GIS算法重寫,我這邊主要目的是為了做國内經緯度糾偏,國外的不需要糾偏)
/// </summary>
/// <param name="lat"></param>
/// <param name="lon"></param>
/// <returns></returns>
private static bool OutOfChina(double lat, double lon)
{
return (lon < 72.004 || lon > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
}
private static double TransformLat(double x, double y)
{
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x));
ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
private static double TransformLon(double x, double y)
{
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));
ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
}
}
2.3.界面設計
幾個RadioButton,一個Button,兩個RichTextBox
界面代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CoordinateConversionForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_change_Click(object sender, EventArgs e)
{
rtb_conversion.Clear();
foreach (var item in rtb_previous.Lines)
{
string[] lonlatArr = item.Split(',');
GPSPoint point = new GPSPoint();
if (rb_bd09Togcj02.Checked)
{
point = CoordinateUtil.BD09ToGCJ02(double.Parse(lonlatArr[1]), double.Parse(lonlatArr[0]));
}
else if (rb_bd09Towgs84.Checked)
{
point = CoordinateUtil.BD09ToWGS84(double.Parse(lonlatArr[1]), double.Parse(lonlatArr[0]));
}
else if (rb_wgs84Tobd09.Checked)
{
point = CoordinateUtil.WGS84ToBD09(double.Parse(lonlatArr[1]), double.Parse(lonlatArr[0]));
}
else if (rb_gcj02Tobd09.Checked)
{
point = CoordinateUtil.GCJ02ToBD09(double.Parse(lonlatArr[1]), double.Parse(lonlatArr[0]));
}
else if (rb_wgs84Togcj02.Checked)
{
point = CoordinateUtil.WGS84ToGCJ02(double.Parse(lonlatArr[1]), double.Parse(lonlatArr[0]));
}
else if (rb_gcj02Towgs84.Checked)
{
point = CoordinateUtil.GCJ02ToWGS84(double.Parse(lonlatArr[1]), double.Parse(lonlatArr[0]));
}
rtb_conversion.Text += point.lon + "," + point.lat + System.Environment.NewLine;
}
}
}
}
另外還有一個經緯度點的封裝實體類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CoordinateConversionForm
{
public class GPSPoint
{
public double lat;
public double lon;
}
}
3.實作效果
4.源碼資源
下載下傳位址