Skip to content

Instantly share code, notes, and snippets.

@tidwall
Last active March 18, 2017 21:17
Show Gist options
  • Save tidwall/f8c8bd6ab79f8d97d9dca7a2da75c7e6 to your computer and use it in GitHub Desktop.
Save tidwall/f8c8bd6ab79f8d97d9dca7a2da75c7e6 to your computer and use it in GitHub Desktop.
Geobin format

Geobin File Format

Geobin File Format

This format covers the latest GeoJSON at https://tools.ietf.org/html/rfc7946.

It does not support foreign members, and only support 2D and 3D objects.

The entire binary should exist inside a container which already knows it's
size, for example a key value store where the value is the geobin bytes.
Therefore the size of the geobin value is already known prior to decoding.

The Varlen type is a compressed integer that supports int8-int32 numbers.
Values 0-253 are stored in one byte.
Values 254-65535 are store in three bytes, where the first byte is 254
Values 65535-2147483647 are store in five bytes, where the first byte is 255


If the sizeof(buf) < 16:
	Empty Object
	## DONE ##

If the sizeof(buf) == 16:
	The object is a simple 2D point where X is the first 8 bytes and Y is 
	the second 8 bytes.
	## DONE ##

Read the first byte (header byte):
The first four (least signifigant) bits contain the flags that identify
properties of the format.

Bit 1:   HAS_2D_BBOX
Bit 2:   HAS_3D_BBOX
Bit 3:   HAS_3D_COORDS
Bit 4:   ???
Bit 5-8: Type

Types are:
0: RawBytes               This is used for whatever suits your fancy
1: Point
2: MultiPoint
3: LineString
4: MultiLineString
5: Polygon
6: MultiPolygon
7: GeometryCollection
8: Feature
9: FeatureCollection


If the type == RawBytes:
	The remaining bytes consist of the actual value.
	## DONE ##

If the type == Point:
	Read BBOX
	Read POSITION
	## DONE ##

If the type == MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon:
	Read BBOX
	Read COORDS where
		Depth is 1 for MultiPoint and LineString
		Depth is 2 for MultiLineString and Polygon
		Depth is 3 for MultiPolygon
	## DONE ##

If the type == GeometryCollection:
	Read BBOX
	Read GEOMETRIES
		VARLEN + OBJECT...
	## DONE ##

If the type == Feature:
	Read Extra byte:
		Bit 1:   HAS_ID
		Bit 2:   HAS_PROPERTIES
	Read BBOX
	Read PROPERTIES
		VARLEN + RAW_JSON(without whitespace)
	Read GEOMETRY
		OBJECT
	## DONE ##

If the type == FeatureCollection:
	Read BBOX
	Read FEATURES
		VARLEN + FEATURE...
	## DONE ##


The BBOX type:
	If HAS_2D_BBOX:
		Four 8-byte floats
	ElseIf HAS_3D_BBOX:
		Six 8-byte floats
	Else
		Nothing

The COORDS type:
	If the Depth is 1
		VARLEN + POSITION
	Else
		VARLEN + COORDS(Depth-1)

The POSITION type:
	If HAS_3D_COORDS:
		Three 8-byte floats
	Else
		Two 8-byte floats

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment