Map coordinate conversion (Mars coordinate system, baidu coordinate system, Google, Tencent, Gaode, etc.)

Posted by Jeremias on Mon, 13 Dec 2021 04:18:53 +0100

Good article record

  1. Conceptual interpretation
    First, find out what Mars coordinates are? What coordinate systems are there? What is the origin?

1.1 interpretation of terms
Coordinate system: a system used for positioning. Just like the two-dimensional Cartesian coordinate system, the unique position of a point in the Cartesian coordinate system can be determined by using (x,y). The coordinate system mentioned here is much more complex than the Cartesian coordinate system, but its function is the same. It is mainly used for positioning, that is, accurately positioning a point on the surface.
Geographic coordinate system: WGS84 is a geographic coordinate system. Geographic coordinate is a simple geometric modeling of the earth. For example, the earth is regarded as a sphere or quasi sphere, and then the points on the surface are projected onto the sphere to form a geographic coordinate system. WGS84 defines how to abstract the earth into a sphere or a sphere like rule. Or simply put, WGS84 is a set of parameters used to establish a sphere or quasi sphere to approximate the earth.
Projection coordinate system: since the earth is a sphere, a certain area of the earth is generally projected on a plane, and the resulting coordinate system is called the projection coordinate system.

1.2 abbreviation and interpretation
WGS84: geographic coordinate system, which is used by Google Earth and Google Map outside China. In addition, at present, almost all devices positioning spatial positions use this coordinate system, such as the GPS system of mobile phones.
GCJ-02: the projection coordinate system, which is commonly referred to as the Mars coordinate system, seems to be used by Google Map China, Gaode and Tencent. This is encrypted by China on the basis of WGS84. The purpose is obvious.
BD09: projection coordinate system, which is used by Baidu map and encrypted twice on the basis of GCJ-02.

Comparison of map API coordinate systems in China

API coordinate system
Baidu map API Baidu coordinates
Tencent search map API Mars coordinates
Sohu Sogou map API Sogou coordinates
Alibaba cloud map API Mars coordinates
Map bar map API coordinates
Gaode MapABC map API Mars coordinates
Lingtu 51ditu map API Mars coordinates
2. Conversion method
After we know the meaning of these nouns and the coordinate system corresponding to API, the next step is to find the method of coordinate system transformation.
1. It can be converted by direct algorithm
2. It can be converted through Web API
3. It can be converted through the third-party SDK API

2.1 algorithm conversion
GPS.java

package com.itbird.myapplication;

/**
 * Coordinate object, composed of longitude and latitude
 * Created by xfkang on 2018/3/28.
 */

public class GPS {
    private double lat;
    private double lon;

    public GPS(double lat, double lon) {
        this.lat = lat;
        this.lon = lon;
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLon() {
        return lon;
    }

    public void setLon(double lon) {
        this.lon = lon;
    }

    public String toString() {
        return "lat:" + lat + "," + "lon:" + lon;
    }
}

GPSConverterUtils.java

package com.itbird.myapplication;

/**
 * Coordinate conversion tool class
 * WGS84: Google Earth Adopted, Google Map is used outside China
 * GCJ02: Mars coordinate system, the coordinate system formulated by the State Bureau of Surveying and mapping of China, is classified by WGS84. Google Map China and search map use, Gaode
 * BD09:Baidu coordinate, GCJ02 confidential coordinate system
 * Sogou coordinate system, graph bar coordinates, etc. are estimated to be encrypted on the basis of GCJ02
 * Created by xfkang on 2018/3/28.
 */

public class GPSConverterUtils {
    public static final String BAIDU_LBS_TYPE = "bd09ll";
    public static double pi = 3.1415926535897932384626;
    public static double a = 6378245.0;
    public static double ee = 0.00669342162296594323;

    /**
     * 84 to Mars coordinate system (gcj-02) world geodetic system = = > Mars geodetic system
     * @param lat
     * @param lon
     */
    public static GPS gps84_To_Gcj02(double lat, double lon) {
        if (outOfChina(lat, lon)) {
            return null;
        }
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        return new GPS(mgLat, mgLon);
    }

