2018-08-09 23:10:05 +02:00
|
|
|
package photoprism
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-09-06 14:47:32 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-08-09 23:10:05 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
2018-09-06 14:47:32 +02:00
|
|
|
"net/http"
|
2018-08-09 23:10:05 +02:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Location struct {
|
|
|
|
gorm.Model
|
2018-09-12 16:37:30 +02:00
|
|
|
LocDisplayName string
|
|
|
|
LocLat float64
|
|
|
|
LocLong float64
|
|
|
|
LocCategory string
|
|
|
|
LocType string
|
|
|
|
LocName string
|
|
|
|
LocCity string
|
|
|
|
LocPostcode string
|
|
|
|
LocCounty string
|
|
|
|
LocState string
|
|
|
|
LocCountry string
|
|
|
|
LocCountryCode string
|
|
|
|
LocFavorite bool
|
2018-08-09 23:10:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type OpenstreetmapAddress struct {
|
|
|
|
Town string `json:"town"`
|
|
|
|
City string `json:"city"`
|
|
|
|
Postcode string `json:"postcode"`
|
|
|
|
County string `json:"county"`
|
|
|
|
State string `json:"state"`
|
|
|
|
Country string `json:"country"`
|
|
|
|
CountryCode string `json:"country_code"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type OpenstreetmapLocation struct {
|
2018-09-06 14:47:32 +02:00
|
|
|
PlaceId string `json:"place_id"`
|
2018-08-09 23:10:05 +02:00
|
|
|
Lat string `json:"lat"`
|
|
|
|
Lon string `json:"lon"`
|
2018-09-11 10:56:52 +02:00
|
|
|
Name string `json:"name"`
|
2018-08-09 23:10:05 +02:00
|
|
|
Category string `json:"category"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
DisplayName string `json:"display_name"`
|
|
|
|
Address *OpenstreetmapAddress `json:"address"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// See https://wiki.openstreetmap.org/wiki/Nominatim#Reverse_Geocoding
|
|
|
|
func (m *MediaFile) GetLocation() (*Location, error) {
|
|
|
|
if m.location != nil {
|
|
|
|
return m.location, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
location := &Location{}
|
|
|
|
|
|
|
|
openstreetmapLocation := &OpenstreetmapLocation{
|
|
|
|
Address: &OpenstreetmapAddress{},
|
|
|
|
}
|
|
|
|
|
|
|
|
if exifData, err := m.GetExifData(); err == nil {
|
|
|
|
url := fmt.Sprintf("https://nominatim.openstreetmap.org/reverse?lat=%f&lon=%f&format=jsonv2", exifData.Lat, exifData.Long)
|
|
|
|
|
|
|
|
if res, err := http.Get(url); err == nil {
|
|
|
|
json.NewDecoder(res.Body).Decode(openstreetmapLocation)
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if id, err := strconv.Atoi(openstreetmapLocation.PlaceId); err == nil && id > 0 {
|
|
|
|
location.ID = uint(id)
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("no location found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if openstreetmapLocation.Address.City != "" {
|
2018-09-12 16:37:30 +02:00
|
|
|
location.LocCity = openstreetmapLocation.Address.City
|
2018-08-09 23:10:05 +02:00
|
|
|
} else {
|
2018-09-12 16:37:30 +02:00
|
|
|
location.LocCity = openstreetmapLocation.Address.Town
|
2018-08-09 23:10:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if lat, err := strconv.ParseFloat(openstreetmapLocation.Lat, 64); err == nil {
|
2018-09-12 16:37:30 +02:00
|
|
|
location.LocLat = lat
|
2018-08-09 23:10:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if lon, err := strconv.ParseFloat(openstreetmapLocation.Lon, 64); err == nil {
|
2018-09-12 16:37:30 +02:00
|
|
|
location.LocLong = lon
|
2018-08-09 23:10:05 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 16:37:30 +02:00
|
|
|
location.LocName = openstreetmapLocation.Name
|
|
|
|
location.LocPostcode = openstreetmapLocation.Address.Postcode
|
|
|
|
location.LocCounty = openstreetmapLocation.Address.County
|
|
|
|
location.LocState = openstreetmapLocation.Address.State
|
|
|
|
location.LocCountry = openstreetmapLocation.Address.Country
|
|
|
|
location.LocCountryCode = openstreetmapLocation.Address.CountryCode
|
|
|
|
location.LocDisplayName = openstreetmapLocation.DisplayName
|
|
|
|
location.LocCategory = openstreetmapLocation.Category
|
2018-09-11 10:56:52 +02:00
|
|
|
|
|
|
|
if openstreetmapLocation.Type != "yes" && openstreetmapLocation.Type != "unclassified" {
|
2018-09-12 16:37:30 +02:00
|
|
|
location.LocType = openstreetmapLocation.Type
|
2018-09-11 10:56:52 +02:00
|
|
|
}
|
2018-08-09 23:10:05 +02:00
|
|
|
|
|
|
|
m.location = location
|
|
|
|
|
|
|
|
return m.location, nil
|
|
|
|
}
|