DB2 Spatial Extender has several functions that generate geometries from binary representations:
The well-known binary representation is a contiguous stream of bytes. It permits geometry to be exchanged between an ODBC client and an SQL database in binary form. Because these geometry functions require the definition of C programming language structures to map the binary representation, they are intended for use within a third generation language (3GL) program. They are not suited for a fourth generation language (4GL) environment. The ST_AsBinary function converts an existing geometry value into a well-known binary representation.
The well-known binary representation for geometry is obtained by serializing a geometry instance as a sequence of numeric types. These types are drawn from the set (unsigned integer, double), and then each numeric type is serialized as a sequence of bytes. The types are serialized using one of two well-defined, standard, binary representations for numeric types (NDR, XDR). A one-byte tag that precedes the serialized bytes describes the specific binary encoding (NDR or XDR) used for a geometry byte stream. The only difference between the two encoding of geometry is one of byte order: The XDR encoding is Big Endian; the NDR encoding is Little Endian.
An unsigned integer is a 32 bit (4 byte) data type that encodes a non-negative integer in the range [0, 4294967295].
A double is a 64 bit (8 byte) double precision data type that encodes a double precision number using the IEEE 754 double precision format.
These definitions are common to both XDR and NDR.
The XDR representation of an unsigned integer is Big Endian (most significant byte first).
The XDR representation of a double is Big Endian (sign bit is first byte).
The NDR representation of an unsigned integer is Little Endian (least significant byte first).
The NDR representation of a double is Little Endian (sign bit is last byte).
Conversion between the NDR and XDR data types for unsigned integers and doubles is a simple operation. It involving reversing the order of bytes within each unsigned integer or double in the byte stream.
This section describes the well-known binary representation for geometry. The basic building block is the byte stream for a point, which consists of two doubles. The byte streams for other geometries are built using the byte streams for geometries that are already defined.
// Basic Type definitions // 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, }; 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; WKBMultiPoint mpoint; WKBMultiLineString mlinestring; WKBMultiPolygon mpolygon; } };
The following figure shows an NDR representation.
Figure 39. Representation in NDR format. (B=1) of type polygon (T=3) with 2 linears (NR=2), each ring having 3 points (NP=3).
The well-known binary representation for geometry is designed to represent instances of the geometry types described in the Geometry Object Model and in the OpenGIS Abstract Specification.
These assertions imply the following for rings, polygons, and multipolygons: