From 582f9e7a8c62246ee9913cafd1d3c8a57f9099e1 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 27 Dec 2022 21:40:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DCoordinateUtil=E5=9D=90?= =?UTF-8?q?=E6=A0=87=E8=BD=AC=E6=8D=A2=E5=8F=82=E6=95=B0=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../cn/hutool/core/util/CoordinateUtil.java | 24 +++++---------- .../hutool/core/util/CoordinateUtilTest.java | 29 ++++++++++++------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d40c00138..d4bf51da5 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ * 【core 】 修复IndexedComparator导致的数据错乱问题(ExcelWriter使用部分别名导致字段丢失)(issue#I66Z6B@Gitee) * 【crypto】 修复sm2构造方法NullPointerException(pr#2820@Github) * 【core 】 修复ConverterRegistry中无效加载导致的问题(issue#2812@Github) +* 【core 】 修复CoordinateUtil坐标转换参数错误(pr#895@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/util/CoordinateUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/CoordinateUtil.java index 04ad3b485..61eeb15c5 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/CoordinateUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/CoordinateUtil.java @@ -135,19 +135,6 @@ public class CoordinateUtil { //----------------------------------------------------------------------------------- Private methods begin - /** - * 转换坐标公共核心 - * - * @param lng 经度坐标 - * @param lat 纬度坐标 - * @return 返回结果 - */ - private static double transCore(double lng, double lat) { - double 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; - return ret; - } - /** * WGS84 与 火星坐标系 (GCJ-02)转换的偏移算法(非精确) * @@ -162,7 +149,7 @@ public class CoordinateUtil { double magic = Math.sin(lat / 180.0 * PI); magic = 1 - CORRECTION_PARAM * magic * magic; - double sqrtMagic = Math.sqrt(magic); + final double sqrtMagic = Math.sqrt(magic); dlng = (dlng * 180.0) / (RADIUS / sqrtMagic * Math.cos(lat / 180.0 * PI) * PI); dlat = (dlat * 180.0) / ((RADIUS * (1 - CORRECTION_PARAM)) / (magic * sqrtMagic) * PI); @@ -184,7 +171,8 @@ public class CoordinateUtil { */ private static double transLng(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 += transCore(lng, lat); + 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; } @@ -197,8 +185,10 @@ public class CoordinateUtil { * @return ret 计算完成后的 */ private static double transLat(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 += transCore(lng, 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; } diff --git a/hutool-core/src/test/java/cn/hutool/core/util/CoordinateUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/CoordinateUtilTest.java index 00b342943..9f7d64126 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/CoordinateUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/CoordinateUtilTest.java @@ -12,30 +12,38 @@ import org.junit.Test; public class CoordinateUtilTest { @Test - public void wgs84ToGcj02Test(){ + public void wgs84ToGcj02Test() { final CoordinateUtil.Coordinate coordinate = CoordinateUtil.wgs84ToGcj02(116.404, 39.915); - Assert.assertEquals(116.41033392216508D, coordinate.getLng(), 0); + Assert.assertEquals(116.41024449916938D, coordinate.getLng(), 0); Assert.assertEquals(39.91640428150164D, coordinate.getLat(), 0); } @Test - public void gcj02ToWgs84Test(){ + public void gcj02ToWgs84Test() { final CoordinateUtil.Coordinate coordinate = CoordinateUtil.gcj02ToWgs84(116.404, 39.915); - Assert.assertEquals(116.39766607783491D, coordinate.getLng(), 0); + Assert.assertEquals(116.39775550083061D, coordinate.getLng(), 0); Assert.assertEquals(39.91359571849836D, coordinate.getLat(), 0); } @Test - public void wgs84toBd09Test(){ + public void wgs84toBd09Test() { final CoordinateUtil.Coordinate coordinate = CoordinateUtil.wgs84ToBd09(116.404, 39.915); - Assert.assertEquals(116.41671695444782D, coordinate.getLng(), 0); - Assert.assertEquals(39.922698713521726D, coordinate.getLat(), 0); + Assert.assertEquals(116.41662724378733D, coordinate.getLng(), 0); + Assert.assertEquals(39.922699552216216D, coordinate.getLat(), 0); } @Test - public void bd09toWgs84Test(){ + public void wgs84toBd09Test2() { + // https://tool.lu/coordinate/ + final CoordinateUtil.Coordinate coordinate = CoordinateUtil.wgs84ToBd09(122.99395597D, 44.99804071D); + Assert.assertEquals(123.00636516028885D, coordinate.getLng(), 0); + Assert.assertEquals(45.0063690918959D, coordinate.getLat(), 0); + } + + @Test + public void bd09toWgs84Test() { final CoordinateUtil.Coordinate coordinate = CoordinateUtil.bd09toWgs84(116.404, 39.915); - Assert.assertEquals(116.39129143419822D, coordinate.getLng(), 0); + Assert.assertEquals(116.3913836995125D, coordinate.getLng(), 0); Assert.assertEquals(39.907253214522164D, coordinate.getLat(), 0); } @@ -47,10 +55,9 @@ public class CoordinateUtilTest { } @Test - public void bd09toGcj02Test(){ + public void bd09toGcj02Test() { final CoordinateUtil.Coordinate coordinate = CoordinateUtil.bd09ToGcj02(116.404, 39.915); Assert.assertEquals(116.39762729119315D, coordinate.getLng(), 0); Assert.assertEquals(39.90865673957631D, coordinate.getLat(), 0); } - }