-
Notifications
You must be signed in to change notification settings - Fork 0
/
geolocations.go
97 lines (82 loc) · 2.14 KB
/
geolocations.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
97
package entities
import (
"errors"
"fmt"
"strconv"
)
var _ = fmt.Println // remove after test
type GeoLocation struct {
Key *Key
latitude float64
longitude float64
}
func NewGeoLocation(lat float64, long float64) (*GeoLocation, error) {
p := new(GeoLocation)
p.Key = makeKey("GeoLocation")
if lat >= -90 && lat <= 90 && long >= -180 && long <= 180 {
p.latitude = lat
p.longitude = long
return p,nil
} else {
err := errors.New("Invalid geolocation " + strconv.FormatFloat(lat, 'f', -1, 64) + " " + strconv.FormatFloat(long, 'f', -1, 64))
return nil,err
}
}
func (a *GeoLocation) setLatitude(n float64) {
if n >= -90 && n <= 90 {
a.latitude = n
} else {
err := errors.New("Invalid latitude: " + strconv.FormatFloat(n, 'f', -1, 64))
fmt.Println(err)
}
}
func (a *GeoLocation) setLongitude(n float64) {
if n >= -180 && n <= 180 {
a.longitude = n
} else {
err := errors.New("Invalid longitude: " + strconv.FormatFloat(n, 'f', -1, 64))
fmt.Println(err)
}
}
func (a *GeoLocation) getLatitude() float64 {
return a.latitude
}
func (a *GeoLocation) getLongitude() float64 {
return a.longitude
}
func (a *GeoLocation) Triples () [][3]string {
var t [][3]string
t = append(t, makeTriple(Triple{(*a).Key,"hasType","GeoLocation",nil}))
t = append(t, makeTriple(Triple{(*a).Key,"hasLatitude",strconv.FormatFloat((*a).latitude, 'f', -1, 64),nil}))
t = append(t, makeTriple(Triple{(*a).Key,"hasLongitude",strconv.FormatFloat((*a).longitude, 'f', -1, 64),nil}))
return t
}
func (a *GeoLocation) Row() []string {
var t []string
t = append(t, a.Key.s)
t = append(t, strconv.FormatFloat((*a).latitude, 'f', -1, 64))
t = append(t, strconv.FormatFloat((*a).longitude, 'f', -1, 64))
return t
}
func FindGeoLocationKey (kf *Key) int {
for i,a := range GeoLocations {
if kf.s == a.Key.s {
return i
}
}
return -1
}
func AddGeoLocationFact(a []string) {
key := new(Key)
key.s = a[0]
i := FindGeoLocationKey(key)
switch a[1] {
case "Latitude":
f,_ := strconv.ParseFloat(a[2], 64)
GeoLocations[i].setLatitude(f)
case "Longitude":
f,_ := strconv.ParseFloat(a[2], 64)
GeoLocations[i].setLongitude(f)
}
}
var GeoLocations []*GeoLocation