-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoordinate.go
75 lines (57 loc) · 1.59 KB
/
coordinate.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
package mapbox
import (
"fmt"
"strings"
)
type Coordinate struct {
Lat float64 `json:"latitude"`
Lng float64 `json:"longitude"`
}
func (c Coordinate) IsZero() bool {
return c.Lat == 0 && c.Lng == 0
}
type Coordinates []Coordinate
// https://docs.mapbox.com/api/#coordinate-format
func (c Coordinate) WGS84Format() string {
var b strings.Builder
b.Grow(21) // 10(lat) + 10(lng) + 1(comma)
fmt.Fprintf(&b, "%v,%v", c.Lng, c.Lat)
return b.String()
}
// https://docs.mapbox.com/api/#coordinate-format
func (c Coordinates) WGS84Format() string {
var b strings.Builder
b.Grow(len(c) * 20) // 10(lat) + 10(lng)
for _, coordinate := range c {
fmt.Fprintf(&b, "%v;", coordinate.WGS84Format())
}
result := b.String()
return result[:len(result)-1] // remove trailing ';'
}
////////////////////////////////////////////////////////////////////////////////
type ExtendedCoordinate struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Accuracy string `json:"accuracy"`
RoutablePoints []RoutablePoint `json:"routable_points,omitempty"`
}
type RoutablePoint struct {
Name string
Latitude float64
Longitude float64
}
////////////////////////////////////////////////////////////////////////////////
const (
GeometryLngIdx = 0
GeometryLatIdx = 1
)
type Geometry struct {
Coordinates []float64 `json:"coordinates"` // [lng, lat]
Type string `json:"type"`
}
func (g Geometry) Latitude() float64 {
return g.Coordinates[GeometryLatIdx]
}
func (g Geometry) Longitude() float64 {
return g.Coordinates[GeometryLngIdx]
}