坐标系
1、WGS84
世界大地测量系统 1984,GPS使用的参考坐标系,也称地心坐标系
2、CGCS2000
2000国家大地坐标系,天地图使用,和WGS84坐标的误差很小
3、GCJ02
火星坐标系,高德使用、google 国内地图使用(PS:使用谷歌地图的时候注意坐标系统),和WGS84的误差一般在50-60米之间
4、bd09II
百度坐标系,在GCJ02坐标基础之上进行加密的坐标
C# 坐标转换代码:
public class GPSChange { private const double pi = 3.14159265358979324; private const double x_pi = 3.14159265358979324 * 3000.0 / 180.0; //克拉索天斯基椭球体参数值 private const double a = 6378245.0; //第一偏心率 private const double ee = 0.00669342162296594323; /// <summary> /// GCJ-02转换BD-09 /// </summary> /// <param name="gg_lat">纬度</param> /// <param name="gg_lon">经度</param> /// <returns></returns> public static GPSPoint GCJ02_to_BD09(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.SetLat(bd_lat); point.SetLng(bd_lon); return point; } /// <summary> /// BD-09转换GCJ-02 /// </summary> /// <param name="bd_lat">纬度</param> /// <param name="bd_lon">经度</param> /// <returns></returns> public static GPSPoint BD09_to_GCJ02(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.SetLat(gg_lat); point.SetLng(gg_lon); return point; } /// <summary> /// WGS-84转换GCJ-02 /// </summary> /// <param name="wgLat">纬度</param> /// <param name="wgLon">经度</param> /// <returns></returns> public static GPSPoint WGS84_to_GCJ02(double wgLat, double wgLon) { GPSPoint point = new GPSPoint(); if (OutOfChina(wgLat, wgLon)) { point.SetLat(wgLat); point.SetLng(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.SetLat(lat); point.SetLng(lon); return point; } /// <summary> /// GCJ02_to_WGS84 /// </summary> /// <param name="wgLat">纬度</param> /// <param name="wgLon">经度</param> /// <returns></returns> public static GPSPoint GCJ02_to_WGS84(double wgLat, double wgLon) { GPSPoint gps = Transform(wgLat, wgLon); double lontitude = wgLon * 2 - gps.lng; double latitude = wgLat * 2 - gps.lat; return new GPSPoint(lontitude, latitude); } public static GPSPoint Transform(double wgLat, double wgLon) { GPSPoint gpspoint =new GPSPoint(wgLat, wgLon); if (OutOfChina(wgLat, wgLon)) { gpspoint.lat = wgLat; gpspoint.lng = wgLon; return gpspoint; } 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); gpspoint.lat = wgLat + dLat; gpspoint.lng = wgLon + dLon; return gpspoint; } private static bool OutOfChina(double lat, double lon) { if (lon < 72.004 || lon > 137.8347) return true; if (lat < 0.8293 || lat > 55.8271) return true; return 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; } /// <summary> /// 经纬度转墨卡托投影坐标 /// </summary> /// <returns></returns> public static XYPoint lonlatTomercator(double lng, double lat) { XYPoint xypoint = new DataSource.XYPoint(); double x = lng * 20037508.34 / 180; double y = Math.Log(Math.Tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180); y = y * 20037508.34 / 180; xypoint.x = x; xypoint.y = y; return xypoint; } /// <summary> /// 墨卡托投影坐标转经纬度坐标 /// </summary> /// <returns></returns> public static GPSPoint mercatorTolonlat(double x,double y) { GPSPoint gpspoint = new DataSource.GPSPoint(); double lng = x / 20037508.34 * 180; double lat = y / 20037508.34 * 180; y= 180/Math.PI*(2*Math.Atan(Math.Exp(y* Math.PI/180))-Math.PI/2); gpspoint.lng = lng; gpspoint.lat = lat; return gpspoint; } } public class XYPoint { public double x { get; set; }// 纬度 public double y { get; set; }// 经度 } public class GPSPoint { public double lat;// 纬度 public double lng;// 经度 public GPSPoint() { } public GPSPoint(double lng, double lat) { this.lng = lng; this.lat = lat; } public double GetLat() { return lat; } public void SetLat(double lat) { this.lat = lat; } public double GetLng() { return lng; } public void SetLng(double lng) { this.lng = lng; } }
posted on 2020-12-07 16:46 Geography爱好者 阅读(850) 评论(0) 编辑 收藏 举报