/
location.go
130 lines (97 loc) Β· 2.66 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
package osm
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/melihmucuk/geocache"
"github.com/photoprism/photoprism/pkg/s2"
)
type Location struct {
ID string `json:"-"`
PlaceID int `json:"place_id"`
LocName string `json:"name"`
LocCategory string `json:"category"`
LocType string `json:"type"`
LocDisplayName string `json:"display_name"`
Address Address `json:"address"`
Cached bool
}
var ReverseLookupURL = "https://nominatim.openstreetmap.org/reverse?lat=%f&lon=%f&format=jsonv2&accept-language=en&zoom=18"
// API docs see https://wiki.openstreetmap.org/wiki/Nominatim#Reverse_Geocoding
func FindLocation(id string) (result Location, err error) {
if len(id) > 16 || len(id) == 0 {
return result, errors.New("osm: invalid location id")
}
lat, lng := s2.LatLng(id)
if lat == 0.0 || lng == 0.0 {
return result, fmt.Errorf("osm: skipping lat %f, lng %f", lat, lng)
}
point := geocache.GeoPoint{Latitude: lat, Longitude: lng}
if hit, ok := geoCache.Get(point); ok {
log.Debugf("osm: cache hit for lat %f, lng %f", lat, lng)
result = hit.(Location)
result.Cached = true
return result, nil
}
url := fmt.Sprintf(ReverseLookupURL, lat, lng)
log.Debugf("osm: query %s", url)
r, err := http.Get(url)
if err != nil {
log.Errorf("osm: %s", err.Error())
return result, err
}
err = json.NewDecoder(r.Body).Decode(&result)
if err != nil {
log.Errorf("osm: %s", err.Error())
return result, err
}
if result.PlaceID == 0 {
result.ID = ""
return result, fmt.Errorf("osm: no result for %s", id)
}
result.ID = id
geoCache.Set(point, result, time.Hour)
result.Cached = false
return result, nil
}
func (l Location) CellID() (result string) {
return l.ID
}
func (l Location) State() (result string) {
result = l.Address.State
return strings.TrimSpace(result)
}
func (l Location) City() (result string) {
if l.Address.City != "" {
result = l.Address.City
} else if l.Address.Town != "" {
result = l.Address.Town
} else if l.Address.Village != "" {
result = l.Address.Village
} else if l.Address.County != "" {
result = l.Address.County
} else if l.Address.State != "" {
result = l.Address.State
}
if len([]rune(result)) > 19 {
result = ""
}
return strings.TrimSpace(result)
}
func (l Location) Suburb() (result string) {
result = l.Address.Suburb
return strings.TrimSpace(result)
}
func (l Location) CountryCode() (result string) {
result = l.Address.CountryCode
return strings.ToLower(strings.TrimSpace(result))
}
func (l Location) Keywords() (result []string) {
return result
}
func (l Location) Source() string {
return "osm"
}