-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.go
68 lines (54 loc) · 1.23 KB
/
point.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
package lbs
import (
"database/sql/driver"
"encoding/binary"
"fmt"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/wkb"
)
// Point wrap wkb.Point
//
// https://stackoverflow.com/a/60577841
type Point struct {
Point wkb.Point
}
// GormDataType ...
func (p *Point) GormDataType() string {
return "geometry"
}
func NewPoint(p *geom.Point) *Point {
return &Point{wkb.Point{Point: p}}
}
func (p *Point) Value() (driver.Value, error) {
if p == nil {
return nil, nil
}
if p.Point.Point == nil {
return nil, nil
}
value, err := p.Point.Value()
if err != nil {
return nil, err
}
buf, ok := value.([]byte)
if !ok {
return nil, fmt.Errorf("did not convert value: expected []byte, but was %T", value)
}
mysqlEncoding := make([]byte, 4)
binary.LittleEndian.PutUint32(mysqlEncoding, 4326)
mysqlEncoding = append(mysqlEncoding, buf...)
return mysqlEncoding, err
}
func (p *Point) Scan(src interface{}) error {
if src == nil {
return nil
}
mysqlEncoding, ok := src.([]byte)
if !ok {
return fmt.Errorf("did not scan: expected []byte but was %T", src)
}
var srid uint32 = binary.LittleEndian.Uint32(mysqlEncoding[0:4])
err := p.Point.Scan(mysqlEncoding[4:])
p.Point.SetSRID(int(srid))
return err
}