package com.gci.boot.common.util;

import java.math.BigDecimal;

/**
 * @Author: lyz
 * @Description: 经纬度转换
 * @Date: 2021-04-20 14:10
 * @version: v1.0
 * @Modified by:
 **/
public class CoordinateConverterUtil
{
    /**
     *赤道半径
     */
    private final static double EARTH_RADIUS = 6378137.0;

    /**
     * PI
     */
    private final static double PI = Math.PI;

    /**
     * e2
     */
    private final static double EE = 0.00669342162296594323;


    /**
     *  GCJ-02 to WGS-84  即高德坐标转换成gps坐标
     * @param latitude
     * @param longitude
     * @return
     */
    public static LatLonPoint toGPSPoint(double latitude, double longitude) {
        LatLonPoint dev = calDev(latitude, longitude);
        double retLat = latitude - dev.getLatitude();
        double retLon = longitude - dev.getLongitude();
        for (int i = 0; i < 1; i++) {
            dev = calDev(retLat, retLon);
            retLat = latitude - dev.getLatitude();
            retLon = longitude - dev.getLongitude();
        }
        retLat = new BigDecimal(retLat).setScale(6,BigDecimal.ROUND_HALF_UP).doubleValue();
        retLon = new BigDecimal(retLon).setScale(6,BigDecimal.ROUND_HALF_UP).doubleValue();
        return new LatLonPoint(retLat, retLon);
    }
    /**
     *  WGS-84 to  GCJ-02  即gps坐标转成高德坐标
     * @param latitude
     * @param longitude
     * @return
     */
    public static LatLonPoint toWGSPoint(double latitude, double longitude){
        LatLonPoint dev = calDev(latitude, longitude);
        double retLat = latitude + dev.getLatitude();
        double retLon = longitude + dev.getLongitude();
        retLat = new BigDecimal(retLat).setScale(6,BigDecimal.ROUND_HALF_UP).doubleValue();
        retLon = new BigDecimal(retLon).setScale(6,BigDecimal.ROUND_HALF_UP).doubleValue();
        return new LatLonPoint(retLat, retLon);
    }

    /**
     *  计算偏差
     * @param wgLat
     * @param wgLon
     * @return
     */
    private static LatLonPoint calDev(double wgLat, double wgLon) {
        if (isOutOfChina(wgLat, wgLon)) {
            return new LatLonPoint(0, 0);
        }
        double dLat = calLat(wgLon - 105.0, wgLat - 35.0);
        double dLon = calLon(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) / ((EARTH_RADIUS * (1 - EE)) / (magic * sqrtMagic) * PI);
        dLon = (dLon * 180.0) / (EARTH_RADIUS / sqrtMagic * Math.cos(radLat) * PI);
        return new LatLonPoint(dLat, dLon);
    }

    /**
     * 判断坐标是否在国外  纬度 3.86~53.55, 经度 73.66~135.05
     * @param lat
     * @param lon
     * @return
     */
    private static boolean isOutOfChina(double lat, double lon) {
        if (lon < 73.66 || lon > 135.05){
            return true;
        }
        if (lat < 3.86 || lat > 53.55){
            return true;
        }
        return false;
    }

    /**
     * 计算纬度
     * @param x
     * @param y
     * @return
     */
    private static double calLat(double x, double y) {
        double resultLat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
                + 0.2 * Math.sqrt(Math.abs(x));
        resultLat += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        resultLat += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
        resultLat += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
        return resultLat;
    }

    /**
     * 计算经度
     * @param x
     * @param y
     * @return
     */
    private static double calLon(double x, double y) {
        double resultLon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
                * Math.sqrt(Math.abs(x));
        resultLon += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        resultLon += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
        resultLon += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0
                * PI)) * 2.0 / 3.0;
        return resultLon;
    }

    public static void main(String[] args){
        LatLonPoint point = new LatLonPoint(29.623759D,104.543263D);
        System.out.println("gps坐标：经度："+point.getLongitude()+" 纬度："+point.getLatitude());
        LatLonPoint point1 = CoordinateConverterUtil.toWGSPoint(point.getLatitude(),point.getLongitude());
        System.out.println("高德坐标：经度："+point1.getLongitude()+" 纬度："+point1.getLatitude());
    }

}
