java js longitude and latitude conversion, geodetic coordinates (Gaussian projection coordinates) and longitude and latitude conversion to each other

In the project, there is a need to convert between geodetic coordinates (Gaussian projection coordinates) and longitude and latitude. I wrote a tool class;

There are java and js codes

As shown in the figure, the coordinate system and bandwidth have been passed into the method as parameters in the java code. When using it, you only need to call different methods:

The js side does not take the coordinate system and bandwidth as parameters. If necessary, you can replace the comment place or modify it to the parameter passing method:

Next, post the code:

java:

CoordinateUtil.java
package coordinate;

/**
 * @date 2023/9/27
 */

public class CoordinateUtil {

    private CoordinateUtil() {
    }

    /**
     * Geodetic coordinates (Gaussian projection coordinates) are converted into longitude and latitude 3-degree zone Xi'an 80 coordinate system
     *
     * @param x x coordinate
     * @param y y coordinate
     * @return Returns the latitude and longitude object
     */
    public static Coordinate zoneWide3Xian80ToLatitudeAndLongitude(double x, double y) {
        return gaussToLatitudeAndLongitude(x, y, "80", 3);
    }

    /**
     * Longitude and latitude 3-degree zone Xi'an 80 coordinate system is converted into geodetic coordinates (Gaussian projection coordinates)
     *
     * @param x x coordinate
     * @param y y coordinate
     * @return Returns the latitude and longitude object
     */
    public static Coordinate latitudeAndLongitudeToZoneWide3Xian80(double x, double y) {
        return latitudeAndLongitudeToGauss(x, y, "80", 3);
    }


    /**
     * Geodetic coordinates (Gaussian projection coordinates) are converted into longitude and latitude 6 degree zone Xi'an 80 coordinate system
     *
     * @param x x coordinate
     * @param y y coordinate
     * @return Returns the latitude and longitude object
     */
    public static Coordinate zoneWide6Xian80ToLatitudeAndLongitude(double x, double y) {
        return gaussToLatitudeAndLongitude(x, y, "80", 6);
    }

    /**
     * Longitude and latitude 6 degree zone Xi'an 80 coordinate system converted into geodetic coordinates (Gaussian projection coordinates)
     *
     * @param x x coordinate
     * @param y y coordinate
     * @return Returns the latitude and longitude object
     */
    public static Coordinate latitudeAndLongitudeToZoneWide6Xian80(double x, double y) {
        return latitudeAndLongitudeToGauss(x, y, "80", 6);
    }

    /**
     * Geodetic coordinates (Gaussian projection coordinates) are converted into longitude and latitude 3-degree zone Beijing 54 coordinate system
     *
     * @param x x coordinate
     * @param y y coordinate
     * @return Returns the latitude and longitude object
     */
    public static Coordinate zoneWide3BeiJing54ToLatitudeAndLongitude(double x, double y) {
        return gaussToLatitudeAndLongitude(x, y, "54", 3);
    }

    /**
     * Longitude and latitude 3-degree zone Beijing 54 coordinate system converted into geodetic coordinates (Gaussian projection coordinates)
     *
     * @param x x coordinate
     * @param y y coordinate
     * @return Returns the latitude and longitude object
     */
    public static Coordinate latitudeAndLongitudeToZoneWide3BeiJing54(double x, double y) {
        return latitudeAndLongitudeToGauss(x, y, "54", 3);
    }


    /**
     * Geodetic coordinates (Gaussian projection coordinates) are converted into longitude and latitude 6 degree zone Beijing 54 coordinate system
     *
     * @param x x coordinate
     * @param y y coordinate
     * @return Returns the latitude and longitude object
     */
    public static Coordinate zoneWide6BeiJing54ToLatitudeAndLongitude(double x, double y) {
        return gaussToLatitudeAndLongitude(x, y, "54", 6);
    }

    /**
     * Longitude and latitude 6 degree zone Beijing 54 coordinate system converted into geodetic coordinates (Gaussian projection coordinates)
     *
     * @param x x coordinate
     * @param y y coordinate
     * @return Returns the latitude and longitude object
     */
    public static Coordinate latitudeAndLongitudeToZoneWide6BeiJing54(double x, double y) {
        return gaussToLatitudeAndLongitude(x, y, "54", 6);
    }


