|
| 1 | +package com.artrun.server.service; |
| 2 | + |
| 3 | +import com.artrun.server.common.BusinessException; |
| 4 | +import com.artrun.server.common.ErrorCode; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.locationtech.jts.geom.Coordinate; |
| 7 | +import org.locationtech.jts.geom.GeometryFactory; |
| 8 | +import org.locationtech.jts.geom.LineString; |
| 9 | +import org.locationtech.jts.geom.PrecisionModel; |
| 10 | +import org.springframework.beans.factory.annotation.Value; |
| 11 | +import org.springframework.stereotype.Service; |
| 12 | +import org.springframework.web.client.RestClient; |
| 13 | +import tools.jackson.databind.JsonNode; |
| 14 | +import tools.jackson.databind.ObjectMapper; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +@Slf4j |
| 20 | +@Service |
| 21 | +public class MapboxMapMatchingService { |
| 22 | + |
| 23 | + private static final GeometryFactory GF = new GeometryFactory(new PrecisionModel(), 4326); |
| 24 | + private static final int MAX_COORDINATES_PER_REQUEST = 100; |
| 25 | + |
| 26 | + private final String apiKey; |
| 27 | + private final ObjectMapper objectMapper; |
| 28 | + private final RestClient restClient; |
| 29 | + |
| 30 | + public MapboxMapMatchingService( |
| 31 | + @Value("${mapbox.api-key:}") String apiKey, |
| 32 | + ObjectMapper objectMapper) { |
| 33 | + this.apiKey = apiKey; |
| 34 | + this.objectMapper = objectMapper; |
| 35 | + this.restClient = RestClient.create(); |
| 36 | + } |
| 37 | + |
| 38 | + public boolean isAvailable() { |
| 39 | + return apiKey != null && !apiKey.isBlank(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * 좌표열을 Mapbox Map Matching API로 도로에 매칭하여 경로를 반환한다. |
| 44 | + * 입력: 도형 윤곽을 따르는 촘촘한 좌표열 (lat/lng) |
| 45 | + * 출력: 도로를 따라가는 LineString |
| 46 | + */ |
| 47 | + public LineString matchToRoads(Coordinate[] coordinates) { |
| 48 | + log.info("Mapbox Map Matching: {} coordinates", coordinates.length); |
| 49 | + |
| 50 | + List<Coordinate> allMatched = new ArrayList<>(); |
| 51 | + |
| 52 | + // Mapbox는 한 번에 최대 100좌표 → 청크로 분할 |
| 53 | + for (int start = 0; start < coordinates.length; start += MAX_COORDINATES_PER_REQUEST - 1) { |
| 54 | + int end = Math.min(start + MAX_COORDINATES_PER_REQUEST, coordinates.length); |
| 55 | + Coordinate[] chunk = new Coordinate[end - start]; |
| 56 | + System.arraycopy(coordinates, start, chunk, 0, end - start); |
| 57 | + |
| 58 | + List<Coordinate> matched = callMapMatchingApi(chunk); |
| 59 | + |
| 60 | + if (!allMatched.isEmpty() && !matched.isEmpty()) { |
| 61 | + matched = matched.subList(1, matched.size()); |
| 62 | + } |
| 63 | + allMatched.addAll(matched); |
| 64 | + } |
| 65 | + |
| 66 | + if (allMatched.size() < 2) { |
| 67 | + throw new BusinessException(ErrorCode.ROUTING_FAILED, "Mapbox Map Matching 실패: 매칭된 좌표가 부족합니다."); |
| 68 | + } |
| 69 | + |
| 70 | + log.info("Mapbox matched: {} -> {} coordinates", coordinates.length, allMatched.size()); |
| 71 | + return GF.createLineString(allMatched.toArray(new Coordinate[0])); |
| 72 | + } |
| 73 | + |
| 74 | + private List<Coordinate> callMapMatchingApi(Coordinate[] coordinates) { |
| 75 | + // coordinates는 JTS 형식 (x=lng, y=lat) |
| 76 | + StringBuilder coordStr = new StringBuilder(); |
| 77 | + StringBuilder radiuses = new StringBuilder(); |
| 78 | + for (int i = 0; i < coordinates.length; i++) { |
| 79 | + if (i > 0) { |
| 80 | + coordStr.append(";"); |
| 81 | + radiuses.append(";"); |
| 82 | + } |
| 83 | + coordStr.append(coordinates[i].x).append(",").append(coordinates[i].y); |
| 84 | + radiuses.append("25"); // 25m 반경 내 도로 매칭 |
| 85 | + } |
| 86 | + |
| 87 | + String url = "https://api.mapbox.com/matching/v5/mapbox/walking/%s?access_token=%s&geometries=geojson&radiuses=%s&overview=full" |
| 88 | + .formatted(coordStr, apiKey, radiuses); |
| 89 | + |
| 90 | + try { |
| 91 | + String response = restClient.get() |
| 92 | + .uri(url) |
| 93 | + .retrieve() |
| 94 | + .body(String.class); |
| 95 | + |
| 96 | + JsonNode root = objectMapper.readTree(response); |
| 97 | + String code = root.path("code").asText(); |
| 98 | + |
| 99 | + if (!"Ok".equals(code)) { |
| 100 | + log.warn("Mapbox Map Matching returned: {}", code); |
| 101 | + return List.of(); |
| 102 | + } |
| 103 | + |
| 104 | + JsonNode matchings = root.path("matchings"); |
| 105 | + if (matchings.isEmpty()) { |
| 106 | + return List.of(); |
| 107 | + } |
| 108 | + |
| 109 | + // 첫 번째 매칭 결과의 geometry 좌표 추출 |
| 110 | + JsonNode coords = matchings.path(0).path("geometry").path("coordinates"); |
| 111 | + List<Coordinate> result = new ArrayList<>(); |
| 112 | + for (JsonNode point : coords) { |
| 113 | + double lng = point.path(0).asDouble(); |
| 114 | + double lat = point.path(1).asDouble(); |
| 115 | + result.add(new Coordinate(lng, lat)); |
| 116 | + } |
| 117 | + |
| 118 | + return result; |
| 119 | + } catch (Exception e) { |
| 120 | + log.error("Mapbox API call failed: {}", e.getMessage()); |
| 121 | + return List.of(); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments