WGS84 Earth coordinate system, GCJ02 Mars coordinate system, BD09 Baidu coordinate system introduction and conversion data collection

Introduction to Wildfire ATGM332D

High-performance, low-power  GPS , Beidou dual-mode positioning module

STM32 GPS Positioning_In order to maintain world peace_'s blog-CSDN Blog

Binghuo multi-functional debugging assistant host computer open source! A total of six software, learn you spit... , - PC PC- Wildfire Electronics Forum- Powered by Discuz!
https://www.firebbs.cn/forum.php?mod=viewthread&tid=11985&fromuid=64Baidu

Map API Small Application Example_Baidu Map Usage Scenario 100 Characters Student_Haizhimu's Blog-CSDN Blog  Handle Practical Problems According to Baidu API

Coordinate Calculation Distance Formula Mars Coordinate System_Map Coordinate Conversion--Mars Coordinates and GPS Coordinates_Dark Night Hunter Vayne's Blog-CSDN Blog

Comparison of domestic map API coordinate systems - Rover.Tang - Blog Park
https://www.cnblogs.com/Tangf/archive/2012/03/15/2398397.html

GPS positioning and communication protocol analysis_gps protocol analysis_Convolutional Neural Network Blog-CSDN Blog

4. GPS module test software

The gps module can be tested with u-center test software, which can support serial port and network port.

 

5. Related software download
Digital Earth software download address: https://download.csdn.net/download/xipengbozai/19774250

GPS test software download address: https://download.csdn.net/download/xipengbozai/19774233

GPS data conversion tool: https://download.csdn.net/download/xipengbozai/19774213

Introduction and conversion of WGS84 Earth coordinate system, GCJ02 Mars coordinate system, BD09 Baidu coordinate system_wgs84 coordinate system and Baidu coordinate system_ThinkWon's Blog-CSDN Blog

Background: The positioning data obtained from GPS and Beidou satellite positioning use the WGS84 coordinate system, that is, the earth coordinate system. However, whether it is Gaode map or Baidu map in China, the WGS84 coordinate system is not used, so it needs to be converted before it can be used. , the conversion speed of the front-end method provided by Baidu API is relatively slow. Collect information through online search, and then write the code test by yourself, summarize the knowledge about the introduction and conversion of the coordinate system, and share it with everyone!
Article catalog
1. Introduction to each coordinate system
2. Conversion of each coordinate system
2.1 Coordinate point entity class
2.2 Conversion tool class of each coordinate system
3. Test
1. Introduction to each coordinate system
WGS84 coordinate system
is the earth coordinate system, a common coordinate system in the world.
The device generally contains a GPS chip or a Beidou chip to obtain the latitude and longitude as the WGS84 geographic coordinate system. Google Maps uses the WGS84 geographic coordinate system (except for China, Google China Map uses the GCJ02 geographic coordinate system.)

GCJ02 coordinate system
is the coordinate system of Mars and the encrypted coordinate system of WGS84 coordinate system.
For national security considerations, all domestic navigation electronic maps must use the encrypted coordinate system formulated by the State Bureau of Surveying and Mapping, that is, to encrypt a real latitude and longitude coordinate into an incorrect latitude and longitude coordinate.

BD09 coordinate system
is the encrypted coordinate system of Baidu coordinate system and GCJ02 coordinate system. The Sogou coordinate system, Tuba coordinate system, etc. are estimated to be encrypted on the basis of GCJ02.

Coordinate systems used by mainstream map APIs
AutoNavi MapABC map API Mars coordinates
Tencent Soso map API Mars coordinates
Alibaba Cloud map API Mars coordinates
Lingtu 51ditu map API Mars coordinates

Baidu Map API Baidu Coordinates
Sohu Sogou Map API Sogou Coordinates
MapBar MapBar Map API MapBar Coordinates

Here are a few commonly used online coordinate tools
Gaode open platform
Baidu picks the coordinate system

Online Coordinate Transformation Tool

2. Conversion of each coordinate system

2.1 Coordinate point entity class

/**
 * Description: 坐标点
 * 
 * @author JourWon
 * @date Created on 2018年6月19日
 */
public class Point implements Serializable {

	private static final long serialVersionUID = 3584864663880053897L;

	/**
	 * 经度
	 */
	private double lng;

	/**
	 * 纬度
	 */
	private double lat;
	

2.2 Various coordinate system conversion tools

package com.jourwon.util;

import com.jourwon.pojo.Point;

/**
 * Description: 各坐标系之间的转换工具类
 * 
 * @author JourWon
 * @date Created on 2018年6月19日
 */
public class CoordinateTransformUtils {

	// 圆周率π
	private static final double PI = 3.1415926535897932384626D;
	
	// 火星坐标系与百度坐标系转换的中间量
	private static final double X_PI = 3.14159265358979324 * 3000.0 / 180.0D;

	// Krasovsky 1940
	// 长半轴a = 6378245.0, 1/f = 298.3
	// b = a * (1 - f)
	// 扁率ee = (a^2 - b^2) / a^2;
	
