diff --git a/pom.xml b/pom.xml index 1a47532..97d5e66 100644 --- a/pom.xml +++ b/pom.xml @@ -112,7 +112,6 @@ logging-interceptor 4.12.0 - org.mapstruct mapstruct @@ -141,6 +140,11 @@ firebase-admin 9.7.0 + + com.uber + h3 + 4.4.0 + diff --git a/src/main/java/SuMoC/backend/h3/H3Configuration.java b/src/main/java/SuMoC/backend/h3/H3Configuration.java new file mode 100644 index 0000000..ba48947 --- /dev/null +++ b/src/main/java/SuMoC/backend/h3/H3Configuration.java @@ -0,0 +1,31 @@ +package SuMoC.backend.h3; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "h3") +@Getter +@Setter +public class H3Configuration { + + /** + * H3 resolution for route pattern detection. + * Typically slightly lower resolution for broader matching. + */ + private int routePatternResolution = 10; + + /** + * Number of hexagonal rings to include when matching route patterns. + * K-ring depth where k=1 includes immediate neighbors (6 hexagons), + * k=2 includes two rings (18 hexagons), etc. + * Higher values allow fuzzier location matching but may incorrectly + * group distinct locations. + * Consider resolution when setting. + * Default: 1 (immediate neighbors only) + */ + private int routePatternNeighborRings = 1; +} + diff --git a/src/main/java/SuMoC/backend/h3/H3Service.java b/src/main/java/SuMoC/backend/h3/H3Service.java new file mode 100644 index 0000000..421ac88 --- /dev/null +++ b/src/main/java/SuMoC/backend/h3/H3Service.java @@ -0,0 +1,85 @@ +package SuMoC.backend.h3; + +import com.uber.h3core.H3Core; +import com.uber.h3core.util.LatLng; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.util.List; + +/** + * Service for H3 spatial indexing operations. + * H3 is a hierarchical hexagonal geospatial indexing system. + */ +@Service +public class H3Service { + + private final H3Core h3; + private final H3Configuration config; + + public H3Service(H3Configuration config) { + this.config = config; + try { + this.h3 = H3Core.newInstance(); + } catch (IOException e) { + throw new RuntimeException("Failed to initialize H3Core", e); + } + } + + /** + * Convert latitude/longitude to H3 cell ID with specific resolution. + * + * @param latitude Latitude in degrees + * @param longitude Longitude in degrees + * @param resolution H3 resolution (0-15, higher = more precise) + * @return H3 cell ID as hex string + */ + public String latLngToCell(double latitude, double longitude, int resolution) { + return h3.latLngToCellAddress(latitude, longitude, resolution); + } + + /** + * Convert latitude/longitude to H3 cell ID using route pattern resolution. + * This is typically used for detecting regular trip patterns. + * + * @param latitude Latitude in degrees + * @param longitude Longitude in degrees + * @return H3 cell ID as hex string + */ + public String latLngToCellForRoute(double latitude, double longitude) { + return latLngToCell(latitude, longitude, config.getRoutePatternResolution()); + } + + /** + * Convert H3 cell ID to center latitude/longitude. + * + * @param cellId H3 cell ID as hex string + * @return LatLng of cell center + */ + public LatLng cellToLatLng(String cellId) { + return h3.cellToLatLng(cellId); + } + + /** + * Get all cells within k distance of the given cell (including the cell itself). + * K-ring distance means cells reachable in k steps on the hexagonal grid. + * + * @param cellId H3 cell ID as hex string + * @param k Distance (number of rings, 0 = only the cell itself) + * @return List of H3 cell IDs including the origin cell + */ + public List getKRingNeighbors(String cellId, int k) { + return h3.gridDisk(cellId, k); + } + + /** + * Get K-ring neighbors using the configured route pattern resolution distance. + * Useful for finding nearby locations when matching trip patterns. + * + * @param cellId H3 cell ID as hex string + * @return List of H3 cell IDs within configured neighbor distance + */ + public List getRoutePatternNeighbors(String cellId) { + return getKRingNeighbors(cellId, config.getRoutePatternNeighborRings()); + } +} diff --git a/src/main/java/SuMoC/backend/trip/TripMapper.java b/src/main/java/SuMoC/backend/trip/TripMapper.java index 4a6f9a6..6f223aa 100644 --- a/src/main/java/SuMoC/backend/trip/TripMapper.java +++ b/src/main/java/SuMoC/backend/trip/TripMapper.java @@ -1,21 +1,37 @@ package SuMoC.backend.trip; +import SuMoC.backend.h3.H3Service; import SuMoC.backend.trip.dtos.NewTripDTO; 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.List; -@Mapper(componentModel = "spring", uses = TripSegmentMapper.class) -public interface TripMapper { +@Mapper(componentModel = "spring", uses = {TripSegmentMapper.class, H3Service.class}) +public abstract class TripMapper { + + @Autowired + protected H3Service h3service; + @Mapping(source = "id", target = "tripId") @Mapping(source = "user.id", target = "userId") - TripDTO toTripDTO(Trip trip); + public abstract TripDTO toTripDTO(Trip trip); - List toTripDTOs(List trips); + public abstract List toTripDTOs(List trips); @Mapping(source = "tripSegments", target = "tripSegments") - Trip toTrip(NewTripDTO newTripDTO); + @Mapping(target = "startH3Cell", expression = "java(calculateH3CellStart(newTripDTO))") + @Mapping(target = "endH3Cell", expression = "java(calculateH3CellEnd(newTripDTO))") + public abstract Trip toTrip(NewTripDTO newTripDTO); + + protected String calculateH3CellStart(NewTripDTO newTripDTO) { + return h3service.latLngToCellForRoute(newTripDTO.getStartLatitude(), newTripDTO.getStartLongitude()); + } + + protected String calculateH3CellEnd(NewTripDTO newTripDTO) { + return h3service.latLngToCellForRoute(newTripDTO.getEndLatitude(), newTripDTO.getEndLongitude()); + } } diff --git a/src/main/java/SuMoC/backend/trip/models/Trip.java b/src/main/java/SuMoC/backend/trip/models/Trip.java index a6b0cda..2542de9 100644 --- a/src/main/java/SuMoC/backend/trip/models/Trip.java +++ b/src/main/java/SuMoC/backend/trip/models/Trip.java @@ -36,6 +36,9 @@ public class Trip { @Column(nullable = false) private double startLongitude; + @Column(nullable = false, name = "start_h3_cell", length = 15) + private String startH3Cell; + @Column(nullable = false) private String endPlace; @@ -45,6 +48,9 @@ public class Trip { @Column(nullable = false) private double endLongitude; + @Column(nullable = false, name = "end_h3_cell", length = 15) + private String endH3Cell; + @Column(nullable = false) private Instant startTime; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 60dc396..557b6dc 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -11,7 +11,7 @@ springdoc.api-docs.path=/v3/api-docs springdoc.swagger-ui.path=/swagger-ui.html spring.profiles.active=dev spring.jackson.serialization.write-dates-as-timestamps=false -# Disable for development +# Disabled for development - TODO set reasonable rhythm sumoc.scheduling.calculate-trend-table-cron=- # Trend detection settings trenddetection.negative-threshold=-25.0