package
0.11.1
Repository: https://github.com/paulmach/orb.git
Documentation: pkg.go.dev

# README

encoding/mvt Godoc Reference

Package mvt provides functions for encoding and decoding Mapbox Vector Tiles. The interface is defined as:

type Layer struct {
    Name     string
    Version  uint32
    Extent   uint32
    Features []*geojson.Feature
}

func MarshalGzipped(layers Layers) ([]byte, error)
func Marshal(layers Layers) ([]byte, error)

func UnmarshalGzipped(data []byte) (Layers, error)
func Unmarshal(data []byte) (Layers, error)

These function decode the geometry and leave it in the "tile coordinates". To project it to and from WGS84 (standard lon/lat) use:

func (l Layer) ProjectToTile(tile maptile.Tile)
func (l Layer) ProjectToWGS84(tile maptile.Tile)

Version 1 vs. Version 2

There is no data format difference between v1 and v2. The difference is v2 requires geometries be simple/clean. e.g. lines that are not self intersecting and polygons that are encoded in the correct winding order.

This library does not do anything to validate the geometry, it just encodes what you give it, so it defaults to v1. I've seen comments from Mapbox about this and they only want you to claim your library is a v2 encoder if it does cleanup/validation.

However, if you know your geometry is simple/clean you can change the layer version manually.

Encoding example

// Start with a set of feature collections defining each layer in lon/lat (WGS84).
collections := map[string]*geojson.FeatureCollection{}

// Convert to a layers object and project to tile coordinates.
layers := mvt.NewLayers(collections)
layers.ProjectToTile(maptile.New(x, y, z))

// In order to be used as source for MapboxGL geometries need to be clipped
// to max allowed extent. (uncomment next line)
// layers.Clip(mvt.MapboxGLDefaultExtentBound)

// Simplify the geometry now that it's in the tile coordinate space.
layers.Simplify(simplify.DouglasPeucker(1.0))

// Depending on use-case remove empty geometry, those two small to be
// represented in this tile space.
// In this case lines shorter than 1, and areas smaller than 1.
layers.RemoveEmpty(1.0, 1.0)

// encoding using the Mapbox Vector Tile protobuf encoding.
data, err := mvt.Marshal(layers) // this data is NOT gzipped.

// Sometimes MVT data is stored and transfered gzip compressed. In that case:
data, err := mvt.MarshalGzipped(layers)

Feature IDs

Since GeoJSON ids can be any number or string they won't necessarily map to vector tile uint64 ids. This is a common incompatibility between the two types.

During marshaling the code tries to convert the geojson.Feature.ID to a positive integer, possibly parsing a string. If the number is negative, the id is omitted. If the number is a positive decimal the number is truncated.

For unmarshaling the id will be converted into a float64 to be consistent with how the encoding/json package decodes numbers.

Geometry Collections

GeoJSON geometry collections are flattened and their features are encoded individually. As a result the "collection" information is lost when encoding and there could be more features in the output (mvt) vs. in the input (GeoJSON)

# Packages

# Functions

Marshal will take a set of layers and encode them into a Mapbox Vector Tile format.
MarshalGzipped will marshal the layers into Mapbox Vector Tile format and gzip the result.
NewLayer is a helper to create a Layer from a feature collection and a name, it sets the default extent and version to 1.
NewLayers creates a set of layers given a set of feature collections.
Unmarshal takes Mapbox Vector Tile (MVT) data and converts into a set of layers, It does not project the coordinates.
UnmarshalGzipped takes gzipped Mapbox Vector Tile (MVT) data and unzips it before decoding it into a set of layers, It does not project the coordinates.

# Constants

DefaultExtent for mapbox vector tiles.

# Variables

MapboxGLDefaultExtentBound holds the default mapbox vector tile bounds used by mapbox-gl.

# Structs

Layer is intermediate MVT layer to be encoded/decoded or projected.

# Type aliases

Layers is a set of layers.