首页 > 代码库 > 空间数据存储格式wkb和wkt(转载)

空间数据存储格式wkb和wkt(转载)

wkt(OGC well-known text)和wkb(OGC well-known binary)是OGC制定的空间数据的组织规范,wkt是以文本形式描述,wkb是以二进制形式描述。
使用wkt和wkb能够很好到和其他系统进行数据交换,目前大部分支持空间数据存储的数据库构造空间数据都采用这两种方式。
wkt的组织结构如下:

Geometry type Text description Comment
ST_Point ‘point empty‘ empty point
ST_Point ‘point z empty‘ empty point with z-coordinate
ST_Point ‘point m empty‘ empty point with measure
ST_Point ‘point zm empty‘ empty point with z-coordinate and measure
ST_Point ‘point ( 10.05 10.28 )‘ point
ST_Point ‘point z( 10.05 10.28 2.51 )‘ point with z-coordinate
ST_Point ‘point m( 10.05 10.28 4.72 )‘ point with measure
ST_Point ‘point zm(10.05 10.28 2.51 4.72 )‘ point with z-coordinate and measure
ST_LineString ‘linestring empty‘ empty linestring
ST_LineString ‘linestring z empty‘ empty linestring with z-coordinates
ST_LineString ‘linestring m empty‘ empty linestring with measures
ST_LineString ‘linestring zm empty‘ empty linestring with z-coordinates and measures
ST_LineString ‘linestring (10.05 10.28 , 20.95 20.89 )‘ linestring
ST_LineString ‘linestring z(10.05 10.28 3.09, 20.95 31.98 4.72, 21.98 29.80 3.51 )‘ linestring with z-coordinates
ST_LineString ‘linestring m(10.05 10.28 5.84, 20.95 31.98 9.01, 21.98 29.80 12.84 )‘ linestring with measures
ST_LineString ‘linestring zm(10.05 10.28 3.09 5.84, 20.95 31.98 4.72 9.01, 21.98 29.80 3.51 12.84)‘ linestring with z-coordinates and measures
ST_Polygon ‘polygon empty‘ empty polygon
ST_Polygon ‘polygon z empty‘ empty polygon with z-coordinates
ST_Polygon ‘polygon m empty‘ empty polygon with measures
ST_Polygon ‘polygon zm empty‘ empty polygon with z-coordinates and measures
ST_Polygon ‘polygon ((10 10, 10 20, 20 20, 20 15, 10 10))‘ polygon
ST_Polygon ‘polygon z((10 10 3, 10 20 3, 20 20 3, 20 15 4, 10 10 3))‘ polygon with z-coordinates
ST_Polygon ‘polygon m((10 10 8, 10 20 9, 20 20 9, 20 15 9, 10 10 8 ))‘ polygon with measures
ST_Polygon ‘polygon zm((10 10 3 8, 10 20 3 9, 20 20 3 9, 20 15 4 9, 10 10 3 8 ))‘ polygon with z-coordinates and measures
ST_MultiPoint ‘multipoint empty‘ empty multipoint
ST_MultiPoint ‘multipoint z empty‘ empty multipoint with z-coordinates
ST_MultiPoint ‘multipoint m empty‘ empty multipoint with measures
ST_MultiPoint ‘multipoint zm empty‘ empty multipoint with z-coordinates and measures
ST_MultiPoint ‘multipoint (10 10, 20 20)‘ multipoint with two points
ST_MultiPoint ‘multipoint z(10 10 2, 20 20 3)‘ multipoint with z-coordinates
ST_MultiPoint ‘multipoint m(10 10 4, 20 20 5)‘ multipoint with measures
ST_MultiPoint ‘multipoint zm(10 10 2 4, 20 20 3 5)‘ multipoint with z-coordinates and measures
ST_MultiLineString ‘multilinestring empty‘ empty multilinestring
ST_MultiLineString ‘multilinestring z empty‘ empty multilinestring with z-coordinates
ST_MultiLineString ‘multilinestring m empty‘ empty multilinestring with measures
ST_MultiLineString ‘multilinestring zm empty‘ empty multilinestring with z-coordinates and measures
ST_MultiLineString ‘multilinestring ((10.05 10.28 , 20.95 20.89 ),( 20.95 20.89, 31.92 21.45))‘ multilinestring
ST_MultiLineString ‘multilinestring z((10.05 10.28 3.4, 20.95 20.89 4.5),( 20.95 20.89 4.5, 31.92 21.45 3.6))‘ multilinestring with z-coordinates
ST_MultiLineString ‘multilinestring m((10.05 10.28 8.4, 20.95 20.89 9.5), (20.95 20.89 9.5, 31.92 21.45 8.6))‘ multilinestring with measures
ST_MultiLineString ‘multilinestring zm((10.05 10.28 3.4 8.4, 20.95 20.89 4.5 9.5), (20.95 20.89 4.5 9.5, 31.92 21.45 3.6 8.6))‘ multilinestring with z-coordinates and measures
ST_MultiPolygon ‘multipolygon empty‘ empty multipolygon
ST_MultiPolygon ‘multipolygon z empty‘ empty multipolygon with z-coordinates
ST_MultiPolygon ‘multipolygon m empty‘ empty multipolygon with measures
ST_MultiPolygon ‘multipolygon zm empty‘ empty
ST_MultiPolygon ‘multipolygon (((10 10, 10 20, 20 20, 20 15 , 10 10), (50 40, 50 50, 60 50, 60 40, 50 40)))‘ multipolygon
ST_MultiPolygon ‘multipolygon z(((10 10 7, 10 20 8, 20 20 7, 20 15 5, 10 10 7), (50 40 6, 50 50 6, 60 50 5, 60 40 6, 50 40 6)))‘ multipolygon with z-coordinates
ST_MultiPolygon ‘multipolygon m(((10 10 2, 10 20 3, 20 20 4, 20 15 5, 10 10 2), (50 40 7, 50 50 3, 60 50 4, 60 40 5, 50 40 7)))‘ multipolygon with measures
ST_MultiPolygon ‘multipolygon zm(((10 10 7 2, 10 20 8 3, 20 20 7 4, 20 15 5 5, 10 10 7 2), (50 40 6 7, 50 50 6 3, 60 50 5 4, 60 40 6 5, 50 40 6 7)))‘ multipolygon with z-coordinates and measures



