首先申明:转换方法都是网上找的,但是都不全,整理了一下
其中wgs84、cgcs2000互转结果有差异,也就是完全还原不了
先附上百度、高德、wgs84互转方法
js
/**
* Created by Wandergis on 2015/7/8.
* 提供了百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
*/
//UMD魔法代码
// if the module has no dependencies, the above pattern can be simplified to
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.coordtransform = factory();
}
}(this, function() {
//定义一些常量
var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
var PI = 3.1415926535897932384626;
var a = 6378245.0;
var ee = 0.00669342162296594323;
var MCBAND = [12890594.86, 8362377.87, 5591021, 3481989.83, 1678043.12, 0];
var LLBAND = [75, 60, 45, 30, 15, 0];
var MC2LL = [
[1.410526172116255e-8, 0.00000898305509648872, -1.9939833816331, 200.9824383106796, -187.2403703815547, 91.6087516669843, -23.38765649603339, 2.57121317296198, -0.03801003308653, 17337981.2],
[-7.435856389565537e-9, 0.000008983055097726239, -0.78625201886289, 96.32687599759846, -1.85204757529826, -59.36935905485877, 47.40033549296737, -16.50741931063887, 2.28786674699375, 10260144.86],
[-3.030883460898826e-8, 0.00000898305509983578, 0.30071316287616, 59.74293618442277, 7.357984074871, -25.38371002664745, 13.45380521110908, -3.29883767235584, 0.32710905363475, 6856817.37],
[-1.981981304930552e-8, 0.000008983055099779535, 0.03278182852591, 40.31678527705744, 0.65659298677277, -4.44255534477492, 0.85341911805263, 0.12923347998204, -0.04625736007561, 4482777.06],
[3.09191371068437e-9, 0.000008983055096812155, 0.00006995724062, 23.10934304144901, -0.00023663490511, -0.6321817810242, -0.00663494467273, 0.03430082397953, -0.00466043876332, 2555164.4],
[2.890871144776878e-9, 0.000008983055095805407, -3.068298e-8, 7.47137025468032, -0.00000353937994, -0.02145144861037, -0.00001234426596, 0.00010322952773, -0.00000323890364, 826088.5]
];
var LL2MC = [
[-0.0015702102444, 111320.7020616939, 1704480524535203, -10338987376042340, 26112667856603880, -35149669176653700, 26595700718403920, -10725012454188240, 1800819912950474, 82.5],
[0.0008277824516172526, 111320.7020463578, 647795574.6671607, -4082003173.641316, 10774905663.51142, -15171875531.51559, 12053065338.62167, -5124939663.577472, 913311935.9512032, 67.5],
[0.00337398766765, 111320.7020202162, 4481351.045890365, -23393751.19931662, 79682215.47186455, -115964993.2797253, 97236711.15602145, -43661946.33752821, 8477230.501135234, 52.5],
[0.00220636496208, 111320.7020209128, 51751.86112841131, 3796837.749470245, 992013.7397791013, -1221952.21711287, 1340652.697009075, -620943.6990984312, 144416.9293806241, 37.5],
[-0.0003441963504368392, 111320.7020576856, 278.2353980772752, 2485758.690035394, 6070.750963243378, 54821.18345352118, 9540.606633304236, -2710.55326746645, 1405.483844121726, 22.5],
[-0.0003218135878613132, 111320.7020701615, 0.00369383431289, 823725.6402795718, 0.46104986909093, 2351.343141331292, 1.58060784298199, 8.77738589078284, 0.37238884252424, 7.45]
];
/**
* 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
* 即 百度 转 谷歌、高德
* @param bd_lon
* @param bd_lat
* @returns {*[]}
*/
var bd09togcj02 = function bd09togcj02(bd_lon, bd_lat) {
var bd_lon = +bd_lon;
var bd_lat = +bd_lat;
var x = bd_lon - 0.0065;
var y = bd_lat - 0.006;
var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_PI);
var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_PI);
var gg_lng = z * Math.cos(theta);
var gg_lat = z * Math.sin(theta);
return [gg_lng, gg_lat]
};
/**
* 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
* 即谷歌、高德 转 百度
* @param lng
* @param lat
* @returns {*[]}
*/
var gcj02tobd09 = function gcj02tobd09(lng, lat) {
var lat = +lat;
var lng = +lng;
var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
var bd_lng = z * Math.cos(theta) + 0.0065;
var bd_lat = z * Math.sin(theta) + 0.006;
return [bd_lng, bd_lat]
};
/**
* WGS84转GCj02
* @param lng
* @param lat
* @returns {*[]}
*/
var wgs84togcj02 = function wgs84togcj02(lng, lat) {
var lat = +lat;
var lng = +lng;
if (out_of_china(lng, lat)) {
return [lng, lat]
} else {
var dlat = transformlat(lng - 105.0, lat - 35.0);
var dlng = transformlng(lng - 105.0, lat - 35.0);
var radlat = lat / 180.0 * PI;
var magic = Math.sin(radlat);
magic = 1 - ee * magic * magic;
var sqrtmagic = Math.sqrt(magic);
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
var mglat = lat + dlat;
var mglng = lng + dlng;
return [mglng, mglat]
}
};
/**
* GCJ02 转换为 WGS84
* @param lng
* @param lat
* @returns {*[]}
*/
var gcj02towgs84 = function gcj02towgs84(lng, lat) {
var lat = +lat;
var lng = +lng;
if (out_of_china(lng, lat)) {
return [lng, lat]
} else {
var dlat = transformlat(lng - 105.0, lat - 35.0);
var dlng = transformlng(lng - 105.0, lat - 35.0);
var radlat = lat / 180.0 * PI;
var magic = Math.sin(radlat);
magic = 1 - ee * magic * magic;
var sqrtmagic = Math.sqrt(magic);
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
var mglat = lat + dlat;
var mglng = lng + dlng;
return [lng * 2 - mglng, lat * 2 - mglat]
}
};
var transformlat = function transformlat(lng, lat) {
var lat = +lat;
var lng = +lng;
var ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
return ret
};
var transformlng = function transformlng(lng, lat) {
var lat = +lat;
var lng = +lng;
var ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
return ret
};
/**
* 判断是否在国内,不在国内则不做偏移
* @param lng
* @param lat
* @returns {boolean}
*/
var out_of_china = function out_of_china(lng, lat) {
var lat = +lat;
var lng = +lng;
// 纬度3.86~53.55,经度73.66~135.05
return !(lng > 73.66 && lng < 135.05 && lat > 3.86 && lat < 53.55);
};
/**
* 百度墨卡托转百度经纬度
* @param lng
* @param lat
* @returns {*[]}
*/
var BD_MKT2WGS = function BD_MKT2WGS(lng, lat) {
var cF = null;
lng = Math.abs(lng);
lat = Math.abs(lat);
for (var cE = 0; cE < MCBAND.length; cE++) {
if (lat >= MCBAND[cE]) {
cF = MC2LL[cE];
break;
}
}
lng = cF[0] + cF[1] * Math.abs(lng);
var cC = Math.abs(lat) / cF[9];
lat = cF[2] + cF[3] * cC + cF[4] * cC * cC + cF[5] * cC * cC * cC + cF[6] * cC * cC * cC * cC + cF[7] * cC * cC * cC * cC * cC + cF[8] * cC * cC * cC * cC * cC * cC;
lng *= (lng < 0 ? -1 : 1);
lat *= (lat < 0 ? -1 : 1);
return [lng, lat];
}
var _getLoop = function _getLoop(lng, min, max) {
while (lng > max) {
lng -= max - min;
}
while (lng < min) {
lng += max - min;
}
return lng;
};
var _getRange = function _getRange(lat, min, max) {
if (min != null) {
lat = Math.max(lat, min);
}
if (max != null) {
lat = Math.min(lat, max);
}
return lat;
};
/**
* 百度经纬度转百度墨卡托
* @param lng
* @param lat
* @returns {*[]}
*/
var BD_WGS2MKT = function BD_WGS2MKT(lng, lat) {
var cF = null;
lng = _getLoop(lng, -180, 180);
lat = _getRange(lat, -74, 74);
for (var i = 0; i < LLBAND.length; i++) {
if (lat >= LLBAND[i]) {
cF = LL2MC[i];
break;
}
}
if (cF != null) {
for (var i = LLBAND.length - 1; i >= 0; i--) {
if (lat <= -LLBAND[i]) {
cF = LL2MC[i];
break;
}
}
}
lng = cF[0] + cF[1] * Math.abs(lng);
var cC = Math.abs(lat) / cF[9];
lat = cF[2] + cF[3] * cC + cF[4] * cC * cC + cF[5] * cC * cC * cC + cF[6] * cC * cC * cC * cC + cF[7] * cC * cC * cC * cC * cC + cF[8] * cC * cC * cC * cC * cC * cC;
lng *= (lng < 0 ? -1 : 1);
lat *= (lat < 0 ? -1 : 1);
return [lng, lat];
}
//经纬度转墨卡托
function latLng2WebMercator(lng, lat) {
var earthRad = 6378137.0;
var x = lng * Math.PI / 180 * earthRad;
var a = lat * Math.PI / 180;
var y = earthRad / 2 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
return [x, y]; //[12727039.383734727, 3579066.6894065146]
}
//墨卡托转经纬度
function webMercator2LngLat(x, y) { //[12727039.383734727, 3579066.6894065146]
var lng = x / 20037508.34 * 180;
var lat = y / 20037508.34 * 180;
lat = 180 / Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);
return [lng, lat]; //[114.32894001591471, 30.58574800385281]
}
return {
bd09togcj02: bd09togcj02,//百度转火星
gcj02tobd09: gcj02tobd09,//火星转百度
wgs84togcj02: wgs84togcj02,//wgs转火星
gcj02towgs84: gcj02towgs84,//火星转wgs
BD_MKT2WGS: BD_MKT2WGS, //百度墨卡托转百度经纬度
BD_WGS2MKT: BD_WGS2MKT, //百度经纬度转百度墨卡托
latLng2WebMercator: latLng2WebMercator, //经纬度转墨卡托
webMercator2LngLat: webMercator2LngLat //墨卡托转经纬度
}
}));
java
package com.gs.common.util;
/**
* 提供了百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
*
* @author Daniel
* @since 2016/7/27 0027
*/
public class CoordTransform {
private static final double x_PI = 3.14159265358979324 * 3000.0 / 180.0;
private static final double PI = 3.1415926535897932384626;
private static final double a = 6378245.0;
private static final double ee = 0.00669342162296594323;
/**
* 百度坐标(BD09)转 GCJ02
*
* @param lng 百度经度
* @param lat 百度纬度
* @return GCJ02 坐标:[经度,纬度]
*/
public static double[] transformBD09ToGCJ02(double lng, double lat) {
double x = lng - 0.0065;
double y = 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 gcj_lng = z * Math.cos(theta);
double gcj_lat = z * Math.sin(theta);
return new double[]{gcj_lng, gcj_lat};
}
/**
* GCJ02 转百度坐标
*
* @param lng GCJ02 经度
* @param lat GCJ02 纬度
* @return 百度坐标:[经度,纬度]
*/
public static double[] transformGCJ02ToBD09(double lng, double lat) {
double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
double bd_lng = z * Math.cos(theta) + 0.0065;
double bd_lat = z * Math.sin(theta) + 0.006;
return new double[]{bd_lng, bd_lat};
}
/**
* GCJ02 转 WGS84
*
* @param lng 经度
* @param lat 纬度
* @return WGS84坐标:[经度,纬度]
*/
public static double[] transformGCJ02ToWGS84(double lng, double lat) {
if (outOfChina(lng, lat)) {
return new double[]{lng, lat};
} else {
double dLat = transformLat(lng - 105.0, lat - 35.0);
double dLng = transformLng(lng - 105.0, lat - 35.0);
double radLat = 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);
dLng = (dLng * 180.0) / (a / sqrtMagic * Math.cos(radLat) * PI);
double mgLat = lat + dLat;
double mgLng = lng + dLng;
return new double[]{lng * 2 - mgLng, lat * 2 - mgLat};
}
}
/**
* WGS84 坐标 转 GCJ02
*
* @param lng 经度
* @param lat 纬度
* @return GCJ02 坐标:[经度,纬度]
*/
public static double[] transformWGS84ToGCJ02(double lng, double lat) {
if (outOfChina(lng, lat)) {
return new double[]{lng, lat};
} else {
double dLat = transformLat(lng - 105.0, lat - 35.0);
double dLng = transformLng(lng - 105.0, lat - 35.0);
double redLat = lat / 180.0 * PI;
double magic = Math.sin(redLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
dLng = (dLng * 180.0) / (a / sqrtMagic * Math.cos(redLat) * PI);
double mgLat = lat + dLat;
double mgLng = lng + dLng;
return new double[]{mgLng, mgLat};
}
}
/**
* 百度坐标BD09 转 WGS84
*
* @param lng 经度
* @param lat 纬度
* @return WGS84 坐标:[经度,纬度]
*/
public static double[] transformBD09ToWGS84(double lng, double lat) {
double[] lngLat = transformBD09ToGCJ02(lng, lat);
return transformGCJ02ToWGS84(lngLat[0], lngLat[1]);
}
/**
* WGS84 转 百度坐标BD09
*
* @param lng 经度
* @param lat 纬度
* @return BD09 坐标:[经度,纬度]
*/
public static double[] transformWGS84ToBD09(double lng, double lat) {
double[] lngLat = transformWGS84ToGCJ02(lng, lat);
return transformGCJ02ToBD09(lngLat[0], lngLat[1]);
}
/**
* 经纬度转墨卡托
* @param lng 经度
* @param lat 纬度
* @return wgs墨卡托[经度,纬度]
*/
public static double [] transformLonLatToMecator(double lng,double lat){
double earthRad = 6378137.0;
double x = lng * Math.PI / 180 * earthRad;
double a = lat * Math.PI / 180;
double y = earthRad / 2 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
return new double []{x, y}; //[12727039.383734727, 3579066.6894065146]
}
/**
* 墨卡托转经纬度
* @param x
* @param y
* @return 经纬度[经度,纬度]
*/
public static double [] transformMercatorToLngLat(double x,double y){
double lng = x / 20037508.34 * 180;
double lat = y / 20037508.34 * 180;
lat = 180 / Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);
return new double []{lng,lat}; //[12727039.383734727, 3579066.6894065146]
}
private static double transformLat(double lng, double lat) {
double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
return ret;
}
;
private static double transformLng(double lng, double lat) {
double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
return ret;
}
;
/**
* 判断坐标是否不在国内
*
* @param lng 经度
* @param lat 纬度
* @return 坐标是否在国内
*/
public static boolean outOfChina(double lng, double lat) {
return (lng < 72.004 || lng > 137.8347) || (lat < 0.8293 || lat > 55.8271);
}
}
下面是2000和wgs84坐标互转方法,写的有点乱,懒得整理了,但是可以互转
package com.gis.util;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class JavaRtkUtils {
private double p = 206264.80624709636D;
public static void main(String[] args) {
// 84 118.79222222222222,32.06444444444444,
// 2000 118.79232883438817,32.064499855041504,
double[] cs2000ToWgs84 = cs2000ToWgs84(118.79232883438817,32.064499855041504, 3);
for (double d : cs2000ToWgs84) {
System.out.println(d);
}
double[] wgs84To2000 = wgs84To2000(118.79222222222222,32.06444444444444, 3);
for (double d : wgs84To2000) {
System.out.println(d);
}
}
private double[] xyTowgs84(double x, double y, double L0) {
double a = 6378137.0D;
double efang = 0.0066943799901413D;
double e2fang = 0.0067394967422764D;
y = y - (double) 500000;
double m0 = 0.0D;
double m2 = 0.0D;
double m4 = 0.0D;
double m6 = 0.0D;
double m8 = 0.0D;
m0 = a * ((double) 1 - efang);
m2 = 1.5D * efang * m0;
m4 = efang * m2 * 5.0D / 4.0D;
m6 = efang * m4 * 7.0D / 6.0D;
m8 = efang * m6 * 9.0D / 8.0D;
double a0 = 0.0D;
double a2 = 0.0D;
double a4 = 0.0D;
double a6 = 0.0D;
double a8 = 0.0D;
a0 = m0 + m2 / 2.0D + m4 * 3.0D / 8.0D + m6 * 5.0D / 16.0D + m8 * 35.0D / 128.0D;
a2 = m2 / 2.0D + m4 / 2.0D + m6 * 15.0D / 32.0D + m8 * 7.0D / 16.0D;
a4 = m4 / 8.0D + m6 * 3.0D / 16.0D + m8 * 7.0D / 32.0D;
a6 = m6 / 32.0D + m8 / 16.0D;
a8 = m8 / 128.0D;
double FBf = 0.0D;
double Bf0 = x / a0;
for (double Bf1 = 0.0D; Bf0 - Bf1 >= 1.0E-4D; Bf0 = (x - FBf) / a0) {
Bf1 = Bf0;
FBf = -a2 * Math.sin((double) 2 * Bf0) / (double) 2 + a4 * Math.sin((double) 4 * Bf0) / (double) 4
- a6 * Math.sin((double) 6 * Bf0) / (double) 6 + a8 * Math.sin((double) 8 * Bf0) / (double) 8;
}
double Wf = Math.sqrt((double) 1 - efang * Math.sin(Bf0) * Math.sin(Bf0));
double Nf = a / Wf;
double Mf = a * ((double) 1 - efang) / Math.pow(Wf, 3.0D);
double nffang = e2fang * Math.cos(Bf0) * Math.cos(Bf0);
double tf = Math.tan(Bf0);
double B = Bf0 - tf * y * y / ((double) 2 * Mf * Nf)
+ tf * ((double) 5 + (double) 3 * tf * tf + nffang - (double) 9 * nffang * tf * tf) * Math.pow(y, 4.0D)
/ ((double) 24 * Mf * Math.pow(Nf, 3.0D))
- tf * ((double) 61 + (double) 90 * tf * tf + (double) 45 * Math.pow(tf, 4.0D)) * Math.pow(y, 6.0D)
/ ((double) 720 * Mf * Math.pow(Nf, 5.0D));
double l = y / (Nf * Math.cos(Bf0))
- ((double) 1 + (double) 2 * tf * tf + nffang) * Math.pow(y, 3.0D)
/ ((double) 6 * Math.pow(Nf, 3.0D) * Math.cos(Bf0))
+ ((double) 5 + (double) 28 * tf * tf + (double) 24 * Math.pow(tf, 4.0D)) * Math.pow(y, 5.0D)
/ ((double) 120 * Math.pow(Nf, 5.0D) * Math.cos(Bf0));
double L = l + L0;
double[] array_B = this.rad2dms(B);
double[] array_L = this.rad2dms(L);
double Bdec = this.dms2dec(array_B);
double Ldec = this.dms2dec(array_L);
return new double[] { Bdec, Ldec };
}
private double[] xyTo2000(double x, double y, double L0) {
double a = 6378137.0D;
double efang = 0.0066943800229008D;
double e2fang = 0.0067394967422764D;
y = y - (double) 500000;
double m0 = 0.0D;
double m2 = 0.0D;
double m4 = 0.0D;
double m6 = 0.0D;
double m8 = 0.0D;
m0 = a * ((double) 1 - efang);
m2 = 1.5D * efang * m0;
m4 = efang * m2 * 5.0D / 4.0D;
m6 = efang * m4 * 7.0D / 6.0D;
m8 = efang * m6 * 9.0D / 8.0D;
double a0 = 0.0D;
double a2 = 0.0D;
double a4 = 0.0D;
double a6 = 0.0D;
double a8 = 0.0D;
a0 = m0 + m2 / 2.0D + m4 * 3.0D / 8.0D + m6 * 5.0D / 16.0D + m8 * 35.0D / 128.0D;
a2 = m2 / 2.0D + m4 / 2.0D + m6 * 15.0D / 32.0D + m8 * 7.0D / 16.0D;
a4 = m4 / 8.0D + m6 * 3.0D / 16.0D + m8 * 7.0D / 32.0D;
a6 = m6 / 32.0D + m8 / 16.0D;
a8 = m8 / 128.0D;
double FBf = 0.0D;
double Bf0 = x / a0;
for (double Bf1 = 0.0D; Bf0 - Bf1 >= 1.0E-4D; Bf0 = (x - FBf) / a0) {
Bf1 = Bf0;
FBf = -a2 * Math.sin((double) 2 * Bf0) / (double) 2 + a4 * Math.sin((double) 4 * Bf0) / (double) 4
- a6 * Math.sin((double) 6 * Bf0) / (double) 6 + a8 * Math.sin((double) 8 * Bf0) / (double) 8;
}
double Wf = Math.sqrt((double) 1 - efang * Math.sin(Bf0) * Math.sin(Bf0));
double Nf = a / Wf;
double Mf = a * ((double) 1 - efang) / Math.pow(Wf, 3.0D);
double nffang = e2fang * Math.cos(Bf0) * Math.cos(Bf0);
double tf = Math.tan(Bf0);
double B = Bf0 - tf * y * y / ((double) 2 * Mf * Nf)
+ tf * ((double) 5 + (double) 3 * tf * tf + nffang - (double) 9 * nffang * tf * tf) * Math.pow(y, 4.0D)
/ ((double) 24 * Mf * Math.pow(Nf, 3.0D))
- tf * ((double) 61 + (double) 90 * tf * tf + (double) 45 * Math.pow(tf, 4.0D)) * Math.pow(y, 6.0D)
/ ((double) 720 * Mf * Math.pow(Nf, 5.0D));
double l = y / (Nf * Math.cos(Bf0))
- ((double) 1 + (double) 2 * tf * tf + nffang) * Math.pow(y, 3.0D)
/ ((double) 6 * Math.pow(Nf, 3.0D) * Math.cos(Bf0))
+ ((double) 5 + (double) 28 * tf * tf + (double) 24 * Math.pow(tf, 4.0D)) * Math.pow(y, 5.0D)
/ ((double) 120 * Math.pow(Nf, 5.0D) * Math.cos(Bf0));
double L = l + L0;
double[] array_B = this.rad2dms(B);
double[] array_L = this.rad2dms(L);
double Bdec = this.dms2dec(array_B);
double Ldec = this.dms2dec(array_L);
return new double[] { Bdec, Ldec };
}
private double gaussLongToDegreen(double B, double L, double N) {
double L00 = (double) Math.round(L / (double) N) * (double) N;
return L00 / (double) 180 * 3.1415926D;
}
private double[] rad2dms(double rad) {
double[] a = new double[] { 0.0D, 0.0D, 0.0D };
double dms = rad * p;
a[0] = Math.floor(dms / 3600.0D);
a[1] = Math.floor((dms - a[0] * (double) 3600) / 60.0D);
a[2] = (double) ((int) Math.floor(dms - a[0] * (double) 3600)) - a[1] * (double) 60;
return a;
}
private double dms2dec(double[] dms) {
double dec = 0.0D;
dec = dms[0] + dms[1] / 60.0D + dms[2] / 3600.0D;
return dec;
}
private double getL0(double B, double L, double degree,int flag){
double a = 6378137.0D;
double b = flag==1?6356752.314245179D:6356752.314140355D;
double e = flag==1?0.081819190842621D:0.0818191910428158D;
double eC = flag==1?0.0820944379496957D:0.0820944381519172D;
double L0 = 0.0D;
int n;
if (degree == 6.0D) {
n = (int) Math.round((L + degree / (double) 2) / degree);
L0 = degree * (double) n - degree / (double) 2;
} else {
n = (int) Math.round(L / degree);
L0 = degree * (double) n;
}
return L0;
}
/**
* @param flag 1 传入2000坐标转84 2 传入84坐标转2000
* @return
*/
private double[] GetXY(double B, double L, double degree,int flag) {
double[] xy = new double[] { 0.0D, 0.0D };
double a = 6378137.0D;
double b = flag==1?6356752.314245179D:6356752.314140355D;
double e = flag==1?0.081819190842621D:0.0818191910428158D;
double eC = flag==1?0.0820944379496957D:0.0820944381519172D;
double L0 = 0.0D;
int n;
if (degree == 6.0D) {
n = (int) Math.round((L + degree / (double) 2) / degree);
L0 = degree * (double) n - degree / (double) 2;
} else {
n = (int) Math.round(L / degree);
L0 = degree * (double) n;
}
double radB = B * 3.141592653589793D / (double) 180;
double radL = L * 3.141592653589793D / (double) 180;
double deltaL = (L - L0) * 3.141592653589793D / (double) 180;
double N = a * a / b / Math.sqrt((double) 1 + eC * eC * Math.cos(radB) * Math.cos(radB));
double C1 = 1.0D + 0.75D * e * e + 0.703125D * Math.pow(e, 4.0D) + 0.68359375D * Math.pow(e, 6.0D)
+ 0.67291259765625D * Math.pow(e, 8.0D);
double C2 = 0.75D * e * e + 0.9375D * Math.pow(e, 4.0D) + 1.025390625D * Math.pow(e, 6.0D)
+ 1.07666015625D * Math.pow(e, 8.0D);
double C3 = 0.234375D * Math.pow(e, 4.0D) + 0.41015625D * Math.pow(e, 6.0D)
+ 0.538330078125D * Math.pow(e, 8.0D);
double C4 = 0.068359375D * Math.pow(e, 6.0D) + 0.15380859375D * Math.pow(e, 8.0D);
double C5 = 0.00240325927734375D * Math.pow(e, 8.0D);
double t = Math.tan(radB);
double eta = eC * Math.cos(radB);
double X = a * ((double) 1 - e * e)
* (C1 * radB - C2 * Math.sin((double) 2 * radB) / (double) 2
+ C3 * Math.sin((double) 4 * radB) / (double) 4 - C4 * Math.sin((double) 6 * radB) / (double) 6
+ C5 * Math.sin((double) 8 * radB));
xy[0] = X
+ N * Math.sin(radB)
* Math.cos(
radB)
* Math.pow(deltaL, 2.0D)
* ((double) 1 + Math.pow(deltaL * Math.cos(radB), 2.0D)
* ((double) 5 - t * t + (double) 9 * eta * eta + (double) 4 * Math.pow(eta, 4.0D))
/ (double) 12
+ Math.pow(deltaL * Math.cos(radB), 4.0D)
* ((double) 61 - (double) 58 * t * t + Math.pow(t, 4.0D)) / (double) 360)
/ (double) 2;
xy[1] = N * deltaL * Math.cos(radB)
* ((double) 1 + Math.pow(deltaL * Math.cos(radB), 2.0D) * ((double) 1 - t * t + eta * eta) / (double) 6
+ Math.pow(deltaL * Math.cos(radB), 4.0D) * ((double) 5 - (double) 18 * t * t
+ Math.pow(t, 4.0D) - (double) 14 * eta * eta - (double) 58 * eta * eta * t * t)
/ (double) 120)
+ (double) 500000;
return new double[] { xy[0], xy[1] };
}
// @将WGS84经纬度转为大地2000坐标。我们是国家电网项目数据很精确的了。
// @param B 纬度
// @param L 经度
// @param degree //
// @param withBand 默认=false
// @return
private static Double[] gps84ToXY(double B, double L, double degree) {
double[] xy = new double[] { 0.0D, 0.0D };
double a = 6378137.0D;
double b = 6356752.314245179D;
double e = 0.081819190842621D;
double eC = 0.0820944379496957D;
double L0 = 0.0D;
int n;
if (degree == 6.0D) {
n = (int) Math.round((L + degree / (double) 2) / degree);
L0 = degree * (double) n - degree / (double) 2;
} else {
n = (int) Math.round(L / degree);
L0 = degree * (double) n;
}
// 开始计算
double radB = B * 3.141592653589793D / (double) 180;
double radL = L * 3.141592653589793D / (double) 180;
double deltaL = (L - L0) * 3.141592653589793D / (double) 180;
double N = a * a / b / Math.sqrt(1 + eC * eC * Math.cos(radB) * Math.cos(radB));
double C1 = 1.0 + 3.0 / 4 * e * e + 45.0 / 64 * Math.pow(e, 4) + 175.0 / 256 * Math.pow(e, 6)
+ 11025.0 / 16384 * Math.pow(e, 8);
double C2 = 3.0 / 4 * e * e + 15.0 / 16 * Math.pow(e, 4) + 525.0 / 512 * Math.pow(e, 6)
+ 2205.0 / 2048 * Math.pow(e, 8);
double C3 = 15.0 / 64 * Math.pow(e, 4) + 105.0 / 256 * Math.pow(e, 6) + 2205.0 / 4096 * Math.pow(e, 8);
double C4 = 35.0 / 512 * Math.pow(e, 6) + 315.0 / 2048 * Math.pow(e, 8);
double C5 = 315.0 / 131072 * Math.pow(e, 8);
double t = Math.tan(radB);
double eta = eC * Math.cos(radB);
double X = a * (1 - e * e) * (C1 * radB - C2 * Math.sin(2 * radB) / 2 + C3 * Math.sin(4 * radB) / 4
- C4 * Math.sin(6 * radB) / 6 + C5 * Math.sin(8 * radB));
xy[0] = X + N * Math.sin(radB) * Math.cos(radB) * Math.pow(deltaL, 2)
* (1 + Math.pow(deltaL * Math.cos(radB), 2) * (5 - t * t + 9 * eta * eta + 4 * Math.pow(eta, 4)) / 12
+ Math.pow(deltaL * Math.cos(radB), 4) * (61 - 58 * t * t + Math.pow(t, 4)) / 360)
/ 2;
xy[1] = N * deltaL * Math.cos(radB)
* (1 + Math.pow(deltaL * Math.cos(radB), 2) * (1 - t * t + eta * eta) / 6
+ Math.pow(deltaL * Math.cos(radB), 4)
* (5 - 18 * t * t + Math.pow(t, 4) - 14 * eta * eta - 58 * eta * eta * t * t) / 120)
+ 500000; // +n * 1000000;
return new Double[] { xy[0], xy[1] };
}
public static double[] wgs84To2000(double longitude, double latitude, double degree){
JavaRtkUtils rtkUtils = new JavaRtkUtils();
double[] getXY2000 = rtkUtils.GetXY(latitude,longitude, degree,2);
double l0 = rtkUtils.getL0(latitude,longitude, degree,2);
double[] xytolatlon = xytolatlon2000(getXY2000[0], getXY2000[1], l0);
double lat = xytolatlon[0];
xytolatlon[0]=xytolatlon[1];
xytolatlon[1]=lat;
return xytolatlon;
}
/**
* @param degree 3度带还是6度带
* @return
*/
public static double[] cs2000ToWgs84(double longitude, double latitude, double degree){
JavaRtkUtils rtkUtils = new JavaRtkUtils();
double[] getXY2000 = rtkUtils.GetXY(latitude,longitude, degree,1);
double gaussLongToDegreen = rtkUtils.gaussLongToDegreen(latitude, longitude, degree);
double[] xyTowgs84 = rtkUtils.xyTowgs84(getXY2000[0], getXY2000[1], gaussLongToDegreen);
double lat = xyTowgs84[0];
xyTowgs84[0]=xyTowgs84[1];
xyTowgs84[1]=lat;
return xyTowgs84;
}
private static double [] xytolatlon(double X, double Y ,double L0) {
double lat ,lon;
Y-=500000;
// double a = 6378137.0D;
// double b = 6356752.314245179D;
// double e = 0.081819190842621D;
// double ee = 0.0820944379496957D;
// double iPI =Math.PI/180;//pi/180
double [] result = new double[2];
double iPI = 0.0174532925199433;//pi/180
double a = 6378137.0; //长半轴 m
double b = 6356752.31414; //短半轴 m
double f = 1/298.257222101;//扁率 a-b/a
double e = 0.0818191910428; //第一偏心率 Math.sqrt(5)
double ee = Math.sqrt(a*a-b*b)/b; //第二偏心率
double bf = 0; //底点纬度
double a0 = 1+(3*e*e/4) + (45*e*e*e*e/64) + (175*e*e*e*e*e*e/256) + (11025*e*e*e*e*e*e*e*e/16384) + (43659*e*e*e*e*e*e*e*e*e*e/65536);
double b0 = X/(a*(1-e*e)*a0);
double c1 = 3*e*e/8 +3*e*e*e*e/16 + 213*e*e*e*e*e*e/2048 + 255*e*e*e*e*e*e*e*e/4096;
double c2 = 21*e*e*e*e/256 + 21*e*e*e*e*e*e/256 + 533*e*e*e*e*e*e*e*e/8192;
double c3 = 151*e*e*e*e*e*e*e*e/6144 + 151*e*e*e*e*e*e*e*e/4096;
double c4 = 1097*e*e*e*e*e*e*e*e/131072;
bf = b0 + c1*Math.sin(2*b0) + c2*Math.sin(4*b0) +c3*Math.sin(6*b0) + c4*Math.sin(8*b0); // bf =b0+c1*sin2b0 + c2*sin4b0 + c3*sin6b0 +c4*sin8b0 +...
double tf = Math.tan(bf);
double n2 = ee*ee*Math.cos(bf)*Math.cos(bf); //第二偏心率平方成bf余弦平方
double c = a*a/b;
double v=Math.sqrt(1+ ee*ee*Math.cos(bf)*Math.cos(bf));
double mf = c/(v*v*v); //子午圈半径
double nf = c/v;//卯酉圈半径
//纬度计算
lat=bf-(tf/(2*mf)*Y)*(Y/nf) * (1-1/12*(5+3*tf*tf+n2-9*n2*tf*tf)*(Y*Y/(nf*nf))+1/360*(61+90*tf*tf+45*tf*tf*tf*tf)*(Y*Y*Y*Y/(nf*nf*nf*nf)));
//经度偏差
lon=1/(nf*Math.cos(bf))*Y -(1/(6*nf*nf*nf*Math.cos(bf)))*(1+2*tf*tf +n2)*Y*Y*Y + (1/(120*nf*nf*nf*nf*nf*Math.cos(bf)))*(5+28*tf*tf+24*tf*tf*tf*tf)*Y*Y*Y*Y*Y;
result[0] =lat/iPI;
result[1] =L0+lon/iPI;
return result;
}
private static double [] xytolatlon2000(double X, double Y ,double L0) {
double lat ,lon;
Y-=500000;
double a = 6378137.0D;
double b = 6356752.314245179D;
double e = 0.081819190842621D;
double ee = 0.0820944379496957D;
double iPI =Math.PI/180;//pi/180
double [] result = new double[2];
// double iPI = 0.0174532925199433;//pi/180
// double a = 6378137.0; //长半轴 m
// double b = 6356752.31414; //短半轴 m
// double e = 0.0818191910428; //第一偏心率 Math.sqrt(5)
// double ee = Math.sqrt(a*a-b*b)/b; //第二偏心率
double f = 1/298.257222101;//扁率 a-b/a
double bf = 0; //底点纬度
double a0 = 1+(3*e*e/4) + (45*e*e*e*e/64) + (175*e*e*e*e*e*e/256) + (11025*e*e*e*e*e*e*e*e/16384) + (43659*e*e*e*e*e*e*e*e*e*e/65536);
double b0 = X/(a*(1-e*e)*a0);
double c1 = 3*e*e/8 +3*e*e*e*e/16 + 213*e*e*e*e*e*e/2048 + 255*e*e*e*e*e*e*e*e/4096;
double c2 = 21*e*e*e*e/256 + 21*e*e*e*e*e*e/256 + 533*e*e*e*e*e*e*e*e/8192;
double c3 = 151*e*e*e*e*e*e*e*e/6144 + 151*e*e*e*e*e*e*e*e/4096;
double c4 = 1097*e*e*e*e*e*e*e*e/131072;
bf = b0 + c1*Math.sin(2*b0) + c2*Math.sin(4*b0) +c3*Math.sin(6*b0) + c4*Math.sin(8*b0); // bf =b0+c1*sin2b0 + c2*sin4b0 + c3*sin6b0 +c4*sin8b0 +...
double tf = Math.tan(bf);
double n2 = ee*ee*Math.cos(bf)*Math.cos(bf); //第二偏心率平方成bf余弦平方
double c = a*a/b;
double v=Math.sqrt(1+ ee*ee*Math.cos(bf)*Math.cos(bf));
double mf = c/(v*v*v); //子午圈半径
double nf = c/v;//卯酉圈半径
//纬度计算
lat=bf-(tf/(2*mf)*Y)*(Y/nf) * (1-1/12*(5+3*tf*tf+n2-9*n2*tf*tf)*(Y*Y/(nf*nf))+1/360*(61+90*tf*tf+45*tf*tf*tf*tf)*(Y*Y*Y*Y/(nf*nf*nf*nf)));
//经度偏差
lon=1/(nf*Math.cos(bf))*Y -(1/(6*nf*nf*nf*Math.cos(bf)))*(1+2*tf*tf +n2)*Y*Y*Y + (1/(120*nf*nf*nf*nf*nf*Math.cos(bf)))*(5+28*tf*tf+24*tf*tf*tf*tf)*Y*Y*Y*Y*Y;
result[0] =lat/iPI;
result[1] =L0+lon/iPI;
return result;
}
}