-------------------------------------------------------------------------

TITLE:  ZM values and SRID for Simple Features 

AUTHOR:	Name: Sandro Santilli
	Email: strk@refractions.net

DATE:   27 December 2005

CATEGORY: Simple Features Revision Proposal

-------------------------------------------------------------------------

1. Background

OpenGIS document 99-402r2 introduces semantic and well-known
representations for Z-geometries. This proposal extend the well-known
representations to optionally also hold a measure (M) and a SRID.
Misures, as Z values, are attributes of 2D vertexes, but their
semantic is unspecified in this document, as they could be used
for any kind 'misurement'. SRID is an attribute of the whole feature.

This document defines how geometry can have Z,M or both values and SRID
in a way which is compatible to the existing 2D OpenGIS Simple Features
specification AND to the Z-Geometry documented in OpenGIS 99-402r2.

2. Proposal

2.1. Definition of ZM-Geometry

a) A geometry can have either 2, 3 or 4 dimensions.
b) 3rd dimension of a 3d geometry can either represent Z or M (3DZ or 3DM).
c) 4d geometries contain both Z and M (in this order).
d) M and Z values are associated with every vertex.
e) M and Z values are undefined within surface interiors.

Any ZM-Geometry can be converted into a 2D geometry by discarding all its
Z and M values. The resulting 2D geometry is the "shadow" of the ZM-Geometry.
2D geometries cannot be safely converted into ZM-Geometries, since their Z
and M values are undefined, and not necessarily zero.

2.2. Extensions to Well-Known-Binary format

The 2d OpenGIS Simple Features specification has the following geometry types:

enum wkbGeometryType {
	wkbPoint = 1,
	wkbLineString = 2,
	wkbPolygon = 3,
	wkbMultiPoint = 4,
	wkbMultiLineString = 5,
	wkbMultiPolygon = 6,
	wkbGeometryCollection = 7
}

Document 99-402r2 introduces a Z-presence flag (wkbZ) which OR'ed
to the type specifies the presence of Z coordinate:

	wkbZ = 0x80000000

This proposal suggest the use of an M-presence flag (wkbM) to
allow for XY, XYM, XYZ and XYZM geometryes, and SRID-presence
flag to allow for embedded SRID:

	wkbM = 0x40000000
	wkbSRID = 0x20000000

Possible resulting geometry types are:

enum wkbGeometryTypeZ {

	wkbPoint = 1,
	wkbLineString = 2,
	wkbPolygon = 3,
	wkbMultiPoint = 4,
	wkbMultiLineString = 5,
	wkbMultiPolygon = 6,
	wkbGeometryCollection = 7,

	// | 0x80000000
	wkbPointZ = 0x80000001,
	wkbLineStringZ = 0x80000002,
	wkbPolygonZ = 0x80000003,
	wkbMultiPointZ = 0x80000004,
	wkbMultiLineStringZ = 0x80000005,
	wkbMultiPolygonZ = 0x80000006,
	wkbGeometryCollectionZ = 0x80000007,

	// | 0x40000000
	wkbPointM = 0x40000001,
	wkbLineStringM = 0x40000002,
	wkbPolygonM = 0x40000003,
	wkbMultiPointM = 0x40000004,
	wkbMultiLineStringM = 0x40000005,
	wkbMultiPolygonM = 0x40000006,
	wkbGeometryCollectionM = 0x40000007,

	// | 0x40000000 | 0x80000000
	wkbPointZM = 0xC0000001,
	wkbLineStringZM = 0xC0000002,
	wkbPolygonZM = 0xC0000003,
	wkbMultiPointZM = 0xC0000004,
	wkbMultiLineStringZM = 0xC0000005,
	wkbMultiPolygonZM = 0xC0000006,
	wkbGeometryCollectionZM = 0xC0000007,

	// | 0x20000000 
	wkbPointS = 0x20000001,
	wkbLineStringS = 0x20000002,
	wkbPolygonS = 0x20000003,
	wkbMultiPointS = 0x20000004,
	wkbMultiLineStringS = 0x20000005,
	wkbMultiPolygonS = 0x20000006,
	wkbGeometryCollectionS = 0x20000007,

	// | 0x20000000 | 0x80000000
	wkbPointZS = 0xA0000001,
	wkbLineStringZS = 0xA0000002,
	wkbPolygonZS = 0xA0000003,
	wkbMultiPointZS = 0xA0000004,
	wkbMultiLineStringZS = 0xA0000005,
	wkbMultiPolygonZS = 0xA0000006,
	wkbGeometryCollectionZS = 0xA0000007,

	// | 0x20000000 | 0x40000000
	wkbPointMS = 0x60000001,
	wkbLineStringMS = 0x60000002,
	wkbPolygonMS = 0x60000003,
	wkbMultiPointMS = 0x60000004,
	wkbMultiLineStringMS = 0x60000005,
	wkbMultiPolygonMS = 0x60000006,
	wkbGeometryCollectionMS = 0x60000007,

	// | 0x20000000 | 0x40000000 | 0x80000000
	wkbPointZMS = 0xE0000001,
	wkbLineStringZMS = 0xE0000002,
	wkbPolygonZMS = 0xE0000003,
	wkbMultiPointZMS = 0xE0000004,
	wkbMultiLineStringZMS = 0xE0000005,
	wkbMultiPolygonZMS = 0xE0000006,
	wkbGeometryCollectionZMS = 0xE0000007,
}


If the SRID flag is set it's value is encoded as a 4byte integer
right after the type integer.

If only wkbZ or wkbM flags are set Point coordinates will
be XYZ or XYM, if both wkbZ and wkbM flags are set Point
coordinates will be XYZM (Z first).

For example, a ZM-Point geometry at the location (10,20) with Z==30,
M==40 and SRID=4326 would be:

WKBPoint {

	byte    byteOrder;      // wkbXDR or wkbNDR

	uint32  wkbType;        // (wkbPoint+wkbZ+wkbM+wkbSRID) =
			        // 0xE0000001

	uint32  SRID;           // 4326

	Point {
		Double    x;    // 10.0
		Double    y;    // 20.0
		Double    z;    // 30.0
		Double    m;    // 40.0
	}
}


2.3. Extensions to Well-Known-Text format

Geometry SRID presence and value would be represented using a
"SRID=#;" prefix to the WKT text:

	"SRID=4326;POINT(1 2)"

3DZ geometry will be represented as:

	"POINT(1 2 3)"

4D geometry will be represented as:

	"POINT(1 2 3 4)"

3DM geometry will be represented as:

	"POINTM(1 2 3)"
	or
	"GEOMETRYCOLLECTIONM(POINTM(1 2 3), LINESTRINGM(1 2 3, 4 5 6))"

Note that the coordinates structure of a geometry must be consistent,
you can't mix dimensions in a single geometry.

