Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yujung7768903 committed Nov 12, 2024
2 parents 3957356 + 516e1f6 commit c99cd2d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,18 @@ public static MoveUserInfo createWithCarPath(
public static MoveUserInfo createWithWalkPath(
Space space,
Participation participation,
int totalTime,
double totalDistance,
TmapWalkPathResponse tmapWalkPathResponse,
List<PathDto>path
) {
MoveUserInfo moveUserInfo = new MoveUserInfo();
moveUserInfo.isAdmin = (participation.getUserId() == space.getAdminId()) ? true : false;
moveUserInfo.userId = participation.getUserId();
moveUserInfo.userName = participation.getUserName();
moveUserInfo.transportationType = participation.getTransportation();
moveUserInfo.totalTime = totalTime;
moveUserInfo.totalDistance = totalDistance;
moveUserInfo.transportationType = TransportationType.WALK;
moveUserInfo.totalTime = tmapWalkPathResponse.getTotalTime();
moveUserInfo.totalDistance = tmapWalkPathResponse.getTotalDistance();
moveUserInfo.path = path;
moveUserInfo.payment = 0;
moveUserInfo.payment = tmapWalkPathResponse.getPayment();
return moveUserInfo;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/moim/backend/domain/space/response/PathDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Objects;

@Getter
@Builder
@NoArgsConstructor
Expand All @@ -17,4 +19,21 @@ public class PathDto {
@JsonProperty("y")
private Double latitude;

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
PathDto comparePath = (PathDto) obj;
return Double.compare(latitude, comparePath.getLatitude()) == 0
&& Double.compare(longitude, comparePath.getLongitude()) == 0;
}

@Override
public int hashCode() {
return Objects.hash(latitude, longitude);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,24 @@
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class TmapWalkPathResponse {
public class TmapWalkPathResponse implements MoveInfoInterface {
public List<Feature> features;

@Override
public int getTotalTime() {
return Math.round(features.get(0).properties.getTotalTime() / 60);
}

@Override
public Double getTotalDistance() {
return Double.valueOf(features.get(0).properties.getDistance());
}

@Override
public int getPayment() {
return 0;
}

@Getter
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -25,25 +40,19 @@ public static class Feature {
@AllArgsConstructor
public static class Geometry {
public String type;
public String coordinates;
public List<Object> coordinates;
}

// Geometry 타입에 따라 시간이나 거리를 넘겨줄 때의 키 값이 달라짐
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class Properties {
private long totalDistance; // 단위: m. Geometry 타입이 "Point"일 때
private long totalTime; // 단위: 초. Geometry 타입이 "Point"일 때
private long distance; // 단위: m. Geometry 타입이 "LineString"일 때
private long time; // 단위: 초. Geometry 타입이 "LineString"일 때

public long getTotalTime() {
return totalTime + time;
}

public long getTotalDistance() {
return totalDistance + distance;
}
// totalDistance는 여러 feature 중 한 개에만 담겨서 옴
// 가장 첫 번째 feature에 담겨서 오는 듯 하나 항상 그런지는 모름.
private long totalDistance; // 단위: m
private long totalTime; // 단위: 초
private long distance; // 단위: m
private long time; // 단위: 초
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,8 @@ private List<MoveUserInfo> getMoveUserInfoList(
List<MoveUserInfo> moveUserInfoList = new ArrayList<>();

for (Participation participation : participationList) {
log.info("participation: {}, ({}, {})", participation.getUserName(), participation.getLatitude(), participation.getLongitude());
log.info("participation id: {}, name: {}", participation.getParticipationId(), participation.getUserName());
log.info("start position: ({}, {})", participation.getLatitude(), participation.getLongitude());
moveUserInfoList.add(getMoveUserInfo(bestPlace, space, participation));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@Service
public class TmapService {
Expand Down Expand Up @@ -42,23 +45,18 @@ public MoveUserInfo createMoveUserInfoWithWalkPath(
TmapWalkPathResponse tmapWalkPathResponse,
BestPlace bestPlace
) {
List<PathDto> path = new ArrayList<>();
int totalTime = 0;
double totalDistance = 0;

Set<PathDto> path = new LinkedHashSet<>();
for (TmapWalkPathResponse.Feature feature : tmapWalkPathResponse.getFeatures()) {
// path 구성 추가
if ("Point".equals(feature.getGeometry().type)) {
path.add(getPathFromPointOfWalkPath(feature.geometry.coordinates));
Optional<PathDto> pathFromPointOfWalkPath = getPathFromPointOfWalkPath(feature.geometry.coordinates);
pathFromPointOfWalkPath.ifPresent(p -> path.add(p));
} else if ("LineString".equals(feature.getGeometry().type)) {
path.addAll(getPathListFromLineStringOfWalkPath(feature));
}
path.add(new PathDto(bestPlace.getLongitude(), bestPlace.getLatitude()));
// geometry 타입에 따라 totalTime에 값
totalTime += feature.properties.getTotalTime();
totalDistance += feature.properties.getTotalDistance();
}
return MoveUserInfo.createWithWalkPath(space, participation, totalTime, totalDistance, path);
path.add(new PathDto(bestPlace.getLongitude(), bestPlace.getLatitude()));
return MoveUserInfo.createWithWalkPath(space, participation, tmapWalkPathResponse, new ArrayList<>(path));
}

// 이동 구간이 도보인 경우, 상세 도보 경로를 path list로 변환
Expand Down Expand Up @@ -91,18 +89,23 @@ private List<PathDto> getPathFromPassStopList(TmapPublicPathResponse.Leg leg) {

private List<PathDto> getPathListFromLineStringOfWalkPath(TmapWalkPathResponse.Feature feature) {
List pathList = new ArrayList<>();
String[] pathStr = feature.geometry.coordinates.replace("[", "").replace("]", "").split(",");
for (String point : pathStr) {
pathList.add(getPathFromPointOfWalkPath(point));
for (Object coordinate : feature.geometry.coordinates) {
if (coordinate instanceof List<?>) {
Optional<PathDto> pathFromPoint = getPathFromPointOfWalkPath((List<Object>) coordinate);
pathFromPoint.ifPresent(p -> pathList.add(p));
}
}
return pathList;
}

private PathDto getPathFromPointOfWalkPath(String point) {
String[] pathStr = point.replace("[", "").replace("]", "").split(",");
return PathDto.builder()
.longitude(Double.parseDouble(pathStr[0]))
.latitude(Double.parseDouble(pathStr[1]))
.build();
private Optional<PathDto> getPathFromPointOfWalkPath(List<Object> point) {
if (point.get(0) instanceof Double && point.get(1) instanceof Double) {
PathDto path = PathDto.builder()
.longitude(Double.parseDouble(point.get(0).toString()))
.latitude(Double.parseDouble(point.get(1).toString()))
.build();
return Optional.ofNullable(path);
}
return Optional.empty();
}
}

0 comments on commit c99cd2d

Please sign in to comment.