在地理信息系统 (GIS) 中,GeometryCollection 可以用来表示复杂的空间对象,如具有多个内部环的多边形、多个线段的路径等。
以下是一个GeometryCollection 其中包含了一个点,一条线,一个多面(多个多边形)
GEOMETRYCOLLECTION(POINT(120 36.1), LINESTRING(120 36.1, 120 36.2, 120 36.3), MULTIPOLYGON(((0 0, 0 10, 10 10, 10 0, 0 0), (2 2, 2 8, 8 8, 8 2, 2 2)), ((15 15, 15 20, 20 20, 20 15, 15 15))))
byte[370] 00 00 00 00, 01, 07 00 00 00, 03 00 00 00, 01, 01 00 00 00, 00 00 00 00 00 00 5E 40, CD CC CC CC CC 0C 42 40, 01, 02 00 00 00, 03 00 00 00, 00 00 00 00 00 00 5E 40, CD CC CC CC CC 0C 42 40, 00 00 00 00 00 00 5E 40, 9A 99 99 99 99 19 42 40, 00 00 00 00 00 00 5E 40, 66 66 66 66 66 26 42 40, 01, 06 00 00 00, 02 00 00 00, 01, 03 00 00 00, 02 00 00 00, 05 00 00 00, 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 24 40 00 00 00 00 00 00 24 40 00 00 00 00 00 00 24 40 00 00 00 00 00 00 24 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 00 20 40 00 00 00 00 00 00 20 40 00 00 00 00 00 00 20 40 00 00 00 00 00 00 20 40 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 40 01 03 00 00 00 01 00 00 00 05 00 00 00 00 00 00 00 00 00 2E 40 00 00 00 00 00 00 2E 40 00 00 00 00 00 00 2E 40 00 00 00 00 00 00 34 40 00 00 00 00 00 00 34 40 00 00 00 00 00 00 34 40 00 00 00 00 00 00 34 40 00 00 00 00 00 00 2E 40 00 00 00 00 00 00 2E 40 00 00 00 00 00 00 2E 40 Byte Order (1 byte): 用于指示字节顺序的字节,常见的取值为 0 表示大端字节序,1 表示小端字节序。 Geometry Type (4 bytes): 用于表示 GeometryCollection 对象的类型,对应于几何类型代码。在 WKB 格式中,GeometryCollection 对象的类型代码为 7。 Num Geometries (4 bytes): 用于表示 GeometryCollection 中包含的几何对象的数量。 Geometry 1, Geometry 2, ..., Geometry N: 表示 GeometryCollection 中的每个几何对象的 WKB 表示。 component size(起-止) decimal hex comment SRID 4(0-3) 0 00 00 00 00 Endian 1(4-4) 1 01 WKB type 4(5-8) 7 07 00 00 00 GeometryCollection Geometry count 4(9-12) 2 03 00 00 00 包含3个直接子元素:1个点,一条线,一个多面(多个多边形) 第0个直接子元素:Point Endian 1(13-13) 1 01 WKB type 4(14-17) 1 01 00 00 00 Point X 8(18-25) 120 00 00 00 00 00 00 5E 40 Y 8(26-33) 36.1 CD CC CC CC CC 0C 42 40 第1个直接子元素:LINESTRING Endian 1(34-34) 1 01 WKB type 4(35-38) 2 02 00 00 00 LINESTRING Point count 4(39-42) 3 03 00 00 00 包含3个点 X 8(43-50) 120 00 00 00 00 00 00 5E 40 Y 8(51-58) 36.1 CD CC CC CC CC 0C 42 40 X 8(59-66) 120 00 00 00 00 00 00 5E 40 Y 8(67-74) 36.2 9A 99 99 99 99 19 42 40 X 8(75-82) 120 00 00 00 00 00 00 5E 40 Y 8(83-90) 36.3 66 66 66 66 66 26 42 40 第2个直接子元素:MULTIPOLYGON Endian 1(91-91) 1 01 WKB type 4(92-95) 6 06 00 00 00 MultiPolygon polygon count 4(96-99) 2 02 00 00 00 共2个直接子元素(Polygon) 面0 Endian 1(100-100) 1 01 针对第0个直接子元素(Polygon) WKB type 4(101-104) 3 03 00 00 00 第0个直接子元素(Polygon)类型:Polygon ring count 4(105-108) 2 02 00 00 00 第0个Polygon中共2个环 面0外环 point count 4(109-112) 5 05 00 00 00 第0个Polygon外环点数量 X1(经度) 8(113-120) 0 00 00 00 00 00 00 00 00 Y1(纬度) 8(121-128) 0 00 00 00 00 00 00 00 00 X2(经度) 8(129-136) 0 00 00 00 00 00 00 00 00 Y2(纬度) 8(137-144) 10 00 00 00 00 00 00 24 40 X3(经度) 8(145-152) 10 00 00 00 00 00 00 24 40 Y3(纬度) 8(153-160) 10 00 00 00 00 00 00 24 40 X4(经度) 8(161-168) 10 00 00 00 00 00 00 24 40 Y4(纬度) 8(169-176) 0 00 00 00 00 00 00 24 40 X5(经度) 8(177-184) 0 00 00 00 00 00 00 00 00 Y6(纬度) 8(185-192) 0 00 00 00 00 00 00 00 00 面0内环0 point count 4(193-196) 5 05 00 00 00 第0个Polygon第0个内环点数量 X1(经度) 8(197-204) 2 00 00 00 00 00 00 00 40 Y1(纬度) 8(205-212) 2 00 00 00 00 00 00 00 40 X2(经度) 8(213-220) 2 00 00 00 00 00 00 00 40 Y2(纬度) 8(221-228) 8 00 00 00 00 00 00 20 40 X3(经度) 8(229-236) 8 00 00 00 00 00 00 20 40 Y3(纬度) 8(235-244) 8 00 00 00 00 00 00 20 40 X4(经度) 8(245-252) 8 00 00 00 00 00 00 20 40 Y4(纬度) 8(253-260) 2 00 00 00 00 00 00 00 40 X5(经度) 8(261-268) 2 00 00 00 00 00 00 00 40 Y5(纬度) 8(269-276) 2 00 00 00 00 00 00 00 40 面1 Endian 1(277-277) 1 01 针对第1个直接子元素(Polygon) WKB type 4(278-281) 3 03 00 00 00 第1个直接子元素(Polygon)类型:Polygon ring count 4(282-285) 1 01 00 00 00 第1个Polygon中共2个环 面1外环 point count 4(286-289) 5 05 00 00 00 第0个Polygon外环点数量 X1(经度) 8(290-297) 15 00 00 00 00 00 00 2E 40 Y1(纬度) 8(298-305) 15 00 00 00 00 00 00 2E 40 X2(经度) 8(306-313) 15 00 00 00 00 00 00 2E 40 Y2(纬度) 8(314-321) 20 00 00 00 00 00 00 34 40 X3(经度) 8(322-329) 20 00 00 00 00 00 00 34 40 Y3(纬度) 8(330-337) 20 00 00 00 00 00 00 34 40 X4(经度) 8(338-345) 20 00 00 00 00 00 00 34 40 Y4(纬度) 8(346-353) 15 00 00 00 00 00 00 2E 40 X5(经度) 8(354-361) 15 00 00 00 00 00 00 2E 40 Y5(纬度) 8(362-369) 15 00 00 00 00 00 00 2E 40
Java解析以上结构,由于这个比较啰嗦,点线面在前几篇也都解析过了,所以直接上最终代码,不逐个字节解析了
/** * 解析MGeometryCollection * @param bytes bytes * @return GeometryCollection */ public static GeometryCollection parseGeometryCollection(byte[] bytes){ GeometryCollection collection = new GeometryCollection(); ByteBuffer buffer = new ByteBuffer(bytes, bytes[4], 9); //Geometry count int geometryCount = buffer.readInt(); for(int g=0; g<geometryCount; g++){ //Endian buffer.step(1); //WKB type int type = buffer.readInt(); Geometry geometry = null; if(type == 1){ geometry = point(buffer); }else if(type == 2){ geometry = line(buffer); }else if(type == 3){ geometry = polygon(buffer); }else if(type == 4){ geometry = multiPoint(buffer); }else if(type == 5){ geometry = multiLine(buffer); }else if(type == 6){ geometry = multiPolygon(buffer); } if(null != geometry) { collection.add(geometry); } } return collection; } /** * 生成wkb格式要 * @param geometry geometry * @return bytes */ public static byte[] wkb(Geometry geometry){ if(geometry instanceof Point){ return wkb((Point)geometry); }else if(geometry instanceof Line){ return wkb((Line)geometry); }else if(geometry instanceof Polygon){ return wkb((Polygon)geometry); }else if(geometry instanceof MultiPoint){ return wkb((MultiPoint)geometry); }else if(geometry instanceof MultiLine){ return wkb((MultiLine)geometry); }else if(geometry instanceof MultiPolygon){ return wkb((MultiPolygon)geometry); }else if(geometry instanceof GeometryCollection){ return wkb((GeometryCollection)geometry); } return null; }源码参考: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