-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat] 코스 polyline 경사 등급 segments 응답 추가 #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/com/semosan/api/domain/mountain/dto/response/SlopeSegmentResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.semosan.api.domain.mountain.dto.response; | ||
|
|
||
| import com.semosan.api.domain.mountain.enums.SlopeGrade; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
|
|
||
| /** | ||
| * 코스 polyline 의 연속된 같은 등급 구간. | ||
| * - startIdx / endIdx: polyline.coordinates 의 점 인덱스. 둘 다 inclusive. | ||
| * - grade: 해당 구간의 경사 등급. | ||
| */ | ||
| @Schema(description = "코스 polyline 의 연속된 같은 경사 등급 구간. 클라이언트는 startIdx~endIdx 범위의 좌표를 grade 별 색으로 그린다.") | ||
| public record SlopeSegmentResponse( | ||
| @Schema(description = "polyline.coordinates 의 시작 인덱스 (inclusive)", example = "0") | ||
| int startIdx, | ||
|
|
||
| @Schema(description = "polyline.coordinates 의 끝 인덱스 (inclusive). 다음 segment 의 startIdx 와 같아 끊김 없이 이어진다.", | ||
| example = "12") | ||
| int endIdx, | ||
|
|
||
| @Schema(description = "경사 등급. STEEP_DOWN(심한 내리막) / MILD_DOWN(약한 내리막) / FLAT(평지) / MILD_UP(약한 오르막) / STEEP_UP(심한 오르막).") | ||
| SlopeGrade grade | ||
| ) { | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/semosan/api/domain/mountain/enums/SlopeGrade.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.semosan.api.domain.mountain.enums; | ||
|
|
||
| /** | ||
| * 코스 polyline 의 segment 별 경사 등급. | ||
| * - 임계: ±5%, ±15% (gradePercent = Δaltitude / Δhorizontalmeters × 100) | ||
| * - 클라이언트는 등급별 색상으로 polyline sub-구간을 그린다. | ||
| */ | ||
| public enum SlopeGrade { | ||
|
|
||
| STEEP_DOWN, | ||
| MILD_DOWN, | ||
| FLAT, | ||
| MILD_UP, | ||
| STEEP_UP; | ||
|
|
||
| private static final double MILD_THRESHOLD = 5.0; | ||
| private static final double STEEP_THRESHOLD = 15.0; | ||
|
|
||
| public static SlopeGrade classify(double gradePercent) { | ||
| if (gradePercent <= -STEEP_THRESHOLD) { | ||
| return STEEP_DOWN; | ||
| } | ||
| if (gradePercent <= -MILD_THRESHOLD) { | ||
| return MILD_DOWN; | ||
| } | ||
| if (gradePercent < MILD_THRESHOLD) { | ||
| return FLAT; | ||
| } | ||
| if (gradePercent < STEEP_THRESHOLD) { | ||
| return MILD_UP; | ||
| } | ||
| return STEEP_UP; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/main/java/com/semosan/api/domain/mountain/service/CourseSlopeSegmentCalculator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package com.semosan.api.domain.mountain.service; | ||
|
|
||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.semosan.api.domain.mountain.dto.response.SlopeSegmentResponse; | ||
| import com.semosan.api.domain.mountain.enums.SlopeGrade; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * 코스 polyline(GeoJSON LineString) + 점별 altitudes 를 받아 경사 등급 segments 로 변환. | ||
| * | ||
| * 처리 단계: | ||
| * 1) altitudes 이동평균(window={@value #SMOOTHING_WINDOW}) smoothing 으로 GPS 노이즈 완화. | ||
| * 2) 인접 점 수평거리(Haversine) 와 고도차로 경사도(%) 산출. | ||
| * 3) {@link SlopeGrade} 로 등급 분류. | ||
| * 4) 같은 등급 연속 segment 묶기. | ||
| * | ||
| * 응답 좌표 인덱스(startIdx, endIdx) 는 polyline.coordinates 의 양 끝 포함(inclusive). | ||
| */ | ||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class CourseSlopeSegmentCalculator { | ||
|
|
||
| static final int SMOOTHING_WINDOW = 5; | ||
| private static final double EARTH_RADIUS_METERS = 6_371_000.0; | ||
|
|
||
| private final ObjectMapper objectMapper; | ||
|
|
||
| public List<SlopeSegmentResponse> calculate(String polylineJson, String altitudesJson) { | ||
| if (polylineJson == null || altitudesJson == null) { | ||
| return List.of(); | ||
| } | ||
| try { | ||
| JsonNode coords = objectMapper.readTree(polylineJson).get("coordinates"); | ||
| if (coords == null || !coords.isArray() || coords.size() < 2) { | ||
| return List.of(); | ||
| } | ||
| List<Double> altitudes = objectMapper.readValue(altitudesJson, new TypeReference<>() {}); | ||
| // altitudesJson 이 "null" (문자열) 인 경우 readValue 가 null 을 반환한다. 명시적으로 가드한다. | ||
| if (altitudes == null || altitudes.size() != coords.size()) { | ||
| return List.of(); | ||
|
JangInho marked this conversation as resolved.
|
||
| } | ||
|
|
||
| List<Double> smoothed = movingAverage(altitudes, SMOOTHING_WINDOW); | ||
|
|
||
| List<SlopeSegmentResponse> result = new ArrayList<>(); | ||
| int n = coords.size(); | ||
| int currentStart = 0; | ||
| SlopeGrade currentGrade = null; | ||
|
|
||
| for (int i = 0; i < n - 1; i++) { | ||
| double lng1 = coords.get(i).get(0).asDouble(); | ||
| double lat1 = coords.get(i).get(1).asDouble(); | ||
| double lng2 = coords.get(i + 1).get(0).asDouble(); | ||
| double lat2 = coords.get(i + 1).get(1).asDouble(); | ||
| double distance = haversine(lat1, lng1, lat2, lng2); | ||
|
|
||
| double gradePercent; | ||
| Double a1 = smoothed.get(i); | ||
| Double a2 = smoothed.get(i + 1); | ||
| if (distance == 0.0 || a1 == null || a2 == null) { | ||
| gradePercent = 0.0; | ||
| } else { | ||
| gradePercent = (a2 - a1) / distance * 100.0; | ||
| } | ||
| SlopeGrade grade = SlopeGrade.classify(gradePercent); | ||
|
|
||
| if (currentGrade == null) { | ||
| currentGrade = grade; | ||
| currentStart = i; | ||
| } else if (currentGrade != grade) { | ||
| result.add(new SlopeSegmentResponse(currentStart, i, currentGrade)); | ||
| currentGrade = grade; | ||
| currentStart = i; | ||
| } | ||
| } | ||
| if (currentGrade != null) { | ||
| result.add(new SlopeSegmentResponse(currentStart, n - 1, currentGrade)); | ||
| } | ||
| return result; | ||
| } catch (Exception e) { | ||
| log.warn("Failed to compute slope segments — returning empty list. polylineLen={} altLen={}", | ||
| polylineJson.length(), altitudesJson.length(), e); | ||
| return List.of(); | ||
| } | ||
| } | ||
|
|
||
| private static List<Double> movingAverage(List<Double> altitudes, int window) { | ||
| int n = altitudes.size(); | ||
| int half = window / 2; | ||
| List<Double> out = new ArrayList<>(n); | ||
| for (int i = 0; i < n; i++) { | ||
| int from = Math.max(0, i - half); | ||
| int to = Math.min(n - 1, i + half); | ||
| double sum = 0.0; | ||
| int cnt = 0; | ||
| for (int j = from; j <= to; j++) { | ||
| Double v = altitudes.get(j); | ||
| if (v != null) { | ||
| sum += v; | ||
| cnt++; | ||
| } | ||
| } | ||
| out.add(cnt > 0 ? sum / cnt : null); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| private static double haversine(double lat1, double lng1, double lat2, double lng2) { | ||
| double phi1 = Math.toRadians(lat1); | ||
| double phi2 = Math.toRadians(lat2); | ||
| double dPhi = Math.toRadians(lat2 - lat1); | ||
| double dLambda = Math.toRadians(lng2 - lng1); | ||
| double a = Math.sin(dPhi / 2) * Math.sin(dPhi / 2) | ||
| + Math.cos(phi1) * Math.cos(phi2) * Math.sin(dLambda / 2) * Math.sin(dLambda / 2); | ||
| double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | ||
| return EARTH_RADIUS_METERS * c; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.