forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoMap.go
147 lines (124 loc) · 3.91 KB
/
geoMap.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"encoding/json"
"io"
"net/http"
"go.goblog.app/app/pkgs/bufferpool"
"go.goblog.app/app/pkgs/contenttype"
)
const defaultGeoMapPath = "/map"
func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) {
blog, bc := a.getBlog(r)
mapPath := bc.getRelativePath(defaultIfEmpty(bc.Map.Path, defaultGeoMapPath))
canonical := a.getFullAddress(mapPath)
allPostsWithLocationRequestConfig := &postsRequestConfig{
blog: blog,
parameters: []string{a.cfg.Micropub.LocationParam, gpxParameter},
withOnlyParameters: []string{a.cfg.Micropub.LocationParam, gpxParameter},
}
allPostsWithLocationRequestConfig.status, allPostsWithLocationRequestConfig.visibility = a.getDefaultPostStates(r)
allPostsWithLocation, err := a.db.countPosts(allPostsWithLocationRequestConfig)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
if allPostsWithLocation == 0 {
a.render(w, r, a.renderGeoMap, &renderData{
Canonical: canonical,
Data: &geoMapRenderData{
noLocations: true,
},
})
return
}
a.render(w, r, a.renderGeoMap, &renderData{
Canonical: canonical,
Data: &geoMapRenderData{
locations: "url:" + canonical + geoMapLocationsSubpath,
tracks: "url:" + canonical + geoMapTracksSubpath,
attribution: a.getMapAttribution(),
minZoom: a.getMinZoom(),
maxZoom: a.getMaxZoom(),
},
})
}
const geoMapTracksSubpath = "/tracks.json"
func (a *goBlog) serveGeoMapTracks(w http.ResponseWriter, r *http.Request) {
blog, _ := a.getBlog(r)
allPostsWithTracksRequestConfig := &postsRequestConfig{
blog: blog,
parameters: []string{gpxParameter},
withOnlyParameters: []string{gpxParameter},
excludeParameter: showRouteParam,
excludeParameterValue: "false", // Don't show hidden route tracks
}
allPostsWithTracksRequestConfig.status, allPostsWithTracksRequestConfig.visibility = a.getDefaultPostStates(r)
allPostsWithTracks, err := a.getPosts(allPostsWithTracksRequestConfig)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
type templateTrack struct {
Paths [][]*trackPoint
Points []*trackPoint
Post string
}
var tracks []*templateTrack
for _, p := range allPostsWithTracks {
if t, err := a.getTrack(p, true); err == nil && t != nil {
tracks = append(tracks, &templateTrack{
Paths: t.Paths,
Points: t.Points,
Post: p.Path,
})
}
}
buf := bufferpool.Get()
defer bufferpool.Put(buf)
err = json.NewEncoder(buf).Encode(tracks)
if err != nil {
a.serveError(w, r, "", http.StatusInternalServerError)
return
}
w.Header().Set(contentType, contenttype.JSONUTF8)
_, _ = io.Copy(w, buf)
}
const geoMapLocationsSubpath = "/locations.json"
func (a *goBlog) serveGeoMapLocations(w http.ResponseWriter, r *http.Request) {
blog, _ := a.getBlog(r)
allPostsWithLocationRequestConfig := &postsRequestConfig{
blog: blog,
parameters: []string{a.cfg.Micropub.LocationParam},
withOnlyParameters: []string{a.cfg.Micropub.LocationParam},
}
allPostsWithLocationRequestConfig.status, allPostsWithLocationRequestConfig.visibility = a.getDefaultPostStates(r)
allPostsWithLocations, err := a.getPosts(allPostsWithLocationRequestConfig)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
type templateLocation struct {
Lat float64
Lon float64
Post string
}
var locations []*templateLocation
for _, p := range allPostsWithLocations {
for _, g := range a.geoURIs(p) {
locations = append(locations, &templateLocation{
Lat: g.Latitude,
Lon: g.Longitude,
Post: p.Path,
})
}
}
buf := bufferpool.Get()
defer bufferpool.Put(buf)
err = json.NewEncoder(buf).Encode(locations)
if err != nil {
a.serveError(w, r, "", http.StatusInternalServerError)
return
}
w.Header().Set(contentType, contenttype.JSONUTF8)
_, _ = io.Copy(w, buf)
}