package handler import ( "time" "git.coopgo.io/coopgo-platform/carpool-service/geoutils" "git.coopgo.io/coopgo-platform/carpool-service/internal" "git.coopgo.io/coopgo-platform/routing-service/encoding/polylines" "github.com/paulmach/orb" "github.com/rs/zerolog/log" ) func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival orb.Point, departureRadius *float64, arrivalRadius *float64, minDate time.Time, maxDate time.Time, count *int64) ([]internal.SearchResult, error) { log.Debug(). Any("departure", departure). Any("arrival", arrival). Any("dep radius", departureRadius). Any("arr radius", arrivalRadius). Str("mindate", minDate.Format(time.RFC3339)). Str("maxdate", maxDate.Format(time.RFC3339)). Any("count", count). Msg("Carpool service handler - GetDriverJourneys") if count == nil { c := int64(10) count = &c } drad := float64(1000) if departureRadius != nil { drad = *departureRadius * 1000 } arad := float64(1000) if arrivalRadius != nil { arad = *arrivalRadius * 1000 } tileset, err := h.Tiles.GetTiles("driver", minDate, departure, arrival) if err != nil { log.Error(). Str("date", minDate.Format(time.RFC3339)). Any("departure", departure). Any("arrival", arrival). Err(err). Msg("could not retrieve tiles") return nil, err } candidate_routes := tileset.GetTiledRoutes() journeys := []internal.SearchResult{} counted := int64(0) for _, r := range candidate_routes { ls := r.Route.Features[2].Geometry.(orb.LineString) // distanceFromDeparture, indexDeparture := planar.DistanceFromWithIndex(ls, departure) // distanceFromArrival, indexArrival := planar.DistanceFromWithIndex(ls, arrival) distanceFromDeparture, indexDeparture := geoutils.DistanceFromLineString(departure, ls) distanceFromArrival, indexArrival := geoutils.DistanceFromLineString(arrival, ls) if indexArrival >= indexDeparture && distanceFromDeparture <= drad && distanceFromArrival < arad { //routePoints := []orb.Point{r.Route.Features[0].Point(), departure, arrival, r.Route.Features[1].Point()} routePoints := []orb.Point{departure, arrival} log.Debug().Any("route points", routePoints).Msg("calculate multipoint route") itinerary, err := h.Routing.Route(routePoints) if err != nil { log.Error().Err(err).Msg("error getting route with viapoints") continue } journeys = append(journeys, internal.SearchResult{ ID: r.ID, Route: r.Route, DepartureDate: r.DepartureDate, Itinerary: itinerary, }) counted = counted + 1 if counted == *count { break } } } if len(journeys) > 0 { err = h.Storage.StoreSearchResults(journeys) if err != nil { log.Error().Err(err).Msg("Error saving search results") return nil, err } } return journeys, nil } func (h *CarpoolServiceHandler) GetPassengerJourneys(departure orb.Point, arrival orb.Point, departureRadius *float64, arrivalRadius *float64, minDate time.Time, maxDate time.Time, count *int64) ([]internal.SearchResult, error) { log.Debug(). Any("departure", departure). Any("arrival", arrival). Any("dep radius", departureRadius). Any("arr radius", arrivalRadius). Str("mindate", minDate.Format(time.RFC3339)). Str("maxdate", maxDate.Format(time.RFC3339)). Any("count", count). Msg("Carpool service handler - GetDriverJourneys") routePoints := []orb.Point{departure, arrival} itinerary, err := h.Routing.Route(routePoints) if err != nil { log.Error().Err(err).Msg("error getting route with viapoints") return nil, err } if count == nil { c := int64(10) count = &c } drad := float64(1000) if departureRadius != nil { drad = *departureRadius * 1000 } arad := float64(1000) if arrivalRadius != nil { arad = *arrivalRadius * 1000 } decodedPolyline := polylines.Decode(&itinerary.Summary.Polyline, 5) points := []orb.Point{departure, arrival} for _, p := range decodedPolyline { points = append(points, p) } tileset, err := h.Tiles.GetTiles("passenger", minDate, points...) if err != nil { log.Error(). Str("date", minDate.Format(time.RFC3339)). Any("departure", departure). Any("arrival", arrival). Err(err). Msg("could not retrieve tiles") return nil, err } candidate_routes := tileset.GetTiledRoutes() journeys := []internal.SearchResult{} counted := int64(0) for _, r := range candidate_routes { ls := decodedPolyline distanceFromDeparture, indexDeparture := geoutils.DistanceFromLineString(r.Route.Features[0].Point(), ls) distanceFromArrival, indexArrival := geoutils.DistanceFromLineString(r.Route.Features[1].Point(), ls) if indexArrival >= indexDeparture && distanceFromDeparture <= drad && distanceFromArrival < arad { routePoints := []orb.Point{r.Route.Features[0].Point(), departure, arrival, r.Route.Features[0].Point()} itinerary, err := h.Routing.Route(routePoints) if err != nil { log.Error().Err(err).Msg("error getting route with viapoints") continue } journeys = append(journeys, internal.SearchResult{ ID: r.ID, Route: r.Route, DepartureDate: r.DepartureDate, Itinerary: itinerary, }) counted = counted + 1 if counted == *count { break } } } if len(journeys) > 0 { err = h.Storage.StoreSearchResults(journeys) if err != nil { log.Error().Err(err).Msg("Error saving search results") return nil, err } } return journeys, nil }