# Survex 3d Format Specification

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 the most recent revision of the 3d format (version 8) which is produced by versions from 1.2.7. A separate document describes older versions.

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: "v8" followed by a linefeed. Any future versions will be "v9", "v10", "v11", etc.
- Assorted string metadata - the sublist below lists these, and they must appear in the order given, separated by zero bytes, with the end of the metadata marked by a linefeed. More items may be added, so ignore any additional ones which are present. Any trailing items with empty values can be omitted along with the separating zero byte before them.
  - Survey title: Human readable description of the data in the file. There's no length limit on this string.
  - Coordinate system: string describing the coordinate system in use which can be passed to PROJ. For a coordinate system with an EPSG code `EPSG:` followed by the code number can be used (we recommend using this if an EPSG code exists). Similarly, an ESRI code can be specified with `ESRI:` followed by the code number.
  - Survey hierarchy separator character. Survey station names form a hierarchy, and this character separates levels in the hierarchy. E.g. `161.entrance.6`. Defaults to `.` if this item is not specified, which is also the recommended character to use unless `.` is used in survey or station names (especially as older software will not see this metadata and will use `.` unconditionally).
- Timestamp: A string consisting of an '@' followed by a count of seconds since the start of 1970 in UTC ("Unix time_t") as a string (for example: "@1371300355"), followed by a linefeed. This is intended to be the time the file was generated, rather than the time the survey data was collected.
- File-wide flags: a single byte. If bit 7 is set, this is an extended elevation. All other bits are reserved - set them to 0 when writing, and ignore them when reading.

## 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 | STYLE_NORMAL / STOP |  | Set style for following legs to tape, compass and clino. If the style is already set to STYLE_NORMAL, this code signifies the end of the data in the 3d file. | ≥8
0x01 | STYLE_DIVING |  | Set style for following legs to diving data | ≥8
0x02 | STYLE_CARTESIAN |  | Set style for following legs to cartesian data | ≥8
0x03 | STYLE_CYLPOLAR |  | Set style for following legs to cylindrical polar data | ≥8
0x04 | STYLE_NOSURVEY |  | Set style for following legs to unsurveyed | ≥8
0x05 - 0x0e |  |  | Reserved
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). | ≥8
0x10 | DATE |  | No survey date information was specified. | ≥8
0x11 | DATE | <date> | Set survey date of legs: date is a 2 byte little-endian unsigned integer counting days from the start of 1900. | ≥8
0x12 | 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. | ≥8
0x13 | 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. | ≥8
0x14 - 0x1e |  |  | Reserved
0x1f | 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) | ≥8
0x20 - 0x2f |  |  | Reserved
0x30 - 0x31 | XSECT | <label> <L> <R> <U> <D> | Modify the current label buffer according to <label> (see below for details). The updated contents of the label buffer give the full name of the survey station which these dimensions were measured at. 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): | ≥8
Flag (N & 0x01) | Meaning
0x01 | Station is last one in this passage
0x32 - 0x33 | XSECT | <label> <L> <R> <U> <D> | Modify the current label buffer according to <label> (see below for details). The updated contents of the label buffer give the full name of the survey station which these dimensions were measured at. Dimensions are 4 byte little-endian signed integers representing values in centimetres (0.01 metres). Omitted dimensions are encoded as 0xffffffff. | ≥8
Flag (N & 0x01) | Meaning
0x01 | Station is last one in this passage
0x34 - 0x3f |  |  | Reserved
0x40 - 0x7f | LINE | <label> <x> <y> <z> | Modify the current label buffer according to <label> (see below for details) - if <label> is omitted due to flag bit 0x20 being set then the current label buffer is used unmodified. The updated contents of the label buffer give the survey that the leg is in. Return leg from current position to coordinates given, and update current position to coordinates given. | ≥8
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 | No change to label (<label> omitted entirely) | ≥8
0x80 - 0xff | LABEL | <label> <x> <y> <z> | Modify the current label buffer according to <label> (see below for details). The updated contents of the label buffer give the survey station's full name. | ≥8
The station flags are encoded in the bottom 7 bits of the item code:
Flag (N & 0x7f) | 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 | Station is anonymous
0x40 | Station is on the passage wall

A <label> value in the table above encodes modifications to the current label buffer, which consist of removing the last D bytes from the buffer, and then appending the next A bytes from the file to the buffer. D and A are encoded as follows:

- Read a byte - if it is non-zero then: D = `byte >> 4`, A = `byte & 0x0f`
- Otherwise (i.e. the first byte is zero):
  - Read a byte and:
    - If it is not 255 then D = `byte`
    - Otherwise, D = 4 byte unsigned integer read from the file
  - Read a byte and:
    - If it is not 255 then A = `byte`
    - Otherwise, A = 4 byte unsigned integer read from the file

## 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