forked from twpayne/go-geom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wkt.go
71 lines (60 loc) · 1.94 KB
/
wkt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//go:generate goyacc -l -o wkt.gen.go -p wkt wkt.y
// TODO remove the following line if https://github.com/golang/tools/pull/304 is accepted
//go:generate sed -i -e "s/wktErrorVerbose = false/wktErrorVerbose = true/" wkt.gen.go
//go:generate gofumpt -extra -w wkt.gen.go
// Package wkt implements Well Known Text encoding and decoding.
package wkt
import (
"errors"
"github.com/twpayne/go-geom"
)
const (
tPoint = "POINT "
tMultiPoint = "MULTIPOINT "
tLineString = "LINESTRING "
tMultiLineString = "MULTILINESTRING "
tPolygon = "POLYGON "
tMultiPolygon = "MULTIPOLYGON "
tGeometryCollection = "GEOMETRYCOLLECTION "
tZ = "Z "
tM = "M "
tZm = "ZM "
tEmpty = "EMPTY"
)
// ErrBraceMismatch is returned when braces do not match.
var ErrBraceMismatch = errors.New("wkt: brace mismatch")
// Encoder encodes WKT based on specified parameters.
type Encoder struct {
maxDecimalDigits int
}
// NewEncoder returns a new encoder with the given options set.
func NewEncoder(applyOptFns ...EncodeOption) *Encoder {
encoder := &Encoder{
maxDecimalDigits: -1,
}
for _, applyOptFn := range applyOptFns {
applyOptFn(encoder)
}
return encoder
}
// An EncodeOption is an encoder option.
type EncodeOption func(*Encoder)
// EncodeOptionWithMaxDecimalDigits sets the maximum decimal digits to encode.
func EncodeOptionWithMaxDecimalDigits(maxDecimalDigits int) EncodeOption {
return func(e *Encoder) {
e.maxDecimalDigits = maxDecimalDigits
}
}
// Marshal translates a geometry to the corresponding WKT.
func Marshal(g geom.T, applyOptFns ...EncodeOption) (string, error) {
return NewEncoder(applyOptFns...).Encode(g)
}
// Unmarshal translates a WKT to the corresponding geometry.
func Unmarshal(wkt string) (geom.T, error) {
wktlex := newWKTLex(wkt)
wktParse(wktlex)
if wktlex.lastErr != nil {
return nil, wktlex.lastErr
}
return wktlex.ret, nil
}