forked from jda/srtm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.go
75 lines (59 loc) · 1.81 KB
/
geo.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 srtm
import (
"fmt"
"regexp"
"strconv"
"github.com/pkg/errors"
)
var negativeDD = regexp.MustCompile(`S|W`)
var positiveDD = regexp.MustCompile(`N|E`)
var latitudeDD = regexp.MustCompile(`N|S`)
// ErrInvalidCoordDegrees is returned when latitude/longitude is
// unparsable or otherwise invalid
var ErrInvalidCoordDegrees = errors.New("invalid lat/lon degrees")
// LatLng represents a location
type LatLng struct {
Latitude float64
Longitude float64
}
func (ll *LatLng) String() string {
return fmt.Sprintf("[%0.7f, %0.7f]", ll.Latitude, ll.Longitude)
}
// dToDecimal accepts a direction-signed coordinate value (e.g. W|E or N|S prefix)
// and returns a positive or negative number instead
func dToDecimal(d string) (dd float64, err error) {
makeNegative := false
// make sure d is long enough so we can't get runtime error on string slicing
if len(d) < 2 {
return dd, errors.Wrap(ErrInvalidCoordDegrees, "too short, must contain direction and at least one digit")
}
dir := d[:1]
// valid direction sign?
if positiveDD.MatchString(d) {
} else if negativeDD.MatchString(d) {
makeNegative = true
} else {
return dd, errors.Wrapf(ErrInvalidCoordDegrees, "%s it not valid cardinal direction", dir)
}
i, err := strconv.Atoi(d[1:])
if err != nil {
return dd, errors.Wrap(ErrInvalidCoordDegrees, "could not convert to coord to int")
}
if i < 0 {
return dd, errors.Wrapf(ErrInvalidCoordDegrees, "negative coord %f should is not valid in combination with direction,", dd)
}
if latitudeDD.MatchString(dir) {
if i > 90 {
return dd, errors.Wrap(ErrInvalidCoordDegrees, "latitude must be between 0 and 90")
}
} else {
if i > 180 {
return dd, errors.Wrap(ErrInvalidCoordDegrees, "longitude must be between 0 and 180")
}
}
dd = float64(i)
if makeNegative {
dd = dd * -1
}
return dd, nil
}