postgreSql? 그래!

김희영·2025년 10월 14일

토막개발지식

목록 보기
14/27

postgreSQL을 사용할 경우, 지리 정보를 다루기 유리 하다.
예를 들어, 위도와 경도 값과 범위만 있다면 해당 범위 안에 있는 데이터를 가져올 수 있다.
다음은 pins 테이블 내의 값을 조회한다.

@Query(
        value = "SELECT * FROM pins p " +
                "WHERE ST_DWithin(" +
                "    ST_SetSRID(ST_MakePoint(p.longitude, p.latitude), 4326)::geography, " + // DB에 저장된 위경도로 Geography Point 생성
                "    ST_SetSRID(ST_MakePoint(:centerLon, :centerLat), 4326)::geography, " +   // 중심 좌표로 Geography Point 생성
                "    :radiusKm * 1000.0" + // 킬로미터(km)를 미터(m)로 변환하여 전달 (ST_DWithin은 미터 단위 사용)
                ")",
        nativeQuery = true
    )
    List<Pin> findPinsWithinRadius(
        @Param("centerLat") Double centerLat,
        @Param("centerLon") Double centerLon,
        @Param("radiusKm") Double radiusKm
    );
}

그러나 이때, postgreSql의 확장은 필수다. 하지 않을 경우 오류 나니 조심!

profile
내는 반드시 엄청난 개발자가 되고 말것어

0개의 댓글