Documentation ¶
Overview ¶
Package polyline implements a Google Maps Encoding Polyline encoder and decoder. See https://developers.google.com/maps/documentation/utilities/polylinealgorithm.
The default codec encodes and decodes two-dimensional coordinates scaled by 1e5. For other dimensionalities and scales create a custom Codec.
The package operates on byte slices. Encoding functions take an existing byte slice as input (which can be nil) and return a new byte slice with the encoded value appended to it, similarly to how Go's append function works. To increase performance, you can pre-allocate byte slices, for example by passing make([]byte, 0, 128) as the input byte slice. Similarly, decoding functions take a byte slice as input and return the remaining unconsumed bytes as output.
Index ¶
- Variables
- func DecodeCoord(buf []byte) ([]float64, []byte, error)
- func DecodeCoords(buf []byte) ([][]float64, []byte, error)
- func DecodeInt(buf []byte) (int, []byte, error)
- func DecodeUint(buf []byte) (uint, []byte, error)
- func EncodeCoord(coord []float64) []byte
- func EncodeCoords(coords [][]float64) []byte
- func EncodeInt(buf []byte, i int) []byte
- func EncodeUint(buf []byte, u uint) []byte
- type Codec
- func (c Codec) DecodeCoord(buf []byte) ([]float64, []byte, error)
- func (c Codec) DecodeCoords(buf []byte) ([][]float64, []byte, error)
- func (c Codec) DecodeFlatCoords(flatCoords []float64, buf []byte) ([]float64, []byte, error)
- func (c Codec) EncodeCoord(buf []byte, coord []float64) []byte
- func (c Codec) EncodeCoords(buf []byte, coords [][]float64) []byte
- func (c Codec) EncodeFlatCoords(buf []byte, flatCoords []float64) ([]byte, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrDimensionalMismatch = errors.New("dimensional mismatch") ErrEmpty = errors.New("empty") ErrInvalidByte = errors.New("invalid byte") ErrOverflow = errors.New("overflow") ErrUnterminatedSequence = errors.New("unterminated sequence") )
Errors.
Functions ¶
func DecodeCoord ¶
DecodeCoord decodes a single coordinate from buf using the default codec. It returns the coordinate, the remaining bytes in buf, and any error.
func DecodeCoords ¶
DecodeCoords decodes an array of coordinates from buf using the default codec. It returns the coordinates, the remaining bytes in buf, and any error.
Example ¶
buf := []byte("_p~iF~ps|U_ulLnnqC_mqNvxq`@") coords, _, _ := polyline.DecodeCoords(buf) fmt.Println(coords)
Output: [[38.5 -120.2] [40.7 -120.95] [43.252 -126.453]]
func DecodeInt ¶
DecodeInt decodes a single signed integer from buf. It returns the decoded int, the remaining unconsumed bytes of buf, and any error.
func DecodeUint ¶
DecodeUint decodes a single unsigned integer from buf. It returns the decoded uint, the remaining unconsumed bytes of buf, and any error.
func EncodeCoord ¶
EncodeCoord returns the encoding of an array of coordinates using the default codec.
func EncodeCoords ¶
EncodeCoords returns the encoding of an array of coordinates using the default codec.
Example ¶
coords := [][]float64{ {38.5, -120.2}, {40.7, -120.95}, {43.252, -126.453}, } fmt.Println(string(polyline.EncodeCoords(coords)))
Output: _p~iF~ps|U_ulLnnqC_mqNvxq`@
func EncodeInt ¶
EncodeInt appends the encoding of a single signed integer i to buf and returns the new buf.
func EncodeUint ¶
EncodeUint appends the encoding of a single unsigned integer u to buf and returns the new buf.
Types ¶
type Codec ¶
A Codec represents an encoder.
func (Codec) DecodeCoord ¶
DecodeCoord decodes a single coordinate from buf. It returns the coordinate, the remaining unconsumed bytes of buf, and any error.
func (Codec) DecodeCoords ¶
DecodeCoords decodes an array of coordinates from buf. It returns the coordinates, the remaining unconsumed bytes of buf, and any error.
func (Codec) DecodeFlatCoords ¶
DecodeFlatCoords decodes coordinates from buf, appending them to a one-dimensional array. It returns the coordinates, the remaining unconsumed bytes in buf, and any error.
func (Codec) EncodeCoord ¶
EncodeCoord encodes a single coordinate to buf and returns the new buf.
func (Codec) EncodeCoords ¶
EncodeCoords appends the encoding of an array of coordinates coords to buf and returns the new buf.