2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2018-09-16 19:09:40 +02:00
|
|
|
|
2019-12-20 20:23:16 +01:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/maps"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/s2"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-12-20 20:23:16 +01:00
|
|
|
)
|
2019-12-16 20:22:46 +01:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Location used to associate photos to location
|
2018-09-16 19:09:40 +02:00
|
|
|
type Location struct {
|
2019-12-31 01:34:27 +01:00
|
|
|
ID string `gorm:"type:varbinary(16);primary_key;auto_increment:false;"`
|
|
|
|
PlaceID string `gorm:"type:varbinary(16);"`
|
2019-12-28 20:24:20 +01:00
|
|
|
Place *Place
|
2020-04-26 14:31:33 +02:00
|
|
|
LocName string `gorm:"type:varchar(255);"`
|
2020-01-22 13:43:07 +01:00
|
|
|
LocCategory string `gorm:"type:varchar(64);"`
|
2019-12-28 12:28:06 +01:00
|
|
|
LocSource string `gorm:"type:varbinary(16);"`
|
2019-12-21 17:24:29 +01:00
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewLocation creates a location using a token extracted from coordinate
|
2020-04-26 11:41:54 +02:00
|
|
|
func NewLocation(lat, lng float32) *Location {
|
2019-12-20 20:23:16 +01:00
|
|
|
result := &Location{}
|
|
|
|
|
2020-04-26 11:41:54 +02:00
|
|
|
result.ID = s2.Token(float64(lat), float64(lng))
|
2019-12-20 20:23:16 +01:00
|
|
|
|
|
|
|
return result
|
2018-09-17 18:40:57 +02:00
|
|
|
}
|
2019-12-16 20:22:46 +01:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Find gets the location using either the db or the api if not in the db
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Location) Find(api string) error {
|
|
|
|
db := Db()
|
|
|
|
|
2020-04-25 16:17:59 +02:00
|
|
|
if err := db.Preload("Place").First(m, "id = ?", m.ID).Error; err == nil {
|
2019-12-28 12:28:06 +01:00
|
|
|
return nil
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 12:28:06 +01:00
|
|
|
l := &maps.Location{
|
2020-01-02 04:08:33 +01:00
|
|
|
ID: m.ID,
|
2019-12-28 12:28:06 +01:00
|
|
|
}
|
|
|
|
|
2020-01-11 01:59:43 +01:00
|
|
|
if err := l.QueryApi(api); err != nil {
|
2019-12-20 20:23:16 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
if place := FindPlaceByLabel(l.S2Token(), l.Label()); place != nil {
|
2020-04-25 16:17:59 +02:00
|
|
|
m.Place = place
|
|
|
|
} else {
|
|
|
|
m.Place = &Place{
|
2020-04-28 19:41:06 +02:00
|
|
|
ID: l.S2Token(),
|
|
|
|
LocLabel: l.Label(),
|
|
|
|
LocCity: l.City(),
|
|
|
|
LocState: l.State(),
|
|
|
|
LocCountry: l.CountryCode(),
|
|
|
|
LocKeywords: l.KeywordString(),
|
2020-04-25 16:17:59 +02:00
|
|
|
}
|
2019-12-28 20:24:20 +01:00
|
|
|
}
|
|
|
|
|
2020-04-28 19:41:06 +02:00
|
|
|
m.LocName = l.Name()
|
|
|
|
m.LocCategory = l.Category()
|
|
|
|
m.LocSource = l.Source()
|
2019-12-28 12:28:06 +01:00
|
|
|
|
2020-04-24 13:21:18 +02:00
|
|
|
if err := db.Create(m).Error; err == nil {
|
|
|
|
return nil
|
2020-04-25 16:17:59 +02:00
|
|
|
} else if err := db.Preload("Place").First(m, "id = ?", m.ID).Error; err != nil {
|
2019-12-20 20:23:16 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Keywords computes keyword based on a Location
|
2020-03-25 14:14:00 +01:00
|
|
|
func (m *Location) Keywords() (result []string) {
|
2020-04-28 19:41:06 +02:00
|
|
|
if m.Place == nil {
|
|
|
|
log.Errorf("location: place for %s is nil - you might have found a bug", m.ID)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:57:07 +02:00
|
|
|
result = append(result, txt.Keywords(txt.ReplaceSpaces(m.City(), "-"))...)
|
|
|
|
result = append(result, txt.Keywords(txt.ReplaceSpaces(m.State(), "-"))...)
|
|
|
|
result = append(result, txt.Keywords(txt.ReplaceSpaces(m.CountryName(), "-"))...)
|
2020-03-25 14:14:00 +01:00
|
|
|
result = append(result, txt.Keywords(m.Category())...)
|
2020-01-07 17:36:49 +01:00
|
|
|
result = append(result, txt.Keywords(m.Name())...)
|
2020-04-28 19:41:06 +02:00
|
|
|
result = append(result, txt.Keywords(m.Place.LocKeywords)...)
|
2019-12-20 20:23:16 +01:00
|
|
|
|
2020-03-25 14:14:00 +01:00
|
|
|
result = txt.UniqueWords(result)
|
|
|
|
|
2019-12-20 20:23:16 +01:00
|
|
|
return result
|
2019-12-16 20:22:46 +01:00
|
|
|
}
|
2019-12-28 12:28:06 +01:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Unknown checks if the location has no id
|
2019-12-28 12:28:06 +01:00
|
|
|
func (m *Location) Unknown() bool {
|
2019-12-31 01:34:27 +01:00
|
|
|
return m.ID == ""
|
2019-12-28 12:28:06 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Name returns name of location
|
2019-12-28 12:28:06 +01:00
|
|
|
func (m *Location) Name() string {
|
|
|
|
return m.LocName
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NoName checks if the location has no name
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Location) NoName() bool {
|
|
|
|
return m.LocName == ""
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Category returns the location category
|
2019-12-28 12:28:06 +01:00
|
|
|
func (m *Location) Category() string {
|
|
|
|
return m.LocCategory
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NoCategory checks id the location has no category
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Location) NoCategory() bool {
|
|
|
|
return m.LocCategory == ""
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Label returns the location place label
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Location) Label() string {
|
|
|
|
return m.Place.Label()
|
2019-12-28 12:28:06 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// City returns the location place city
|
2019-12-28 12:28:06 +01:00
|
|
|
func (m *Location) City() string {
|
2019-12-28 20:24:20 +01:00
|
|
|
return m.Place.City()
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// LongCity checks if the city name is more than 16 char
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Location) LongCity() bool {
|
|
|
|
return len(m.City()) > 16
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NoCity checks if the location has no city
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Location) NoCity() bool {
|
|
|
|
return m.City() == ""
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// CityContains checks if the location city contains the text string
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Location) CityContains(text string) bool {
|
|
|
|
return strings.Contains(text, m.City())
|
2019-12-28 12:28:06 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// State returns the location place state
|
2019-12-28 12:28:06 +01:00
|
|
|
func (m *Location) State() string {
|
2019-12-28 20:24:20 +01:00
|
|
|
return m.Place.State()
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NoState checks if the location place has no state
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Location) NoState() bool {
|
|
|
|
return m.Place.State() == ""
|
2019-12-28 12:28:06 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// CountryCode returns the location place country code
|
2019-12-28 12:28:06 +01:00
|
|
|
func (m *Location) CountryCode() string {
|
2019-12-28 20:24:20 +01:00
|
|
|
return m.Place.CountryCode()
|
2019-12-28 12:28:06 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// CountryName returns the location place country name
|
2019-12-28 12:28:06 +01:00
|
|
|
func (m *Location) CountryName() string {
|
2019-12-28 20:24:20 +01:00
|
|
|
return m.Place.CountryName()
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Notes returns the locations place notes
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Location) Notes() string {
|
|
|
|
return m.Place.Notes()
|
2019-12-28 12:28:06 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Source returns the source of location information
|
2019-12-28 12:28:06 +01:00
|
|
|
func (m *Location) Source() string {
|
|
|
|
return m.LocSource
|
|
|
|
}
|