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"
|
2020-01-02 06:10:28 +01:00
|
|
|
"sync"
|
2019-12-20 20:23:16 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/photoprism/photoprism/internal/maps"
|
2020-01-08 19:51:21 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
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-01-02 06:10:28 +01:00
|
|
|
var locationMutex = sync.Mutex{}
|
|
|
|
|
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-01-22 13:43:07 +01:00
|
|
|
LocName string `gorm:"type:varchar(128);"`
|
|
|
|
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
|
|
|
// Lock location for updates
|
2020-01-02 06:10:28 +01:00
|
|
|
func (Location) Lock() {
|
|
|
|
locationMutex.Lock()
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Unlock location for updates
|
2020-01-02 06:10:28 +01:00
|
|
|
func (Location) Unlock() {
|
|
|
|
locationMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewLocation creates a location using a token extracted from coordinate
|
2019-12-20 20:23:16 +01:00
|
|
|
func NewLocation(lat, lng float64) *Location {
|
|
|
|
result := &Location{}
|
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
result.ID = s2.Token(lat, 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-01-06 06:59:35 +01:00
|
|
|
func (m *Location) Find(db *gorm.DB, api string) error {
|
2020-01-08 19:51:21 +01:00
|
|
|
mutex.Db.Lock()
|
|
|
|
defer mutex.Db.Unlock()
|
2020-01-06 04:24:49 +01:00
|
|
|
|
2019-12-27 23:58:51 +01:00
|
|
|
if err := db.First(m, "id = ?", m.ID).Error; err == nil {
|
2019-12-28 20:24:20 +01:00
|
|
|
m.Place = FindPlace(m.PlaceID, db)
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:24:20 +01:00
|
|
|
m.Place = FindPlaceByLabel(l.ID, l.LocLabel, db)
|
|
|
|
|
|
|
|
if m.Place.NoID() {
|
|
|
|
m.Place.ID = l.ID
|
|
|
|
m.Place.LocLabel = l.LocLabel
|
|
|
|
m.Place.LocCity = l.LocCity
|
|
|
|
m.Place.LocState = l.LocState
|
|
|
|
m.Place.LocCountry = l.LocCountry
|
|
|
|
}
|
|
|
|
|
2019-12-28 12:28:06 +01:00
|
|
|
m.LocName = l.LocName
|
|
|
|
m.LocCategory = l.LocCategory
|
|
|
|
m.LocSource = l.LocSource
|
|
|
|
|
2019-12-20 20:23:16 +01:00
|
|
|
if err := db.Create(m).Error; err != nil {
|
|
|
|
log.Errorf("location: %s", err)
|
|
|
|
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-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())...)
|
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
|
|
|
|
}
|