diff --git a/sql/ttstd/sys_sn_location_coord.sql b/sql/ttstd/sys_sn_location_coord.sql new file mode 100644 index 00000000..526422b5 --- /dev/null +++ b/sql/ttstd/sys_sn_location_coord.sql @@ -0,0 +1,30 @@ +-- 定位坐标系归一化:主经纬度统一为 GCJ-02,原始上报值留痕。 +-- +-- 背景:设备端百度定位 SDK 历史上以 setCoorType("bd09ll") 上报,后端原样存储, +-- 各端各自猜测坐标系导致打点偏移。现约定后端内部只认 GCJ-02: +-- - 新上报:入库前按 coordType 归一化到 GCJ-02,raw_* 保留原值 +-- - 历史行:coord_type 默认标记为 BD09LL,查询出参时由 LocationServiceImpl 转换 +-- - wgs84_*:设备端系统 GNSS 原值(可选),供海外 / GIS 分析使用 +-- +-- 说明:不需要批量 UPDATE 主坐标,历史数据靠 coord_type 在读取时兼容,零迁移风险。 + +ALTER TABLE sys_sn_location + ADD COLUMN coord_type VARCHAR(16) NOT NULL DEFAULT 'BD09LL' + COMMENT '主经纬度坐标系:GCJ02 / BD09LL / WGS84;历史数据默认 BD09LL', + ADD COLUMN raw_longitude VARCHAR(32) NULL + COMMENT '设备上报的原始经度(留痕,不参与业务计算)', + ADD COLUMN raw_latitude VARCHAR(32) NULL + COMMENT '设备上报的原始纬度(留痕,不参与业务计算)', + ADD COLUMN raw_coord_type VARCHAR(16) NULL + COMMENT '原始坐标的坐标系', + ADD COLUMN wgs84_longitude VARCHAR(32) NULL + COMMENT '系统 GNSS 原始 WGS84 经度(可选)', + ADD COLUMN wgs84_latitude VARCHAR(32) NULL + COMMENT '系统 GNSS 原始 WGS84 纬度(可选)'; + +-- 回填历史行的原始值,便于审计与回滚 +UPDATE sys_sn_location +SET raw_longitude = longitude, + raw_latitude = latitude, + raw_coord_type = 'BD09LL' +WHERE raw_longitude IS NULL; diff --git a/src/main/java/com/youlai/boot/common/util/CoordinateConverter.java b/src/main/java/com/youlai/boot/common/util/CoordinateConverter.java new file mode 100644 index 00000000..d3612736 --- /dev/null +++ b/src/main/java/com/youlai/boot/common/util/CoordinateConverter.java @@ -0,0 +1,165 @@ +package com.youlai.boot.common.util; + +/** + * 坐标系转换工具(全链路唯一实现)。 + * + *
约定:后端内部只认 GCJ-02。设备上报在进入业务层前统一归一化, + * 历史 BD-09 数据在出参时按 coordType 转换,业务代码禁止再零散转换。 + * + *
参考:GCJ-02 正向偏移为公开的近似算法;GCJ-02 → WGS-84 无官方算法,
+ * 这里用迭代逼近(残差可收敛到米级以内),仅供海外/数据导出使用。
+ * 百度 BD-09 是在 GCJ-02 基础上再做一次极坐标偏移,正向/反向均有确定性公式。
+ */
+public final class CoordinateConverter {
+
+ public static final String WGS84 = "WGS84";
+ public static final String GCJ02 = "GCJ02";
+ public static final String BD09LL = "BD09LL";
+
+ /** 老设备未上报 coordType 时按 BD-09 处理(历史行为)。 */
+ public static final String LEGACY_DEFAULT = BD09LL;
+
+ private static final double PI = Math.PI;
+ private static final double A = 6378245.0;
+ private static final double EE = 0.00669342162296594323;
+
+ private CoordinateConverter() {
+ }
+
+ public enum CoordType {
+ WGS84, GCJ02, BD09LL
+ }
+
+ /** 经纬度值对象。 */
+ public static final class LatLng {
+ public final double longitude;
+ public final double latitude;
+
+ public LatLng(double longitude, double latitude) {
+ this.longitude = longitude;
+ this.latitude = latitude;
+ }
+ }
+
+ public static CoordType parse(String value) {
+ if (value == null || value.isBlank()) {
+ return CoordType.BD09LL;
+ }
+ switch (value.trim().toUpperCase()) {
+ case WGS84:
+ case "WGS84LL":
+ return CoordType.WGS84;
+ case GCJ02:
+ case "GCJ02LL":
+ case "COMMON":
+ return CoordType.GCJ02;
+ case BD09LL:
+ case "BD09":
+ return CoordType.BD09LL;
+ default:
+ return CoordType.BD09LL;
+ }
+ }
+
+ /** 任意来源坐标 → GCJ-02(主入口)。国外坐标直通。 */
+ public static LatLng toGcj02(double longitude, double latitude, String from) {
+ return toGcj02(longitude, latitude, parse(from));
+ }
+
+ public static LatLng toGcj02(double longitude, double latitude, CoordType from) {
+ switch (from) {
+ case GCJ02:
+ return new LatLng(longitude, latitude);
+ case WGS84:
+ return wgs84ToGcj02(longitude, latitude);
+ case BD09LL:
+ default:
+ return bd09ToGcj02(longitude, latitude);
+ }
+ }
+
+ /** BD-09 → GCJ-02。 */
+ public static LatLng bd09ToGcj02(double longitude, double latitude) {
+ double x = longitude - 0.0065;
+ double y = latitude - 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 lng = z * Math.cos(theta);
+ double lat = z * Math.sin(theta);
+ if (outOfChina(lng, lat)) {
+ return new LatLng(longitude, latitude);
+ }
+ return new LatLng(lng, lat);
+ }
+
+ /** GCJ-02 → BD-09。 */
+ public static LatLng gcj02ToBd09(double longitude, double latitude) {
+ if (outOfChina(longitude, latitude)) {
+ return new LatLng(longitude, latitude);
+ }
+ double z = Math.sqrt(longitude * longitude + latitude * latitude)
+ + 0.00002 * Math.sin(latitude * PI);
+ double theta = Math.atan2(latitude, longitude)
+ + 0.000003 * Math.cos(longitude * PI);
+ return new LatLng(z * Math.cos(theta) + 0.0065, z * Math.sin(theta) + 0.006);
+ }
+
+ /** WGS-84 → GCJ-02。 */
+ public static LatLng wgs84ToGcj02(double longitude, double latitude) {
+ if (outOfChina(longitude, latitude)) {
+ return new LatLng(longitude, latitude);
+ }
+ double dLat = transformLat(longitude - 105.0, latitude - 35.0);
+ double dLng = transformLng(longitude - 105.0, latitude - 35.0);
+ double radLat = latitude / 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);
+ return new LatLng(longitude + dLng, latitude + dLat);
+ }
+
+ /** GCJ-02 → WGS-84(迭代反解,残差通常 < 1 m)。 */
+ public static LatLng gcj02ToWgs84(double longitude, double latitude) {
+ if (outOfChina(longitude, latitude)) {
+ return new LatLng(longitude, latitude);
+ }
+ double wgsLng = longitude;
+ double wgsLat = latitude;
+ for (int i = 0; i < 8; i++) {
+ LatLng gcj = wgs84ToGcj02(wgsLng, wgsLat);
+ double dLng = gcj.longitude - longitude;
+ double dLat = gcj.latitude - latitude;
+ if (Math.abs(dLng) < 1e-9 && Math.abs(dLat) < 1e-9) {
+ break;
+ }
+ wgsLng -= dLng;
+ wgsLat -= dLat;
+ }
+ return new LatLng(wgsLng, wgsLat);
+ }
+
+ public static boolean outOfChina(double longitude, double latitude) {
+ return longitude < 72.004 || longitude > 137.8347
+ || latitude < 0.8293 || latitude > 55.8271;
+ }
+
+ private 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;
+ }
+
+ private static double transformLng(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;
+ }
+}
diff --git a/src/main/java/com/youlai/boot/device/controller/MobileController.java b/src/main/java/com/youlai/boot/device/controller/MobileController.java
index 4974b2e1..2044784d 100644
--- a/src/main/java/com/youlai/boot/device/controller/MobileController.java
+++ b/src/main/java/com/youlai/boot/device/controller/MobileController.java
@@ -8,6 +8,7 @@ import com.youlai.boot.common.exception.BusinessException;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.common.result.ResultCode;
import com.youlai.boot.common.util.HashUtils;
+import com.youlai.boot.common.util.CoordinateConverter;
import com.youlai.boot.device.model.entity.SnDeviceSystemInfo;
import com.youlai.boot.device.model.entity.SnDeviceHardwareInfo;
import com.youlai.boot.device.model.entity.SnDeviceNetworkInfo;
@@ -569,6 +570,32 @@ public class MobileController {
SnLocation location = new SnLocation();
BeanUtils.copyProperties(locationReq, location);
location.setSn(sn);
+ // 全链路统一为 GCJ-02:老设备不传 coordType 时按 BD-09 处理,
+ // 原始上报值保留在 raw_* 列便于审计与回滚。
+ String sourceType = (locationReq.getCoordType() == null || locationReq.getCoordType().isBlank())
+ ? CoordinateConverter.LEGACY_DEFAULT
+ : locationReq.getCoordType();
+ String rawLng = locationReq.getLongitude();
+ String rawLat = locationReq.getLatitude();
+ location.setRawLongitude(rawLng);
+ location.setRawLatitude(rawLat);
+ location.setRawCoordType(sourceType);
+ location.setWgs84Longitude(locationReq.getWgs84Longitude());
+ location.setWgs84Latitude(locationReq.getWgs84Latitude());
+ location.setCoordType(sourceType);
+ try {
+ double lng = Double.parseDouble(rawLng);
+ double lat = Double.parseDouble(rawLat);
+ if (lng != 0 || lat != 0) {
+ CoordinateConverter.LatLng gcj =
+ CoordinateConverter.toGcj02(lng, lat, sourceType);
+ location.setLongitude(String.valueOf(gcj.longitude));
+ location.setLatitude(String.valueOf(gcj.latitude));
+ location.setCoordType(CoordinateConverter.GCJ02);
+ }
+ } catch (NumberFormatException | NullPointerException ignored) {
+ // 仅上报 mapError 而无坐标时保留原值,coordType 维持来源类型。
+ }
// 上次定位成功时间由服务端统一写入,避免移动端时间不准或 LocalDateTime 序列化格式不一致导致为空
location.setLastSuccessfulTime(LocalDateTime.now());
diff --git a/src/main/java/com/youlai/boot/device/model/entity/SnLocation.java b/src/main/java/com/youlai/boot/device/model/entity/SnLocation.java
index d6a09a61..a6750ea7 100644
--- a/src/main/java/com/youlai/boot/device/model/entity/SnLocation.java
+++ b/src/main/java/com/youlai/boot/device/model/entity/SnLocation.java
@@ -96,6 +96,36 @@ public class SnLocation extends BaseEntity {
*/
private String latitude;
+ /**
+ * 主经纬度坐标系:GCJ02(归一化后)/ BD09LL(历史数据)
+ */
+ private String coordType;
+
+ /**
+ * 设备上报的原始经度(留痕,不参与业务计算)
+ */
+ private String rawLongitude;
+
+ /**
+ * 设备上报的原始纬度(留痕,不参与业务计算)
+ */
+ private String rawLatitude;
+
+ /**
+ * 原始坐标的坐标系
+ */
+ private String rawCoordType;
+
+ /**
+ * 系统 GNSS 原始 WGS84 经度(可选)
+ */
+ private String wgs84Longitude;
+
+ /**
+ * 系统 GNSS 原始 WGS84 纬度(可选)
+ */
+ private String wgs84Latitude;
+
/**
*失败原因
*/
diff --git a/src/main/java/com/youlai/boot/device/model/req/SnLocationReq.java b/src/main/java/com/youlai/boot/device/model/req/SnLocationReq.java
index 36c7136d..b9e35c0d 100644
--- a/src/main/java/com/youlai/boot/device/model/req/SnLocationReq.java
+++ b/src/main/java/com/youlai/boot/device/model/req/SnLocationReq.java
@@ -60,6 +60,15 @@ public class SnLocationReq {
@Schema(description = "纬度")
private String latitude;
+ @Schema(description = "上报坐标的坐标系:GCJ02 / BD09LL / WGS84;老设备不传时按 BD09LL 处理")
+ private String coordType;
+
+ @Schema(description = "系统 GNSS 原始 WGS84 经度(可选,供海外/GIS 使用)")
+ private String wgs84Longitude;
+
+ @Schema(description = "系统 GNSS 原始 WGS84 纬度(可选,供海外/GIS 使用)")
+ private String wgs84Latitude;
+
@Schema(description = "失败原因")
private String mapError;
diff --git a/src/main/java/com/youlai/boot/device/model/vo/DeviceLocationVO.java b/src/main/java/com/youlai/boot/device/model/vo/DeviceLocationVO.java
index 08e04264..3d42dc2b 100644
--- a/src/main/java/com/youlai/boot/device/model/vo/DeviceLocationVO.java
+++ b/src/main/java/com/youlai/boot/device/model/vo/DeviceLocationVO.java
@@ -61,6 +61,15 @@ public class DeviceLocationVO implements Serializable {
@Schema(description = "纬度")
private String latitude;
+ @Schema(description = "主经纬度坐标系,固定为 GCJ02")
+ private String coordType;
+
+ @Schema(description = "系统 GNSS 原始 WGS84 经度(可选,海外/GIS 用)")
+ private String wgs84Longitude;
+
+ @Schema(description = "系统 GNSS 原始 WGS84 纬度(可选,海外/GIS 用)")
+ private String wgs84Latitude;
+
@Schema(description = "定位失败原因,成功时为 '-'")
private String mapError;
diff --git a/src/main/java/com/youlai/boot/device/service/impl/LocationServiceImpl.java b/src/main/java/com/youlai/boot/device/service/impl/LocationServiceImpl.java
index 846224fa..e97535bb 100644
--- a/src/main/java/com/youlai/boot/device/service/impl/LocationServiceImpl.java
+++ b/src/main/java/com/youlai/boot/device/service/impl/LocationServiceImpl.java
@@ -2,6 +2,7 @@ package com.youlai.boot.device.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.spring.service.impl.ServiceImpl;
+import com.youlai.boot.common.util.CoordinateConverter;
import com.youlai.boot.device.mapper.LocationMapper;
import com.youlai.boot.device.model.entity.SnLocation;
import com.youlai.boot.device.model.vo.DeviceLocationVO;
@@ -30,6 +31,24 @@ public class LocationServiceImpl extends ServiceImpl