Annotate refreshment stops, improved logging and error handling.

This commit is contained in:
Ray Miller 2020-04-20 23:08:57 +01:00
parent 7d3b208e5c
commit 52d41490f3
5 changed files with 195 additions and 25 deletions

93
pkg/cafes/ctccam.go Normal file
View file

@ -0,0 +1,93 @@
package cafes
import (
"encoding/xml"
"fmt"
"io"
"net/http"
"github.com/dhconnelly/rtreego"
"github.com/fofanov/go-osgb"
)
const ctcCamWaypointsUrl = "https://ctccambridge.org.uk/ctccambridge-waypoints.gpx"
type Waypoint struct {
Lat float64 `xml:"lat,attr"`
Lon float64 `xml:"lon,attr"`
Name string `xml:"name"`
Url string `xml:"url"`
}
type Waypoints struct {
Waypoints []Waypoint `xml:"wpt"`
}
type RefreshmentStop struct {
Name string
Url string
Easting float64
Northing float64
}
func (s *RefreshmentStop) Bounds() *rtreego.Rect {
p := rtreego.Point{s.Easting, s.Northing}
return p.ToRect(100)
}
func (s *RefreshmentStop) Contains(p rtreego.Point) bool {
if len(p) != 2 {
panic("Expected a 2-dimensional point")
}
bounds := s.Bounds()
for i := 0; i < 2; i++ {
if p[i] < bounds.PointCoord(i) || p[i] > bounds.PointCoord(i)+bounds.LengthsCoord(i) {
return false
}
}
return true
}
func BuildCtcCamIndex(r io.Reader) (*rtreego.Rtree, error) {
dec := xml.NewDecoder(r)
var wpt Waypoints
err := dec.Decode(&wpt)
if err != nil {
return nil, err
}
trans, err := osgb.NewOSTN15Transformer()
if err != nil {
return nil, err
}
stops := make([]rtreego.Spatial, len(wpt.Waypoints))
for i, w := range wpt.Waypoints {
gpsCoord := osgb.NewETRS89Coord(w.Lon, w.Lat, 0)
ngCoord, err := trans.ToNationalGrid(gpsCoord)
if err != nil {
return nil, fmt.Errorf("Error translating coordinates %v: %v", gpsCoord, err)
}
stops[i] = &RefreshmentStop{
Name: w.Name,
Url: w.Url,
Easting: ngCoord.Easting,
Northing: ngCoord.Northing,
}
}
return rtreego.NewTree(2, 25, 50, stops...), nil
}
func FetchCtcCamIndex() (*rtreego.Rtree, error) {
res, err := http.Get(ctcCamWaypointsUrl)
if err != nil {
return nil, fmt.Errorf("error getting %s: %v", ctcCamWaypointsUrl, err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status fetching %s: %s", ctcCamWaypointsUrl, res.Status)
}
index, err := BuildCtcCamIndex(res.Body)
if err != nil {
return nil, fmt.Errorf("error building CTC Cambridge stops index: %v", err)
}
return index, nil
}

View file

@ -9,10 +9,12 @@ import (
"github.com/dhconnelly/rtreego"
"github.com/fofanov/go-osgb"
"github.com/twpayne/go-gpx"
"github.com/ray1729/gpx-utils/pkg/cafes"
)
type GPXSummarizer struct {
rt *rtreego.Rtree
poi *rtreego.Rtree
trans osgb.CoordinateTransformer
}
@ -25,7 +27,7 @@ func NewGPXSummarizer() (*GPXSummarizer, error) {
if err != nil {
return nil, err
}
return &GPXSummarizer{rt, trans}, nil
return &GPXSummarizer{poi: rt, trans: trans}, nil
}
func distance(p1, p2 rtreego.Point) float64 {
@ -46,6 +48,12 @@ type POI struct {
Distance float64
}
type RefreshmentStop struct {
Name string
Url string
Distance float64
}
type TrackSummary struct {
Name string
Time time.Time
@ -55,9 +63,10 @@ type TrackSummary struct {
Distance float64
Ascent float64
PointsOfInterest []POI
RefreshmentStops []RefreshmentStop `json:",omitempty"`
}
func (gs *GPXSummarizer) SummarizeTrack(r io.Reader) (*TrackSummary, error) {
func (gs *GPXSummarizer) SummarizeTrack(r io.Reader, stops *rtreego.Rtree) (*TrackSummary, error) {
g, err := gpx.Read(r)
if err != nil {
return nil, err
@ -76,6 +85,7 @@ func (gs *GPXSummarizer) SummarizeTrack(r io.Reader) (*TrackSummary, error) {
var prevPlacePoint rtreego.Point
var prevPoint rtreego.Point
var prevHeight float64
var prevStop *cafes.RefreshmentStop
init := true
for _, trk := range g.Trk {
@ -88,7 +98,7 @@ func (gs *GPXSummarizer) SummarizeTrack(r io.Reader) (*TrackSummary, error) {
}
thisPoint := rtreego.Point{ngCoord.Easting, ngCoord.Northing}
thisHeight := ngCoord.Height
nn, _ := gs.rt.NearestNeighbor(thisPoint).(*NamedBoundary)
nn, _ := gs.poi.NearestNeighbor(thisPoint).(*NamedBoundary)
if init {
s.Start = nn.Name
prevPlace = nn.Name
@ -108,6 +118,17 @@ func (gs *GPXSummarizer) SummarizeTrack(r io.Reader) (*TrackSummary, error) {
prevPlace = nn.Name
prevPlacePoint = thisPoint
}
if stops != nil {
stop, ok := stops.NearestNeighbor(thisPoint).(*cafes.RefreshmentStop)
if ok && stop.Contains(thisPoint) && (prevStop == nil || stop.Name != prevStop.Name) {
s.RefreshmentStops = append(s.RefreshmentStops, RefreshmentStop{
Name: stop.Name,
Url: stop.Url,
Distance: s.Distance,
})
prevStop = stop
}
}
prevPoint = thisPoint
prevHeight = thisHeight
}

View file

@ -1,7 +1,6 @@
package rwgps
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
@ -15,6 +14,14 @@ func (e *ErrNotFound) Error() string {
return fmt.Sprintf("RideWithGPS track %d not found", e.RouteId)
}
type ErrNotPublic struct {
RouteId int
}
func (e *ErrNotPublic) Error() string {
return fmt.Sprintf("RideWithGPS track %d is not public", e.RouteId)
}
func FetchTrack(routeId int) ([]byte, error) {
url := fmt.Sprintf("https://ridewithgps.com/routes/%d.gpx?sub_format=track", routeId)
resp, err := http.Get(url)
@ -22,16 +29,18 @@ func FetchTrack(routeId int) ([]byte, error) {
return nil, fmt.Errorf("error getting %s: %v", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusNotFound {
return nil, &ErrNotFound{routeId}
}
if resp.StatusCode == http.StatusForbidden {
return nil, &ErrNotPublic{routeId}
}
return nil, fmt.Errorf("error retrieving route %d: %s", routeId, resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response from %s: %v", url, err)
}
if IsNotFound(data) {
return nil, &ErrNotFound{routeId}
}
return data, nil
}
func IsNotFound(data []byte) bool {
return bytes.HasPrefix(data, []byte("<!DOCTYPE html>")) && bytes.Contains(data, []byte("Error (404 not found)"))
}