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
* @date 2025/3/24
* @modified by:
* @since 2025/3/24
*/
public class ConvertUtil {

View File

@ -1,14 +1,18 @@
package com.cdzy.operations.handler;
import com.cdzy.common.ex.EbikeException;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.postgresql.geometric.PGpoint;
import org.postgresql.util.PGobject;
import com.cdzy.common.utils.CoordinateUtil;
import java.sql.*;
/**
* PGpoint 类型处理器 - 直接处理 PGpoint 对象
* PGpoint 类型处理器 - 集成 CoordinateUtil 实现坐标转换
* 写入时GCJ-02 WGS-84
* 读取时WGS-84 GCJ-02
*/
public class PGpointTypeHandler implements TypeHandler<PGpoint> {
@ -17,29 +21,39 @@ public class PGpointTypeHandler implements TypeHandler<PGpoint> {
if (parameter == null) {
ps.setNull(i, Types.OTHER);
} else {
// 直接设置 PGpoint 对象
ps.setObject(i, parameter);
// 写入数据库时GCJ-02 WGS-84
PGpoint wgs84Point = gcj02ToWgs84(parameter);
ps.setObject(i, wgs84Point);
}
}
@Override
public PGpoint getResult(ResultSet rs, String columnName) throws SQLException {
Object object = rs.getObject(columnName);
return convertToPGpoint(object);
PGpoint wgs84Point = convertToPGpoint(object);
// 读取时WGS-84 GCJ-02
return wgs84ToGcj02(wgs84Point);
}
@Override
public PGpoint getResult(ResultSet rs, int columnIndex) throws SQLException {
Object object = rs.getObject(columnIndex);
return convertToPGpoint(object);
PGpoint wgs84Point = convertToPGpoint(object);
// 读取时WGS-84 GCJ-02
return wgs84ToGcj02(wgs84Point);
}
@Override
public PGpoint getResult(CallableStatement cs, int columnIndex) throws SQLException {
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) {
if (object == null) {
return null;
@ -62,4 +76,38 @@ public class PGpointTypeHandler implements TypeHandler<PGpoint> {
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;
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.Id;
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;
/**
@ -88,7 +96,7 @@ public class EbikeBikeInfo implements Serializable {
/**
* 创建时间
*/
@Column(onInsertValue = "now")
@Column(onInsertValue = "now()")
private LocalDateTime createdAt;
/**
@ -99,7 +107,7 @@ public class EbikeBikeInfo implements Serializable {
/**
* 修改时间
*/
@Column(onUpdateValue = "now")
@Column(onUpdateValue = "now()")
private LocalDateTime updatedAt;
/**

View File

@ -3,12 +3,10 @@ package com.cdzy.operations.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.cdzy.common.ex.EbikeException;
import com.cdzy.operations.enums.BatteryStatus;
import com.cdzy.operations.enums.BikeQrStatus;
import com.cdzy.operations.enums.BikeStatus;
import com.cdzy.operations.mapper.*;
import com.cdzy.operations.model.entity.EbikeBatteryInfo;
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.entity.*;
import com.cdzy.operations.model.vo.EbikeBikeBindVo;
import com.cdzy.operations.model.vo.EbikeInventoryVo;
import com.cdzy.operations.service.EbikeBikeInfoService;
@ -39,6 +37,9 @@ public class EbikeBikeInfoServiceImpl extends ServiceImpl<EbikeBikeInfoMapper, E
@Resource
EbikeBatteryInfoMapper batteryInfoMapper;
@Resource
EbikeBikeQrMapper bikeQrMapper;
@Resource
EbikeEcuInfoMapper ebikeEcuInfoMapper;
@ -66,9 +67,23 @@ public class EbikeBikeInfoServiceImpl extends ServiceImpl<EbikeBikeInfoMapper, E
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("所选组件属于不同的运营商");
}
//更改二维码绑定状态
ebikeBikeQr.setStatus(BikeQrStatus.BIND);
bikeQrMapper.update(ebikeBikeQr);
Long operatorId = batteryInfo.getOperatorId();
queryWrapper.clear();

View File

@ -76,8 +76,8 @@ public class EbikeInventoryServiceImpl extends ServiceImpl<EbikeInventoryMapper,
@Override
@Transactional
public void reduceInventory(EbikeInventoryVo ebikeInventory) {
if (ebikeInventory.getInventoryNum() <= 0){
throw new EbikeException("添加库存时填入数量不能大于/等于0");
if (ebikeInventory.getInventoryNum() >= 0){
throw new EbikeException("减少库存时填入数量不能大于/等于0");
}
QueryWrapper queryWrapper = QueryWrapper.create()
.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.model.entity.EbikeBikeInfo;
import com.mybatisflex.core.query.QueryWrapper;
import org.junit.jupiter.api.Test;
import org.postgresql.geometric.PGpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static com.cdzy.operations.model.entity.table.EbikeBikeInfoTableDef.EBIKE_BIKE_INFO;
@SpringBootTest
class EbikeOperationsApplicationTests {
@ -15,11 +18,12 @@ class EbikeOperationsApplicationTests {
@Test
void contextLoads(){
EbikeBikeInfo bikeInfo = new EbikeBikeInfo();
// 创建测试点:X经度纬度
PGpoint point =new PGpoint(116.3974, 39.9093);
bikeInfo.setLocation(point);
ebikeBikeInfoMapper.insert(bikeInfo);
QueryWrapper queryWrapper = new QueryWrapper()
.where(EBIKE_BIKE_INFO.BIKE_INFO_ID.eq("338128115027443712"));
EbikeBikeInfo info = ebikeBikeInfoMapper.selectOneByQuery(queryWrapper);
PGpoint pGpoint = new PGpoint(104.066541, 30.572269);
info.setLocation(pGpoint);
ebikeBikeInfoMapper.update(info);
}
}