1
===PostGIS Serialized Geometry Format===
7
This document describe the serialied form of
8
Geometries as implemented starting from version 1.x.
10
Prior versions used a different format.
12
The new format is aimed at reducing geometries size, for
13
this reason we refer to the old format as HWGEOM (High Weight GEOMetry)
14
and to the new one as LWGEOM (Light Weight GEOMetry).
17
==Serialized Form and PostgreSQL varlena objects==
20
PostGIS serialized form is a recursive structure, where
21
elements in a collection (multi* or geometrycollections)
22
are complete serialized forms themself.
24
PostgreSQL requires to add an header to variable-lenght
25
user defined types, for this reason, the type actually
26
stored on disk is a SERIALIZED geometry wrapped in this
27
required header. We call the complete PostgreSQL type
31
==Geometry Type byte==
34
All serialiezd geometries start with a single byte
35
encoding geometry type (lower nibble) and flags
44
B = 16 byte BOX2DFLOAT4 follows (probably not aligned) [before SRID]
45
S = 4 byte SRID attached (0= not attached (-1), 1= attached)
46
ZM = dimensionality (hasZ, hasM)
54
wkbMultiLineString = 5
56
wkbGeometryCollection = 7
58
* Note that the following values do not correspond to wkb type values.
65
==Bounding box and SRID==
68
Following the Geometry type byte we find optional bounding box
69
and/or SRID values. Presence of them is encoded in the type byte.
71
If both objects are present the bounding box come first.
73
Bounding box is composed by 4 32-bit float:
75
[FLOAT32] [FLOAT32] [FLOAT32] [FLOAT32]
78
SRID is composed by a 32-bit integer:
87
When it comes to ordinate values all geometries use arrays of 64-bit
88
floats. Number and semantic of values depend on the dimension flags
89
on the geometry type byte:
96
[FLOAT64] [FLOAT64] [FLOAT64]
100
[FLOAT64] [FLOAT64] [FLOAT64]
104
[FLOAT64] [FLOAT64] [FLOAT64] [FLOAT64]
110
What you find after the type byte and optional bbox and SRID
111
(COMMON_HEADER from now on) depends on actual geometry type.
115
A Point geometry is as follows:
118
<ORDINATE_ARRAY> -- single element
122
A LineString geometry is as follows:
125
<UINT32> -- number of elements in following ORDINATE_ARRAY
130
A CircularString geometry is as follows:
133
<UINT32> -- number of elements in following ORDINATE_ARRAY
138
A CompoundString geometry is as follows:
141
<UINT32> -- number of segments
142
One or more geometries as specified by previous uint32:
143
<SERIALIZED_GEOMETRY> -- Must be some combination of
144
LineStrings and CircularStrings.
148
A Polygon geometry is as follows:
152
<UINT32> -- number of ORDINATE_ARRAYs (at least 1, the shell)
154
One or more arrays (rings) as specified by previous uint32:
156
<UINT32> -- number of elements in following ORDINATE_ARRAY
157
<ORDINATE_ARRAY> -- single element
161
A CurvePolygon geometry is as follows:
164
<UINT32> -- number of rings
165
One or more geometries as specified by previous uint32:
166
<SERIALIZED_GEOMETRY> -- Must be some combination of
167
LineStrings and CircularStrings.
168
CompoundString are not yet supported.
172
All collection types (MultiPoint, MultiLineString, MultiPolygon,
173
GeometryCollection) have the same structure. Real type is just
174
an hint on what one should expect from element geometries.
175
Note that the GeometryCollection type is also used for EMPTY
176
Geometries (number_of_geometries is set to 0 in this case).
178
A Collection geometry is as follows:
182
<UINT32> -- number of GEOMETRIES
184
Zero (EMPTY) or more geometries as specified by previous uint32:
186
<SERIALIZED_GEOMETRY> -- starting from type byte again
192
=3DZ point, no bounding box, no SRID=
194
<char> TYPE - flags:__Z_ type:1
199
=3DM point, bounding box, no SRID=
201
<char> TYPE - flags:B__M type:1
210
=2D LineString, no bounding box, SRID=
212
<char> TYPE - flags:_S__ type:2
227
=2D polygon, no bounding box, no SRID=
229
<char> TYPE - flags:____ type:3
257
=2D Collection of a point and a line, SRID and bbox present=
259
<char> TYPE - flags:BS__ type:7
272
<char> TYPE - flags:____ type:1 ( the point )
276
<char> TYPE - flags:____ type:2 ( the line )
285
Note that in the inner geometries:
287
- SRID value is inherited by parent geometry type.
289
- BBOX value is not present (can be, however)
291
- ZM flags must exactly match those in the outer geometry type byte