diff --git a/scrape.go b/scrape.go index 1938915..d0b3453 100644 --- a/scrape.go +++ b/scrape.go @@ -75,7 +75,17 @@ func main() { panic(err) } + sq, err := newStationQuery("data/stations.json") + if err != nil { + panic(err) + } + restaurants, err := scrapeUrls("data/urls.txt", wc, gc) + for i, _ := range restaurants { + r := &restaurants[i] + r.closestStnName, r.closestStnDist = sq.closestStation(r.latitude, r.longitude) + } + if err == nil { log.Print(len(restaurants)) } else { diff --git a/scraper.go b/scraper.go index 3a73727..dcec611 100644 --- a/scraper.go +++ b/scraper.go @@ -39,6 +39,9 @@ type restaurant struct { latitude float64 longitude float64 + + closestStnName string + closestStnDist float64 } type scraper interface { diff --git a/stations.go b/stations.go index fb98874..f4b8ae1 100644 --- a/stations.go +++ b/stations.go @@ -24,7 +24,10 @@ package main import ( "encoding/json" + "math" "os" + + "github.com/kellydunn/golang-geo" ) type station struct { @@ -53,6 +56,18 @@ func newStationQuery(filename string) (*stationQuery, error) { } func (s *stationQuery) closestStation(latitude, longitude float64) (name string, distance float64) { + queryPt := geo.NewPoint(latitude, longitude) - return "", 0 + var closestStn string + minDist := math.MaxFloat64 + + for name, station := range s.stations { + stnPt := geo.NewPoint(station.Latitude, station.Longitude) + if currDist := queryPt.GreatCircleDistance(stnPt); currDist < minDist { + closestStn = name + minDist = currDist + } + } + + return closestStn, minDist }