-
Notifications
You must be signed in to change notification settings - Fork 0
/
guest.go
96 lines (76 loc) · 2.5 KB
/
guest.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"errors"
"fmt"
"log"
"math"
)
// Guest is a structure modelling guest instance
type Guest struct {
Id int `json:"user_id"`
Latitude float64 `json:"latitude,string"`
Longitude float64 `json:"longitude,string"`
Name string `json:"name"`
}
// Guest.String returns formatted string for pretty Guest formatting
func (g *Guest) String() string {
return fmt.Sprintf("Guest id: %d, name: %s, lat: %f, long: %f",
g.Id, g.Name, g.Latitude, g.Longitude)
}
// Guest.isWithinDistance calculates whether a Guest object is within a given
// latitude and longitude point. Longitude and Latitude are passed in radians
// The method will return true if the guest is within a range, false otherwise.
// In case of problems, appropriate non-null error will be returned
func (g *Guest) isWithinDistance(longRad, latRad, distance float64) (bool, error) {
if distance < 0.0 {
return false, errors.New("Distance cannot be negative")
}
guestLat, guestLong, err := ConvertDegToRad(g.Latitude, g.Longitude)
if err != nil {
msg := fmt.Sprintf("Guest %d, error while converting Deg To Rad: %s\n",
g.Id, err)
return false, errors.New(msg)
} else {
longDelta := math.Abs(guestLong - longRad)
angle := math.Acos(math.Sin(latRad)*math.Sin(guestLat) +
math.Cos(latRad)*math.Cos(guestLat)*math.Cos(longDelta))
return angle*EarthRadius <= distance, nil
}
}
// Guests abstracts an iterable collection of *Guest objects
type Guests []*Guest
// Guests.Len returns length of Guests objects
func (guests Guests) Len() int {
return len(guests)
}
// Guests.Less returns true if i'th element is smaller than j'th element of
// Guests' collection
func (guests Guests) Less(i, j int) bool {
return guests[i].Id < guests[j].Id
}
// Guests.Swap swaps i'th and j'th element in Guests collection
func (guests Guests) Swap(i, j int) {
guest := guests[i]
guests[i] = guests[j]
guests[j] = guest
}
// Guests.FindGuestsWithinRange returns a Guests collection with all the Guest
// objects who are located within a given distance from the point depicted by
// longitude and latitude (both given in radians)
func (guests Guests) FindGuestsWithinRange(longitude, latitude, distance float64) Guests {
guestsNearBy := make(Guests, 0, len(guests))
var ok bool
var err error
for _, guest := range guests {
ok, err = guest.isWithinDistance(
longitude,
latitude,
distance)
if err != nil {
log.Println(err)
} else if ok {
guestsNearBy = append(guestsNearBy, guest)
}
}
return guestsNearBy
}