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/jinzhu/gorm"
|
|
|
|
"github.com/photoprism/photoprism/internal/classify"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (m *Photo) UpdateLocation(db *gorm.DB, geoApi string) (keywords []string, labels classify.Labels) {
|
2020-04-16 20:57:00 +02:00
|
|
|
var location = NewLocation(m.PhotoLat, m.PhotoLng)
|
|
|
|
|
|
|
|
err := location.Find(db, geoApi)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if location.Place.New {
|
|
|
|
event.Publish("count.places", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Location = location
|
|
|
|
m.LocationID = location.ID
|
|
|
|
m.Place = location.Place
|
|
|
|
m.PlaceID = location.PlaceID
|
2020-04-18 23:20:54 +02:00
|
|
|
m.PhotoCountry = location.CountryCode()
|
|
|
|
|
|
|
|
if m.TakenSrc != SrcManual {
|
|
|
|
m.TimeZone = m.GetTimeZone()
|
|
|
|
m.TakenAt = m.GetTakenAt()
|
|
|
|
}
|
2020-04-16 20:57:00 +02:00
|
|
|
|
|
|
|
country := NewCountry(location.CountryCode(), location.CountryName()).FirstOrCreate(db)
|
|
|
|
|
|
|
|
if country.New {
|
|
|
|
event.Publish("count.countries", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
locCategory := location.Category()
|
|
|
|
keywords = append(keywords, location.Keywords()...)
|
|
|
|
|
|
|
|
// Append category from reverse location lookup
|
|
|
|
if locCategory != "" {
|
|
|
|
labels = append(labels, classify.LocationLabel(locCategory, 0, -1))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Warn(err)
|
|
|
|
|
2020-04-25 14:22:47 +02:00
|
|
|
m.Place = &UnknownPlace
|
2020-04-16 20:57:00 +02:00
|
|
|
m.PlaceID = UnknownPlace.ID
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:20:54 +02:00
|
|
|
if m.Place != nil && (m.PhotoCountry == "" || m.PhotoCountry == "zz") {
|
2020-04-16 20:57:00 +02:00
|
|
|
m.PhotoCountry = m.Place.LocCountry
|
|
|
|
}
|
|
|
|
|
|
|
|
return keywords, labels
|
|
|
|
}
|