0
点赞
收藏
分享

微信扫一扫

WGS84转CGS2000 国家大地坐标系转换

罗子僧 2022-04-13 阅读 88
java
    public static Point WGS84ToCGS2000(double xCoordinates, double yCoordinates)//参数 经度,纬度
    {
        // 数值过低可能是因为行政区代码错误导致的
        if (xCoordinates <= 0.0000001 || yCoordinates <= 0.0000001) {
            return null;
        }
        int ProjNo = 0;
        Point point = new Point(xCoordinates, yCoordinates);
        if (CheckIsWGS84Coordinates(xCoordinates, yCoordinates)) {
            point = Wgs84ToCgs2000(point, ProjNo);
        } else {
            if (!String.valueOf(point.getX()).startsWith(String.valueOf(ProjNo))) {
                //point = CoordinateTrans.Xian80ToWgs84(point, null);
                //point = ShapeAreaHelper.TransCoordinatesFrom84To2000(point, ProjNo);
            }
        }
        return point;
    }
    
    /**
     * 84经纬度坐标转2000平面坐标
     * @param sourcePoint
     * @param ProjNo
     * @return
     */
    public static Point Wgs84ToCgs2000(Point sourcePoint, int ProjNo) {
        if (sourcePoint == null) {
            //throw new Exception("sourcePoint");
        }
        Point targetPoint = GaussProjCal(sourcePoint, ProjNo);
        return targetPoint;
    }

    /**
     * 高斯投影正算
     * @param sourcePoint
     * @param ProjNo
     * @return
     */
    public static Point GaussProjCal(Point sourcePoint, int ProjNo) {
        Point point = GaussProjCal(sourcePoint.getX(), sourcePoint.getY(), EarthParam.CGS2000.A, EarthParam.CGS2000.F, ProjNo);
        return point;
    }

    private static Point GaussProjCal(double longitude, double latitude, double a, double f, int ProjNo) {
        //int ProjNo = 0;
        int ZoneWide;   带宽
        double longitude1, latitude1, longitude0, latitude0, X0, Y0, xval, yval;
        double e2, ee, NN, T, C, A, M, iPI;
        iPI = Math.PI / 180;   3.1415926535898/180.0;
        ZoneWide = 3;     3度带宽
        //a = 6378245.0; f = 1.0 / 298.3;      //54年北京坐标系参数
        //a=6378140.0;   f=1/298.257;      //80年西安坐标系参数
        //a=6378137m;f=1/298.257223563;//WGS-84坐标系
        if (ProjNo == 0) {
            ProjNo = (int) Math.round(longitude / ZoneWide);
        }
        longitude0 = ProjNo * ZoneWide;
        longitude0 = longitude0 * iPI;
        latitude0 = 0;
        //经度转换为弧度
        longitude1 = longitude * iPI;
        //纬度转换为弧度
        latitude1 = latitude * iPI;
        e2 = 2 * f - f * f;
        ee = e2 * (1.0 - e2);
        NN = a / Math.sqrt(1.0 - e2 * Math.sin(latitude1) * Math.sin(latitude1));
        T = Math.tan(latitude1) * Math.tan(latitude1);
        C = ee * Math.cos(latitude1) * Math.cos(latitude1);
        A = (longitude1 - longitude0) * Math.cos(latitude1);
        M = a * ((1 - e2 / 4 - 3 * e2 * e2 / 64 - 5 * e2 * e2 * e2 / 256) * latitude1 - (3 * e2 / 8 + 3 * e2 * e2 / 32 + 45 * e2 * e2 * e2 / 1024) * Math.sin(2 * latitude1)
                + (15 * e2 * e2 / 256 + 45 * e2 * e2 * e2 / 1024) * Math.sin(4 * latitude1) - (35 * e2 * e2 * e2 / 3072) * Math.sin(6 * latitude1));
        xval = NN * (A + (1 - T + C) * A * A * A / 6 + (5 - 18 * T + T * T + 72 * C - 58 * ee) * A * A * A * A * A / 120);
        yval = M + NN * Math.tan(latitude1) * (A * A / 2 + (5 - T + 9 * C + 4 * C * C) * A * A * A * A / 24
                + (61 - 58 * T + T * T + 600 * C - 330 * ee) * A * A * A * A * A * A / 720);
        X0 = 1000000L * (ProjNo) + 500000L;
        Y0 = 0;
        xval = xval + X0;
        yval = yval + Y0;
        return new Point(xval, yval);
    }


    /**
     * 判断是否wgs84坐标
     * @param xCoordinates
     * @param yCoordinates
     * @return
     */
    public static boolean CheckIsWGS84Coordinates(double xCoordinates, double yCoordinates) {
        boolean res = false;
        if (xCoordinates < 360 && xCoordinates != 0 && yCoordinates < 360 && yCoordinates != 0) {
            res = true;
        }
        return res;
    }
    
	/**
     * 保留三位小数
     * @param para
     * @return
     */
    public double getBigDecimal(double para) {
        BigDecimal b = new BigDecimal(para);
        //3 为保留小数位数 四舍五入
        double pi1 = b.setScale(3, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
        return pi1;
    }

	/**
     * 保留三位小数
     * 测试方法
     * @return
     */
	@Test
    void test4() {
        /*String path = "E:\\dev\\cgdb\\CompressedPackage\\DB\\20220331154011253\\420322郧西县\\1716641\\(420322)郧西县_20220331154011253.db";
        //DBUtil.setConnection(path);
        Integer integer = DBUtil.selectTBCount(path);
        System.out.println(integer);*/
        Point point = WGS84ToCGS2000(111.591111974096, 32.3265852655767);
        double bigDecimal = getBigDecimal(point.getX());
        double bigDecimal1 = getBigDecimal(point.getY());
        System.out.println(bigDecimal);
        System.out.println(bigDecimal1);

        System.out.println("X:" + point.getX() + "      Y:" + point.getY());

    }
public class EarthParam {

    /**
     * 长轴 eg:6378137
     */
    //private double A;

    /**
     * 扁心率 eg:1 / 298.257223563
     */
    //private double F;

    public class CGS2000{
        /**
         * 精确 =
         */
        public static final double A = 6378137;
        /**
         * 模糊 like
         */
        public static final double F = 1.0 / 298.257222101;
    }

    public class WGS84{
        /**
         * 精确 =
         */
        public static final double A = 6378137;
        /**
         * 模糊 like
         */
        public static final double F = 1.0 / 298.257223563;
    }

    public class XIAN80{
        /**
         * 精确 =
         */
        public static final double A = 6378140;
        /**
         * 模糊 like
         */
        public static final double F = 1.0 / 298.25722101;
    }

    public class BJ54{
        /**
         * 精确 =
         */
        public static final double A = 6378245.0;
        /**
         * 模糊 like
         */
        public static final double F = 1.0 / 298.257;
    }
举报

相关推荐

WGS84坐标系转换到J2000坐标系

0 条评论