Browse Source

“经纬度问题”

master
review512jwy@163.com 1 month ago
parent
commit
523188f00b
  1. 13
      src/main/java/com/youlai/boot/common/handler/PointTypeHandler.java
  2. 17
      src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalServiceImpl.java
  3. 16
      src/main/resources/mapper/mini/MiniStrayAnimalMapper.xml

13
src/main/java/com/youlai/boot/common/handler/PointTypeHandler.java

@ -44,14 +44,21 @@ public class PointTypeHandler extends BaseTypeHandler<Point> {
}
private Point toPoint(byte[] bytes) {
if (bytes == null) {
if (bytes == null || bytes.length <= 4) {
return null;
}
try {
WKBReader reader = new WKBReader();
return (Point) reader.read(bytes);
// MySQL 返回的数据前 4 个字节是 SRID (Little Endian 存储的 4326)
// 我们需要跳过这 4 个字节才能获取标准的 WKB 内容
byte[] wkbBytes = new byte[bytes.length - 4];
System.arraycopy(bytes, 4, wkbBytes, 0, wkbBytes.length);
Point point = (Point) reader.read(wkbBytes);
point.setSRID(4326);
return point;
} catch (ParseException e) {
throw new RuntimeException("Failed to parse WKB for Point", e);
throw new RuntimeException("無法解析 MySQL 空間數據", e);
}
}
}

17
src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalServiceImpl.java

@ -319,15 +319,26 @@ public class StrayAnimalServiceImpl extends ServiceImpl<MiniStrayAnimalMapper, M
}
private void handleLngLat(MiniStrayAnimal entity, Double lng, Double lat) {
// 1. 初始化工厂
GeometryFactory geometryFactory = new GeometryFactory();
Point gdPoint = geometryFactory.createPoint(new Coordinate(lat, lng));
// 2. 高德/腾讯座标 (GCJ02)
// JTS 的 X 是经度,Y 是纬度
Point gdPoint = geometryFactory.createPoint(new Coordinate(lng, lat));
gdPoint.setSRID(4326);
// 3. 转换为 WGS84 座标
double[] wgs84 = CoordinateTransformUtils.gcj02ToWgs84(lng, lat);
Point wgsPoint = geometryFactory.createPoint(new Coordinate(wgs84[1], wgs84[0]));
// 假设数组 wgs84[0] 是经度, wgs84[1] 是纬度
Point wgsPoint = geometryFactory.createPoint(new Coordinate(wgs84[0], wgs84[1]));
wgsPoint.setSRID(4326);
// 4. 赋值给实体
entity.setGdLocationPoint(gdPoint);
entity.setWgs84LocationPoint(wgsPoint);
entity.setWgs84Geohash( GeoHash.withCharacterPrecision(wgs84[1], wgs84[0], CommonConstants.GEOHASH_LEVEL).toBase32());
// 5. GeoHash 计算 (注意:大多数 GeoHash 库的第一个参数是 Latitude)
entity.setWgs84Geohash(GeoHash.withCharacterPrecision(wgs84[1], wgs84[0], CommonConstants.GEOHASH_LEVEL).toBase32());
}
private void validateInput(List<MultipartFile> images, List<MultipartFile> videos) {

16
src/main/resources/mapper/mini/MiniStrayAnimalMapper.xml

@ -57,11 +57,11 @@
<if test="address != null">#{address},</if>
<if test="gdLocationPoint != null">
ST_GeomFromWKB(#{gdLocationPoint, typeHandler=com.youlai.boot.common.handler.PointTypeHandler}, 4326),
ST_GeomFromWKB(#{gdLocationPoint, typeHandler=com.youlai.boot.common.handler.PointTypeHandler}, 4326, 'axis-order=long-lat'),
</if>
<if test="wgs84LocationPoint != null">
ST_GeomFromWKB(#{wgs84LocationPoint, typeHandler=com.youlai.boot.common.handler.PointTypeHandler}, 4326),
ST_GeomFromWKB(#{wgs84LocationPoint, typeHandler=com.youlai.boot.common.handler.PointTypeHandler}, 4326, 'axis-order=long-lat'),
</if>
<if test="wgs84Geohash != null">#{wgs84Geohash},</if>
@ -100,12 +100,12 @@
<if test="gdLocationPoint != null">
gd_location_point =
ST_GeomFromWKB(#{gdLocationPoint, typeHandler=com.youlai.boot.common.handler.PointTypeHandler}, 4326),
ST_GeomFromWKB(#{gdLocationPoint, typeHandler=com.youlai.boot.common.handler.PointTypeHandler}, 4326, 'axis-order=long-lat'),
</if>
<if test="wgs84LocationPoint != null">
wgs84_location_point =
ST_GeomFromWKB(#{wgs84LocationPoint, typeHandler=com.youlai.boot.common.handler.PointTypeHandler}, 4326),
ST_GeomFromWKB(#{wgs84LocationPoint, typeHandler=com.youlai.boot.common.handler.PointTypeHandler}, 4326, 'axis-order=long-lat'),
</if>
<if test="wgs84Geohash != null">wgs84_geohash = #{wgs84Geohash},</if>
@ -250,8 +250,8 @@
a.province,
a.city,
a.district,
ST_X(a.gd_location_point) AS lng,
ST_Y(a.gd_location_point) AS lat,
ST_Longitude(a.gd_location_point) AS lng,
ST_Latitude(a.gd_location_point) AS lat,
a.address
FROM
mini_stray_animal a
@ -267,8 +267,8 @@
animal_type,
color,
size,
ST_X(gd_location_point) AS lng,
ST_Y(gd_location_point) AS lat
ST_Longitude(gd_location_point) AS lng,
ST_Latitude(gd_location_point) AS lat
FROM
mini_stray_animal
WHERE is_deleted = 0

Loading…
Cancel
Save