add h3 cells to trip model as preparation for route pattern detection
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<String> 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<String> getRoutePatternNeighbors(String cellId) {
|
||||
return getKRingNeighbors(cellId, config.getRoutePatternNeighborRings());
|
||||
}
|
||||
}
|
||||
@@ -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<TripDTO> toTripDTOs(List<Trip> trips);
|
||||
public abstract List<TripDTO> toTripDTOs(List<Trip> 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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user