/
location.go
143 lines (116 loc) Β· 2.61 KB
/
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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package maps
import (
"errors"
"strings"
"github.com/photoprism/photoprism/internal/hub/places"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/s2"
"github.com/photoprism/photoprism/pkg/txt"
)
// Location represents a geolocation.
type Location struct {
ID string
placeID string
LocName string
LocStreet string
LocPostcode string
LocCategory string
LocLabel string
LocDistrict string
LocCity string
LocState string
LocCountry string
LocKeywords []string
LocSource string
}
type LocationSource interface {
CellID() string
PlaceID() string
Name() string
Street() string
Category() string
Postcode() string
District() string
City() string
State() string
CountryCode() string
Keywords() []string
Source() string
}
func (l *Location) QueryApi(api string) error {
switch api {
case places.ApiName:
return l.QueryPlaces()
}
return errors.New("maps: location lookup disabled")
}
func (l *Location) QueryPlaces() error {
s, err := places.FindLocation(l.ID)
if err != nil {
return err
}
l.placeID = s.PlaceID()
l.LocSource = s.Source()
l.LocName = s.Name()
l.LocStreet = s.Street()
l.LocPostcode = s.Postcode()
l.LocCategory = s.Category()
l.LocLabel = s.Label()
l.LocDistrict = s.District()
l.LocCity = s.City()
l.LocState = s.State()
l.LocCountry = s.CountryCode()
l.LocKeywords = s.Keywords()
return nil
}
func (l *Location) Unknown() bool {
return l.ID == ""
}
func (l Location) PlaceID() string {
return l.placeID
}
func (l Location) S2Token() string {
return l.ID
}
func (l Location) PrefixedToken() string {
return s2.Prefix(l.ID)
}
func (l Location) Name() string {
return txt.Clip(l.LocName, 200)
}
func (l Location) Street() string {
return txt.Clip(l.LocStreet, 100)
}
func (l Location) Postcode() string {
return txt.Clip(l.LocPostcode, 50)
}
func (l Location) Category() string {
return txt.Clip(l.LocCategory, 50)
}
func (l Location) Label() string {
return txt.Clip(l.LocLabel, 400)
}
func (l Location) City() string {
return txt.Clip(l.LocCity, 100)
}
func (l Location) District() string {
return txt.Clip(l.LocDistrict, 100)
}
func (l Location) CountryCode() string {
return txt.Clip(l.LocCountry, 2)
}
func (l Location) State() string {
return txt.Clip(clean.State(l.LocState, l.CountryCode()), 100)
}
func (l Location) CountryName() string {
return CountryNames[l.LocCountry]
}
func (l Location) Keywords() []string {
return l.LocKeywords
}
func (l Location) KeywordString() string {
return txt.Clip(strings.Join(l.LocKeywords, ", "), 300)
}
func (l Location) Source() string {
return l.LocSource
}