Skip to content

refactor: 재배치 거리 계산 방식 PostGIS 적용 - #104

Merged
ri-naa merged 8 commits into
mainfrom
refactor/relocation-distance-logic
Jun 26, 2026
Merged

refactor: 재배치 거리 계산 방식 PostGIS 적용#104
ri-naa merged 8 commits into
mainfrom
refactor/relocation-distance-logic

Conversation

@ri-naa

@ri-naa ri-naa commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • DB 내부에 설치된 PostGIS의 공간 인덱스(GIST)와 내장 함수 ST_DWithin, ST_DistanceSphere 를 활용하여, 공간 연산 책임을 애플리케이션 서버에서 데이터베이스 단으로 위임
  • RelocationAssignRequestusePostGis 플래그를 추가하여, 기존 방식과 PostGIS 방식 비교 테스트 환경 구현

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces PostGIS-based spatial queries to optimize distance-based relocation assignments, adding a geom point column to StandWeight and a fallback to application-side Haversine calculations. Feedback suggests defining the geom column as geography(Point, 4326) instead of geometry to utilize spatial indexes (GIST) and avoid casting overhead in the repository query. Additionally, the application-side fallback logic should be optimized to prevent redundant Haversine calculations on the entire dataset.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +31 to +41
targetList = allStands.map { stand ->
val dist = calculateHaversine(request.currentLat, request.currentLon, stand.latitude, stand.longitude)
TargetStand(stand.standId, stand.weight, dist, stand.latitude, stand.longitude)
}.filter { it.distance <= 10000.0 }

if (targetList.isEmpty()) {
targetList = allStands.map { stand ->
val dist = calculateHaversine(request.currentLat, request.currentLon, stand.latitude, stand.longitude)
TargetStand(stand.standId, stand.weight, dist, stand.latitude, stand.longitude)
}.filter { it.distance <= 30000.0 }
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

allStands에 대해 하버사인 거리를 계산하고 TargetStand로 매핑하는 작업이 targetList.isEmpty()인 경우에 중복으로 수행되고 있습니다. 전체 정거장 데이터가 많을 경우 불필요한 연산으로 인해 성능 저하가 발생할 수 있으므로, 거리 계산 및 매핑을 최초 1회만 수행한 후 필터링하도록 개선하는 것이 좋습니다.

            val mappedStands = allStands.map { stand ->
                val dist = calculateHaversine(request.currentLat, request.currentLon, stand.latitude, stand.longitude)
                TargetStand(stand.standId, stand.weight, dist, stand.latitude, stand.longitude)
            }
            targetList = mappedStands.filter { it.distance <= 10000.0 }
            
            if (targetList.isEmpty()) {
                targetList = mappedStands.filter { it.distance <= 30000.0 }
            }

Comment on lines +31 to +32
@Column(columnDefinition = "geometry(Point, 4326)")
val geom: org.locationtech.jts.geom.Point? = null,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

공간 인덱스(GIST)를 효율적으로 활용하고 거리 계산 시 매번 geography로 캐스팅하는 비용을 줄이기 위해, 컬럼 정의를 geometry(Point, 4326) 대신 geography(Point, 4326)로 변경하는 것을 권장합니다. geography 타입을 사용하면 미터(m) 단위의 거리 연산(ST_DWithin, ST_Distance 등) 시 별도의 캐스팅 없이 공간 인덱스를 직접 탈 수 있습니다.

Suggested change
@Column(columnDefinition = "geometry(Point, 4326)")
val geom: org.locationtech.jts.geom.Point? = null,
@Column(columnDefinition = "geography(Point, 4326)")
val geom: org.locationtech.jts.geom.Point? = null,

@ri-naa
ri-naa merged commit dba8f74 into main Jun 26, 2026
7 checks passed
@ri-naa
ri-naa deleted the refactor/relocation-distance-logic branch June 26, 2026 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant