package org.jeecg.modules.zrzyqc.qcsb.Untils;

/**
 * @Description: TODO
 * @author: qiudongbao
 * @date: 2022年06月27日 14:49
 */
public class GisUtil {
    /**
     * 直角坐标转换为经纬度坐标
     * @param x 直角坐标x
     * @param y 直角坐标y
     * @param centerLine 中央经线
     * @return 纬度,经度
     */
    public  String coordinateTransform(double x, double y, double centerLine) {
        // TODO: 2022/6/27 2000椭球的长轴
        double ElliA = 6378137;
        // TODO: 2022/6/27 2000椭球的第一偏心率
        double ElliE = 0.00669438002290;
        // TODO: 2022/6/27 2000椭球的第二偏心率
        double ElliET = 0.00673949677548;
        double BB, ee, Ao, Bo, Co, Do, Eo, mCosB, B1, pccn, pcct, pccr, mgs1, mgs2, mgs3, mgs4;
        double ZhiJiaoY = 0;
        if(y > 1000000){
            double temp = y/1000000;
            ZhiJiaoY = (temp - Math.floor(temp))*1000000;
        }else{
            ZhiJiaoY = y;
        }
        ZhiJiaoY =  ZhiJiaoY - 500000;
        ee = ElliET;
        Ao = ElliA * (1 - ElliE) * (1 + 3 / 4d * ElliE + 45 / 64d * Math.pow(ElliE, 2) + 350 / 512d * Math.pow(ElliE, 3) + 11025 / 16384d * Math.pow(ElliE, 4)) * 4 * Math.atan(1) / 180d;
        Bo = ElliA * (1 - ElliE) * (3 / 4d * ElliE + 60 / 64d * Math.pow(ElliE, 2) + 525 / 512d * Math.pow(ElliE, 3) + 17640 / 16384d * Math.pow(ElliE, 4)) / 2d;
        Co = ElliA * (1 - ElliE) * (15 / 64d * Math.pow(ElliE, 2) + 210 / 512d * Math.pow(ElliE, 3) + 8820 / 16384d * Math.pow(ElliE, 4)) / 4d;
        Do = ElliA * (1 - ElliE) * (35 / 512d * Math.pow(ElliE, 3) + 2520 / 16384d * Math.pow(ElliE, 4)) / 6d;
        Eo = ElliA * (1 - ElliE) * (315 / 16384d * Math.pow(ElliE, 4)) / 8d;
        BB = x / Ao;
        B1 = BB;
        BB = 0;
        while ((B1 - BB) > 0.0000000001) {
            BB = B1;
            B1 = 0 - Bo * Math.sin(BB * 2 * 4 * Math.atan(1) / 180d) + Co * Math.sin(BB * 4 * 4 * Math.atan(1) / 180d) - Do * Math.sin(BB * 6 * 4 * Math.atan(1) / 180d) + Eo * Math.sin(BB * 8 * 4 * Math.atan(1) / 180d);
            B1 = (x - B1) / Ao;
        }
        BB = B1;
        pccn = ElliA / Math.sqrt(1d - ElliE * Math.pow(Math.sin(BB * 4 * Math.atan(1) / 180d), 2));
        pcct = Math.tan(BB * 4 * Math.atan(1) / 180d);
        mCosB = Math.cos(BB * 4 * Math.atan(1) / 180d);
        pccr = ee * Math.pow(mCosB, 2);

        mgs1 = (1d + pccr) * pcct * Math.pow((ZhiJiaoY / pccn), 2) / 2d * 180d / 4 / Math.atan(1);
        mgs2 = (1d + pccr) * pcct * Math.pow((ZhiJiaoY / pccn), 4) / 24d * (5d + 3d * Math.pow(pcct, 2) + pccr - 9d * pccr * Math.pow(pcct, 2)) * 180d / 4 / Math.atan(1);
        mgs3 = (1d + pccr) * pcct * Math.pow((ZhiJiaoY / pccn), 6) / 720d * (61d + 90d * Math.pow(pcct, 2) + 45d * Math.pow(pcct, 4)) * 180d / 4 / Math.atan(1);
        double dB = BB - mgs1 + mgs2 - mgs3;
        dB = dB * 4 * Math.atan(1) / 180d;
        mgs1 = (ZhiJiaoY / pccn) / mCosB * 180d / 4 / Math.atan(1);
        mgs2 = Math.pow((ZhiJiaoY / pccn), 3) / mCosB * (1d + 2 * Math.pow(pcct, 2) + pccr) / 6d * 180d / 4 / Math.atan(1);
        mgs3 = Math.pow((ZhiJiaoY / pccn), 5) / mCosB * (5d + 28 * Math.pow(pcct, 2) + 24d * Math.pow(pcct, 4) + 6 * pccr + 8 * pccr * Math.pow(pcct, 2)) / 120d * 180d / 4 / Math.atan(1);
        mgs4 = Math.pow((ZhiJiaoY / pccn), 7) / mCosB * (61d + 662 * Math.pow(pcct, 2) + 1320d * Math.pow(pcct, 4) + 720 * Math.pow(pccr, 6)) / 5040 * 180d / 4 / Math.atan(1);
        double dL = mgs1 - mgs2 + mgs3 - mgs4 + centerLine;
        dL = dL * 4 * Math.atan(1) / 180d;
        return String.format("%.8f,%.8f",dL*180/Math.PI, dB*180/Math.PI);
    }


}
