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"
|
2021-07-17 15:21:03 +02:00
|
|
|
"gopkg.in/photoprism/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 {
|
2021-01-26 19:18:37 +01:00
|
|
|
location, err := time.LoadLocation(m.TimeZone)
|
2020-04-18 23:20:54 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return m.TakenAt
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:18:37 +01:00
|
|
|
if takenAt, err := time.ParseInLocation("2006-01-02T15:04:05", m.TakenAtLocal.Format("2006-01-02T15:04:05"), location); err != nil {
|
2020-04-18 23:20:54 +02:00
|
|
|
return m.TakenAt
|
|
|
|
} else {
|
|
|
|
return takenAt.UTC()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:18:37 +01:00
|
|
|
// GetTakenAtLocal returns local time for TakenAt.
|
|
|
|
func (m *Photo) GetTakenAtLocal() time.Time {
|
|
|
|
location, err := time.LoadLocation(m.TimeZone)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return m.TakenAtLocal
|
|
|
|
}
|
|
|
|
|
|
|
|
if takenAtLocal, err := time.ParseInLocation("2006-01-02T15:04:05", m.TakenAt.In(location).Format("2006-01-02T15:04:05"), time.UTC); err != nil {
|
|
|
|
return m.TakenAtLocal
|
|
|
|
} else {
|
|
|
|
return takenAtLocal.UTC()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:20:54 +02:00
|
|
|
// UpdateLocation updates location and labels based on latitude and longitude.
|
2020-12-05 00:13:44 +01:00
|
|
|
func (m *Photo) UpdateLocation() (keywords []string, labels classify.Labels) {
|
2020-05-28 15:12:18 +02:00
|
|
|
if m.HasLatLng() {
|
2020-07-12 08:27:05 +02:00
|
|
|
var location = NewCell(m.PhotoLat, m.PhotoLng)
|
2020-04-16 20:57:00 +02:00
|
|
|
|
2020-12-05 00:13:44 +01: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-12-04 23:16:22 +01:00
|
|
|
log.Warnf("photo: failed fetching geo data (uid %s, cell %s)", m.PhotoUID, location.ID)
|
|
|
|
} else if err == nil && location.ID != UnknownLocation.ID {
|
2020-07-12 08:27:05 +02:00
|
|
|
m.Cell = location
|
|
|
|
m.CellID = 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 != "" {
|
2021-02-21 17:18:36 +01:00
|
|
|
labels = append(labels, classify.LocationLabel(locCategory, 0))
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-07-12 08:27:05 +02:00
|
|
|
m.Cell = &UnknownLocation
|
|
|
|
m.CellID = UnknownLocation.ID
|
2021-02-11 19:48:33 +01:00
|
|
|
|
|
|
|
// Remove place estimate if better data is available.
|
|
|
|
if SrcPriority[m.PlaceSrc] > SrcPriority[SrcEstimate] {
|
|
|
|
m.Place = &UnknownPlace
|
|
|
|
m.PlaceID = UnknownPlace.ID
|
|
|
|
}
|
2020-06-04 22:26:00 +02:00
|
|
|
} else if err := m.LoadLocation(); err == nil {
|
2020-07-12 08:27:05 +02:00
|
|
|
m.Place = m.Cell.Place
|
|
|
|
m.PlaceID = m.Cell.PlaceID
|
2020-06-05 10:17:00 +02:00
|
|
|
} else {
|
2021-02-11 19:48:33 +01:00
|
|
|
log.Warnf("photo: location %s not found in %s", m.CellID, m.PhotoName)
|
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 {
|
2021-02-11 19:48:33 +01:00
|
|
|
log.Warnf("photo: place %s not found in %s", m.PlaceID, m.PhotoName)
|
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
|
|
|
|
}
|