From 0ea54f87a641d3aec1deb00a6cd178ef1bcae117 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sat, 13 Nov 2021 20:19:46 +0100 Subject: [PATCH] Show tracks on map as well --- geoMap.go | 27 +++++++++++++++++++++++---- geoTrack.go | 9 ++++----- postsDb.go | 15 ++++++++++++++- postsDb_test.go | 5 +++++ templates/assets/js/geomap.js | 21 +++++++++++++++------ templates/assets/js/geotrack.js | 12 ++++-------- templates/geomap.gohtml | 2 +- 7 files changed, 66 insertions(+), 25 deletions(-) diff --git a/geoMap.go b/geoMap.go index cb121d9..6a9edda 100644 --- a/geoMap.go +++ b/geoMap.go @@ -14,8 +14,8 @@ func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) { allPostsWithLocation, err := a.getPosts(&postsRequestConfig{ blog: blog, status: statusPublished, - parameter: a.cfg.Micropub.LocationParam, - withOnlyParameters: []string{a.cfg.Micropub.LocationParam}, + parameters: []string{a.cfg.Micropub.LocationParam, gpxParameter}, + withOnlyParameters: []string{a.cfg.Micropub.LocationParam, gpxParameter}, }) if err != nil { a.serveError(w, r, err.Error(), http.StatusInternalServerError) @@ -38,7 +38,13 @@ func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) { Post string } + type templateTrack struct { + Paths [][]*trackPoint + Post string + } + var locations []*templateLocation + var tracks []*templateTrack for _, p := range allPostsWithLocation { if g := a.geoURI(p); g != nil { locations = append(locations, &templateLocation{ @@ -47,9 +53,21 @@ func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) { Post: p.Path, }) } + if t, err := a.getTrack(p); err == nil && t != nil { + tracks = append(tracks, &templateTrack{ + Paths: t.Paths, + Post: p.Path, + }) + } } - jb, err := json.Marshal(locations) + locationsJsonBytes, err := json.Marshal(locations) + if err != nil { + a.serveError(w, r, err.Error(), http.StatusInternalServerError) + return + } + + tracksJsonBytes, err := json.Marshal(tracks) if err != nil { a.serveError(w, r, err.Error(), http.StatusInternalServerError) return @@ -60,7 +78,8 @@ func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) { BlogString: blog, Canonical: a.getFullAddress(mapPath), Data: map[string]interface{}{ - "locations": string(jb), + "locations": string(locationsJsonBytes), + "tracks": string(tracksJsonBytes), }, }) } diff --git a/geoTrack.go b/geoTrack.go index 1ec8278..804e6e4 100644 --- a/geoTrack.go +++ b/geoTrack.go @@ -3,7 +3,6 @@ package main import ( "encoding/json" "errors" - "log" "math" "github.com/tkrajina/gpxgo/gpx" @@ -11,8 +10,10 @@ import ( "golang.org/x/text/message" ) +const gpxParameter = "gpx" + func (p *post) HasTrack() bool { - return p.firstParameter("gpx") != "" + return p.firstParameter(gpxParameter) != "" } type trackResult struct { @@ -25,7 +26,7 @@ type trackResult struct { } func (a *goBlog) getTrack(p *post) (result *trackResult, err error) { - gpxString := p.firstParameter("gpx") + gpxString := p.firstParameter(gpxParameter) if gpxString == "" { return nil, errors.New("no gpx parameter in post") } @@ -58,8 +59,6 @@ func (a *goBlog) getTrack(p *post) (result *trackResult, err error) { ), } - log.Println(string(pathsJSON)) - return result, nil } diff --git a/postsDb.go b/postsDb.go index 1f882b6..523112a 100644 --- a/postsDb.go +++ b/postsDb.go @@ -272,7 +272,8 @@ type postsRequestConfig struct { status postStatus taxonomy *configTaxonomy taxonomyValue string - parameter string + parameters []string // Ignores parameterValue + parameter string // Ignores parameters parameterValue string publishedYear, publishedMonth, publishedDay int randomOrder bool @@ -317,6 +318,18 @@ func buildPostsQuery(c *postsRequestConfig, selection string) (query string, arg queryBuilder.WriteString(" and path in (select path from post_parameters where parameter = @param and length(coalesce(value, '')) > 0)") args = append(args, sql.Named("param", c.parameter)) } + } else if len(c.parameters) > 0 { + queryBuilder.WriteString(" and path in (select path from post_parameters where parameter in (") + for i, param := range c.parameters { + if i > 0 { + queryBuilder.WriteString(", ") + } + named := "param" + strconv.Itoa(i) + queryBuilder.WriteByte('@') + queryBuilder.WriteString(named) + args = append(args, param) + } + queryBuilder.WriteString(") and length(coalesce(value, '')) > 0)") } if c.taxonomy != nil && len(c.taxonomyValue) > 0 { queryBuilder.WriteString(" and path in (select path from post_parameters where parameter = @taxname and lowerx(value) = lowerx(@taxval))") diff --git a/postsDb_test.go b/postsDb_test.go index faf7757..a1d2c90 100644 --- a/postsDb_test.go +++ b/postsDb_test.go @@ -90,6 +90,11 @@ func Test_postsDb(t *testing.T) { must.NoError(err) is.Equal(0, count) + // Check by multiple parameters + count, err = app.db.countPosts(&postsRequestConfig{parameters: []string{"tags", "title"}}) + must.NoError(err) + is.Equal(1, count) + // Delete post _, err = app.deletePostFromDb("/test/abc") must.NoError(err) diff --git a/templates/assets/js/geomap.js b/templates/assets/js/geomap.js index 69b3184..9258f33 100644 --- a/templates/assets/js/geomap.js +++ b/templates/assets/js/geomap.js @@ -1,6 +1,7 @@ (function () { let mapEl = document.getElementById('map') let locations = JSON.parse(mapEl.dataset.locations) + let tracks = JSON.parse(mapEl.dataset.tracks) let map = L.map('map') @@ -8,13 +9,21 @@ attribution: '© OpenStreetMap contributors' }).addTo(map) - let markers = [] + let mapFeatures = [] + locations.forEach(loc => { - let marker = [loc.Lat, loc.Lon] - L.marker(marker).addTo(map).on('click', function () { + mapFeatures.push(L.marker([loc.Lat, loc.Lon]).addTo(map).on('click', function () { window.open(loc.Post, '_blank').focus() - }) - markers.push(marker) + })) }) - map.fitBounds(markers, { padding: [5, 5] }) + + tracks.forEach(track => { + track.Paths.forEach(path => { + mapFeatures.push(L.polyline(path.map(point => [point.Lat, point.Lon]), { color: 'blue' }).addTo(map).on('click', function () { + window.open(track.Post, '_blank').focus() + })) + }) + }) + + map.fitBounds(L.featureGroup(mapFeatures).getBounds(), { padding: [5, 5] }) })() \ No newline at end of file diff --git a/templates/assets/js/geotrack.js b/templates/assets/js/geotrack.js index 4f5363c..33036ad 100644 --- a/templates/assets/js/geotrack.js +++ b/templates/assets/js/geotrack.js @@ -9,14 +9,10 @@ }).addTo(map) let polylines = [] + paths.forEach(path => { - let pathPoints = [] - path.forEach(point => { - pathPoints.push([point.Lat, point.Lon]) - }) - let pl = L.polyline(pathPoints, { color: 'blue' }).addTo(map) - polylines.push(pl) + polylines.push(L.polyline(path.map(point => [point.Lat, point.Lon]), { color: 'blue' }).addTo(map)) }) - let fgb = L.featureGroup(polylines).getBounds() - map.fitBounds(fgb, { padding: [5, 5] }) + + map.fitBounds(L.featureGroup(polylines).getBounds(), { padding: [5, 5] }) })() \ No newline at end of file diff --git a/templates/geomap.gohtml b/templates/geomap.gohtml index b9176bf..3811142 100644 --- a/templates/geomap.gohtml +++ b/templates/geomap.gohtml @@ -11,7 +11,7 @@ {{ if .Data.nolocations }}

{{ string .Blog.Lang "nolocations" }}

{{ else }} -
+
{{ end }}