Java使用Mybatis获取数据库Geometry

本文介绍了在Java中使用Mybatis获取数据库Geometry的两种方案:方案A通过ST_AsText转换可能速度慢,而方案B则是通过获取byte[]字节流并利用GeoTools等库进行转换,特别提到了删除前4位SRID值的过程。
摘要由CSDN通过智能技术生成

Java使用Mybatis获取数据库Geometry

方案A 使用ST_AsText(l.coordinates) 查询速度会慢因转换字符串数据大小会大

将几何对象转换为文本

mapper层

select ST_AsText(coordinates) as 'coordinates' from table1

domain 层

public class Entry implements Serializable {
    private Long id;
    private String coordinates;
    private String name;
  	//省略set get 构造方法
}

方案B 获取geometry的字节流

首先来说我们Mybatis是不支持获取Geometry的,如果我们想要获取到那么就要以byte[]字节流的方式获取这个Geometry字段,然后在将字节流删除前4位转换为十六进制,然后在通过Geo工具如 org.geotools.data.postgis.WKBReader or com.vividsolutions.jts.io.WKBReader or org.locationtech.jts.io.WKBReader 进行转换为我们数据库所对应的Geometry数据。

为什么删除前4位

因为Mysql的Geometry存放的是WKB格式,前4位是SRID值,所以需要删除前4位再去进行处理。

SRID是“空间参考标识符” (Spatial Reference Identifier)

数据库查询代码

@Select("SELECT coordinates FROM locations")
public List<Entry> getall();

实体类方法

public class Entry implements Serializable {
    private Long id;
    private byte[] coordinates;
    private String name;
    //省略set get 构造方法
}

转换业务方法

		//测试
    @GetMapping("/test")
    public String test() throws SQLException, IOException, ParseException {
				//从数据库获取Geometry
        List<Entry> getall = testMapper.getall();
      	//封装好返回给前端的Geometry
        List<Geometry> list = new ArrayList<>();
      	//处理数据转换从字节流转换为我们想要的Geometry
        for (Entry entry : getall) {
            byte[] coordinates = entry.getCoordinates();
            String string = byteArrayToHexString(coordinates);
            Geometry geometry = bytesToGeometry(string);
            list.add(geometry);
        }
      
        return list.toString();
    }

		//下面的代码不要碰
		private static String byteArrayToHexString(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (int i = 4; i < bytes.length; i++) {
            result.append(String.format("%02X", bytes[i]));
        }

        return result.toString();
    }

    private static org.locationtech.jts.geom.Geometry bytesToGeometry(String geomBytes) {
        byte[] bytes =  WKBReader.hexToBytes(geomBytes);

        //用org.geotools.data.postgis.WKBReader 或者com.vividsolutions.jts.io.WKBReader、org.locationtech.jts.io.WKBReader转都ok
        org.locationtech.jts.geom.Geometry geo = null;
        try {
            WKBReader wkbReader = new  WKBReader();
            geo = wkbReader.read(bytes);
            //System.out.println("geotools: " + geo.getCoordinates().length);
        } catch (ParseException e) {
            //System.err.println("geotoolsError: " + e.getMessage());
        }
//
//        try {
//            com.vividsolutions.jts.io.WKBReader wkbReader1 = new com.vividsolutions.jts.io.WKBReader();
//            Geometry geometry = wkbReader1.read(bytes);
//            System.out.println("vividsolutions: " + geometry.getCoordinates().length);
//        } catch (ParseException e) {
//            System.err.println("vividsolutionsError: " + e.getMessage());
//        }
//
//        try {
//            org.locationtech.jts.io.WKBReader wkbReader2 = new org.locationtech.jts.io.WKBReader();
//            org.locationtech.jts.geom.Geometry geometry = wkbReader2.read(bytes);
//            System.out.println("locationtech: " + geometry.getCoordinates().length);
//        } catch (org.locationtech.jts.io.ParseException e) {
//            System.err.println("locationtechError: " + e.getMessage());
//        }
        return geo;
    }
  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哇塞大嘴好帅(DaZuiZui)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值