2021-05-26 15:41:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-01-04 09:37:48 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2021-11-10 10:13:30 +00:00
|
|
|
"embed"
|
2021-05-26 15:41:14 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-06-18 12:32:03 +00:00
|
|
|
gogeouri "git.jlel.se/jlelse/go-geouri"
|
2022-01-04 09:37:48 +00:00
|
|
|
"github.com/carlmjohnson/requests"
|
2021-05-26 15:41:14 +00:00
|
|
|
geojson "github.com/paulmach/go.geojson"
|
|
|
|
"github.com/thoas/go-funk"
|
|
|
|
)
|
|
|
|
|
2021-06-19 06:37:16 +00:00
|
|
|
func (a *goBlog) geoTitle(g *gogeouri.Geo, lang string) string {
|
2021-06-18 12:32:03 +00:00
|
|
|
if name, ok := g.Parameters["name"]; ok && len(name) > 0 && name[0] != "" {
|
|
|
|
return name[0]
|
|
|
|
}
|
2021-06-19 06:37:16 +00:00
|
|
|
ba, err := a.photonReverse(g.Latitude, g.Longitude, lang)
|
2021-05-26 15:41:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
fc, err := geojson.UnmarshalFeatureCollection(ba)
|
|
|
|
if err != nil || len(fc.Features) < 1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
f := fc.Features[0]
|
|
|
|
name := f.PropertyMustString("name", "")
|
|
|
|
city := f.PropertyMustString("city", "")
|
|
|
|
state := f.PropertyMustString("state", "")
|
|
|
|
country := f.PropertyMustString("country", "")
|
|
|
|
return strings.Join(funk.FilterString([]string{name, city, state, country}, func(s string) bool { return s != "" }), ", ")
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:37:16 +00:00
|
|
|
func (a *goBlog) photonReverse(lat, lon float64, lang string) ([]byte, error) {
|
2021-05-26 15:51:31 +00:00
|
|
|
cacheKey := fmt.Sprintf("photon-%v-%v-%v", lat, lon, lang)
|
2021-06-19 06:37:16 +00:00
|
|
|
cache, _ := a.db.retrievePersistentCache(cacheKey)
|
2021-05-26 15:41:14 +00:00
|
|
|
if cache != nil {
|
|
|
|
return cache, nil
|
|
|
|
}
|
2022-01-04 09:37:48 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
rb := requests.URL("https://photon.komoot.io/reverse").Client(a.httpClient).UserAgent(appUserAgent).ToBytesBuffer(&buf)
|
|
|
|
rb.Param("lat", fmt.Sprintf("%v", lat)).Param("lon", fmt.Sprintf("%v", lon))
|
2021-05-26 15:51:31 +00:00
|
|
|
if lang == "de" || lang == "fr" || lang == "it" {
|
2022-01-04 09:37:48 +00:00
|
|
|
rb.Param("lang", lang)
|
2021-05-26 15:51:31 +00:00
|
|
|
} else {
|
2022-01-04 09:37:48 +00:00
|
|
|
rb.Param("lang", "en")
|
2021-05-26 15:51:31 +00:00
|
|
|
}
|
2022-01-04 09:37:48 +00:00
|
|
|
if err := rb.Fetch(context.Background()); err != nil {
|
2021-05-26 15:41:14 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-04 09:37:48 +00:00
|
|
|
_ = a.db.cachePersistently(cacheKey, buf.Bytes())
|
|
|
|
return buf.Bytes(), nil
|
2021-05-26 15:41:14 +00:00
|
|
|
}
|
2021-06-18 12:32:03 +00:00
|
|
|
|
|
|
|
func geoOSMLink(g *gogeouri.Geo) string {
|
|
|
|
return fmt.Sprintf("https://www.openstreetmap.org/?mlat=%v&mlon=%v", g.Latitude, g.Longitude)
|
|
|
|
}
|
2021-11-10 10:13:30 +00:00
|
|
|
|
|
|
|
//go:embed leaflet/*
|
|
|
|
var leafletFiles embed.FS
|