-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 대한민국을 2km단위로 나눠 해당 지역의 충전소 개수를 계산한다 (#880)
* feat: 적당히 자르는 기능 구현 [#819] * feat: 대한민국을 자르는 기능 구현 [#819] * feat: grid 내의 충전소 개수를 저장하는 기능 추가 [#819] * feat: 클러스터 기능 추가 * fix: 실패하는 테스트 수정 * feat: 혼잡도 계산 로직 중지 * refactor: 코드 리팩토링 * refactor: 코드 리팩토링 * refactor: 그리드 충전소 지정 속도 개선 * refactor: final 추가 * refactor: final 추가 및 불필요한 어노테이션 제거 * refactor: 메서드 분리 및 상수화 * refactor: 메서드 분리 * refactor: given-when-then 주석 추가 * refactor: 빈줄 삭제 * refactor: 반복 횟수 변경 * feat: grid의 마커 위치 정확도 수정 [#819] * feat: 위도 경도 변경 [#819] * infra: hikari CP size 변경 [#819] * fix: 실패하는 테스트 수정 [#819] * refactor: 메서드 분리 [#819] * refactor: 싱글 스레드로 변경 [#819] * refactor: 초기화 방식 변경 [#819] * refactor: 초기화 방식 변경 [#819] --------- Co-authored-by: drunkenhw <[email protected]>
- Loading branch information
Showing
24 changed files
with
681 additions
and
23 deletions.
There are no files selected for viewing
Empty file.
29 changes: 29 additions & 0 deletions
29
backend/src/main/java/com/carffeine/carffeine/station/config/InitialStationGridLoader.java
This file contains 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,29 @@ | ||
package com.carffeine.carffeine.station.config; | ||
|
||
import com.carffeine.carffeine.station.service.station.StationGridCacheService; | ||
import com.carffeine.carffeine.station.service.station.StationGridFacadeService; | ||
import com.carffeine.carffeine.station.service.station.dto.GridWithCount; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Configuration | ||
public class InitialStationGridLoader implements ApplicationRunner { | ||
|
||
private final StationGridFacadeService stationGridFacadeService; | ||
private final StationGridCacheService stationGridCacheService; | ||
|
||
@Override | ||
public void run(ApplicationArguments args) { | ||
log.info("initialize station grid"); | ||
List<GridWithCount> gridWithCounts = stationGridFacadeService.createGridWithCounts(); | ||
stationGridCacheService.initialize(gridWithCounts); | ||
log.info("cache size : {}", gridWithCounts.size()); | ||
} | ||
} |
This file contains 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
86 changes: 86 additions & 0 deletions
86
backend/src/main/java/com/carffeine/carffeine/station/domain/station/Grid.java
This file contains 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,86 @@ | ||
package com.carffeine.carffeine.station.domain.station; | ||
|
||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
|
||
@ToString | ||
@Getter | ||
public class Grid { | ||
|
||
private static final BigDecimal HALF = BigDecimal.valueOf(2); | ||
private static final Random RANDOM = new Random(); | ||
|
||
private final String id; | ||
private final Point top; | ||
private final Point bottom; | ||
private final List<Point> points; | ||
private int count; | ||
|
||
public Grid(Point top, Point bottom) { | ||
this.id = UUID.randomUUID().toString(); | ||
this.top = top; | ||
this.bottom = bottom; | ||
this.points = new ArrayList<>(); | ||
} | ||
|
||
public boolean isContain(Point point) { | ||
Latitude topLatitude = top.getLatitude(); | ||
Latitude bottomLatitude = bottom.getLatitude(); | ||
Latitude pointLatitude = point.getLatitude(); | ||
Longitude topLongitude = top.getLongitude(); | ||
Longitude bottomLongitude = bottom.getLongitude(); | ||
Longitude pointLongitude = point.getLongitude(); | ||
|
||
if (topLatitude.isHigher(bottomLatitude) && (pointLatitude.isBetween(topLatitude, bottomLatitude))) { | ||
return pointLongitude.isBetween(topLongitude, bottomLongitude); | ||
} | ||
return pointLongitude.isBetween(bottomLongitude, topLongitude); | ||
} | ||
|
||
public BigDecimal calculateCenterLatitude() { | ||
Latitude topLatitude = top.getLatitude(); | ||
Latitude bottomLatitude = bottom.getLatitude(); | ||
BigDecimal latitudeDistance = topLatitude.add(bottomLatitude); | ||
return latitudeDistance.divide(HALF, 4, RoundingMode.CEILING); | ||
} | ||
|
||
public BigDecimal calculateCenterLongitude() { | ||
Longitude topLongitude = top.getLongitude(); | ||
Longitude bottomLongitude = bottom.getLongitude(); | ||
BigDecimal longitudeDistance = topLongitude.add(bottomLongitude); | ||
return longitudeDistance.divide(HALF, 4, RoundingMode.CEILING); | ||
} | ||
|
||
public Point randomPoint() { | ||
int randomIndex = RANDOM.nextInt(points.size()); | ||
|
||
return points.get(randomIndex); | ||
} | ||
|
||
public void addPoint(Point point) { | ||
points.add(point); | ||
} | ||
|
||
public int stationSize() { | ||
return points.size(); | ||
} | ||
|
||
public boolean hasStation() { | ||
return !points.isEmpty(); | ||
} | ||
|
||
public boolean existsCount() { | ||
return count > 0; | ||
} | ||
|
||
public void addCount(int count) { | ||
this.count += count; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
backend/src/main/java/com/carffeine/carffeine/station/domain/station/GridGenerator.java
This file contains 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,83 @@ | ||
package com.carffeine.carffeine.station.domain.station; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
|
||
public class GridGenerator { | ||
|
||
private static final BigDecimal TOP_LATITUDE = BigDecimal.valueOf(38.6341); | ||
private static final BigDecimal TOP_LONGITUDE = BigDecimal.valueOf(124.5377); | ||
private static final BigDecimal BOTTOM_LATITUDE = BigDecimal.valueOf(33.1906); | ||
private static final BigDecimal BOTTOM_LONGITUDE = BigDecimal.valueOf(131.8795); | ||
|
||
public List<Grid> create(Point top, Point bottom, int latitudeDivisionSize, int longitudeDivisionSize) { | ||
|
||
List<Grid> grids = new ArrayList<>(); | ||
List<Latitude> latitudes = divideLatitude(top, bottom, latitudeDivisionSize); | ||
List<Longitude> longitudes = divideLongitude(top, bottom, longitudeDivisionSize); | ||
BigDecimal latInterval = getLatInterval(top, bottom, latitudeDivisionSize); | ||
BigDecimal longInterval = getLongInterval(top, bottom, longitudeDivisionSize); | ||
for (Latitude latitude : latitudes) { | ||
for (Longitude longitude : longitudes) { | ||
Point topPoint = Point.of(latitude.getValue(), longitude.getValue()); | ||
Point bottomPoint = Point.of(latitude.getValue().add(latInterval), longitude.getValue().add(longInterval)); | ||
grids.add(new Grid(topPoint, bottomPoint)); | ||
} | ||
} | ||
return grids; | ||
} | ||
|
||
private BigDecimal getLongInterval(Point top, Point bottom, int divisionSize) { | ||
Longitude topLongitude = top.getLongitude(); | ||
Longitude bottomLongitude = bottom.getLongitude(); | ||
BigDecimal length = topLongitude.subtract(bottomLongitude); | ||
return length.divide(BigDecimal.valueOf(divisionSize), 4, RoundingMode.HALF_EVEN); | ||
} | ||
|
||
private BigDecimal getLatInterval(Point top, Point bottom, int divisionSize) { | ||
Latitude topLatitude = top.getLatitude(); | ||
Latitude bottomLatitude = bottom.getLatitude(); | ||
BigDecimal length = topLatitude.subtract(bottomLatitude); | ||
return length.divide(BigDecimal.valueOf(divisionSize), 4, RoundingMode.HALF_EVEN); | ||
} | ||
|
||
private List<Longitude> divideLongitude(Point top, Point bottom, int divisionSize) { | ||
Longitude topLongitude = top.getLongitude(); | ||
Longitude bottomLongitude = bottom.getLongitude(); | ||
BigDecimal length = topLongitude.subtract(bottomLongitude); | ||
BigDecimal interval = length.divide(BigDecimal.valueOf(divisionSize), 4, RoundingMode.HALF_EVEN); | ||
return IntStream.range(0, divisionSize) | ||
.mapToObj(index -> calculateGridLongitude(index, bottomLongitude, interval)) | ||
.toList(); | ||
} | ||
|
||
private Longitude calculateGridLongitude(int index, Longitude longitude, BigDecimal interval) { | ||
BigDecimal distance = interval.multiply(new BigDecimal(index)); | ||
return Longitude.from(longitude.getValue().add(distance)); | ||
|
||
} | ||
|
||
private List<Latitude> divideLatitude(Point top, Point bottom, int divisionSize) { | ||
Latitude topLatitude = top.getLatitude(); | ||
Latitude bottomLatitude = bottom.getLatitude(); | ||
BigDecimal length = topLatitude.subtract(bottomLatitude); | ||
BigDecimal interval = length.divide(BigDecimal.valueOf(divisionSize), 4, RoundingMode.HALF_EVEN); | ||
return IntStream.range(0, divisionSize) | ||
.mapToObj(index -> calculateGridLatitude(index, bottomLatitude, interval)) | ||
.toList(); | ||
} | ||
|
||
private Latitude calculateGridLatitude(int index, Latitude latitude, BigDecimal interval) { | ||
BigDecimal distance = interval.multiply(new BigDecimal(index)); | ||
return Latitude.from(latitude.getValue().add(distance)); | ||
} | ||
|
||
public List<Grid> createKorea() { | ||
Point top = new Point(Latitude.from(TOP_LATITUDE), Longitude.from(TOP_LONGITUDE)); | ||
Point bottom = new Point(Latitude.from(BOTTOM_LATITUDE), Longitude.from(BOTTOM_LONGITUDE)); | ||
return create(top, bottom, 300, 240); | ||
} | ||
} |
This file contains 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 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
30 changes: 30 additions & 0 deletions
30
backend/src/main/java/com/carffeine/carffeine/station/domain/station/Point.java
This file contains 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,30 @@ | ||
package com.carffeine.carffeine.station.domain.station; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
import java.math.BigDecimal; | ||
|
||
@ToString | ||
@EqualsAndHashCode | ||
@Getter | ||
@AllArgsConstructor | ||
public class Point { | ||
|
||
private final Latitude latitude; | ||
private final Longitude longitude; | ||
|
||
public static Point of(long latitude, long longitude) { | ||
return new Point(Latitude.from(BigDecimal.valueOf(latitude)), Longitude.from(BigDecimal.valueOf(longitude))); | ||
} | ||
|
||
public static Point of(double latitude, double longitude) { | ||
return new Point(Latitude.from(BigDecimal.valueOf(latitude)), Longitude.from(BigDecimal.valueOf(longitude))); | ||
} | ||
|
||
public static Point of(BigDecimal latitude, BigDecimal longitude) { | ||
return new Point(Latitude.from(latitude), Longitude.from(longitude)); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.