2019-12-20 11:30:58 +01:00
|
|
|
package maps
|
|
|
|
|
|
|
|
import (
|
2019-12-20 12:04:26 +01:00
|
|
|
"errors"
|
2019-12-20 11:30:58 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/maps/osm"
|
|
|
|
)
|
|
|
|
|
2019-12-28 20:24:20 +01:00
|
|
|
/* TODO
|
|
|
|
|
|
|
|
(SELECT pl.loc_label as album_name, pl.loc_country, YEAR(ph.taken_at) as taken_year, round(count(ph.id)) as photo_count FROM photos ph
|
|
|
|
JOIN places pl ON ph.place_id = pl.id AND pl.id <> 1
|
|
|
|
GROUP BY album_name, taken_year HAVING photo_count > 5) UNION (
|
|
|
|
SELECT c.country_name AS album_name, pl.loc_country, YEAR(ph.taken_at) as taken_year, round(count(ph.id)) as photo_count FROM photos ph
|
|
|
|
JOIN places pl ON ph.place_id = pl.id AND pl.id <> 1
|
|
|
|
JOIN countries c ON c.id = pl.loc_country
|
|
|
|
GROUP BY album_name, taken_year
|
|
|
|
HAVING photo_count > 10)
|
|
|
|
ORDER BY loc_country, album_name, taken_year;
|
|
|
|
|
2019-12-28 20:39:51 +01:00
|
|
|
*/
|
2019-12-28 20:24:20 +01:00
|
|
|
|
2019-12-20 11:30:58 +01:00
|
|
|
// Photo location
|
|
|
|
type Location struct {
|
2019-12-28 12:28:06 +01:00
|
|
|
ID uint64
|
2019-12-27 05:18:52 +01:00
|
|
|
LocLat float64
|
|
|
|
LocLng float64
|
2019-12-28 12:28:06 +01:00
|
|
|
LocName string
|
|
|
|
LocCategory string
|
|
|
|
LocSuburb string
|
2019-12-28 20:24:20 +01:00
|
|
|
LocLabel string
|
2019-12-28 12:28:06 +01:00
|
|
|
LocCity string
|
|
|
|
LocState string
|
|
|
|
LocCountry string
|
|
|
|
LocSource string
|
2019-12-20 12:04:26 +01:00
|
|
|
}
|
|
|
|
|
2019-12-20 12:20:35 +01:00
|
|
|
type LocationSource interface {
|
2019-12-20 23:05:44 +01:00
|
|
|
CountryCode() string
|
2019-12-20 12:20:35 +01:00
|
|
|
Latitude() float64
|
|
|
|
Longitude() float64
|
2019-12-20 23:05:44 +01:00
|
|
|
Category() string
|
2019-12-27 05:18:52 +01:00
|
|
|
Name() string
|
2019-12-20 12:20:35 +01:00
|
|
|
City() string
|
|
|
|
Suburb() string
|
|
|
|
State() string
|
|
|
|
Source() string
|
2019-12-20 11:30:58 +01:00
|
|
|
}
|
|
|
|
|
2019-12-21 17:24:29 +01:00
|
|
|
func NewLocation(lat, lng float64) *Location {
|
2019-12-27 23:58:51 +01:00
|
|
|
id := S2Encode(lat, lng)
|
2019-12-20 20:23:16 +01:00
|
|
|
|
|
|
|
result := &Location{
|
2019-12-21 17:24:29 +01:00
|
|
|
ID: id,
|
2019-12-20 20:23:16 +01:00
|
|
|
LocLat: lat,
|
|
|
|
LocLng: lng,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Location) Query() error {
|
|
|
|
o, err := osm.FindLocation(l.LocLat, l.LocLng)
|
2019-12-20 12:04:26 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:20:35 +01:00
|
|
|
return l.Assign(o)
|
2019-12-20 12:04:26 +01:00
|
|
|
}
|
|
|
|
|
2019-12-20 12:20:35 +01:00
|
|
|
func (l *Location) Assign(s LocationSource) error {
|
|
|
|
l.LocSource = s.Source()
|
2019-12-20 20:23:16 +01:00
|
|
|
|
2019-12-21 17:24:29 +01:00
|
|
|
if l.LocLat == 0 {
|
|
|
|
l.LocLat = s.Latitude()
|
|
|
|
}
|
|
|
|
if l.LocLng == 0 {
|
|
|
|
l.LocLng = s.Longitude()
|
|
|
|
}
|
2019-12-20 11:30:58 +01:00
|
|
|
|
|
|
|
if l.Unknown() {
|
2019-12-20 23:05:44 +01:00
|
|
|
l.LocCategory = "unknown"
|
2019-12-20 12:04:26 +01:00
|
|
|
return errors.New("maps: unknown location")
|
2019-12-20 11:30:58 +01:00
|
|
|
}
|
|
|
|
|
2019-12-27 23:58:51 +01:00
|
|
|
if l.ID == 0 {
|
|
|
|
l.ID = S2Encode(l.LocLat, l.LocLng)
|
2019-12-21 17:24:29 +01:00
|
|
|
}
|
2019-12-20 20:23:16 +01:00
|
|
|
|
2019-12-27 05:18:52 +01:00
|
|
|
l.LocName = s.Name()
|
2019-12-20 12:20:35 +01:00
|
|
|
l.LocCity = s.City()
|
|
|
|
l.LocSuburb = s.Suburb()
|
|
|
|
l.LocState = s.State()
|
2019-12-27 05:18:52 +01:00
|
|
|
l.LocCountry = s.CountryCode()
|
2019-12-20 23:05:44 +01:00
|
|
|
l.LocCategory = s.Category()
|
2019-12-28 20:24:20 +01:00
|
|
|
l.LocLabel = l.label()
|
2019-12-20 12:04:26 +01:00
|
|
|
|
|
|
|
return nil
|
2019-12-20 11:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Location) Unknown() bool {
|
2019-12-20 12:04:26 +01:00
|
|
|
if l.LocLng == 0.0 && l.LocLat == 0.0 {
|
2019-12-20 11:30:58 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:24:20 +01:00
|
|
|
func (l *Location) label() string {
|
2019-12-20 11:30:58 +01:00
|
|
|
if l.Unknown() {
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
|
2019-12-20 20:23:16 +01:00
|
|
|
var countryName = l.CountryName()
|
2019-12-20 11:30:58 +01:00
|
|
|
var loc []string
|
2019-12-20 20:23:16 +01:00
|
|
|
|
2019-12-20 11:30:58 +01:00
|
|
|
shortCountry := len([]rune(countryName)) <= 20
|
|
|
|
|
2019-12-28 20:24:20 +01:00
|
|
|
if l.LocCity != "" {
|
2019-12-20 12:04:26 +01:00
|
|
|
loc = append(loc, l.LocCity)
|
2019-12-20 11:30:58 +01:00
|
|
|
}
|
|
|
|
|
2019-12-20 12:04:26 +01:00
|
|
|
if shortCountry && l.LocState != "" && l.LocCity != l.LocState {
|
|
|
|
loc = append(loc, l.LocState)
|
2019-12-20 11:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if countryName != "" {
|
|
|
|
loc = append(loc, countryName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(loc[:], ", ")
|
|
|
|
}
|
2019-12-20 20:23:16 +01:00
|
|
|
|
|
|
|
func (l Location) Latitude() float64 {
|
|
|
|
return l.LocLat
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) Longitude() float64 {
|
|
|
|
return l.LocLng
|
|
|
|
}
|
|
|
|
|
2019-12-27 05:18:52 +01:00
|
|
|
func (l Location) Name() string {
|
|
|
|
return l.LocName
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 12:28:06 +01:00
|
|
|
func (l Location) Category() string {
|
|
|
|
return l.LocCategory
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) Suburb() string {
|
|
|
|
return l.LocSuburb
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:24:20 +01:00
|
|
|
func (l Location) Label() string {
|
|
|
|
return l.LocLabel
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 12:28:06 +01:00
|
|
|
func (l Location) City() string {
|
|
|
|
return l.LocCity
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 12:28:06 +01:00
|
|
|
func (l Location) State() string {
|
|
|
|
return l.LocState
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) CountryCode() string {
|
2019-12-27 05:18:52 +01:00
|
|
|
return l.LocCountry
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) CountryName() string {
|
2019-12-27 05:18:52 +01:00
|
|
|
return CountryNames[l.LocCountry]
|
2019-12-20 20:23:16 +01:00
|
|
|
}
|
2019-12-28 12:28:06 +01:00
|
|
|
|
|
|
|
func (l Location) Source() string {
|
|
|
|
return l.LocSource
|
|
|
|
}
|