    /**
     * Convert geodetic coordinates (Gaussian projection coordinates) into longitude and latitude
     *
     * @param x x coordinate
     * @param y y coordinate
     * @param zbx coordinate system, "54": Beijing coordinate system parameters in 1954,
     * "80": Xi'an coordinate system parameters in 1980
     * @param zoneWide bandwidth, value 3, 6
     * @return Returns the latitude and longitude object
     */
    public static Coordinate gaussToLatitudeAndLongitude(double x, double y, String zbx, int zoneWide) {

        double f, a;

        double iPI = Math.PI / 180;

        if (zbx.contains("54")) {
            a = 6378245.0;
            f = 1.0 / 298.3; //Beijing coordinate system parameters in 1954
        } else if (zbx.contains("80")) {
            a = 6378140.0;
            f = 1 / 298.257; //Xi’an coordinate system parameters in 1980
        } else {
            throw new IllegalArgumentException("The coordinate system zbx is wrong, it can only be 80, 54");
        }

        int projNo = (int) (x / 1000000L); //Find the band number

        double longitude0 = 0; // ((projNo - 1) * zoneWide + (zoneWide >> 1)) * iPI; //Central meridian

        if (zoneWide == 3) {
            longitude0 = 3 * projNo * iPI;
        } else if (zoneWide == 6) {
            longitude0 = (6 * projNo - 3) * iPI;
        }

        double X0 = projNo * 1000000L + 500000L;
        double Y0 = 0;

        double xval = x - X0;
        double yval = y - Y0; //In-band geodetic coordinates

        double e2 = 2 * f - f * f;

        double e1 = (1.0 - Math.sqrt(1 - e2)) / (1.0 + Math.sqrt(1 - e2));

        double ee = e2 / (1 - e2);
        double u = yval / (a * (1 - e2 / 4 - 3 * Math.pow(e2, 2) / 64 - 5 * Math.pow(e2, 3) / 256));

        double fai = u
                 + (3 * e1 / 2 - 27 * Math.pow(e1, 3) / 32) * Math.sin(2 * u)
                 + (21 * Math.pow(e1, 2) / 16 - 55 * Math.pow(e1, 4) / 32) * Math.sin(4 * u)
                 + (151 * Math.pow(e1, 3) / 96) * Math.sin(6 * u)
                 + (1097 * Math.pow(e1, 4) / 512) * Math.sin(8 * u);

        double c = ee * Math.pow(Math.cos(fai), 2);
        double t = Math.pow(Math.tan(fai), 2);

        double temp = 1.0 - e2 * Math.pow(Math.sin(fai), 2);

        double nn = a / Math.sqrt(temp);
        double r = a * (1 - e2) / Math.sqrt(Math.pow((temp), 3));

        double d = xval / nn;

        //Calculate longitude (Longitude) latitude (Latitude)
        double longitude1 = longitude0
                 + (d - (1 + 2 * t + c) * Math.pow(d, 3) / 6
                 + (5 - 2 * c + 28 * t - 3 * c * c + 8 * ee + 24 * t * t) * Math.pow(d, 5) / 120) / Math.cos(fai);

        double latitude1 = fai
                - (nn * Math.tan(fai) / r) * (d * d / 2 - (5 + 3 * t + 10 * c - 4 * c * c - 9 * ee) * Math.pow(d, 4 ) / twenty four
                 + (61 + 90 * t + 298 * c + 45 * t * t - 256 * ee - 3 * c * c) * Math.pow(d, 6) / 720);

        //convert to degrees DD
        double longitude = longitude1 / iPI;
        double latitude = latitude1 / iPI;

        return new Coordinate() {
            @Override
            public double getLongitude() {
                return longitude;
            }

            @Override
            public double getLatitude() {
                return latitude;
            }
        };

    }

    /**
     * Convert longitude and latitude into geodetic coordinates (Gaussian projection coordinates)
     *
     * @param longitude
     * @param latitude
     * @param zbx coordinate system, "54": Beijing coordinate system parameters in 1954,
     * "80": Xi'an coordinate system parameters in 1980
     * @param zoneWide bandwidth, value 3, 6
     * @return Gaussian projection coordinates
     */
    public static Coordinate latitudeAndLongitudeToGauss(double longitude, double latitude, String zbx, int zoneWide) { //Latitude and longitude => Gaussian projection
        int ProjNo = 0;
        double longitude1, latitude1, longitude0 = 0, latitude0, X0, Y0, xval, yval;
        double a, f, e2, ee, NN, T, C, A, M, iPI;
        iPI = 3.14159265358979324 / 180.0; // 3.1415926535898 / 180.0;
        if (zbx.contains("54")) {
            a = 6378245.0;
            f = 1.0 / 298.3; //Beijing coordinate system parameters in 1954
        } else if (zbx.contains("80")) {
            a = 6378140.0;
            f = 1 / 298.257; //Xi'an coordinate system parameters in 1980
        } else {
            throw new IllegalArgumentException("The coordinate system zbx is wrong, it can only be 80, 54");
        }

        ProjNo = (int) (longitude / zoneWide);
        if (zoneWide == 3) {
            longitude0 = ProjNo * zoneWide + zoneWide;
        } else if (zoneWide == 6) {
            longitude0 = ProjNo * zoneWide + zoneWide / 2;

        }
        longitude0 = longitude0 * iPI;
        latitude0 = 0;
        longitude1 = longitude * iPI; // Convert longitude to radians
        latitude1 = latitude * iPI; // Convert latitude to radians
        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 = 1000000 * (ProjNo + 1) + 500000;
        Y0 = 0;
        xval = xval + X0;
        yval = yval + Y0;

        double finalXval = xval;
        double finalYval = yval;
        return new Coordinate() {
            @Override
            public double getLongitude() {
                return finalXval;
            }

            @Override
            public double getLatitude() {
                return finalYval;
            }
        };
    }
}
Coordinate.java
package coordinate;


