Documentation
¶
Overview ¶
Package twkb encodes and decodes geometries in Tiny Well-known Binary (TWKB) for github.com/twpayne/go-geom.
TWKB (https://github.com/TWKB/Specification) stores coordinates as delta-encoded, zigzag varint integers at a chosen decimal precision. For GPS tracks it is typically 4-8 times smaller than WKB, which stores every coordinate as an 8-byte float.
The encoder follows the behaviour of PostGIS ST_AsTWKB so that output is byte-for-byte identical for the same input and options:
- Coordinates are scaled by 10^precision and rounded half away from zero.
- Within a geometry, deltas continue across rings and parts; each member of a geometry collection starts from zero with its own header.
- Points in a line or ring that become identical to the previous point after rounding are dropped, keeping at least 2 points per line and 4 per ring. Use KeepRepeatedPoints to keep them.
- Members of a geometry collection inherit the size and bounding box options.
Example ¶
package main
import (
"fmt"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/wkb"
"github.com/FeltGIS/go-twkb"
)
func main() {
// A short GPS track in UTM 33N, kept to the centimetre, with elevation
// kept to the decimetre.
track := geom.NewLineStringFlat(geom.XYZ, []float64{
264776.12, 6787281.98, 512.3,
264778.50, 6787283.25, 512.8,
264781.75, 6787280.50, 513.0,
})
data, err := twkb.Marshal(track, twkb.Precision(2), twkb.PrecisionZ(1))
if err != nil {
panic(err)
}
wkbData, _ := wkb.Marshal(track, wkb.NDR)
fmt.Printf("WKB %d bytes, TWKB %d bytes\n", len(wkbData), len(data))
g, err := twkb.Unmarshal(data)
if err != nil {
panic(err)
}
c := g.(*geom.LineString).Coord(1)
fmt.Printf("%.2f %.2f %.1f\n", c[0], c[1], c[2])
}
Output: WKB 81 bytes, TWKB 25 bytes 264778.50 6787283.25 512.8
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidPrecision is returned for a precision outside the range TWKB // can store: -8..7 for x/y and 0..7 for z and m. ErrInvalidPrecision = errors.New("twkb: invalid precision") // ErrUnsupportedGeometry is returned for geometry types or layouts TWKB // cannot represent. ErrUnsupportedGeometry = errors.New("twkb: unsupported geometry") // ErrCoordinateOutOfRange is returned when a scaled coordinate is not a // finite value that fits in an int64. ErrCoordinateOutOfRange = errors.New("twkb: coordinate out of range") // ErrInvalidIDs is returned when the id list does not match the geometry. ErrInvalidIDs = errors.New("twkb: invalid id list") // ErrMalformed is returned when the input is not valid TWKB. ErrMalformed = errors.New("twkb: malformed input") )
Functions ¶
Types ¶
type Option ¶
type Option func(*encoder)
Option configures Marshal.
func KeepRepeatedPoints ¶
func KeepRepeatedPoints() Option
KeepRepeatedPoints keeps points in lines and rings that are identical to the previous point after rounding. By default they are dropped, as PostGIS ST_AsTWKB does.
func Precision ¶
Precision sets the number of decimal digits kept for x and y, -8..7. Negative values round to tens, hundreds and so on. The default is 0.
func PrecisionM ¶
PrecisionM sets the number of decimal digits kept for m, 0..7.
func PrecisionZ ¶
PrecisionZ sets the number of decimal digits kept for z, 0..7.