first implementation of iterative greedy algorithm to detect regular trips
This commit is contained in:
@@ -2,13 +2,19 @@ package SuMoC.backend.trip;
|
||||
|
||||
import SuMoC.backend.h3.H3Service;
|
||||
import SuMoC.backend.trip.dtos.NewTripDTO;
|
||||
import SuMoC.backend.trip.dtos.NewTripSegmentDTO;
|
||||
import SuMoC.backend.trip.dtos.RegularTripDetectionDTO;
|
||||
import SuMoC.backend.trip.dtos.TripDTO;
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Mapper(componentModel = "spring", uses = {TripSegmentMapper.class, H3Service.class})
|
||||
public abstract class TripMapper {
|
||||
@@ -25,8 +31,17 @@ public abstract class TripMapper {
|
||||
@Mapping(source = "tripSegments", target = "tripSegments")
|
||||
@Mapping(target = "startH3Cell", expression = "java(calculateH3CellStart(newTripDTO))")
|
||||
@Mapping(target = "endH3Cell", expression = "java(calculateH3CellEnd(newTripDTO))")
|
||||
@Mapping(target = "mainModality", expression = "java(mainModalityByDistance(newTripDTO))")
|
||||
public abstract Trip toTrip(NewTripDTO newTripDTO);
|
||||
|
||||
@Mapping(source = "id", target = "tripId")
|
||||
@Mapping(source = "user.id", target = "userId")
|
||||
@Mapping(target = "startH3CellKRingNeighboors", expression = "java(calculateStartH3KRingNeighboords(trip))")
|
||||
@Mapping(target = "endH3CellKRingNeighboors", expression = "java(calculateEndtH3KRingNeighboords(trip))")
|
||||
public abstract RegularTripDetectionDTO toRegularTripDetectionDTO(Trip trip);
|
||||
|
||||
public abstract List<RegularTripDetectionDTO> toRegularTripDetectionDTOS(List<Trip> trips);
|
||||
|
||||
protected String calculateH3CellStart(NewTripDTO newTripDTO) {
|
||||
return h3service.latLngToCellForRoute(newTripDTO.getStartLatitude(), newTripDTO.getStartLongitude());
|
||||
}
|
||||
@@ -34,4 +49,33 @@ public abstract class TripMapper {
|
||||
protected String calculateH3CellEnd(NewTripDTO newTripDTO) {
|
||||
return h3service.latLngToCellForRoute(newTripDTO.getEndLatitude(), newTripDTO.getEndLongitude());
|
||||
}
|
||||
|
||||
protected List<String> calculateStartH3KRingNeighboords(Trip trip) {
|
||||
return h3service.getRoutePatternNeighbors(trip.getStartH3Cell());
|
||||
}
|
||||
|
||||
protected List<String> calculateEndtH3KRingNeighboords(Trip trip) {
|
||||
return h3service.getRoutePatternNeighbors(trip.getEndH3Cell());
|
||||
}
|
||||
|
||||
protected Modality mainModalityByDistance(NewTripDTO newTripDTO) {
|
||||
HashMap<Modality, Float> map = Arrays.stream(Modality.values())
|
||||
.collect(Collectors.toMap(
|
||||
modality -> modality,
|
||||
modality -> 0f,
|
||||
(a, b) -> a,
|
||||
HashMap::new
|
||||
));
|
||||
|
||||
for (NewTripSegmentDTO segmentDTO : newTripDTO.getTripSegments()) {
|
||||
Modality modality = segmentDTO.getModality();
|
||||
map.put(modality, map.get(modality) + segmentDTO.getDistanceMeters());
|
||||
}
|
||||
|
||||
return map.entrySet().stream()
|
||||
.max(Map.Entry.comparingByValue())
|
||||
.map(Map.Entry::getKey)
|
||||
.orElse(Modality.IDLE);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user