GPS坐标系转换

This commit is contained in:
attiya 2025-10-21 14:29:10 +08:00
parent d514c43a42
commit 992fcc6bcb
6 changed files with 96 additions and 22 deletions

View File

@ -7,8 +7,7 @@ import java.time.format.DateTimeFormatter;
* 数据类型转换工具类 * 数据类型转换工具类
* *
* @author dingchao * @author dingchao
* @date 2025/3/24 * @since 2025/3/24
* @modified by:
*/ */
public class ConvertUtil { public class ConvertUtil {

View File

@ -1,14 +1,18 @@
package com.cdzy.operations.handler; package com.cdzy.operations.handler;
import com.cdzy.common.ex.EbikeException;
import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.TypeHandler;
import org.postgresql.geometric.PGpoint; import org.postgresql.geometric.PGpoint;
import org.postgresql.util.PGobject; import org.postgresql.util.PGobject;
import com.cdzy.common.utils.CoordinateUtil;
import java.sql.*; import java.sql.*;
/** /**
* PGpoint 类型处理器 - 直接处理 PGpoint 对象 * PGpoint 类型处理器 - 集成 CoordinateUtil 实现坐标转换
* 写入时GCJ-02 WGS-84
* 读取时WGS-84 GCJ-02
*/ */
public class PGpointTypeHandler implements TypeHandler<PGpoint> { public class PGpointTypeHandler implements TypeHandler<PGpoint> {
@ -17,29 +21,39 @@ public class PGpointTypeHandler implements TypeHandler<PGpoint> {
if (parameter == null) { if (parameter == null) {
ps.setNull(i, Types.OTHER); ps.setNull(i, Types.OTHER);
} else { } else {
// 直接设置 PGpoint 对象 // 写入数据库时GCJ-02 WGS-84
ps.setObject(i, parameter); PGpoint wgs84Point = gcj02ToWgs84(parameter);
ps.setObject(i, wgs84Point);
} }
} }
@Override @Override
public PGpoint getResult(ResultSet rs, String columnName) throws SQLException { public PGpoint getResult(ResultSet rs, String columnName) throws SQLException {
Object object = rs.getObject(columnName); Object object = rs.getObject(columnName);
return convertToPGpoint(object); PGpoint wgs84Point = convertToPGpoint(object);
// 读取时WGS-84 GCJ-02
return wgs84ToGcj02(wgs84Point);
} }
@Override @Override
public PGpoint getResult(ResultSet rs, int columnIndex) throws SQLException { public PGpoint getResult(ResultSet rs, int columnIndex) throws SQLException {
Object object = rs.getObject(columnIndex); Object object = rs.getObject(columnIndex);
return convertToPGpoint(object); PGpoint wgs84Point = convertToPGpoint(object);
// 读取时WGS-84 GCJ-02
return wgs84ToGcj02(wgs84Point);
} }
@Override @Override
public PGpoint getResult(CallableStatement cs, int columnIndex) throws SQLException { public PGpoint getResult(CallableStatement cs, int columnIndex) throws SQLException {
Object object = cs.getObject(columnIndex); Object object = cs.getObject(columnIndex);
return convertToPGpoint(object); PGpoint wgs84Point = convertToPGpoint(object);
// 读取时WGS-84 GCJ-02
return wgs84ToGcj02(wgs84Point);
} }
/**
* 将数据库对象转换为 PGpoint
*/
private PGpoint convertToPGpoint(Object object) { private PGpoint convertToPGpoint(Object object) {
if (object == null) { if (object == null) {
return null; return null;
@ -62,4 +76,38 @@ public class PGpointTypeHandler implements TypeHandler<PGpoint> {
return null; return null;
} }
/**
* GCJ-02 WGS-84写入数据库时调用
*/
private PGpoint gcj02ToWgs84(PGpoint gcj02Point) {
if (gcj02Point == null) {
return null;
}
try {
// 使用 CoordinateUtil 进行坐标转换GCJ-02 WGS-84
double[] wgs84 = CoordinateUtil.GCJ02ToWGS84(gcj02Point.x, gcj02Point.y);
return new PGpoint(wgs84[0], wgs84[1]);
} catch (Exception e) {
throw new RuntimeException("GCJ-02 → WGS-84 坐标系转换成错误");
}
}
/**
* WGS-84 GCJ-02从数据库读取时调用
*/
private PGpoint wgs84ToGcj02(PGpoint wgs84Point) {
if (wgs84Point == null) {
return null;
}
try {
// 使用 CoordinateUtil 进行坐标转换WGS-84 GCJ-02
double[] gcj02 = CoordinateUtil.WGS84ToGCJ02(wgs84Point.x, wgs84Point.y);
return new PGpoint(gcj02[0], gcj02[1]);
} catch (Exception e) {
throw new EbikeException("WGS-84 → GCJ-02 坐标系转换成错误");
}
}
} }

View File

@ -1,5 +1,10 @@
package com.cdzy.operations.model.entity; package com.cdzy.operations.model.entity;
import com.cdzy.operations.handler.PGpointDeserializer;
import com.cdzy.operations.handler.PGpointSerializer;
import com.cdzy.operations.handler.PGpointTypeHandler;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.mybatisflex.annotation.Column; import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table; import com.mybatisflex.annotation.Table;
@ -68,6 +73,9 @@ public class EbikeBikeInfo implements Serializable {
/** /**
* 定位 * 定位
*/ */
@Column(typeHandler = PGpointTypeHandler.class)
@JsonSerialize(using = PGpointSerializer.class)
@JsonDeserialize(using = PGpointDeserializer.class)
private PGpoint location; private PGpoint location;
/** /**
@ -88,7 +96,7 @@ public class EbikeBikeInfo implements Serializable {
/** /**
* 创建时间 * 创建时间
*/ */
@Column(onInsertValue = "now") @Column(onInsertValue = "now()")
private LocalDateTime createdAt; private LocalDateTime createdAt;
/** /**
@ -99,7 +107,7 @@ public class EbikeBikeInfo implements Serializable {
/** /**
* 修改时间 * 修改时间
*/ */
@Column(onUpdateValue = "now") @Column(onUpdateValue = "now()")
private LocalDateTime updatedAt; private LocalDateTime updatedAt;
/** /**

View File

@ -3,12 +3,10 @@ package com.cdzy.operations.service.impl;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import com.cdzy.common.ex.EbikeException; import com.cdzy.common.ex.EbikeException;
import com.cdzy.operations.enums.BatteryStatus; import com.cdzy.operations.enums.BatteryStatus;
import com.cdzy.operations.enums.BikeQrStatus;
import com.cdzy.operations.enums.BikeStatus; import com.cdzy.operations.enums.BikeStatus;
import com.cdzy.operations.mapper.*; import com.cdzy.operations.mapper.*;
import com.cdzy.operations.model.entity.EbikeBatteryInfo; import com.cdzy.operations.model.entity.*;
import com.cdzy.operations.model.entity.EbikeBikeInfo;
import com.cdzy.operations.model.entity.EbikeEcuInfo;
import com.cdzy.operations.model.entity.EbikeInventoryRecord;
import com.cdzy.operations.model.vo.EbikeBikeBindVo; import com.cdzy.operations.model.vo.EbikeBikeBindVo;
import com.cdzy.operations.model.vo.EbikeInventoryVo; import com.cdzy.operations.model.vo.EbikeInventoryVo;
import com.cdzy.operations.service.EbikeBikeInfoService; import com.cdzy.operations.service.EbikeBikeInfoService;
@ -39,6 +37,9 @@ public class EbikeBikeInfoServiceImpl extends ServiceImpl<EbikeBikeInfoMapper, E
@Resource @Resource
EbikeBatteryInfoMapper batteryInfoMapper; EbikeBatteryInfoMapper batteryInfoMapper;
@Resource
EbikeBikeQrMapper bikeQrMapper;
@Resource @Resource
EbikeEcuInfoMapper ebikeEcuInfoMapper; EbikeEcuInfoMapper ebikeEcuInfoMapper;
@ -66,9 +67,23 @@ public class EbikeBikeInfoServiceImpl extends ServiceImpl<EbikeBikeInfoMapper, E
throw new EbikeException("该中控不存在"); throw new EbikeException("该中控不存在");
} }
if (!Objects.equals(batteryInfo.getOperatorId(), ecuInfo.getOperatorId())) { queryWrapper.clear();
EbikeBikeQr ebikeBikeQr = bikeQrMapper.selectOneByQuery(queryWrapper);
if (ebikeBikeQr == null){
throw new EbikeException("该车辆二维码无效");
}
ebikeBikeQr.setStatus(BikeQrStatus.BIND);
bikeQrMapper.update(ebikeBikeQr);
if (!Objects.equals(batteryInfo.getOperatorId(), ecuInfo.getOperatorId()) && !Objects.equals(ecuInfo.getOperatorId(), ebikeBikeQr.getOperatorId()) && !Objects.equals(batteryInfo.getOperatorId(), ebikeBikeQr.getOperatorId())) {
throw new EbikeException("所选组件属于不同的运营商"); throw new EbikeException("所选组件属于不同的运营商");
} }
//更改二维码绑定状态
ebikeBikeQr.setStatus(BikeQrStatus.BIND);
bikeQrMapper.update(ebikeBikeQr);
Long operatorId = batteryInfo.getOperatorId(); Long operatorId = batteryInfo.getOperatorId();
queryWrapper.clear(); queryWrapper.clear();

View File

@ -76,8 +76,8 @@ public class EbikeInventoryServiceImpl extends ServiceImpl<EbikeInventoryMapper,
@Override @Override
@Transactional @Transactional
public void reduceInventory(EbikeInventoryVo ebikeInventory) { public void reduceInventory(EbikeInventoryVo ebikeInventory) {
if (ebikeInventory.getInventoryNum() <= 0){ if (ebikeInventory.getInventoryNum() >= 0){
throw new EbikeException("添加库存时填入数量不能大于/等于0"); throw new EbikeException("减少库存时填入数量不能大于/等于0");
} }
QueryWrapper queryWrapper = QueryWrapper.create() QueryWrapper queryWrapper = QueryWrapper.create()
.where(EBIKE_INVENTORY.INVENTORY_TYPE.eq(ebikeInventory.getInventoryType())); .where(EBIKE_INVENTORY.INVENTORY_TYPE.eq(ebikeInventory.getInventoryType()));

View File

@ -2,11 +2,14 @@ package com.cdzy.operations;
import com.cdzy.operations.mapper.EbikeBikeInfoMapper; import com.cdzy.operations.mapper.EbikeBikeInfoMapper;
import com.cdzy.operations.model.entity.EbikeBikeInfo; import com.cdzy.operations.model.entity.EbikeBikeInfo;
import com.mybatisflex.core.query.QueryWrapper;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.postgresql.geometric.PGpoint; import org.postgresql.geometric.PGpoint;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import static com.cdzy.operations.model.entity.table.EbikeBikeInfoTableDef.EBIKE_BIKE_INFO;
@SpringBootTest @SpringBootTest
class EbikeOperationsApplicationTests { class EbikeOperationsApplicationTests {
@ -15,11 +18,12 @@ class EbikeOperationsApplicationTests {
@Test @Test
void contextLoads(){ void contextLoads(){
EbikeBikeInfo bikeInfo = new EbikeBikeInfo(); QueryWrapper queryWrapper = new QueryWrapper()
// 创建测试点:X经度纬度 .where(EBIKE_BIKE_INFO.BIKE_INFO_ID.eq("338128115027443712"));
PGpoint point =new PGpoint(116.3974, 39.9093); EbikeBikeInfo info = ebikeBikeInfoMapper.selectOneByQuery(queryWrapper);
bikeInfo.setLocation(point); PGpoint pGpoint = new PGpoint(104.066541, 30.572269);
ebikeBikeInfoMapper.insert(bikeInfo); info.setLocation(pGpoint);
ebikeBikeInfoMapper.update(info);
} }
} }