gpt4 book ai didi

java - JDBC:如何读取MySQL点格式

转载 作者:行者123 更新时间:2023-11-29 03:17:48 25 4
gpt4 key购买 nike

我在 MySQL 中有一个 2D POINT 列。我使用 JDBC 将其读入 Java 对象。结果是一个字节[25]。我想知道这是什么格式,以便我可以将其转换为我的观点(例如 double 值)

点是这样设置的:

UPDATE ... SET point=GeomFromText('POINT(18 63)')

数据库中的结果字节:

0x00000000010100000000000000000032400000000000804F40

最佳答案

因此,对于解决方案,我使用了 WKB 阅读器中的代码:https://github.com/simplegeo/jts/tree/master/src/com/vividsolutions/jts/io

因为我不想包含所有内容,所以我只使用了从 ByteOrderValues.java 中以字节序格式读取 double 的代码:

/**
* Read a long from a byte buffer in big endian format.
* @param buf must be 8 bytes
*/
public static long readLongFromBytesBigEndian(byte[] buf) {
return (long) (buf[0] & 0xff) << 56
| (long) (buf[1] & 0xff) << 48
| (long) (buf[2] & 0xff) << 40
| (long) (buf[3] & 0xff) << 32
| (long) (buf[4] & 0xff) << 24
| (long) (buf[5] & 0xff) << 16
| (long) (buf[6] & 0xff) << 8
| (long) (buf[7] & 0xff);
}

/**
* Read a long from a byte buffer in little endian format.
* @param buf must be 8 bytes
*/
public static long readLongFromBytesLittleEndian(byte[] buf) {
return (long) (buf[7] & 0xff) << 56
| (long) (buf[6] & 0xff) << 48
| (long) (buf[5] & 0xff) << 40
| (long) (buf[4] & 0xff) << 32
| (long) (buf[3] & 0xff) << 24
| (long) (buf[2] & 0xff) << 16
| (long) (buf[1] & 0xff) << 8
| (long) (buf[0] & 0xff);
}

/**
* Read a long from a byte buffer in big or little endian format.
* @param bigEndian true for big endian, false for little endian
* @param buf must be 8 bytes
*/
public static double readDoubleFromBytes(byte[] buf, boolean bigEndian) {
long longVal = bigEndian ? readLongFromBytesBigEndian(buf)
: readLongFromBytesLittleEndian(buf);
return Double.longBitsToDouble(longVal);
}

/**
* Read a long from a byte buffer in big or little endian format.
* @param bigEndian true for big endian, false for little endian
* @param buf must be 8 bytes length after offset
*/
public static double readDoubleFromBytes(byte[] buf, int offset, boolean bigEndian) {
byte[] bufOf8Bytes = Arrays.copyOfRange(buf, offset, offset + 8);
return readDoubleFromBytes(bufOf8Bytes, bigEndian);
}

/**
* Read a coordinate from a byte array in WKB format.
* @param wkbBytes must be 25 bytes long
*/
public static Coordinate readCoordinateFromWkbBytes(byte[] wkbBytes) {
// Points are stored in MySQL marked as big endian, but in reality is little endian. Not good
boolean isBigEndian = false; // readIsWkbBigEndianByteOrder(wkbBytes[0]);
double x = readDoubleFromBytes(wkbBytes, 9, isBigEndian);
double y = readDoubleFromBytes(wkbBytes, 17, isBigEndian);
Coordinate coordinate = new Coordinate();
coordinate.setX(x);
coordinate.setY(y);
return coordinate;
}

public static boolean readIsWkbBigEndianByteOrder(byte b) {
final byte BIG_ENDIAN = 0;
final byte LITTLE_ENDIAN = 1;
return b == BIG_ENDIAN;
}

注意:我点的第一个字节,表示big/little endian,设置为0,根据WKBReader应该是big endian。但是我发现 littl endian 才是实际的格式。

关于java - JDBC:如何读取MySQL点格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51817883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com