package com.mosty.yjzl.cluster;

import java.util.Arrays;

/**
 * 坐标系转换工具类 </br>
 * WGS84坐标系 地球坐标系，国际通用坐标系 </br>
 * GCJ02坐标系 火星坐标系，WGS84坐标系加密后的坐标系；Google国内地图、高德、QQ地图 使用 </br>
 * BD09坐标系 百度坐标系，GCJ02坐标系加密后的坐标系
 */
public class PositionUtil {

	private static double pi = 3.1415926535897932384626;
	private static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
	private static double a = 6378245.0;
	private static double ee = 0.00669342162296594323;

	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;
	}

	private static double[] transform(double latitude, double longitude) {
		if (outOfChina(latitude, longitude)) {
			return new double[] { latitude, longitude };
		}
		double dLat = transformLat(longitude - 105.0, latitude - 35.0);
		double dLon = transformLon(longitude - 105.0, latitude - 35.0);
		double radLat = latitude / 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 = latitude + dLat;
		double mgLon = longitude + dLon;
		return new double[] { mgLat, mgLon };
	}

	private static boolean outOfChina(double latitude, double longitude) {
		if (longitude < 72.004 || longitude > 137.8347) {
			return true;
		}
		if (latitude < 0.8293 || latitude > 55.8271) {
			return true;
		}
		return false;
	}

	/**
	 * WGS-84 转 火星坐标系
	 *
	 * @param latitude
	 * @param longitude
	 * @return
	 */
	public static double[] wgs84ToGcj02(double latitude, double longitude) {
		if (outOfChina(latitude, longitude)) {
			return new double[] { latitude, longitude };
		}
		double dLat = transformLat(longitude - 105.0, latitude - 35.0);
		double dLon = transformLon(longitude - 105.0, latitude - 35.0);
		double radLat = latitude / 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 = latitude + dLat;
		double mgLon = longitude + dLon;
		return new double[] { mgLat, mgLon };
	}
	
	public static void main(String[] args) {
		double[] wgs84ToGcj02 = wgs84ToGcj02(30.55956521, 104.27152736);
		System.out.println(Arrays.toString(wgs84ToGcj02));
	}

	/**
	 * 火星坐标系 (GCJ-02) 转 WGS-84
	 *
	 * @param latitude
	 * @param longitude
	 * @return
	 */
	public static double[] gcj02ToWgs84(double latitude, double longitude) {
		double[] wgs = transform(latitude, longitude);
		double longitudetitude = longitude * 2 - wgs[1];
		double latitudeitude = latitude * 2 - wgs[0];
		return new double[] { latitudeitude, longitudetitude };
	}

	/**
	 * 火星坐标系 (GCJ-02) 转百度坐标系 (BD-09)
	 *
	 * @param latitude
	 * @param longitude
	 * @return
	 */
	public static double[] gcj02ToBd09(double latitude, double longitude) {
		double x = longitude, y = latitude;
		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 tempLon = z * Math.cos(theta) + 0.0065;
		double tempLat = z * Math.sin(theta) + 0.006;
		double[] wgs = { tempLat, tempLon };
		return wgs;
	}

	/**
	 * 百度坐标系 (BD-09) 转 火星坐标系 (GCJ-02)
	 *
	 * @param latitude
	 * @param longitude
	 * @return
	 */
	public static double[] bd09ToGcj02(double latitude, double longitude) {
		double x = longitude - 0.0065, y = latitude - 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 tempLon = z * Math.cos(theta);
		double tempLat = z * Math.sin(theta);
		double[] wgs = { tempLat, tempLon };
		return wgs;
	}

	/**
	 * WGS-84 转 百度坐标系 (BD-09)
	 *
	 * @param latitude
	 * @param longitude
	 * @return
	 */
	public static double[] wgs84ToBd09(double latitude, double longitude) {
		double[] gcj02 = wgs84ToGcj02(latitude, longitude);
		double[] bd09 = gcj02ToBd09(gcj02[0], gcj02[1]);
		return bd09;
	}

	/**
	 * 百度坐标系 (BD-09) 转 WGS-84
	 *
	 * @param latitude
	 * @param longitude
	 * @return
	 */
	public static double[] bd09ToWgs84(double latitude, double longitude) {
		double[] gcj02 = bd09ToGcj02(latitude, longitude);
		double[] wgs84 = gcj02ToWgs84(gcj02[0], gcj02[1]);
		// 保留小数点后六位
		wgs84[0] = retain6(wgs84[0]);
		wgs84[1] = retain6(wgs84[1]);
		return wgs84;
	}

	/**
	 * 保留小数点后六位
	 *
	 * @param num
	 * @return
	 */
	private static double retain6(double num) {
		String result = String.format("%.6f", num);
		return Double.valueOf(result);
	}

	/**
	 * 计算两个坐标之间的距离
	 * 
	 * @param latitude1
	 * @param longitude1
	 * @param latitude2
	 * @param longitude2
	 * @return
	 */
	public static long getDistance(double latitude1, double longitude1, double latitude2, double longitude2) {
		double rad = pi / 180.0;
		double radlatitude1 = latitude1 * rad;
		double radlatitude2 = latitude2 * rad;
		double s = 2
				* Math.asin(Math.sqrt(Math.pow(Math.sin((radlatitude1 - radlatitude2) / 2), 2) + Math.cos(radlatitude1)
						* Math.cos(radlatitude2) * Math.pow(Math.sin(((longitude1 - longitude2) * rad) / 2), 2)));
		return Math.round(s * a * 100) / 100;
	}

}
