2019-12-20 11:30:58 +01:00
|
|
|
package maps
|
|
|
|
|
|
|
|
import (
|
2019-12-20 12:04:26 +01:00
|
|
|
"errors"
|
2019-12-20 11:30:58 +01:00
|
|
|
"strings"
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/hub/places"
|
2021-11-12 05:09:17 +01:00
|
|
|
|
2020-06-05 16:49:32 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/s2"
|
2021-11-09 11:42:10 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-12-20 11:30:58 +01:00
|
|
|
)
|
|
|
|
|
2021-11-09 11:42:10 +01:00
|
|
|
// Location represents a geolocation.
|
2019-12-20 11:30:58 +01:00
|
|
|
type Location struct {
|
2019-12-31 01:34:27 +01:00
|
|
|
ID string
|
2019-12-28 12:28:06 +01:00
|
|
|
LocName string
|
|
|
|
LocCategory string
|
2019-12-28 20:24:20 +01:00
|
|
|
LocLabel string
|
2019-12-28 12:28:06 +01:00
|
|
|
LocCity string
|
2021-11-12 05:09:17 +01:00
|
|
|
LocDistrict string
|
2019-12-28 12:28:06 +01:00
|
|
|
LocState string
|
|
|
|
LocCountry string
|
|
|
|
LocSource string
|
2020-04-28 19:41:06 +02:00
|
|
|
LocKeywords []string
|
2019-12-20 12:04:26 +01:00
|
|
|
}
|
|
|
|
|
2019-12-20 12:20:35 +01:00
|
|
|
type LocationSource interface {
|
2019-12-31 07:16:11 +01:00
|
|
|
CellID() string
|
2019-12-20 23:05:44 +01:00
|
|
|
CountryCode() string
|
|
|
|
Category() string
|
2019-12-27 05:18:52 +01:00
|
|
|
Name() string
|
2019-12-20 12:20:35 +01:00
|
|
|
City() string
|
2021-11-12 05:09:17 +01:00
|
|
|
District() string
|
2019-12-20 12:20:35 +01:00
|
|
|
State() string
|
|
|
|
Source() string
|
2020-04-28 19:41:06 +02:00
|
|
|
Keywords() []string
|
2019-12-20 11:30:58 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 05:09:17 +01:00
|
|
|
func NewLocation(id, name, category, label, city, district, state, country, source string, keywords []string) *Location {
|
2019-12-20 20:23:16 +01:00
|
|
|
result := &Location{
|
2020-01-19 13:27:15 +01:00
|
|
|
ID: id,
|
|
|
|
LocName: name,
|
|
|
|
LocCategory: category,
|
|
|
|
LocLabel: label,
|
|
|
|
LocCity: city,
|
2021-11-12 05:09:17 +01:00
|
|
|
LocDistrict: district,
|
2020-01-19 13:27:15 +01:00
|
|
|
LocCountry: country,
|
2021-11-11 16:00:42 +01:00
|
|
|
LocState: txt.NormalizeState(state, country),
|
2020-01-19 13:27:15 +01:00
|
|
|
LocSource: source,
|
2020-04-28 19:41:06 +02:00
|
|
|
LocKeywords: keywords,
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-01-11 01:59:43 +01:00
|
|
|
func (l *Location) QueryApi(api string) error {
|
2020-01-06 06:59:35 +01:00
|
|
|
switch api {
|
|
|
|
case "places":
|
|
|
|
return l.QueryPlaces()
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("maps: reverse lookup disabled")
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
func (l *Location) QueryPlaces() error {
|
|
|
|
s, err := places.FindLocation(l.ID)
|
2019-12-20 12:04:26 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
l.LocSource = s.Source()
|
|
|
|
l.LocName = s.Name()
|
|
|
|
l.LocCity = s.City()
|
2021-11-12 05:09:17 +01:00
|
|
|
l.LocDistrict = s.District()
|
2019-12-31 07:16:11 +01:00
|
|
|
l.LocState = s.State()
|
|
|
|
l.LocCountry = s.CountryCode()
|
|
|
|
l.LocCategory = s.Category()
|
|
|
|
l.LocLabel = s.Label()
|
2020-04-28 19:41:06 +02:00
|
|
|
l.LocKeywords = s.Keywords()
|
2019-12-31 07:16:11 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-20 11:30:58 +01:00
|
|
|
func (l *Location) Unknown() bool {
|
2019-12-31 07:16:11 +01:00
|
|
|
return l.ID == ""
|
2019-12-20 11:30:58 +01:00
|
|
|
}
|
|
|
|
|
2020-04-28 19:41:06 +02:00
|
|
|
func (l Location) S2Token() string {
|
|
|
|
return l.ID
|
|
|
|
}
|
|
|
|
|
2020-06-05 16:49:32 +02:00
|
|
|
func (l Location) PrefixedToken() string {
|
|
|
|
return s2.Prefix(l.ID)
|
|
|
|
}
|
|
|
|
|
2019-12-27 05:18:52 +01:00
|
|
|
func (l Location) Name() string {
|
2021-11-12 05:09:17 +01:00
|
|
|
return txt.Shorten(l.LocName, txt.ClipTitle, txt.Ellipsis)
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 12:28:06 +01:00
|
|
|
func (l Location) Category() string {
|
2021-11-12 05:09:17 +01:00
|
|
|
return txt.Shorten(l.LocCategory, txt.ClipKeyword, txt.Ellipsis)
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 20:24:20 +01:00
|
|
|
func (l Location) Label() string {
|
2021-11-12 05:09:17 +01:00
|
|
|
return txt.Shorten(l.LocLabel, txt.ClipLabel, txt.Ellipsis)
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 12:28:06 +01:00
|
|
|
func (l Location) City() string {
|
2021-11-12 05:09:17 +01:00
|
|
|
return txt.Shorten(l.LocCity, txt.ClipPlace, txt.Ellipsis)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) District() string {
|
|
|
|
return txt.Shorten(l.LocDistrict, txt.ClipPlace, txt.Ellipsis)
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) CountryCode() string {
|
2021-11-12 05:09:17 +01:00
|
|
|
return txt.Clip(l.LocCountry, txt.ClipCountryCode)
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
2021-11-11 16:00:42 +01:00
|
|
|
func (l Location) State() string {
|
2021-11-12 05:09:17 +01:00
|
|
|
return txt.Shorten(txt.NormalizeState(l.LocState, l.CountryCode()), txt.ClipPlace, txt.Ellipsis)
|
2021-11-11 16:00:42 +01:00
|
|
|
}
|
|
|
|
|
2019-12-20 20:23:16 +01:00
|
|
|
func (l Location) CountryName() string {
|
2019-12-27 05:18:52 +01:00
|
|
|
return CountryNames[l.LocCountry]
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
2019-12-28 12:28:06 +01:00
|
|
|
|
|
|
|
func (l Location) Source() string {
|
|
|
|
return l.LocSource
|
|
|
|
}
|
2020-04-28 19:41:06 +02:00
|
|
|
|
|
|
|
func (l Location) Keywords() []string {
|
|
|
|
return l.LocKeywords
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) KeywordString() string {
|
2021-11-12 05:09:17 +01:00
|
|
|
return txt.Clip(strings.Join(l.LocKeywords, ", "), txt.ClipVarchar)
|
2020-04-28 19:41:06 +02:00
|
|
|
}
|