polyline

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2022 License: BSD-2-Clause Imports: 3 Imported by: 29

README

go-polyline

Build Status PkgGoDev Coverage Status

Package polyline implements a Google Maps Encoding Polyline encoder and decoder.

Encoding example

func ExampleEncodeCoords() {
	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`@
}

Decoding example

func ExampleDecodeCoords() {
	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]]
}

License

BSD-2-Clause

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

Examples

Constants

This section is empty.

Variables

View Source
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

func DecodeCoord(buf []byte) ([]float64, []byte, error)

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

func DecodeCoords(buf []byte) ([][]float64, []byte, error)

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

func DecodeInt(buf []byte) (int, []byte, error)

DecodeInt decodes a single signed integer from buf. It returns the decoded int, the remaining unconsumed bytes of buf, and any error.

func DecodeUint

func DecodeUint(buf []byte) (uint, []byte, error)

DecodeUint decodes a single unsigned integer from buf. It returns the decoded uint, the remaining unconsumed bytes of buf, and any error.

func EncodeCoord

func EncodeCoord(coord []float64) []byte

EncodeCoord returns the encoding of an array of coordinates using the default codec.

func EncodeCoords

func EncodeCoords(coords [][]float64) []byte

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

func EncodeInt(buf []byte, i int) []byte

EncodeInt appends the encoding of a single signed integer i to buf and returns the new buf.

func EncodeUint

func EncodeUint(buf []byte, u uint) []byte

EncodeUint appends the encoding of a single unsigned integer u to buf and returns the new buf.

Types

type Codec

type Codec struct {
	Dim   int     // Dimensionality, normally 2
	Scale float64 // Scale, normally 1e5
}

A Codec represents an encoder.

func (Codec) DecodeCoord

func (c Codec) DecodeCoord(buf []byte) ([]float64, []byte, error)

DecodeCoord decodes a single coordinate from buf. It returns the coordinate, the remaining unconsumed bytes of buf, and any error.

func (Codec) DecodeCoords

func (c Codec) DecodeCoords(buf []byte) ([][]float64, []byte, error)

DecodeCoords decodes an array of coordinates from buf. It returns the coordinates, the remaining unconsumed bytes of buf, and any error.

func (Codec) DecodeFlatCoords

func (c Codec) DecodeFlatCoords(flatCoords []float64, buf []byte) ([]float64, []byte, error)

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

func (c Codec) EncodeCoord(buf []byte, coord []float64) []byte

EncodeCoord encodes a single coordinate to buf and returns the new buf.

func (Codec) EncodeCoords

func (c Codec) EncodeCoords(buf []byte, coords [][]float64) []byte

EncodeCoords appends the encoding of an array of coordinates coords to buf and returns the new buf.

func (Codec) EncodeFlatCoords

func (c Codec) EncodeFlatCoords(buf []byte, flatCoords []float64) ([]byte, error)

EncodeFlatCoords encodes a one-dimensional array of coordinates to buf. It returns the new buf and any error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL