/
location.go
executable file
·51 lines (43 loc) · 1.03 KB
/
location.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
package field
import (
"context"
"fmt"
"strings"
"github.com/twpayne/go-geom/encoding/wkb"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// Location Create from customized data type
type Location struct {
Point string
}
// GormDataType GormDataType
func (loc Location) GormDataType() string {
return "geometry"
}
// Scan implements the sql.Scanner interface
func (loc *Location) Scan(v interface{}) error {
if v == nil {
return nil
}
mysqlEncoding, ok := v.([]byte)
if !ok {
return fmt.Errorf("did not scan: expected []byte but was %T", v)
}
var point wkb.Point
point.Scan(mysqlEncoding[4:])
co := point.Coords()
loc.Point = fmt.Sprintf("POINT(%f %f)", co.X(), co.Y())
return nil
}
// GormValue gorm value
func (loc Location) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
//return clause.Expr{}
l := strings.Replace(loc.Point, ",", " ", -1)
l = strings.Replace(l, "POINT(", "", -1)
l = strings.Replace(l, ")", "", -1)
return clause.Expr{
SQL: "ST_GeomFromText(?)",
Vars: []interface{}{"POINT(" + l + ")"},
}
}