	// 长半轴
	private static final double SEMI_MAJOR = 6378245.0D;
	
	// 扁率
	private static final double FLATTENING = 0.00669342162296594323D;
	
	// WGS84=>GCJ02 地球坐标系=>火星坐标系
	public static Point wgs84ToGcj02(double lng, double lat) {
		if (outOfChina(lng, lat)) {
			return new Point(lng, lat);
		}

		double[] offset = offset(lng, lat);
		double mglng = lng + offset[0];
		double mglat = lat + offset[1];

		return new Point(mglng, mglat);
	}

	// GCJ02=>WGS84 火星坐标系=>地球坐标系(粗略)
	public static Point gcj02ToWgs84(double lng, double lat) {
		if (outOfChina(lng, lat)) {
			return new Point(lng, lat);
		}

		double[] offset = offset(lng, lat);
		double mglng = lng - offset[0];
		double mglat = lat - offset[1];

		return new Point(mglng, mglat);
	}

	// GCJ02=>WGS84 火星坐标系=>地球坐标系(精确)
	public static Point gcj02ToWgs84Exactly(double lng, double lat) {
		if (outOfChina(lng, lat)) {
			return new Point(lng, lat);
		}
		
		double initDelta = 0.01;
		double threshold = 0.000000001;
		double dLat = initDelta, dLon = initDelta;
		double mLat = lat - dLat, mLon = lng - dLon;
		double pLat = lat + dLat, pLon = lng + dLon;
		double wgsLat, wgsLng, i = 0;
		while (true) {
			wgsLat = (mLat + pLat) / 2;
			wgsLng = (mLon + pLon) / 2;
			Point point = wgs84ToGcj02(wgsLng, wgsLat);
			dLon = point.getLng() - lng;
			dLat = point.getLat() - lat;
			if ((Math.abs(dLat) < threshold) && (Math.abs(dLon) < threshold))
				break;

			if (dLat > 0)
				pLat = wgsLat;
			else
				mLat = wgsLat;
			if (dLon > 0)
				pLon = wgsLng;
			else
				mLon = wgsLng;

			if (++i > 10000)
				break;
		}

		return new Point(wgsLng, wgsLat);
	}

	// GCJ-02=>BD09 火星坐标系=>百度坐标系
	public static Point gcj02ToBd09(double lng, double lat) {
		double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * X_PI);
		double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * X_PI);
		double bd_lng = z * Math.cos(theta) + 0.0065;
		double bd_lat = z * Math.sin(theta) + 0.006;
		return new Point(bd_lng, bd_lat);
	}

	// BD09=>GCJ-02 百度坐标系=>火星坐标系
	public static Point bd09ToGcj02(double lng, double lat) {
		double x = lng - 0.0065;
		double y = lat - 0.006;
		double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
		double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
		double gcj_lng = z * Math.cos(theta);
		double gcj_lat = z * Math.sin(theta);
		return new Point(gcj_lng, gcj_lat);
	}
	
	// WGS84=>BD09 地球坐标系=>百度坐标系
	public static Point wgs84ToBd09(double lng, double lat) {
		Point point = wgs84ToGcj02(lng, lat);
		return gcj02ToBd09(point.getLng(), point.getLat());
	}

	// BD09=>WGS84 百度坐标系=>地球坐标系
	public static Point bd09ToWgs84(double lng, double lat) {
		Point point = bd09ToGcj02(lng, lat);
		return gcj02ToWgs84(point.getLng(), point.getLat());
	}

	/**
	 * Description: 中国境外返回true,境内返回false
	 * @param lng 	经度
	 * @param lat	纬度
	 * @return
	 */
	public static boolean outOfChina(double lng, double lat) {
		if (lng < 72.004 || lng > 137.8347)
			return true;
		if (lat < 0.8293 || lat > 55.8271)
			return true;
		return false;
	}

	// 经度偏移量
	private static double transformLng(double lng, double lat) {
		double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
		ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
		ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
		ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
		return ret;
	}
	
	// 纬度偏移量
	private static double transformLat(double lng, double lat) {
		double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat
				+ 0.2 * Math.sqrt(Math.abs(lng));
		ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
		ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
		ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
		return ret;
	}
	
	// 偏移量
	public static double[] offset(double lng, double lat) {
		double[] lngLat = new double[2];
		double dlng = transformLng(lng - 105.0, lat - 35.0);
		double dlat = transformLat(lng - 105.0, lat - 35.0);
		double radlat = lat / 180.0 * PI;
		double magic = Math.sin(radlat);
		magic = 1 - FLATTENING * magic * magic;
		double sqrtmagic = Math.sqrt(magic);
		dlng = (dlng * 180.0) / (SEMI_MAJOR / sqrtmagic * Math.cos(radlat) * PI);
		dlat = (dlat * 180.0) / ((SEMI_MAJOR * (1 - FLATTENING)) / (magic * sqrtmagic) * PI);
		lngLat[0] = dlng;
		lngLat[1] = dlat;
		return lngLat;
	}

}

3. Test

