-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
17 changed files
with
305 additions
and
68 deletions.
There are no files selected for viewing
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
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
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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/amorgakco/backend/group/service/search/CityLevelSearchStrategy.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,26 @@ | ||
package com.amorgakco.backend.group.service.search; | ||
|
||
import com.amorgakco.backend.group.dto.GroupSearchRequest; | ||
import com.google.common.geometry.S2LatLng; | ||
import com.google.common.geometry.S2LatLngRect; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CityLevelSearchStrategy extends GroupSearchStrategy { | ||
|
||
private static final double LATITUDE_SIZE = 0.07; | ||
private static final double LONGITUDE_SIZE = 0.14; | ||
|
||
@Override | ||
public boolean isValid(final double diagonalSize) { | ||
return diagonalSize > DiagonalDistanceConst.MAX_DISTANCE.getValue(); | ||
} | ||
|
||
@Override | ||
public S2LatLngRect createRectangle(final GroupSearchRequest request) { | ||
return S2LatLngRect.fromCenterSize( | ||
S2LatLng.fromDegrees(request.centerLat(), request.centerLon()), | ||
S2LatLng.fromDegrees(LATITUDE_SIZE, LONGITUDE_SIZE)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/amorgakco/backend/group/service/search/DiagonalDistanceConst.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,13 @@ | ||
package com.amorgakco.backend.group.service.search; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum DiagonalDistanceConst { | ||
MAX_DISTANCE(14200), | ||
MIN_DISTANCE(7500); | ||
|
||
private final double value; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/amorgakco/backend/group/service/search/DongLevelSearchStrategy.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,21 @@ | ||
package com.amorgakco.backend.group.service.search; | ||
|
||
import com.amorgakco.backend.group.dto.GroupSearchRequest; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
public class DongLevelSearchStrategy extends GroupSearchStrategy { | ||
|
||
@Override | ||
public boolean isValid(final double diagonalSize) { | ||
return diagonalSize <= DiagonalDistanceConst.MIN_DISTANCE.getValue(); | ||
} | ||
|
||
@Override | ||
public List<String> selectCellTokens(final GroupSearchRequest request) { | ||
return getCoveringCells(request); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/amorgakco/backend/group/service/search/GroupSearchStrategy.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,46 @@ | ||
package com.amorgakco.backend.group.service.search; | ||
|
||
import com.amorgakco.backend.global.GoogleS2Const; | ||
import com.amorgakco.backend.group.dto.GroupSearchRequest; | ||
import com.google.common.geometry.S2CellId; | ||
import com.google.common.geometry.S2LatLng; | ||
import com.google.common.geometry.S2LatLngRect; | ||
import com.google.common.geometry.S2RegionCoverer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
|
||
public abstract class GroupSearchStrategy { | ||
public abstract boolean isValid(double diagonalSize); | ||
|
||
public S2LatLngRect createRectangle(final GroupSearchRequest request) { | ||
return S2LatLngRect.fromPointPair( | ||
S2LatLng.fromDegrees(request.southWestLat(), request.southWestLon()), | ||
S2LatLng.fromDegrees(request.northEastLat(), request.northEastLon())); | ||
} | ||
|
||
protected final List<String> getCoveringCells(final GroupSearchRequest request) { | ||
final S2LatLngRect rectangle = createRectangle(request); | ||
final S2RegionCoverer coverer = | ||
S2RegionCoverer.builder() | ||
.setMinLevel(GoogleS2Const.S2_CELL_LEVEL.getValue()) | ||
.setMaxLevel(GoogleS2Const.S2_CELL_LEVEL.getValue()) | ||
.build(); | ||
final ArrayList<S2CellId> cellIds = new ArrayList<>(); | ||
coverer.getCovering(rectangle, cellIds); | ||
return cellIds.stream().map(S2CellId::toToken).toList(); | ||
} | ||
|
||
public List<String> selectCellTokens(final GroupSearchRequest request) { | ||
final List<String> cellTokens = getCoveringCells(request); | ||
return findHalfOfCells(cellTokens); | ||
} | ||
|
||
private List<String> findHalfOfCells(final List<String> cellTokens) { | ||
return IntStream.range(0, cellTokens.size() - 1) | ||
.filter(i -> i % 2 == 0) | ||
.mapToObj(cellTokens::get) | ||
.toList(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/amorgakco/backend/group/service/search/GuLevelSearchStrategy.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,13 @@ | ||
package com.amorgakco.backend.group.service.search; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class GuLevelSearchStrategy extends GroupSearchStrategy { | ||
|
||
@Override | ||
public boolean isValid(final double diagonalSize) { | ||
return DiagonalDistanceConst.MIN_DISTANCE.getValue() < diagonalSize | ||
&& diagonalSize <= DiagonalDistanceConst.MAX_DISTANCE.getValue(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/amorgakco/backend/group/service/search/S2CellSearch.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,39 @@ | ||
package com.amorgakco.backend.group.service.search; | ||
|
||
import com.amorgakco.backend.global.exception.IllegalAccessException; | ||
import com.amorgakco.backend.group.domain.location.LocationCalculator; | ||
import com.amorgakco.backend.group.dto.GroupSearchRequest; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class S2CellSearch { | ||
|
||
private final List<GroupSearchStrategy> searchStrategies; | ||
|
||
public List<String> getCellTokens(final GroupSearchRequest request) { | ||
final double diagonalDistance = getDiagonalDistance(request); | ||
final GroupSearchStrategy groupSearchStrategy = getSearchStrategy(diagonalDistance); | ||
return groupSearchStrategy.selectCellTokens(request); | ||
} | ||
|
||
private GroupSearchStrategy getSearchStrategy(final double diagonalDistance) { | ||
return searchStrategies.stream() | ||
.filter(s -> s.isValid(diagonalDistance)) | ||
.findFirst() | ||
.orElseThrow(IllegalAccessException::invalidDiagonalDistance); | ||
} | ||
|
||
private double getDiagonalDistance(final GroupSearchRequest request) { | ||
return LocationCalculator.getDistance( | ||
request.southWestLon(), | ||
request.southWestLat(), | ||
request.northEastLon(), | ||
request.northEastLat()); | ||
} | ||
} |
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.