forked from dolanor/caldav-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.go
70 lines (55 loc) · 1.88 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
package values
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/andrewzeneski/caldav-go/utils"
)
var _ = log.Print
// a representation of a geographical point for iCalendar
type Geo struct {
coords []float64
}
// creates a new icalendar geo representation
func NewGeo(lat, lng float64) *Geo {
return &Geo{coords: []float64{lat, lng}}
}
// returns the latitude encoded into the geo point
func (g *Geo) Lat() float64 {
return g.coords[0]
}
// returns the longitude encoded into the geo point
func (g *Geo) Lng() float64 {
return g.coords[1]
}
// validates the geo value against the iCalendar specification
func (g *Geo) ValidateICalValue() error {
if len(g.coords) != 2 {
return utils.NewError(g.ValidateICalValue, "geo value must have length of 2", g, nil)
}
if g.Lat() < -90 || g.Lat() > 90 {
return utils.NewError(g.ValidateICalValue, "geo latitude must be between -90 and 90 degrees", g, nil)
}
if g.Lng() < -180 || g.Lng() > 180 {
return utils.NewError(g.ValidateICalValue, "geo longitude must be between -180 and 180 degrees", g, nil)
}
return nil
}
// encodes the geo value for the iCalendar specification
func (g *Geo) EncodeICalValue() (string, error) {
return fmt.Sprintf("%f %f", g.Lat(), g.Lng()), nil
}
// decodes the geo value from the iCalendar specification
func (g *Geo) DecodeICalValue(value string) error {
if latlng := strings.Split(value, " "); len(latlng) < 2 {
return utils.NewError(g.DecodeICalValue, "geo value must have both a latitude and longitude component", g, nil)
} else if lat, err := strconv.ParseFloat(latlng[0], 64); err != nil {
return utils.NewError(g.DecodeICalValue, "unable to decode latitude component", g, err)
} else if lng, err := strconv.ParseFloat(latlng[1], 64); err != nil {
return utils.NewError(g.DecodeICalValue, "unable to decode latitude component", g, err)
} else {
*g = Geo{coords: []float64{lat, lng}}
return nil
}
}