# Survex 3d Format Specification (v3-v7)

If you're writing in C or C++ it's strongly recommended that you use the img routine provided with Survex to read and write 3d files. Doing so means that you can take advantage of any revisions to the 3d format by simply rebuilding your software with the updated img routines, rather than having to update your own code. It also allows you to read other processed survey data formats (those from Larry Fish's Compass and from Bob Thrun's CMAP), and allows reading a sub-set of the data in a file, restricted by survey prefix.

This document only describes 3d format revisions 3 to 7 (inclusive). Newer versions are described in a separate document. Older format versions are only documented by the code to read them in img.c - they had version strings "v0.01", "Bv0.01", "bv0.01", and "v2").

The following table document which Survex versions generate which 3d file format versions. A version is able to read the format it generates and any older versions, but in addition Survex 1.0.40 has support for reading all versions up to v7 (but writes v3).

Format | Survex versions
--- | ---
v3 | 0.97 | 1.0.40
v4 | 1.1.0 | 1.1.3
v5 | 1.1.4 | 1.1.10
v6 | 1.1.11 | 1.1.14
v7 | 1.1.15 | 1.2.6

If you try to use this specification and find details which aren't spelled out clearly enough (or at all!) or any errors, please let us know. At least two people have successfully written code to read 3d files using this document, but that doesn't mean it can't be improved.

## File Header

This consists of:

- File ID: the string "Survex 3D Image File" followed by a linefeed (decimal 10, hex 0a). [Note: v0.01 files can have a carriage return before this and other linefeeds - this is a file format error in any other format version].
- File format version: "v3", "v4", "v5", "v6", "v7" followed by a linefeed. Any future versions will be "v8", "v9", "v10", "v11", etc.
- Survey title: A string followed by a linefeed. There's no length limit on this string.
- Timestamp: A string followed by a linefeed. This is intended to be the time the file was generated, rather than the time the survey data was collected. The easiest way to generate this is with the strftime() format "%a,%Y.%m.%d %H:%M:%S %Z" if you have access to strftime(). An example timestamp is "Sun,2002.03.17 14:01:07 GMT".

## Items

Following the header are a number of items. The last item must be a 0x00 byte when the current label is empty, which marks the end of the data. The first byte of an item is a code identifying what the item is:

Code | Type | Data | Meaning | Version
--- | --- | --- | --- | ---
0x00 | STOP |  | If the current label is empty, signifies the end of the data in the 3d file; if the current label isn't empty, make it empty. | ≥3

0x01 - 0x0e

TRIM

Trim the last 16 characters of the current label, then trim back N (i.e. 1-14) dots ("."), everything after that particular dot. It's incorrect if the label ends up empty, or you attempt to trim more label than there is. The rationale for removing 16 characters first is that removal of 1-16 characters can be encoded by 0x10-0x1f (see below) and we can make this encoding more powerful by not overlapping what can be encoded.

≥3

0x0f | MOVE | <x> <y> <z> | Set current position to the coordinates given. Coordinates are 4 byte little-endian signed integers representing values in centimetres (0.01 metres). | ≥3
0x10 - 0x1f | TRIM |  | Remove N-15 (i.e. 1-16) characters from the current label. It's incorrect if the label ends up empty, or you attempt to trim more label than there is. | ≥3
0x20 | DATE (old version) | <date> | Set survey date of legs: date is 4 byte little-endian unsigned integer counting seconds since 1970. | ≥4 and ≤6
0x20 | DATE | <date> | Set survey date of legs: date is a 2 byte little-endian unsigned integer counting days from the start of 1900. | ≥7
0x21 | DATE (old version) | <date1><date2> | Set survey date of legs to a range: date1, date2 are 4 byte little-endian unsigned integer counting seconds since 1970. | ≥4 and ≤6
0x21 | DATE | <date1><datespan> | Set survey date of legs to a range: date1 is a 2 byte little-endian unsigned integer counting days since the start of 1900, and datespan is an unsigned byte counting days from date1. | ≥7
0x22 | ERROR | <legs><length><E><H><V> | Error information for the current traverse. <legs> is the number of legs. <length> is the total length of the traverse in cm (0.01m). E, H and V are the error and the horizontal and vertical components in cm. (All values are 4 byte little-endian signed integers) | ≥6
0x23 | DATE | <date1><date2> | Set survey date of legs to a range: date1, date2 are 2 byte little-endian unsigned integers counting days since the start of 1900. | ≥7
0x24 | DATE |  | No survey date information was specified. | ≥7
0x25 - 0x2f |  |  | Reserved
0x30 - 0x31 | XSECT | <len> <label> <L> <R> <U> <D> | Dimensions are 2 byte little-endian signed integers representing values in centimetres (0.01 metres). Omitted dimensions are encoded as 0xffff. Station flags are (N & 0x01): | ≥5
Flag (N & 0x01) | Meaning
0x01 | Station is last one in this passage
0x32 - 0x33 | XSECT | <len> <label> <L> <R> <U> <D> | Dimensions are 4 byte little-endian signed integers representing values in centimetres (0.01 metres). Omitted dimensions are encoded as 0xffffffff. | ≥5
Flag (N & 0x01) | Meaning
0x01 | Station is last one in this passage
0x34 - 0x3f |  |  | Reserved
0x40 - 0x7f | LABEL | <len> <label> <x> <y> <z> | Append label to the current label buffer. The updated contents of the label buffer give the survey stations full name. The length of label is given by length, which is encoded as follows: | ≥3
Length | Encoding
0 - 253 | byte 0x00 - 0xfd
254-65789 | byte 0xfe 2 byte little-endian unsigned integer len-254 0x0000-0xffff
65790 and greater | byte 0xff 4 byte little-endian unsigned integer len 0x000100fd-0xffffffff
The station flags are encoded in the bottom 6 bits of the item code:
Flag (N & 0x3f) | Meaning
0x01 | Station is on leg above ground
0x02 | Station is on an underground leg (both may be true at an entrance)
0x04 | Station is marked as an entrance (with *entrance)
0x08 | Station is exported (i.e. may be used as a connection point to other surveys)
0x10 | Station is a fixed point (control point)
0x20 | Reserved
0x80 - 0xbf | LINE | <len> <label> <x> <y> <z> | Append label to the current label buffer. The length of the label is encoded as for a station label above. Return leg from current position to coordinates given, and update current position to coordinates given. The updated contents of the label buffer give the survey that the leg is in. | ≥3
Flag (N & 0x3f) | Meaning
0x01 | Leg is above ground
0x02 | Leg duplicates data in another leg (e.g. resurvey along a passage to tie into a known station)
0x04 | Leg is a splay shot in a chamber (radial shots from a central point)
0x08 | Reserved
0x10 | Reserved
0x20 | Reserved
0xc0 - 0xff |  |  | Reserved

## Item order

- A continuous section of centreline is defined by a <MOVE> item, followed by one or more <LINE> items.
- <LABEL> items may appear anywhere in the file after the header, including within a <MOVE><LINE>... sequence.
- Duplicate <LABEL> items are permitted provided they also have identical coordinate values. (The same coordinate values may also be shared by any number of different <LABEL> items).
- Stations must be defined in a <LABEL> item before being referenced (e.g. in <XSECT> items)

Authors: Olly Betts and Mike McCombe, last updated: 2025-01-13