在MySQL中的数据类型用LineString
Linestring的WKT(Well-Known Text)格式:LINESTRING (x1 y1, x2 y2, ..., xn yn)其中,x和y表示点的坐标,n表示Linestring中点的数量。
例如,以下是一个包含三个点的Linestring:LINESTRING (0 0, 1 1, 2 2)这个Linestring由三个点组成,它们的坐标分别是(0,0)、(1,1)和(2,2),它们按照顺序连接起来形成一条线。对应的WKB(Well-KnownBinary)MySQL存储格式:
LINESTRING(1 2, 15 15, 11 22) bytes[61]: 00 00 00 00, 01, 02 00 00 00 ,03 00 00 00, 00 00 00 00 00 00 F0 3F, 00 00 00 00 00 00 00 40, \ 00 00 00 00 00 00 2E 40, 00 00 00 00 00 00 2E 40, 00 00 00 00 00 00 26 40 00 00 00 00 00 00 36 40 component size(起-止) decimal hex SRID 4(0-3) 0 00 00 00 00 Byte order 1(4-4) 1 01 WKB type 4(5-8) 2 02 00 00 00 point count 4(9-12) 3 03 00 00 00 X(经度) 8(13-20) 1 00 00 00 00 00 00 F0 3F Y(纬度) 8(21-28) 2 00 00 00 00 00 00 00 40 X(经度) 8(29-36) 15 00 00 00 00 00 00 2E 40 Y(纬度) 8(37-44) 15 00 00 00 00 00 00 2E 40 X(经度) 8(45-52) 11 00 00 00 00 00 00 2E 40 Y(纬度) 8(53-60) 22 00 00 00 00 00 00 2E 40
//根据上一篇的解析type=2表示LineString
Java解析以上结构,为了便于理解我们逐个字节解析(实际应用中不会这样解析,具体参考最后的源码) boolean bigEndian = (bytes[4] == 0x00); //点的数量 int count = NumberUtil.byte2int(bytes, 9, 4, bigEndian); List<Point> points = new ArrayList(); for(int i=0; i<count; i++){ double x = NumberUtil.byte2double(bytes, 13+8*i*2); double y = NumberUtil.byte2double(bytes, 21+8*i*2); Point point = new Point(x, y); points.add(point); } Line line = new Line(points);
源码参考:http://gitee.com/anyline/anyline/blob/master/anyline-data-jdbc-dialect/anyline-data-jdbc-mysql/src/main/java/org/anyline/data/jdbc/mysql/MySQLGeometryAdapter.java
NumberUtil参考:http://gitee.com/anyline/anyline/blob/master/anyline-core/src/main/java/org/anyline/util/NumberUtil.java