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"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
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
|
2021-11-18 00:46:34 +01:00
|
|
|
placeID string
|
2019-12-28 12:28:06 +01:00
|
|
|
LocName string
|
2021-11-18 00:46:34 +01:00
|
|
|
LocStreet string
|
|
|
|
LocPostcode string
|
2019-12-28 12:28:06 +01:00
|
|
|
LocCategory string
|
2019-12-28 20:24:20 +01:00
|
|
|
LocLabel string
|
2021-11-12 05:09:17 +01:00
|
|
|
LocDistrict string
|
2021-11-18 00:46:34 +01:00
|
|
|
LocCity string
|
2019-12-28 12:28:06 +01:00
|
|
|
LocState string
|
|
|
|
LocCountry string
|
2020-04-28 19:41:06 +02:00
|
|
|
LocKeywords []string
|
2021-11-18 00:46:34 +01:00
|
|
|
LocSource 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
|
2021-11-18 00:46:34 +01:00
|
|
|
PlaceID() string
|
2019-12-27 05:18:52 +01:00
|
|
|
Name() string
|
2021-11-18 00:46:34 +01:00
|
|
|
Street() string
|
|
|
|
Category() string
|
|
|
|
Postcode() string
|
2021-11-12 05:09:17 +01:00
|
|
|
District() string
|
2021-11-18 00:46:34 +01:00
|
|
|
City() string
|
2019-12-20 12:20:35 +01:00
|
|
|
State() string
|
2021-11-18 00:46:34 +01:00
|
|
|
CountryCode() string
|
2020-04-28 19:41:06 +02:00
|
|
|
Keywords() []string
|
2021-11-18 00:46:34 +01:00
|
|
|
Source() string
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-12-09 01:10:15 +01:00
|
|
|
case places.ApiName:
|
2020-01-06 06:59:35 +01:00
|
|
|
return l.QueryPlaces()
|
|
|
|
}
|
|
|
|
|
2021-12-09 01:10:15 +01:00
|
|
|
return errors.New("maps: location lookup disabled")
|
2020-01-06 06:59:35 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-18 00:46:34 +01:00
|
|
|
l.placeID = s.PlaceID()
|
2019-12-31 07:16:11 +01:00
|
|
|
l.LocSource = s.Source()
|
|
|
|
l.LocName = s.Name()
|
2021-11-18 00:46:34 +01:00
|
|
|
l.LocStreet = s.Street()
|
|
|
|
l.LocPostcode = s.Postcode()
|
|
|
|
l.LocCategory = s.Category()
|
|
|
|
l.LocLabel = s.Label()
|
2021-11-12 05:09:17 +01:00
|
|
|
l.LocDistrict = s.District()
|
2021-11-18 00:46:34 +01:00
|
|
|
l.LocCity = s.City()
|
2019-12-31 07:16:11 +01:00
|
|
|
l.LocState = s.State()
|
|
|
|
l.LocCountry = s.CountryCode()
|
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
|
|
|
}
|
|
|
|
|
2021-11-18 00:46:34 +01:00
|
|
|
func (l Location) PlaceID() string {
|
2021-11-20 16:36:34 +01:00
|
|
|
return l.placeID
|
2021-11-18 00:46:34 +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-18 00:46:34 +01:00
|
|
|
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)
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 12:28:06 +01:00
|
|
|
func (l Location) Category() string {
|
2021-11-18 00:46:34 +01:00
|
|
|
return txt.Clip(l.LocCategory, 50)
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 20:24:20 +01:00
|
|
|
func (l Location) Label() string {
|
2021-11-18 00:46:34 +01:00
|
|
|
return txt.Clip(l.LocLabel, 400)
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 12:28:06 +01:00
|
|
|
func (l Location) City() string {
|
2021-11-18 00:46:34 +01:00
|
|
|
return txt.Clip(l.LocCity, 100)
|
2021-11-12 05:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) District() string {
|
2021-11-18 00:46:34 +01:00
|
|
|
return txt.Clip(l.LocDistrict, 100)
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) CountryCode() string {
|
2021-11-18 00:46:34 +01:00
|
|
|
return txt.Clip(l.LocCountry, 2)
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
2021-11-11 16:00:42 +01:00
|
|
|
func (l Location) State() string {
|
2022-04-15 09:42:07 +02:00
|
|
|
return txt.Clip(clean.State(l.LocState, l.CountryCode()), 100)
|
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
|
|
|
|
2020-04-28 19:41:06 +02:00
|
|
|
func (l Location) Keywords() []string {
|
|
|
|
return l.LocKeywords
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) KeywordString() string {
|
2021-11-18 00:46:34 +01:00
|
|
|
return txt.Clip(strings.Join(l.LocKeywords, ", "), 300)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) Source() string {
|
|
|
|
return l.LocSource
|
2020-04-28 19:41:06 +02:00
|
|
|
}
|