Coordinate transformation Utils

Coordinate transformation Utils:

import static java.lang.StrictMath.abs;
import static java.lang.StrictMath.atan2;
import static java.lang.StrictMath.cos;
import static java.lang.StrictMath.sin;
import static java.lang.StrictMath.sqrt;

/**
 * Coordinate system conversion tool
 * @author dsc
 *
 */
public class CoordinateUtil {

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

    private static boolean outOfChina(double lat, double lon) {
        return lon < 72.004 || lon > 137.8347 || lat < 0.8293 || lat > 55.8271;

    }

    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 * sqrt(abs(x));
        stay + = (20.0 * sin (6.0 * x * PI) + 20.0 * sin (2.0 * x * most)) * 2.0 / 3.0 ;
        ret + = (20.0 * sin (y * pi) + 40.0 * sin (y / 3.0 * pi)) * 2.0 / 3.0 ;
        ret + = (160.0 * sin (y / 12.0 * pi) + 320 * sin (pi * and / 30.0)) * 2.0 / 3.0 ;

        Return the right;
    }

    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 * sqrt(abs(x));
        stay + = (20.0 * sin (6.0 * x * PI) + 20.0 * sin (2.0 * x * most)) * 2.0 / 3.0 ;
        stay + = (20.0 * sin (x * PI) + 40.0 * sin (x / 3.0 * most)) * 2.0 / 3.0 ;
        stay + = (150.0 * sin (x / 12.0 * more) + 300.0 * sin (x / 30.0 * most)) * 2.0 / 3.0 ;

        Return the right;
    }

    /**
     * Earth coordinates into coordinates Mars
     * World Geodetic System ==> Mars Geodetic System
     * Map: latitude,longitude
     *
     * @Param MGS Earth coordinates Coordinate
     * @return  Coordinate 火星坐标
     */
    public static Coordinate transform2Mars(Coordinate mgs) {
        Coordinate marsCoordinate = getCoordinateInstance();

        double wgLat = mgs.getLatitude();
        double wgLon = mgs.getLongitude();

        if (CoordinateUtil.outOfChina(wgLat, wgLon)) {
            marsCoordinate.setLongitude (wgLon);
            marsCoordinate.setLatitude (wgLat);

            return marsCoordinate;
        }

        // earth coordinate to a coordinate Mars algorithm 
        Double dlat = transformLat (wgLon - 105.0, wgLat - 35.0 );
         Double dlon = transformLon (wgLon - 105.0, wgLat - 35.0 );
         Double radLat = wgLat / 180.0 * PI;
         Double Magic = SiN ( radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (* dLon 180.0) / (a / sqrtMagic * cos (radLat) * pi);

        marsCoordinate.setLongitude (wgLon + dLon);
        marsCoordinate.setLatitude (wgLat + dLat);

        return marsCoordinate;
    }

    /**
     * Mars coordinates to coordinate Baidu
     * Mars Geodetic System ==> BaiDu Geodetic System
     * Map: latitude,longitude
     *
     * @Param MGS Mars coordinates Coordinate
     * @return  Coordinate 百度坐标
     */
    public static Coordinate bdEncrypt(Coordinate mgs) {
        Coordinate bdCoordinate = getCoordinateInstance();

        double x = mgs.getLongitude();
        double y = mgs.getLatitude();
        double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
        double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);

        bdCoordinate.setLongitude(z * cos(theta) + 0.0065);
        bdCoordinate.setLatitude(z * sin(theta) + 0.006);

        return bdCoordinate;
    }

    /**
     * Baidu coordinate to a coordinate Mars
     * BaiDu Geodetic System ==> Mars Geodetic System
     *
     * @Param BGS coordinates Coordinate Baidu
     * @return  Coordinate 火星坐标
     */
    public static Coordinate bdDecrypt(Coordinate bgs) {
        Coordinate marsCoordinate = getCoordinateInstance();

        double x = bgs.getLongitude() - 0.0065;
        double y = bgs.getLatitude() - 0.006;
        double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
        double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);

        marsCoordinate.setLongitude (z * cos (theta));
        marsCoordinate.setLatitude(z * sin(theta));

        return marsCoordinate;
    }
    public static Coordinate getCoordinateInstance(){
        return _COORDINATE_INSTANCE;
    }
    private static Coordinate _COORDINATE_INSTANCE = new CoordinateUtil().new Coordinate();
    private  class Coordinate{
        private double longitude;
        private double latitude;
        public double getLongitude() {
            return longitude;
        }
        public void setLongitude(double longitude) {
            this.longitude = longitude;
        }
        public double getLatitude() {
            return latitude;
        }
        public void setLatitude(double latitude) {
            this.latitude = latitude;
        }
    }

    public  static  void main (String [] args) {
         // Baidu coordinate
 //         116.40727,39.978862
         // High German coordinate
 //         116.400897,39.972546 

        the Coordinate Coordinate = CoordinateUtil.getCoordinateInstance ();
        coordinate.setLongitude(106.1013515);
        coordinate.setLatitude(30.758687);

        Coordinate marsCoordinate = transform2Mars(coordinate);
        System.out.println(marsCoordinate.getLongitude());
        System.out.println(marsCoordinate.getLatitude());
    }
}

Guess you like

Origin www.cnblogs.com/wangquanyi/p/12106523.html