forked from cairnapp/go-geobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
102 lines (92 loc) · 2.29 KB
/
options.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package encode
import (
"github.com/cairnapp/go-geobuf/pkg/geojson"
"github.com/cairnapp/go-geobuf/pkg/geometry"
"github.com/cairnapp/go-geobuf/pkg/math"
)
type EncodingConfig struct {
Dimension uint
Precision uint
Keys KeyStore
}
type EncodingOption func(o *EncodingConfig)
func WithPrecision(precision uint) EncodingOption {
return func(o *EncodingConfig) {
o.Precision = uint(math.DecodePrecision(uint32(precision)))
}
}
func WithDimension(dimension uint) EncodingOption {
return func(o *EncodingConfig) {
o.Dimension = dimension
}
}
func WithKeyStore(store KeyStore) EncodingOption {
return func(o *EncodingConfig) {
o.Keys = store
}
}
func FromAnalysis(obj interface{}) EncodingOption {
return func(o *EncodingConfig) {
analyze(obj, o)
}
}
func analyze(obj interface{}, opts *EncodingConfig) {
opts.Dimension = 2
switch t := obj.(type) {
case *geojson.FeatureCollection:
for _, feature := range t.Features {
analyze(feature, opts)
}
case *geojson.Feature:
analyze(geojson.NewGeometry(t.Geometry), opts)
for key, _ := range t.Properties {
opts.Keys.Add(key)
}
case *geojson.Geometry:
switch t.Type {
case GeometryPoint:
updatePrecision(t.Coordinates.(geometry.Point), opts)
case GeometryMultiPoint:
coords := t.Coordinates.(geometry.MultiPoint)
for _, coord := range coords {
updatePrecision(coord, opts)
}
case GeometryLineString:
coords := t.Coordinates.(geometry.LineString)
for _, coord := range coords {
updatePrecision(coord, opts)
}
case GeometryMultiLineString:
lines := t.Coordinates.(geometry.MultiLineString)
for _, line := range lines {
for _, coord := range line {
updatePrecision(coord, opts)
}
}
case GeometryPolygon:
lines := t.Coordinates.(geometry.Polygon)
for _, line := range lines {
for _, coord := range line {
updatePrecision(coord, opts)
}
}
case GeometryMultiPolygon:
polygons := t.Coordinates.(geometry.MultiPolygon)
for _, rings := range polygons {
for _, ring := range rings {
for _, coord := range ring {
updatePrecision(coord, opts)
}
}
}
}
}
}
func updatePrecision(point geometry.Point, opt *EncodingConfig) {
for _, val := range point {
e := math.GetPrecision(val)
if e > opt.Precision {
opt.Precision = e
}
}
}