Well-Known Text and Well-Known Binary (WKT and WKB)

Well-known text (WKT) is a text markup language for representing vector geometry objects on a map and spatial reference systems of spatial objects. A binary equivalent, known as well-known binary (WKB) is used to transfer and store the same information for geometry objects. Both formats are regulated by the Open Geospatial Consortium (OGC). The background information brought here draws on OGC and Wikipedia articles on the topic. See: http://www.opengeospatial.org/standards/sfa and http://en.wikipedia.org/wiki/Well-known_binary.

 

Well-Known Text representation of spatial reference systems

The Well-Known Text representation of spatial reference systems provides a standard textual representation for spatial reference system information. A spatial reference system, also referred to as a coordinate system, is a projected (X, Y), geographic (latitude-longitude), or geocentric (X, Y, Z) coordinate system. A data set's coordinate system is identified by the PROJCS keyword if the data are in projected coordinates, by GEOGCS if in geographic coordinates, or by GEOCCS if in geocentric coordinates (A geocentric coordinate system has its origin at the center of the Earth).

 

§   For projected coordinates, the PROJCS keyword is followed by all of the components which define the projected coordinate system: the projected coordinate system name, the geographic coordinate system, the map projection, 0 or more parameters, and the linear unit of measure.

§   A geographic coordinate system is defined by its name, the datum, the prime meridian, and the angular unit of measure.

§   A geocentric coordinate system, quite similar to a geographic coordinate system, is defined by its name, the datum, the prime meridian, and the linear unit of measure.

 

Each system component has a keyword in upper case (for example, DATUM or UNIT) followed by the defining, comma-delimited, parameters of the component in brackets. Some components are composed of sub-components so the result is a nested structure.

 

Example Coordinate System WKT

PROJCS["Zone 10N (126 W to 120 W)",

                                                GEOGCS["WGS84 Coordinate System",

                                                                DATUM["WGS 1984",

                                                                                SPHEROID["WGS 1984",6378137,298.257223563],

                                                                                TOWGS84[0,0,0,0,0,0,0],

                                                                                AUTHORITY["EPSG","6326"]],

                                                                PRIMEM["Greenwich",0],

                                                                UNIT["degree",0.0174532925199433],

                                                                AUTHORITY["EPSG","4326"]],

                                                PROJECTION["Transverse_Mercator"],

                                                PARAMETER["false_easting",500000],

                                                PARAMETER["false_northing",0],

                                                PARAMETER["latitude_of_origin",0],

                                                PARAMETER["central_meridian",-123],

                                                PARAMETER["scale_factor",0.9996],

                                                UNIT["METERS",1],

                                                AUTHORITY["EPSG","32610"],

                                                AUTHORITY["SBMG","UTM,UTM-10N,WGS84,METERS"]]

 

Well-Known Text representation for geometric objects

The Well-Known Text representation for geometric objects can be used both to construct new instances of the type and to convert existing instances to textual form for alphanumeric display. Geometric objects that can be represented with WKT include: points, lines, polygons, multi-geometries that represent more than one geometry of the same dimension in a single object, and geometry collections that store geometries of different dimensions.

 

The Well-Known Text description is made up of three components: geometry type, coordinate type, and coordinate list. The coordinate type specifies whether or not the geometry has Z coordinates and/or a linear referencing system. If the geometry has neither, this argument is left blank. If the geometry has Z coordinates, the coordinate type is set to Z, if the geometry has a linear referencing system, it is set it to M and if it has both, then to ZM.

The coordinate list defines the double-precision vertices of the geometry. Coordinate lists are separated by commas and enclosed by parentheses. If a geometry has multiple components, parentheses must enclose each component part (e.g. MultiPoint ((10 10), (20 20))) If the geometry is empty, the keyword EMPTY follows the geometry type.

 

The following are some example geometric WKT strings.

 

Geometry Type

Text Literal Representation

Comment

Point

Point (4 6)

A Point

Point

Point ZM (1 2 5 40)

A Point with Z coordinates and a linear referencing system.

Point

Point M (1 2 80)

A Point with a linear referencing system.

Point

Point Empty

An empty geometry

LineString

LineString ( 10 10, 20 20, 30 40)

A LineString with 3 points

Polygon

Polygon ((10 10, 10 20, 20 20, 20 15, 10 10))

A Polygon with 1 exteriorRing and 0 interiorRings

Multipoint

MultiPoint ((10 10), (20 20))

A MultiPoint with 2 points

MultiLineString

MultiLineString ( (10 10, 20 20), (15 15, 30 15) )

A MultiLineString with 2 linestrings

MultiPolygon

MultiPolygon ( ((10 10, 10 20, 20 20, 20 15, 10 10)), ((60 60, 70 70, 80 60, 60 60 )) )

A MultiPolygon with 2 polygons

GeomCollection

GeometryCollection ( POINT (10 10), POINT (30 30), LINESTRING (15 15, 20 20) )

A Geometry Collection consisting of 2 Point values and a LineString value

 

Well-Known Binary representation for geometry

The Well-Known Binary Representation for Geometry (WKBGeometry) provides a portable representation of a geometric object as a contiguous stream of bytes.

 

The first byte in the stream identifies how the binary values are represented, either: NDR (Network Data Representation or XDR (eXtended Data Representation). The difference between the two encodings is byte order. NDR is little endian, which means that an unsigned integer – a 32 bit data type that encodes a nonnegative integer - stores the least significant byte first, while a double – a 64 bit double precision data type that encodes a double precision number using the IEEE 54 double precision format - stores the sign bit as the last byte. XDR is big endian, so the byte order is reversed.

The next component in the stream indicates the geometry type. Values from 1 through 7 indicate Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection.

If a geometry consists of multiple geometries, additional bytes indicate the number of geometries.

The next byte component indicates the number of points in the first shape, followed by the X,Y coordinates of each of the points. For each additional shape, a byte indicates the number of points, followed by bytes defining each point’s coordinate values.