From 3caaf59506e1fee611f72102480a2bc4266a3e1a Mon Sep 17 00:00:00 2001 From: Ray Miller Date: Mon, 20 Apr 2020 23:09:27 +0100 Subject: [PATCH] Add missing file. --- pkg/cafes/cyclingmaps.go | 70 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 pkg/cafes/cyclingmaps.go diff --git a/pkg/cafes/cyclingmaps.go b/pkg/cafes/cyclingmaps.go new file mode 100644 index 0000000..c1dc0ed --- /dev/null +++ b/pkg/cafes/cyclingmaps.go @@ -0,0 +1,70 @@ +package cafes + +import ( + "encoding/json" + "fmt" + "io" + "io/ioutil" + "log" + "net/http" + + "github.com/dhconnelly/rtreego" + "github.com/fofanov/go-osgb" +) + +const cyclingMapsCafesUrl = "https://cafes.cyclingmaps.net/data/cafes.json" + +type CyclingMapsCafe struct { + Name string + Website string + Lat float64 + Lng float64 +} + +func BuildCyclingMapsIndex(r io.Reader) (*rtreego.Rtree, error) { + var cafes []CyclingMapsCafe + data, err := ioutil.ReadAll(r) + if err != nil { + return nil, err + } + err = json.Unmarshal(data, &cafes) + if err != nil { + return nil, err + } + trans, err := osgb.NewOSTN15Transformer() + if err != nil { + return nil, err + } + stops := make([]rtreego.Spatial, 0, len(cafes)) + for _, c := range cafes { + gpsCoord := osgb.NewETRS89Coord(c.Lng, c.Lat, 0) + ngCoord, err := trans.ToNationalGrid(gpsCoord) + if err != nil { + log.Printf("Error translating coordinates %v: %v", gpsCoord, err) + continue + } + stops = append(stops, &RefreshmentStop{ + Name: c.Name, + Url: c.Website, + Easting: ngCoord.Easting, + Northing: ngCoord.Northing, + }) + } + return rtreego.NewTree(2, 25, 50, stops...), nil +} + +func FetchCyclingMapsIndex() (*rtreego.Rtree, error) { + res, err := http.Get(cyclingMapsCafesUrl) + if err != nil { + return nil, fmt.Errorf("error getting %s: %v", cyclingMapsCafesUrl, err) + } + defer res.Body.Close() + if res.StatusCode != http.StatusOK { + return nil, fmt.Errorf("unexpected status fetching %s: %s", cyclingMapsCafesUrl, res.Status) + } + index, err := BuildCyclingMapsIndex(res.Body) + if err != nil { + return nil, fmt.Errorf("error building cyclingmaps.net cafe stops index: %v", err) + } + return index, nil +}