/**
 *
 * @date 2023/9/27
 */

public interface Coordinate {

    double getLongitude();

    double getLatitude();
}

js:

location.js

// JS geodetic coordinates and latitude and longitude coordinates are converted to each other
export function GaussToBL(X, Y) { // Gaussian projection coordinates are back calculated into latitude and longitude
    letProjNo;
    let ZoneWide;
    let output = new Array(2);
    let longitude1, latitude1, longitude0, X0, Y0, xval, yval;
    let e1, e2, f, a, ee, NN, T, C, M, D, R, u, fai, iPI;
    iPI = 3.14159265358979324 / 180.0; // 3.1415926535898 / 180.0;
    // a = 6378245.0; f = 1.0/298.3; //Beijing coordinate system parameters in 1954
    a = 6378140.0;
    f = 1.0 / 298.257; //Xi’an coordinate system parameters in 1980
    ZoneWide = 3;
    ProjNo = parseInt(X / 1000000);
    longitude0 = ProjNo * iPI * ZoneWide;

    X0 = ProjNo * 1000000 + 500000;
    Y0 = 0;
    xval = X - X0;
    yval = Y - Y0;
    e2 = 2 * f - f * f;
    e1 = (1.0 - Math.sqrt(1 - e2)) / (1.0 + Math.sqrt(1 - e2));
    ee = e2 / (1 - e2);
    M = yval;
    u = M / (a * (1 - e2 / 4 - 3 * e2 * e2 / 64 - 5 * e2 * e2 * e2 / 256));
    fai = u + (3 * e1 / 2 - 27 * e1 * e1 * e1 / 32) * Math.sin(2 * u) + (21 * e1 * e1 / 16 - 55 * e1 * e1 *
        e1 * e1 / 32) * Math.sin(4 * u) + (151 * e1 * e1 * e1 / 96) * Math.sin(6 * u) + (1097 * e1 *
        e1 * e1 * e1 / 512) * Math.sin(8 * u);
    C = ee * Math.cos(fai) * Math.cos(fai);
    T = Math.tan(fai) * Math.tan(fai);
    NN = a / Math.sqrt(1.0 - e2 * Math.sin(fai) * Math.sin(fai));
    R = a * (1 - e2) / Math.sqrt((1 - e2 * Math.sin(fai) * Math.sin(fai)) * (1 - e2 * Math.sin(fai) * Math
        .sin(fai)) * (1 - e2 * Math.sin(fai) * Math.sin(fai)));
    D = xval/NN;
    // Calculate longitude (Longitude) latitude (Latitude)
    longitude1 = longitude0 + (D - (1 + 2 * T + C) * D * D * D / 6 + (5 - 2 * C + 28 * T - 3 * C * C + 8 *
        ee + 24 * T * T) * D * D * D * D * D / 120) / Math.cos(fai);
    latitude1 = fai - (NN * Math.tan(fai) / R) * (D * D / 2 - (5 + 3 * T + 10 * C - 4 * C * C - 9 * ee) *
        D * D * D * D / 24 + (61 + 90 * T + 298 * C + 45 * T * T - 256 * ee - 3 * C * C) * D * D * D *
        D * D * D / 720);
    //Convert to degrees DD
    output[0] = longitude1 / iPI;
    output[1] = latitude1 / iPI;
    return output;
}

export function BLToGauss(longitude, latitude) { //Latitude and longitude => Gaussian projection
    let ProjNo = 0;
    let ZoneWide;
    let ret = Array(2);
    let longitude1, latitude1, longitude0, latitude0, X0, Y0, xval, yval;
    let a, f, e2, ee, NN, T, C, A, M, iPI;
    iPI = 3.14159265358979324 / 180.0; // 3.1415926535898 / 180.0;
    // a=6378245.0; f=1.0/298.3; //Beijing coordinate system parameters in 1954
    a = 6378140.0;
    f = 1 / 298.257; //Xi’an coordinate system parameters in 1980
    ZoneWide = 3;
    ProjNo = parseInt(longitude / ZoneWide);
    longitude0 = ProjNo * ZoneWide + ZoneWide;
    longitude0 = longitude0 * iPI;
    latitude0 = 0;
    longitude1 = longitude * iPI; // Convert longitude to radians
    latitude1 = latitude * iPI; // Convert latitude to radians
    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 = 1000000 * (ProjNo + 1) + 500000;
    Y0 = 0;
    xval = xval + X0;
    yval = yval + Y0;
    ret[0] = xval;
    ret[1] = yval;

    return ret;
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 137269 people are learning the system