2019-12-20 11:30:58 +01:00
|
|
|
package osm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-12-31 07:16:11 +01:00
|
|
|
"errors"
|
2019-12-20 11:30:58 +01:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/melihmucuk/geocache"
|
2019-12-31 07:16:11 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/s2"
|
2019-12-20 11:30:58 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Location struct {
|
2019-12-31 07:16:11 +01:00
|
|
|
ID string `json:"-"`
|
2019-12-20 23:05:44 +01:00
|
|
|
PlaceID int `json:"place_id"`
|
2019-12-27 05:18:52 +01:00
|
|
|
LocName string `json:"name"`
|
2019-12-20 23:05:44 +01:00
|
|
|
LocCategory string `json:"category"`
|
|
|
|
LocType string `json:"type"`
|
|
|
|
LocDisplayName string `json:"display_name"`
|
|
|
|
Address Address `json:"address"`
|
|
|
|
Cached bool
|
2019-12-20 11:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-12-31 07:16:11 +01:00
|
|
|
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)
|
|
|
|
|
2019-12-20 11:30:58 +01:00
|
|
|
if lat == 0.0 || lng == 0.0 {
|
2019-12-20 12:04:26 +01:00
|
|
|
return result, fmt.Errorf("osm: skipping lat %f, lng %f", lat, lng)
|
2019-12-20 11:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
point := geocache.GeoPoint{Latitude: lat, Longitude: lng}
|
|
|
|
|
|
|
|
if hit, ok := geoCache.Get(point); ok {
|
2019-12-20 12:04:26 +01:00
|
|
|
log.Debugf("osm: cache hit for lat %f, lng %f", lat, lng)
|
2019-12-20 11:30:58 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
if result.PlaceID == 0 {
|
|
|
|
result.ID = ""
|
|
|
|
|
|
|
|
return result, fmt.Errorf("osm: no result for %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
result.ID = id
|
|
|
|
|
2019-12-20 11:30:58 +01:00
|
|
|
geoCache.Set(point, result, time.Hour)
|
|
|
|
|
|
|
|
result.Cached = false
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
func (l Location) CellID() (result string) {
|
|
|
|
return l.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) State() (result string) {
|
|
|
|
result = l.Address.State
|
2019-12-20 11:30:58 +01:00
|
|
|
|
|
|
|
return strings.TrimSpace(result)
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
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
|
2019-12-20 11:30:58 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 20:24:20 +01:00
|
|
|
if len([]rune(result)) > 19 {
|
|
|
|
result = ""
|
|
|
|
}
|
|
|
|
|
2019-12-20 11:30:58 +01:00
|
|
|
return strings.TrimSpace(result)
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
func (l Location) Suburb() (result string) {
|
|
|
|
result = l.Address.Suburb
|
2019-12-20 11:30:58 +01:00
|
|
|
|
|
|
|
return strings.TrimSpace(result)
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
func (l Location) CountryCode() (result string) {
|
|
|
|
result = l.Address.CountryCode
|
2019-12-20 11:30:58 +01:00
|
|
|
|
|
|
|
return strings.ToLower(strings.TrimSpace(result))
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
func (l Location) Keywords() (result []string) {
|
|
|
|
return util.Keywords(l.LocDisplayName)
|
2019-12-20 11:30:58 +01:00
|
|
|
}
|
2019-12-20 12:20:35 +01:00
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
func (l Location) Source() string {
|
2019-12-20 12:20:35 +01:00
|
|
|
return "osm"
|
|
|
|
}
|