func SearchNearestStations() {

	if len(os.Args) < 3 {
		fmt.Println("Try nearest <lat>,<lon>")
		return
	}

	latlon := strings.Split(os.Args[2], ",")
	if len(latlon) != 2 {
		fmt.Println("Try nearest <lat>,<lon>")
		return
	}
	coords := [2]float64{}
	for i, c := range latlon {
		coords[i], _ = strconv.ParseFloat(c, 64)
	}

	x, y := openapi.GeodeticToGrid(coords[0], coords[1])

	api := openapi.NewOpenAPI()

	result, err := api.NearestStation(x, y, 1000)
	if err != nil {
		fmt.Println(err)
		return
	}

	PrintNearestStopAreas(result.NearestStopAreas)
}
Esempio n. 2
0
func NearestStationsHandler(w http.ResponseWriter, req *http.Request) {

	var lat, lon float64
	var err error

	path := req.URL.Path
	segments := strings.Split(path, "/")
	lastSegment := segments[len(segments)-1]
	latlon := strings.Split(lastSegment, ",")

	if len(latlon) != 2 {
		http.Error(w, "Coordinates should be <lat>,<lon>", 400)
		return
	}

	if lat, err = strconv.ParseFloat(latlon[0], 64); err != nil {
		http.Error(w, "Illegal coordinate", 400)
		return
	}
	if lon, err = strconv.ParseFloat(latlon[1], 64); err != nil {
		http.Error(w, "Illegal coordinate", 400)
		return
	}

	api := GetApiClient(req)

	x, y := openapi.GeodeticToGrid(lat, lon)

	result, err := api.NearestStation(x, y, 500)
	if err != nil {
		http.Error(w, "Could not find stations nearby", 500)
		return
	}

	WriteGeoJSONHeaders(w)
	result.WriteJSON(w)
}