mysql point类型 java_java语言实现将mysql的linestring、point 数据类型解析成double类型...

mysql的linestring、point从jdbc读入为byte[] 类型,下面的java代码实现逐次读取经纬度数据,返回double数组:

public static double[] bytestoPoints(byte[] arr){

if(arr==null){

return null;

}

if(arr.length==25){

return bytesToOnePoint(arr);

}

return bytesToMutiPoints(arr);

}

private static double bytes2Double(byte[] arr,int start) {

long value = 0;

for (int i = 0; i < 8; i++) {

value |= ((long) (arr[start+i] & 0xff)) << (8 * i);

}

return Double.longBitsToDouble(value);

}

private static double[] bytesToOnePoint(byte[] arr){

return new double[]{bytes2Double(arr,9),bytes2Double(arr,17)};

}

private static double[] bytesToMutiPoints(byte[] arr){

int len=(arr.length-13)/8;

double[] result=new double[len];

for(int i=0;i

result[i]=bytes2Double(arr,13+i*8);

}

return result;

}

将double型经纬度转成mysql的point类型数据:

byte[] convert2Point(double lat,double lng){

byte[] bytelat=double2Bytes(lat);

byte[] bytelng=double2Bytes(lng);

byte[] bpoint=new byte[25];

bpoint[4]=0x01;

bpoint[5]=0x01;

for(int i=0;i<8;++i){

bpoint[9+i]=bytelng[i];

bpoint[17+i]=bytelat[i];

}

return bpoint;

}

byte[] double2Bytes(double d) {

long value = Double.doubleToRawLongBits(d);

byte[] byteRet = new byte[8];

for (int i = 0; i < 8; i++) {

byteRet[i] = (byte) ((value >> 8 * i) & 0xff);

}

return byteRet;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 MySQLPointLineString 类型Java 中可以使用 JDBC 驱动程序来将其解析为 double 类型。 这里提供一个示例代码,使用 JDBC 驱动程序连接 MySQL 数据库,查询 Point 类型的数据并解析为 double 类型: ```java import java.sql.*; public class MySQLExample { public static void main(String[] args) { // MySQL 数据库连接信息 String url = "jdbc:mysql://localhost:3306/testdb"; String username = "root"; String password = "password"; try (Connection conn = DriverManager.getConnection(url, username, password)) { // 查询 Point 类型的数据 String sql = "SELECT point_column FROM table_name WHERE id = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, 1); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { // 解析 Point 类型的数据 String pointStr = rs.getString("point_column"); String[] coordinates = pointStr.split("\\s+"); double x = Double.parseDouble(coordinates[0].substring(6)); double y = Double.parseDouble(coordinates[1].substring(0, coordinates[1].length() - 1)); System.out.println("x: " + x + ", y: " + y); } } catch (SQLException e) { e.printStackTrace(); } } } ``` 这个示例代码中,首先连接 MySQL 数据库,然后查询 Point 类型的数据。接着,通过 split() 方法将 Point 类型的字符串分割坐标数组,再使用 Double.parseDouble() 方法将坐标字符串转换为 double 类型的坐标值。 对于 LineString 类型的数据,也可以使用类似的方法进行解析。不过需要注意的是,LineString 类型的数据是由多个 Point 类型的坐标构的,需要对每个 Point 进行解析
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值