/**
 * Description: 坐标系转换工具测试类
 * 
 * @author JourWon
 * @date Created on 2018年6月19日
 */
public class CoordinateTransformUtilsTest {

	/**
	 * Description: 地球坐标系 =>火星坐标系、百度坐标系
	 */
	@Test
	public void test01() {
		// 广州市中大地铁站
		Point point = new Point(113.28749670783887D, 23.094783676708065D);
		System.out.println("地球坐标系 : " + point);

		Point wgs84ToGcj02 = CoordinateTransformUtils.wgs84ToGcj02(point.getLng(), point.getLat());
		System.out.println("火星坐标系 : " + wgs84ToGcj02);

		Point wgs84ToBd09 = CoordinateTransformUtils.wgs84ToBd09(point.getLng(), point.getLat());
		System.out.println("百度坐标系 : " + wgs84ToBd09);
	}

	/**
	 * Description: 火星坐标系=>地球坐标系、百度坐标系
	 */
	@Test
	public void test02() {
		// 广州市珠江新城地铁站
		Point point = new Point(113.321171D, 23.119285D);
		System.out.println("火星坐标系 : " + point);

		Point wgs84ToGcj02 = CoordinateTransformUtils.gcj02ToWgs84(point.getLng(), point.getLat());
		System.out.println("地球坐标系 : " + wgs84ToGcj02);

		Point wgs84ToBd09 = CoordinateTransformUtils.gcj02ToBd09(point.getLng(), point.getLat());
		System.out.println("百度坐标系 : " + wgs84ToBd09);
	}

	/**
	 * Description: 百度坐标系=>地球坐标系、火星坐标系
	 */
	@Test
	public void test03() {
		// 广州市体育西路地铁站
		Point point = new Point(113.328035D, 23.136929D);
		System.out.println("百度坐标系 : " + point);

		Point wgs84ToGcj02 = CoordinateTransformUtils.bd09ToWgs84(point.getLng(), point.getLat());
		System.out.println("地球坐标系 : " + wgs84ToGcj02);

		Point wgs84ToBd09 = CoordinateTransformUtils.bd09ToGcj02(point.getLng(), point.getLat());
		System.out.println("火星坐标系 : " + wgs84ToBd09);
	}

}

Complete code download link

Baidu coordinates should be transferred to GCJ-02 first, and then transferred to WGS84

(1) WGS84: indicates the coordinates obtained by GPS;

(2) GCJ02: The coordinate system of the geographic information system formulated by the State Bureau of Surveying and Mapping of China . The coordinate system encrypted by the WGS84 coordinate system;

(3) BD09: Baidu coordinate system, encrypted again on the basis of GCJ02 coordinate system. Among them, BD09II represents Baidu longitude and latitude coordinates, and BD09MC represents Baidu Mercator metric coordinates.

public class Transform {
    double x_PI = 3.14159265358979324 * 3000.0 / 180.0;
    double PI = 3.1415926535897932384626;
    double a = 6378245.0;
    double ee = 0.00669342162296594323;
 
    /**
     * 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
     * 即 百度 转 谷歌、高德
     * @param bd_lon
     * @param bd_lat
     * @returns {*[]}
     */
    public Point bd09togcj02(double bd_lon, double bd_lat){
        double x = bd_lon - 0.0065;
        double y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_PI);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_PI);
        double gg_lng = z * Math.cos(theta);
        double gg_lat = z * Math.sin(theta);
        Point point=new Point(gg_lng, gg_lat);
        return point;
    }
 
    /**
     * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
     * 即谷歌、高德 转 百度
     * @param lng
     * @param lat
     * @returns {*[]}
     */
    public Point gcj02tobd09(double lng, double lat){
        double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
        double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
        double bd_lng = z * Math.cos(theta) + 0.0065;
        double bd_lat = z * Math.sin(theta) + 0.006;
        Point point=new Point(bd_lng, bd_lat);
        return point;
    };
 
    /**
     * WGS84转GCj02
     * @param lng
     * @param lat
     * @returns {*[]}
     */
    public Point wgs84togcj02(double lng, double lat){
        double dlat = transformlat(lng - 105.0, lat - 35.0);
        double dlng = transformlng(lng - 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);
        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
        double mglat = lat + dlat;
        double mglng = lng + dlng;
        Point point=new Point(mglng, mglat);
        return point;
    };
 
    /**
     * GCJ02 转换为 WGS84
     * @param lng
     * @param lat
     * @returns {*[]}
     */
    public Point gcj02towgs84(double lng, double lat){
        double dlat = transformlat(lng - 105.0, lat - 35.0);
        double dlng = transformlng(lng - 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);
        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
        double mglat = lat + dlat;
        double mglng = lng + dlng;
        Point point=new Point(mglng, mglat);
        return point;
    };
 
 
    private double transformlat(double lng,double lat){
        double ret= -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
        ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }
 
    private double transformlng(double lng,double lat){
        double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
        ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
        return ret;
    }
 
}

Guess you like

Origin blog.csdn.net/m0_37777700/article/details/132421014