-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.go
50 lines (39 loc) · 1023 Bytes
/
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
package models
import (
"regexp"
"github.com/golang/geo/s2"
"github.com/pkg/errors"
"github.com/tzneal/coordconv"
)
type Location struct {
Name string
Latitude float64
Longitude float64
Precision int
}
func (l *Location) Mgrs() string {
c := s2.LatLngFromDegrees(l.Latitude, l.Longitude)
m, _ := coordconv.DefaultMGRSConverter.ConvertFromGeodetic(c, l.Precision)
return m
}
func MgrsPrecision(mgrs string) (int, error) {
r := regexp.MustCompile(`\d{1,2}[[:alpha:]]{3}(\d+)`)
match := r.FindStringSubmatch(mgrs)
if len(match) < 2 {
return -1, errors.Errorf("attempt to determine precision of invalid MGRS string: %s", mgrs)
}
return len(match[1]) / 2, nil
}
func ParseLocationFromMgrs(name, mgrs string) (Location, error) {
l, err := coordconv.DefaultMGRSConverter.ConvertToGeodetic(mgrs)
if err != nil {
return Location{}, err
}
p, err := MgrsPrecision(mgrs)
return Location{
Name: name,
Latitude: l.Lat.Degrees(),
Longitude: l.Lng.Degrees(),
Precision: p,
}, nil
}