-
Notifications
You must be signed in to change notification settings - Fork 7
/
encode.go
58 lines (52 loc) · 1.27 KB
/
encode.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
package geobuf
import (
"github.com/cairnapp/go-geobuf/pkg/encode"
"github.com/cairnapp/go-geobuf/pkg/geojson"
"github.com/cairnapp/go-geobuf/pkg/math"
"github.com/cairnapp/go-geobuf/proto"
)
func Encode(obj interface{}) *proto.Data {
data, err := EncodeWithOptions(obj, encode.FromAnalysis(obj))
if err != nil {
panic(err)
}
return data
}
func EncodeWithOptions(obj interface{}, opts ...encode.EncodingOption) (*proto.Data, error) {
cfg := &encode.EncodingConfig{
Dimension: 2,
Precision: 1,
Keys: encode.NewKeyStore(),
}
for _, opt := range opts {
opt(cfg)
}
data := &proto.Data{
Keys: cfg.Keys.Keys(),
Dimensions: uint32(cfg.Dimension),
Precision: math.EncodePrecision(cfg.Precision),
}
switch t := obj.(type) {
case *geojson.FeatureCollection:
collection, err := encode.EncodeFeatureCollection(t, cfg)
if err != nil {
return nil, err
}
data.DataType = &proto.Data_FeatureCollection_{
FeatureCollection: collection,
}
case *geojson.Feature:
feature, err := encode.EncodeFeature(t, cfg)
if err != nil {
return nil, err
}
data.DataType = &proto.Data_Feature_{
Feature: feature,
}
case *geojson.Geometry:
data.DataType = &proto.Data_Geometry_{
Geometry: encode.EncodeGeometry(t, cfg),
}
}
return data, nil
}