2020-04-16 20:57:00 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
2020-04-18 23:20:54 +02:00
|
|
|
"time"
|
2020-04-16 20:57:00 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/classify"
|
2020-05-28 15:12:18 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/maps"
|
2020-04-18 23:20:54 +02:00
|
|
|
"gopkg.in/ugjka/go-tz.v2/tz"
|
2020-04-16 20:57:00 +02:00
|
|
|
)
|
|
|
|
|
2020-04-18 23:20:54 +02:00
|
|
|
// GetTimeZone uses PhotoLat and PhotoLng to guess the time zone of the photo.
|
|
|
|
func (m *Photo) GetTimeZone() string {
|
|
|
|
result := "UTC"
|
|
|
|
|
|
|
|
if m.HasLatLng() {
|
|
|
|
zones, err := tz.GetZone(tz.Point{
|
2020-04-26 11:41:54 +02:00
|
|
|
Lat: float64(m.PhotoLat),
|
|
|
|
Lon: float64(m.PhotoLng),
|
2020-04-18 23:20:54 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil && len(zones) > 0 {
|
|
|
|
result = zones[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
// CountryName returns the photo country name.
|
|
|
|
func (m *Photo) CountryName() string {
|
2020-05-31 17:45:58 +02:00
|
|
|
if name, ok := maps.CountryNames[m.CountryCode()]; ok {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
return UnknownCountry.CountryName
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CountryCode returns the photo country code.
|
|
|
|
func (m *Photo) CountryCode() string {
|
2020-05-31 17:45:58 +02:00
|
|
|
if len(m.PhotoCountry) != 2 {
|
|
|
|
m.PhotoCountry = UnknownCountry.ID
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
return m.PhotoCountry
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:20:54 +02:00
|
|
|
// GetTakenAt returns UTC time for TakenAtLocal.
|
|
|
|
func (m *Photo) GetTakenAt() time.Time {
|
|
|
|
loc, err := time.LoadLocation(m.TimeZone)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return m.TakenAt
|
|
|
|
}
|
|
|
|
|
|
|
|
if takenAt, err := time.ParseInLocation("2006-01-02T15:04:05", m.TakenAtLocal.Format("2006-01-02T15:04:05"), loc); err != nil {
|
|
|
|
return m.TakenAt
|
|
|
|
} else {
|
|
|
|
return takenAt.UTC()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateLocation updates location and labels based on latitude and longitude.
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Photo) UpdateLocation(geoApi string) (keywords []string, labels classify.Labels) {
|
2020-05-28 15:12:18 +02:00
|
|
|
if m.HasLatLng() {
|
|
|
|
var location = NewLocation(m.PhotoLat, m.PhotoLng)
|
2020-04-16 20:57:00 +02:00
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
err := location.Find(geoApi)
|
2020-04-16 20:57:00 +02:00
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
if location.Place == nil {
|
2020-05-29 12:56:24 +02:00
|
|
|
log.Warnf("photo: location place is nil (uid %s, location %s) - bug?", m.PhotoUID, location.ID)
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
2020-05-25 19:10:44 +02:00
|
|
|
|
2020-05-29 12:56:24 +02:00
|
|
|
if err == nil && location.Place != nil && location.ID != UnknownLocation.ID {
|
2020-05-28 15:12:18 +02:00
|
|
|
m.Location = location
|
2020-05-29 12:56:24 +02:00
|
|
|
m.LocationID = location.ID
|
2020-05-28 15:12:18 +02:00
|
|
|
m.Place = location.Place
|
2020-05-29 12:56:24 +02:00
|
|
|
m.PlaceID = location.PlaceID
|
2020-05-28 15:12:18 +02:00
|
|
|
m.PhotoCountry = location.CountryCode()
|
2020-04-18 23:20:54 +02:00
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
if m.TakenSrc != SrcManual {
|
|
|
|
m.TimeZone = m.GetTimeZone()
|
|
|
|
m.TakenAt = m.GetTakenAt()
|
|
|
|
}
|
2020-04-16 20:57:00 +02:00
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
FirstOrCreateCountry(NewCountry(location.CountryCode(), location.CountryName()))
|
2020-04-16 20:57:00 +02:00
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
locCategory := location.Category()
|
|
|
|
keywords = append(keywords, location.Keywords()...)
|
2020-04-16 20:57:00 +02:00
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
// Append category from reverse location lookup
|
|
|
|
if locCategory != "" {
|
|
|
|
labels = append(labels, classify.LocationLabel(locCategory, 0, -1))
|
|
|
|
}
|
|
|
|
|
|
|
|
return keywords, labels
|
2020-04-16 20:57:00 +02:00
|
|
|
}
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
keywords = []string{}
|
|
|
|
labels = classify.Labels{}
|
2020-04-16 20:57:00 +02:00
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
if m.UnknownLocation() {
|
2020-05-25 19:10:44 +02:00
|
|
|
m.Location = &UnknownLocation
|
2020-05-29 12:56:24 +02:00
|
|
|
m.LocationID = UnknownLocation.ID
|
2020-06-04 22:26:00 +02:00
|
|
|
} else if err := m.LoadLocation(); err == nil {
|
2020-05-28 15:12:18 +02:00
|
|
|
m.Place = m.Location.Place
|
2020-05-29 12:56:24 +02:00
|
|
|
m.PlaceID = m.Location.PlaceID
|
2020-06-05 10:17:00 +02:00
|
|
|
} else {
|
|
|
|
log.Warn(err)
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if m.UnknownPlace() {
|
2020-04-25 14:22:47 +02:00
|
|
|
m.Place = &UnknownPlace
|
2020-05-29 12:56:24 +02:00
|
|
|
m.PlaceID = UnknownPlace.ID
|
2020-06-04 22:26:00 +02:00
|
|
|
} else if err := m.LoadPlace(); err == nil {
|
2020-05-28 15:12:18 +02:00
|
|
|
m.PhotoCountry = m.Place.CountryCode()
|
2020-06-05 10:17:00 +02:00
|
|
|
} else {
|
|
|
|
log.Warn(err)
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if m.UnknownCountry() {
|
2020-06-01 09:45:24 +02:00
|
|
|
m.EstimateCountry()
|
2020-04-16 20:57:00 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
if m.HasCountry() {
|
|
|
|
FirstOrCreateCountry(NewCountry(m.CountryCode(), m.CountryName()))
|
2020-04-16 20:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return keywords, labels
|
|
|
|
}
|