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