-
Notifications
You must be signed in to change notification settings - Fork 26
/
geo.go
102 lines (96 loc) · 2.78 KB
/
geo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"context"
"embed"
"encoding/json"
"fmt"
"strings"
"time"
gogeouri "git.jlel.se/jlelse/go-geouri"
"github.com/carlmjohnson/requests"
geojson "github.com/paulmach/go.geojson"
"go.goblog.app/app/pkgs/bufferpool"
)
func (a *goBlog) geoTitle(g *gogeouri.Geo, lang string) string {
if name, ok := g.Parameters["name"]; ok && len(name) > 0 && name[0] != "" {
return name[0]
}
fallbackTitle := fmt.Sprintf("%.4f, %.4f", g.Latitude, g.Longitude)
fc, err := a.nominatimReverse(g.Latitude, g.Longitude, lang)
if err != nil || len(fc.Features) == 0 {
return fallbackTitle
}
feature := fc.Features[0]
address, ok := feature.Properties["address"].(map[string]interface{})
if !ok {
return fallbackTitle
}
getFirstNonEmpty := func(keys ...string) string {
for _, key := range keys {
if value, ok := address[key].(string); ok && value != "" {
return value
}
}
return ""
}
titleParts := []string{
getFirstNonEmpty("suburb", "neighborhood", "hamlet"),
getFirstNonEmpty("village", "town", "city"),
getFirstNonEmpty("state", "county"),
getFirstNonEmpty("country"),
}
var nonEmptyParts []string
for _, part := range titleParts {
if part != "" {
nonEmptyParts = append(nonEmptyParts, part)
}
}
if title := strings.Join(nonEmptyParts, ", "); title != "" {
return title
}
return fallbackTitle
}
func (a *goBlog) nominatimReverse(lat, lon float64, lang string) (*geojson.FeatureCollection, error) {
key := fmt.Sprintf("nominatim-%v-%v-%v", lat, lon, lang)
fc, err, _ := a.nominatimGroup.Do(key, func() (*geojson.FeatureCollection, error) {
// Check cache
var cached []byte
if cache, _ := a.db.retrievePersistentCache(key); cache != nil {
cached = cache
} else {
// No cache, fetch from Nominatim
buf := bufferpool.Get()
defer bufferpool.Put(buf)
// Create request
rb := requests.URL("https://nominatim.openstreetmap.org/reverse").Client(a.httpClient).ToBytesBuffer(buf)
// Set parameters
rb.Param("format", "geojson")
rb.Param("lat", fmt.Sprintf("%v", lat))
rb.Param("lon", fmt.Sprintf("%v", lon))
rb.Param("accept-language", lang+", en")
rb.Param("zoom", "14")
rb.Param("layer", "address")
// Do request
ctx, cancelFunc := context.WithTimeout(context.Background(), 3*time.Second)
defer cancelFunc()
if err := rb.Fetch(ctx); err != nil {
return nil, err
}
cached = buf.Bytes()
// Cache response
_ = a.db.cachePersistently(key, cached)
}
// Unmarshal response
fc := geojson.NewFeatureCollection()
if err := json.Unmarshal(cached, fc); err != nil {
return nil, err
}
return fc, nil
})
return fc, err
}
func geoOSMLink(g *gogeouri.Geo) string {
return fmt.Sprintf("https://www.openstreetmap.org/?mlat=%v&mlon=%v", g.Latitude, g.Longitude)
}
//go:embed leaflet/*
var leafletFiles embed.FS