    /**
     * * Mars coordinate system (GCJ-02) to 84 * * @param lon * @param lat * @return
     */
    public static GPS gcj_To_Gps84(double lat, double lon) {
        GPS gps = transform(lat, lon);
        double lontitude = lon * 2 - gps.getLon();
        double latitude = lat * 2 - gps.getLat();
        return new GPS(latitude, lontitude);
    }

    /**
     * The conversion algorithm between Mars coordinate system (GCJ-02) and Baidu coordinate system (BD-09) converts GCJ-02 coordinates into BD-09 coordinates
     *
     * @param gg_lat
     * @param gg_lon
     */
    public static GPS gcj02_To_Bd09(double gg_lat, double gg_lon) {
        double x = gg_lon, y = gg_lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * pi);
        double bd_lon = z * Math.cos(theta) + 0.0065;
        double bd_lat = z * Math.sin(theta) + 0.006;
        return new GPS(bd_lat, bd_lon);
    }

    /**
     * * Conversion algorithm between Mars coordinate system (GCJ-02) and Baidu coordinate system (BD-09) * convert BD-09 coordinates into GCJ-02 coordinates * * @ param
     * bd_lat * @param bd_lon * @return
     */
    public static GPS bd09_To_Gcj02(double bd_lat, double bd_lon) {
        double x = bd_lon - 0.0065, y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);
        double gg_lon = z * Math.cos(theta);
        double gg_lat = z * Math.sin(theta);
        return new GPS(gg_lat, gg_lon);
    }

    /**
     * (BD-09)-->84
     * @param bd_lat
     * @param bd_lon
     * @return
     */
    public static GPS bd09_To_Gps84(double bd_lat, double bd_lon) {

        GPS gcj02 = bd09_To_Gcj02(bd_lat, bd_lon);
        GPS map84 = gcj_To_Gps84(gcj02.getLat(),
                gcj02.getLon());
        return map84;

    }

    /**
     * is or not outOfChina
     * @param lat
     * @param lon
     * @return
     */
    public static boolean outOfChina(double lat, double lon) {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }

    public static GPS transform(double lat, double lon) {
        if (outOfChina(lat, lon)) {
            return new GPS(lat, lon);
        }
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        return new GPS(mgLat, mgLon);
    }

    public 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 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }


    public 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
                * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
                * pi)) * 2.0 / 3.0;
        return ret;
    }
}

2.2 conversion through Web API
Gaud coordinate transformation Web API
Online API documentation: http://lbs.amap.com/api/webservice/guide/api/convert/
Applicable scenarios:
In order to use Gaode service, only non Gaode coordinates can be converted to Gaode coordinates
instructions:
Step 1: apply for the "Web Service API" Key;
The second step is to splice the HTTP request URL. The Key applied in the first step must be sent together as a required parameter;
The third step is to receive the data returned by the HTTP request (JSON or XML format) and parse the data.
Unless otherwise stated, the input parameters and output data codes of the interface are unified as UTF-8.
Use example:
http://restapi.amap.com/v3/assistant/coordinate/convert?locations=116.481499 , 39.990475 & coordsys = GPS & output = XML & key = < user's key >
Example of Gaud coordinate conversion API png
Baidu coordinate conversion Web API
Online api documentation: http://lbsyun.baidu.com/index.php?title=webapi/guide/changeposition
Applicable scenarios:
Support multiple coordinate conversion
Baidu coordinate conversion webapi png
GPS coordinate conversion Web API
Online API documentation: http://www.gpsspg.com/api/convert/latlng/
Applicable scenarios:
Support multiple coordinate conversion
GPS coordinate conversion webapi png
2.3 API conversion through SDK
Gaode sdk api conversion
Online API documentation: http://lbs.amap.com/api/android-sdk/guide/computing-equipment/coordinate-transformation/
Gaode sdk api conversion png
Baidu sdk api conversion
Online api documentation: http://lbsyun.baidu.com/index.php?title=androidsdk/guide/tool/coordinate
Baidu sdk api conversion png
Summary:
So far, the introduction of the origin of several map coordinate systems and several methods of mutual conversion have been introduced. Select the appropriate method for your project according to the situation and complete the coordinate conversion.

Reprint website: https://www.jianshu.com/p/c39a2c72dc65?from=singlemessage

Topics: Java