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
将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; }