将几何对象转换为文本
select ST_AsText(coordinates) as 'coordinates' from table1
public class Entry implements Serializable {
private Long id;
private String coordinates;
private String name;
//省略set get 构造方法
}
首先来说我们Mybatis是不支持获取Geometry的,如果我们想要获取到那么就要以byte[]字节流的方式获取这个Geometry字段,然后在将字节流删除前4位转换为十六进制,然后在通过Geo工具如 org.geotools.data.postgis.WKBReader or com.vividsolutions.jts.io.WKBReader or org.locationtech.jts.io.WKBReader 进行转换为我们数据库所对应的Geometry数据。
因为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;
}