2020-12-30 11:11:58 +01:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2021-11-20 16:36:34 +01:00
|
|
|
|
2020-12-30 11:11:58 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2021-11-22 18:18:41 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/geo"
|
2021-12-14 20:01:39 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
2020-12-30 11:11:58 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
2021-11-24 16:56:57 +01:00
|
|
|
const Accuracy1Km = 1000
|
|
|
|
|
2020-12-30 11:11:58 +01:00
|
|
|
// EstimateCountry updates the photo with an estimated country if possible.
|
|
|
|
func (m *Photo) EstimateCountry() {
|
2021-11-25 12:48:07 +01:00
|
|
|
if SrcPriority[m.PlaceSrc] > SrcPriority[SrcEstimate] || m.HasLocation() || m.HasPlace() {
|
|
|
|
// Keep existing data.
|
|
|
|
return
|
|
|
|
} else if m.UnknownCamera() && m.PhotoType == TypeImage {
|
|
|
|
// Don't estimate if it seems to be a non-photographic image.
|
2020-12-30 11:11:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-24 12:49:20 +01:00
|
|
|
// Reset country.
|
2020-12-30 11:11:58 +01:00
|
|
|
unknown := UnknownCountry.ID
|
|
|
|
countryCode := unknown
|
|
|
|
|
2021-11-24 12:49:20 +01:00
|
|
|
// Try to guess country from photo title.
|
|
|
|
if code := txt.CountryCode(m.PhotoTitle); code != unknown && m.TitleSrc != SrcAuto {
|
2020-12-30 11:11:58 +01:00
|
|
|
countryCode = code
|
|
|
|
}
|
|
|
|
|
2021-11-24 12:49:20 +01:00
|
|
|
// Try to guess country from filename and path.
|
2020-12-30 11:11:58 +01:00
|
|
|
if countryCode == unknown {
|
|
|
|
if code := txt.CountryCode(m.PhotoName); code != unknown && !fs.IsGenerated(m.PhotoName) {
|
|
|
|
countryCode = code
|
|
|
|
} else if code := txt.CountryCode(m.PhotoPath); code != unknown {
|
|
|
|
countryCode = code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 12:49:20 +01:00
|
|
|
// Try to guess country from original filename.
|
2020-12-30 11:11:58 +01:00
|
|
|
if countryCode == unknown && m.OriginalName != "" && !fs.IsGenerated(m.OriginalName) {
|
|
|
|
if code := txt.CountryCode(m.OriginalName); code != UnknownCountry.ID {
|
|
|
|
countryCode = code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 12:49:20 +01:00
|
|
|
// Set new country?
|
2020-12-30 11:11:58 +01:00
|
|
|
if countryCode != unknown {
|
|
|
|
m.PhotoCountry = countryCode
|
|
|
|
m.PlaceSrc = SrcEstimate
|
2021-11-23 19:23:10 +01:00
|
|
|
m.EstimatedAt = TimePointer()
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Debugf("photo: estimated country for %s is %s", m, sanitize.Log(m.CountryName()))
|
2020-12-30 11:11:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 18:18:41 +01:00
|
|
|
// EstimateLocation updates the photo with an estimated place and country if possible.
|
|
|
|
func (m *Photo) EstimateLocation(force bool) {
|
2021-11-24 14:46:06 +01:00
|
|
|
if SrcPriority[m.PlaceSrc] > SrcPriority[SrcEstimate] || m.HasLocation() && m.PlaceSrc == SrcAuto {
|
2021-11-25 12:48:07 +01:00
|
|
|
// Keep existing data.
|
2021-11-18 02:23:25 +01:00
|
|
|
return
|
|
|
|
} else if force || m.EstimatedAt == nil {
|
|
|
|
// Proceed.
|
|
|
|
} else if hours := TimeStamp().Sub(*m.EstimatedAt) / time.Hour; hours < MetadataEstimateInterval {
|
2021-11-25 12:48:07 +01:00
|
|
|
// Ignore if location has been estimated recently (in the last 7 days by default).
|
2020-12-30 11:11:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-24 21:06:30 +01:00
|
|
|
m.EstimatedAt = TimePointer()
|
|
|
|
|
2021-11-25 12:48:07 +01:00
|
|
|
// Don't estimate if it seems to be a non-photographic image.
|
2021-11-25 09:38:59 +01:00
|
|
|
if m.UnknownCamera() && m.PhotoType == TypeImage {
|
2021-11-24 21:06:30 +01:00
|
|
|
m.RemoveLocation(SrcEstimate, false)
|
2021-11-25 14:52:26 +01:00
|
|
|
m.RemoveLocationLabels()
|
2021-11-24 21:06:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Estimate country if TakenAt is unreliable.
|
2021-11-24 12:49:20 +01:00
|
|
|
if SrcPriority[m.TakenSrc] <= SrcPriority[SrcName] {
|
2021-11-24 16:56:57 +01:00
|
|
|
m.RemoveLocation(SrcEstimate, false)
|
2021-11-25 14:52:26 +01:00
|
|
|
m.RemoveLocationLabels()
|
2021-11-20 16:36:34 +01:00
|
|
|
m.EstimateCountry()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-18 02:23:25 +01:00
|
|
|
var err error
|
|
|
|
|
2021-11-23 19:23:10 +01:00
|
|
|
rangeMin := m.TakenAt.Add(-1 * time.Hour * 37)
|
|
|
|
rangeMax := m.TakenAt.Add(time.Hour * 37)
|
2021-11-18 02:23:25 +01:00
|
|
|
|
|
|
|
// Find photo with location info taken at a similar time...
|
2021-11-22 18:18:41 +01:00
|
|
|
var mostRecent Photos
|
2020-12-30 11:11:58 +01:00
|
|
|
|
|
|
|
switch DbDialect() {
|
|
|
|
case MySQL:
|
2021-11-18 02:23:25 +01:00
|
|
|
err = UnscopedDb().
|
2021-11-22 18:18:41 +01:00
|
|
|
Where("photo_lat <> 0 AND photo_lng <> 0").
|
|
|
|
Where("place_src <> '' AND place_src <> ? AND place_id IS NOT NULL AND place_id <> '' AND place_id <> 'zz'", SrcEstimate).
|
|
|
|
Where("taken_src <> '' AND taken_at BETWEEN CAST(? AS DATETIME) AND CAST(? AS DATETIME)", rangeMin, rangeMax).
|
2021-11-23 19:23:10 +01:00
|
|
|
Order(gorm.Expr("ABS(TIMESTAMPDIFF(SECOND, taken_at, ?))", m.TakenAt)).Limit(2).
|
2021-11-22 18:18:41 +01:00
|
|
|
Preload("Place").Find(&mostRecent).Error
|
2021-12-09 07:47:23 +01:00
|
|
|
case SQLite3:
|
2021-11-18 02:23:25 +01:00
|
|
|
err = UnscopedDb().
|
2021-11-22 18:18:41 +01:00
|
|
|
Where("photo_lat <> 0 AND photo_lng <> 0").
|
|
|
|
Where("place_src <> '' AND place_src <> ? AND place_id IS NOT NULL AND place_id <> '' AND place_id <> 'zz'", SrcEstimate).
|
|
|
|
Where("taken_src <> '' AND taken_at BETWEEN ? AND ?", rangeMin, rangeMax).
|
2021-11-23 19:23:10 +01:00
|
|
|
Order(gorm.Expr("ABS(JulianDay(taken_at) - JulianDay(?))", m.TakenAt)).Limit(2).
|
2021-11-22 18:18:41 +01:00
|
|
|
Preload("Place").Find(&mostRecent).Error
|
2020-12-30 11:11:58 +01:00
|
|
|
default:
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Warnf("photo: unsupported sql dialect %s", sanitize.Log(DbDialect()))
|
2020-12-30 11:11:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-23 19:23:10 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("photo: %s while estimating position", err)
|
|
|
|
}
|
|
|
|
|
2021-11-18 02:23:25 +01:00
|
|
|
// Found?
|
2021-11-23 19:23:10 +01:00
|
|
|
if len(mostRecent) == 0 {
|
|
|
|
log.Debugf("photo: unknown position at %s", m.TakenAt)
|
2021-11-24 16:56:57 +01:00
|
|
|
m.RemoveLocation(SrcEstimate, false)
|
2021-11-25 14:52:26 +01:00
|
|
|
m.RemoveLocationLabels()
|
2020-12-30 11:11:58 +01:00
|
|
|
m.EstimateCountry()
|
2021-11-22 18:18:41 +01:00
|
|
|
} else if recentPhoto := mostRecent[0]; recentPhoto.HasLocation() && recentPhoto.HasPlace() {
|
2021-11-18 02:23:25 +01:00
|
|
|
// Too much time difference?
|
2020-12-30 11:11:58 +01:00
|
|
|
if hours := recentPhoto.TakenAt.Sub(m.TakenAt) / time.Hour; hours < -36 || hours > 36 {
|
2021-11-23 19:23:10 +01:00
|
|
|
log.Debugf("photo: skipping %s, %d hours time difference to recent position", m, hours)
|
2021-11-24 16:56:57 +01:00
|
|
|
m.RemoveLocation(SrcEstimate, false)
|
2021-11-25 14:52:26 +01:00
|
|
|
m.RemoveLocationLabels()
|
2021-11-23 19:23:10 +01:00
|
|
|
m.EstimateCountry()
|
2021-11-25 09:38:59 +01:00
|
|
|
} else if len(mostRecent) == 1 || m.UnknownCamera() {
|
2021-11-24 16:56:57 +01:00
|
|
|
m.AdoptPlace(recentPhoto, SrcEstimate, false)
|
2021-11-25 09:38:59 +01:00
|
|
|
} else {
|
2021-11-22 18:18:41 +01:00
|
|
|
p1 := mostRecent[0]
|
|
|
|
p2 := mostRecent[1]
|
|
|
|
|
2021-11-23 19:23:10 +01:00
|
|
|
movement := geo.NewMovement(p1.Position(), p2.Position())
|
2021-11-22 18:18:41 +01:00
|
|
|
|
2021-11-24 16:56:57 +01:00
|
|
|
// Ignore inaccurate coordinate estimates.
|
|
|
|
if estimate := movement.EstimatePosition(m.TakenAt); movement.Km() < 100 && estimate.Accuracy < Accuracy1Km {
|
|
|
|
m.SetPosition(estimate, SrcEstimate, false)
|
2021-11-22 18:18:41 +01:00
|
|
|
} else {
|
2021-11-24 16:56:57 +01:00
|
|
|
m.AdoptPlace(recentPhoto, SrcEstimate, false)
|
2021-11-22 18:18:41 +01:00
|
|
|
}
|
2020-12-30 11:11:58 +01:00
|
|
|
}
|
2021-11-25 09:38:59 +01:00
|
|
|
} else if recentPhoto.HasCountry() {
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Debugf("photo: estimated country for %s is %s", m, sanitize.Log(m.CountryName()))
|
2021-11-25 09:38:59 +01:00
|
|
|
m.RemoveLocation(SrcEstimate, false)
|
2021-11-25 14:52:26 +01:00
|
|
|
m.RemoveLocationLabels()
|
2021-11-25 09:38:59 +01:00
|
|
|
m.PhotoCountry = recentPhoto.PhotoCountry
|
|
|
|
m.PlaceSrc = SrcEstimate
|
|
|
|
m.UpdateTimeZone(recentPhoto.TimeZone)
|
2021-11-22 18:18:41 +01:00
|
|
|
} else {
|
2021-11-23 19:23:10 +01:00
|
|
|
log.Warnf("photo: %s has no location, uid %s", recentPhoto.PhotoName, recentPhoto.PhotoUID)
|
2021-11-24 16:56:57 +01:00
|
|
|
m.RemoveLocation(SrcEstimate, false)
|
2021-11-25 14:52:26 +01:00
|
|
|
m.RemoveLocationLabels()
|
2021-11-22 18:18:41 +01:00
|
|
|
m.EstimateCountry()
|
2020-12-30 11:11:58 +01:00
|
|
|
}
|
|
|
|
}
|