Java实现经纬度坐标转换
目录
- 一、坐标系统简介
- 1.经纬度坐标系
- 2.坐标系统
- 3.坐标转换简介
- 二、地图经纬度转换工具类(CGCS2000坐标 与 WGS84坐标 互转)
- 1.对于 CGCS2000 需要引入 proj4j依赖:
- 2.坐标转换工具类
- 三、地图经纬度转换工具类(gps84 、GCJ-02、 BD-09)
一、坐标系统简介
坐标是描述位置的一组数值,按坐标的维度一般分为一维坐标(公路里程碑)和二维坐标(笛卡尔平面直角坐标、高斯平面直角坐标)、三维坐标(大地坐标、空间直角坐标)
。
为了描述或确定位置,必须建立坐标系统,坐标只有存在于某个坐标系统才有实际的意义与具体的位置。
地球是一个球体,球面上的位置,是以经纬度
来表示,它称为“球面坐标系统”或“地理坐标系统”。
1.经纬度坐标系
经纬度坐标系是一种地理坐标系统,用于描述地球表面上任意位置的坐标。它是基于地球的自转和赤道的划分而建立的。
-
经度(Longitude):表示地球表面上一个点相对于本初子午线的东西方向的位置。
经度的度量单位是度(°),范围从0°到180°,以东经为正值,西经为负值。
本初子午线位于英国伦敦的皇家格林尼治天文台,它被定义为经度0°。 -
纬度(Latitude):
表示地球表面上一个点相对于赤道的北南方向的位置。纬度的度量单位也是度(°),范围从0°到90°,以北纬为正值,南纬为负值。赤道位于纬度0°。
经纬度坐标系统是全球通用的 地理坐标系统。
经纬度坐标系统使用经度和纬度的组合来确定地球表面上的特定位置。一个点的经纬度坐标表示为两个数值的组合,例如:40°N,120°E 表示北纬40度,东经120度的位置。
2.坐标系统
(1)WGS84(World Geodetic System 1984,GPS标准)
(2)GCJ-02(国测局坐标系,也被称为火星坐标系)
(3)BD-09(Baidu Coordinate System)
(4)CGCS2000(中国2000国家大地坐标系)
3.坐标转换简介
在地图应用中,不同的地图服务商通常使用不同的坐标系,坐标转换就是将一个地图服务商的坐标系转换为另一个地图服务商的坐标系,以便在不同的地图上显示相同的位置信息。
二、地图经纬度转换工具类(CGCS2000坐标 与 WGS84坐标 互转)
1.对于 CGCS2000 需要引入 proj4j依赖:
<dependency>
<groupId>org.locationtech.proj4j</groupId>
<artifactId>proj4j</artifactId>
<version>1.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.locationtech.proj4j/proj4j-epsg -->
<dependency>
<groupId>org.locationtech.proj4j</groupId>
<artifactId>proj4j-epsg</artifactId>
<version>1.3.0</version>
</dependency>
2.坐标转换工具类
package com.example.demo.util;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.proj4j.*;
/**
* 坐标转换工具类 CGCS2000\WGS84 互转
* @author qzz
* @date 2024/7/15
*/
@Slf4j
public class CoordinateTransformUtil {
// 定义CGCS2000的坐标系 4490
private final static String CGCS2000 = "EPSG:4490";
// 定义WGS84的坐标系
final static String WGS84 = "EPSG:4326";
/**
* CGCS2000 转 WGS84
*
* @param lng CGCS2000经度
* @param lat CGCS2000纬度
* @return
*/
public static ProjCoordinate cgcs2000ToWgs84(double lng, double lat) {
//创建坐标系工厂
CRSFactory crsFactory = new CRSFactory();
// 创建CGCS2000的坐标参考系统
CoordinateReferenceSystem sourceCRS = crsFactory.createFromName(CGCS2000);
// 创建WGS84的坐标参考系统
CoordinateReferenceSystem targetCRS = crsFactory.createFromName(WGS84);
// 定义坐标转换器
CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
// 创建转换器
CoordinateTransform transform = ctFactory.createTransform(sourceCRS, targetCRS);
// 执行坐标转换
ProjCoordinate srcCoord = new ProjCoordinate(lng, lat);
ProjCoordinate targetCoord = new ProjCoordinate();
transform.transform(srcCoord, targetCoord);
// 4. 输出转换后的正常经纬度坐标
return targetCoord;
}
/**
* WGS84 转 CGCS2000
*
* @param lng WGS84经度
* @param lat WGS84纬度
* @return
*/
public static ProjCoordinate wgs84ToCgcs2000(double lng, double lat) {
CRSFactory crsFactory = new CRSFactory();
// 定义源和目标投影
CoordinateReferenceSystem sourceCRS = crsFactory.createFromName(WGS84);
CoordinateReferenceSystem targetCRS = crsFactory.createFromName(CGCS2000);
// 定义坐标转换器
CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
// 创建转换器
CoordinateTransform transform = ctFactory.createTransform(sourceCRS, targetCRS);
// 执行坐标转换
ProjCoordinate srcCoord = new ProjCoordinate(lng, lat);
ProjCoordinate targetCoord = new ProjCoordinate();
transform.transform(srcCoord, targetCoord);
// 输出转换后的正常经纬度坐标
return targetCoord;
}
public static void main(String[] args) {
/**
* CGCS2000
*/
double CGCS2000Lon4 = 108.887314;
double CGCS2000Lat4 = 34.230897;
ProjCoordinate CGCS2000ToWgs84 = CoordinateTransformUtil.cgcs2000ToWgs84(CGCS2000Lon4, CGCS2000Lat4);
log.info("中国2000 CGCS2000ToWgs84 : longitude={}, latitude={}", CGCS2000ToWgs84.x, CGCS2000ToWgs84.y);
}
}
控制台打印:
"D:\Program Files\Java\jdk-17.0.3.1\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=59554:D:\Program Files\JetBrains\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -classpath F:\gps-transfer\demo\target\classes;D:\mvnrepository\org\springframework\boot\spring-boot-starter-web\3.3.1\spring-boot-starter-web-3.3.1.jar;D:\mvnrepository\org\springframework\boot\spring-boot-starter\3.3.1\spring-boot-starter-3.3.1.jar;D:\mvnrepository\org\springframework\boot\spring-boot\3.3.1\spring-boot-3.3.1.jar;D:\mvnrepository\org\springframework\boot\spring-boot-autoconfigure\3.3.1\spring-boot-autoconfigure-3.3.1.jar;D:\mvnrepository\org\springframework\boot\spring-boot-starter-logging\3.3.1\spring-boot-starter-logging-3.3.1.jar;D:\mvnrepository\ch\qos\logback\logback-classic\1.5.6\logback-classic-1.5.6.jar;D:\mvnrepository\ch\qos\logback\logback-core\1.5.6\logback-core-1.5.6.jar;D:\mvnrepository\org\apache\logging\log4j\log4j-to-slf4j\2.23.1\log4j-to-slf4j-2.23.1.jar;D:\mvnrepository\org\apache\logging\log4j\log4j-api\2.23.1\log4j-api-2.23.1.jar;D:\mvnrepository\org\slf4j\jul-to-slf4j\2.0.13\jul-to-slf4j-2.0.13.jar;D:\mvnrepository\jakarta\annotation\jakarta.annotation-api\2.1.1\jakarta.annotation-api-2.1.1.jar;D:\mvnrepository\org\yaml\snakeyaml\2.2\snakeyaml-2.2.jar;D:\mvnrepository\org\springframework\boot\spring-boot-starter-json\3.3.1\spring-boot-starter-json-3.3.1.jar;D:\mvnrepository\com\fasterxml\jackson\core\jackson-databind\2.17.1\jackson-databind-2.17.1.jar;D:\mvnrepository\com\fasterxml\jackson\core\jackson-annotations\2.17.1\jackson-annotations-2.17.1.jar;D:\mvnrepository\com\fasterxml\jackson\core\jackson-core\2.17.1\jackson-core-2.17.1.jar;D:\mvnrepository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.17.1\jackson-datatype-jdk8-2.17.1.jar;D:\mvnrepository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.17.1\jackson-datatype-jsr310-2.17.1.jar;D:\mvnrepository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.17.1\jackson-module-parameter-names-2.17.1.jar;D:\mvnrepository\org\springframework\boot\spring-boot-starter-tomcat\3.3.1\spring-boot-starter-tomcat-3.3.1.jar;D:\mvnrepository\org\apache\tomcat\embed\tomcat-embed-core\10.1.25\tomcat-embed-core-10.1.25.jar;D:\mvnrepository\org\apache\tomcat\embed\tomcat-embed-el\10.1.25\tomcat-embed-el-10.1.25.jar;D:\mvnrepository\org\apache\tomcat\embed\tomcat-embed-websocket\10.1.25\tomcat-embed-websocket-10.1.25.jar;D:\mvnrepository\org\springframework\spring-web\6.1.10\spring-web-6.1.10.jar;D:\mvnrepository\org\springframework\spring-beans\6.1.10\spring-beans-6.1.10.jar;D:\mvnrepository\io\micrometer\micrometer-observation\1.13.1\micrometer-observation-1.13.1.jar;D:\mvnrepository\io\micrometer\micrometer-commons\1.13.1\micrometer-commons-1.13.1.jar;D:\mvnrepository\org\springframework\spring-webmvc\6.1.10\spring-webmvc-6.1.10.jar;D:\mvnrepository\org\springframework\spring-aop\6.1.10\spring-aop-6.1.10.jar;D:\mvnrepository\org\springframework\spring-context\6.1.10\spring-context-6.1.10.jar;D:\mvnrepository\org\springframework\spring-expression\6.1.10\spring-expression-6.1.10.jar;D:\mvnrepository\org\projectlombok\lombok\1.18.32\lombok-1.18.32.jar;D:\mvnrepository\org\slf4j\slf4j-api\2.0.13\slf4j-api-2.0.13.jar;D:\mvnrepository\org\springframework\spring-core\6.1.10\spring-core-6.1.10.jar;D:\mvnrepository\org\springframework\spring-jcl\6.1.10\spring-jcl-6.1.10.jar;D:\mvnrepository\org\locationtech\proj4j\proj4j\1.3.0\proj4j-1.3.0.jar;D:\mvnrepository\org\locationtech\proj4j\proj4j-epsg\1.3.0\proj4j-epsg-1.3.0.jar com.example.demo.util.CoordinateTransformUtil
10:54:02.257 [main] INFO com.example.demo.util.CoordinateTransformUtil -- 中国2000 CGCS2000ToWgs84 : longitude=108.887314, latitude=34.230897
Process finished with exit code 0
三、地图经纬度转换工具类(gps84 、GCJ-02、 BD-09)
- gps84 与 GCJ-02 坐标互转
- GCJ-02 与 BD-09 坐标互转
- BD-09 与 gps84 坐标互转
package com.example.demo.util;
/**
* gps84 与 GCJ-02 坐标互转
* GCJ-02 与 BD-09 坐标互转
* BD-09 与 gps84 坐标互转
* @author qzz
* @date 2024/7/16
*/
public class GpsUtil {
public static double pi = 3.1415926535897932384626;
public static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
public static double a = 6378245.0;
public static double ee = 0.00669342162296594323;
/**
* 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
*
* @param lat
* @param lon
* @return
*/
public static double[] gps84_To_Gcj02(double lat, double lon) {
if (outOfChina(lat, lon)) {
return new double[]{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 double[]{mgLat, mgLon};
}
/**
* * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return
*/
public static double[] gcj02_To_Gps84(double lat, double lon) {
double[] gps = transform(lat, lon);
double lontitude = lon * 2 - gps[1];
double latitude = lat * 2 - gps[0];
return new double[]{latitude, lontitude};
}
/**
* 火星坐标系 (GCJ-02) 转 百度坐标系 (BD-09)
*
* @param lat
* @param lon
*/
public static double[] gcj02_To_Bd09(double lat, double lon) {
double x = lon, y = lat;
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 tempLon = z * Math.cos(theta) + 0.0065;
double tempLat = z * Math.sin(theta) + 0.006;
double[] gps = {tempLat, tempLon};
return gps;
}
/**
* * 百度坐标系 (BD-09) 转 火星坐标系 (GCJ-02)
* bd_lat * @param bd_lon * @return
*/
public static double[] bd09_To_Gcj02(double lat, double lon) {
double x = lon - 0.0065, 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 tempLon = z * Math.cos(theta);
double tempLat = z * Math.sin(theta);
double[] gps = {tempLat, tempLon};
return gps;
}
/**
* 将gps84转为bd09
*
* @param lat
* @param lon
* @return
*/
public static double[] gps84_To_bd09(double lat, double lon) {
double[] gcj02 = gps84_To_Gcj02(lat, lon);
double[] bd09 = gcj02_To_Bd09(gcj02[0], gcj02[1]);
return bd09;
}
/**
* 将bd09转为gps84
*
* @param lat
* @param lon
* @return
*/
public static double[] bd09_To_gps84(double lat,double lon){
double[] gcj02 = bd09_To_Gcj02(lat, lon);
double[] gps84 = gcj02_To_Gps84(gcj02[0], gcj02[1]);
//保留小数点后六位
gps84[0] = retain6(gps84[0]);
gps84[1] = retain6(gps84[1]);
return gps84;
}
/**保留小数点后六位
* @param num
* @return
*/
private static double retain6(double num) {
String result = String.format("%.6f", num);
return Double.valueOf(result);
}
public static boolean outOfChina(double lat, double lon) {
if (lon < 72.004 || lon > 137.8347)
return true;
return lat < 0.8293 || lat > 55.8271;
}
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;
}
public static double[] transform(double lat, double lon) {
if (outOfChina(lat, lon)) {
return new double[]{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 double[]{mgLat, mgLon};
}
}