wkb的组织结构如下:
基本类型定义:
byte : 1 byte
uint32 : 32 bit unsigned integer  (4 bytes)
double : double precision number (8 bytes)
Building Blocks : Point, LinearRing
Point {
double x;
double y;
};
LinearRing   {
uint32 numPoints;
Point points[numPoints];
}
enum wkbGeometryType {      
wkbPoint = 1,
wkbLineString = 2,
wkbPolygon = 3,
wkbMultiPoint = 4,
wkbMultiLineString = 5,
wkbMultiPolygon = 6,
wkbGeometryCollection = 7
};
enum wkbByteOrder {
wkbXDR = 0,     Big Endian
wkbNDR = 1     Little Endian
};
WKBPoint {
byte    byteOrder;
uint32   wkbType;       1
Point    point;
}
WKBLineString {
byte    byteOrder;
uint32   wkbType;      2
uint32   numPoints;
Point    points[numPoints];
}
WKBPolygon {
byte    byteOrder;
uint32   wkbType;      3
uint32   numRings;
LinearRing  rings[numRings];
}
WKBMultiPoint {
byte    byteOrder;
uint32   wkbType;          4
uint32   num_wkbPoints;
WKBPoint   WKBPoints[num_wkbPoints];
}
WKBMultiLineString {
byte    byteOrder;
uint32   wkbType;      5
uint32   num_wkbLineStrings;
WKBLineString WKBLineStrings[num_wkbLineStrings];
}
wkbMultiPolygon {    
byte    byteOrder;       

uint32   wkbType;      6
uint32   num_wkbPolygons;
WKBPolygon  wkbPolygons[num_wkbPolygons];
}
WKBGeometry  {
union {
WKBPoint      point;
WKBLineString    linestring;
WKBPolygon     polygon;
WKBGeometryCollection collection;
WKBMultiPoint    mpoint;
WKBMultiLineString  mlinestring;
WKBMultiPolygon   mpolygon;
}
};
WKBGeometryCollection {
byte    byte_order;
uint32   wkbType;      7
uint32   num_wkbGeometries;
WKBGeometry  wkbGeometries[num_wkbGeometries]
}
下面是一个point(1,1) 使用WKB存储的例子:
0101000000000000000000F03F000000000000F03F
这个2进制流可以按照WKBPoint的结构进行拆分:
Byte order : 01
WKB type   : 01000000
X          : 000000000000F03F
Y          : 000000000000F03F
byte order要么为0,要么为1,0为使用little-endian编码(NDR),1为使用big-endian编码(XDR)。
WKB type 是几何类型,在wkbGeometryType中定义. 值为1-7,分别对应 Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection. 
x,y为点的坐标值,为double类型。

空间数据存储格式wkb和wkt(转载)