Skip to content

Commit

Permalink
[feat] 경로 길이, 소요시간 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
doyeoo committed Feb 24, 2024
1 parent 48996ec commit f8b0cae
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@
@Getter
public class ShortestPathResponseDTO {

private int totalDistance;
private int totalTime;
private int totalDistance;
private List<List<Double>> coordinates;

@Builder
private ShortestPathResponseDTO(
int totalTime,
int totalDistance,
List<List<Double>> coordinates) {
this.totalTime = totalTime;
this.totalDistance = totalDistance;
this.coordinates = coordinates;
}

public static ShortestPathResponseDTO from(List<List<Double>> coordinates) {
public static ShortestPathResponseDTO from(TmapPathResponseDTO path) {
return ShortestPathResponseDTO.builder()
.coordinates(coordinates)
.totalTime(path.getTotalTime())
.totalDistance(path.getTotalDistance())
.coordinates(path.getCoordinates())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package sungdong29.backend.domain.walk.dto.response;

import lombok.Builder;
import lombok.Getter;

import java.util.List;

@Getter
public class TmapPathResponseDTO {

private int totalDistance;
private int totalTime;
private List<List<Double>> coordinates;

@Builder
private TmapPathResponseDTO(
int totalDistance,
int totalTime,
List<List<Double>> coordinates) {
this.totalDistance = totalDistance;
this.totalTime = totalTime;
this.coordinates = coordinates;
}

public static TmapPathResponseDTO of(
int totalDistance,
int totalTime,
List<List<Double>> coordinates) {
return TmapPathResponseDTO.builder()
.totalDistance(totalDistance)
.totalTime(totalTime)
.coordinates(coordinates)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,27 @@ private WalkPathResponseDTO(
String name,
double xCoordinate,
double yCoordinate,
int totalTime,
int totalDistance,
List<List<Double>> coordinates) {
this.id = id;
this.name = name;
this.xCoordinate = xCoordinate;
this.yCoordinate = yCoordinate;
this.totalTime = totalTime;
this.totalDistance = totalDistance;
this.coordinates = coordinates;
}

public static WalkPathResponseDTO of(Walk walk, List<List<Double>> coordinates) {
public static WalkPathResponseDTO of(Walk walk, TmapPathResponseDTO path) {
return WalkPathResponseDTO.builder()
.id(walk.getId())
.name(walk.getName())
.xCoordinate(walk.getXCoordinate())
.yCoordinate(walk.getYCoordinate())
.coordinates(coordinates)
.totalTime(path.getTotalTime())
.totalDistance(path.getTotalDistance())
.coordinates(path.getCoordinates())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.stereotype.Component;
import sungdong29.backend.domain.place.domain.Place;
import sungdong29.backend.domain.walk.domain.Walk;
import sungdong29.backend.domain.walk.dto.response.TmapPathResponseDTO;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -20,7 +22,7 @@ public class WalkHelper {
@Value("${openapi.tmap.key}")
private String TMAP_APP_KEY;

public List<List<Double>> getLineString(String startX, String startY, Place place, double walkX, double walkY) {
public TmapPathResponseDTO getLineString(String startX, String startY, Place place, double walkX, double walkY) {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");

Expand All @@ -47,7 +49,9 @@ public List<List<Double>> getLineString(String startX, String startY, Place plac
return null;
}

private static List<List<Double>> extractLineStringCoordinates(String stringResponse) {
private static TmapPathResponseDTO extractLineStringCoordinates(String stringResponse) {
int totalTime = 0;
int totalDistance = 0;
List<List<Double>> lineStringCoordinates = new ArrayList<>();

JSONObject jsonObject = new JSONObject(stringResponse);
Expand All @@ -56,6 +60,12 @@ private static List<List<Double>> extractLineStringCoordinates(String stringResp
for (int i = 0; i < features.length(); i++) {
JSONObject feature = features.getJSONObject(i);
JSONObject geometry = feature.getJSONObject("geometry");
JSONObject properties = feature.getJSONObject("properties");

if(properties.has("totalDistance") && properties.has("totalTime")) {
totalDistance = properties.getInt("totalDistance");
totalTime = properties.getInt("totalTime");
}

if ("LineString".equals(geometry.getString("type"))) {
JSONArray coordinatesArray = geometry.getJSONArray("coordinates");
Expand All @@ -74,7 +84,6 @@ private static List<List<Double>> extractLineStringCoordinates(String stringResp
}
}
}
return lineStringCoordinates;
return TmapPathResponseDTO.of(totalDistance, totalTime, lineStringCoordinates);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sungdong29.backend.domain.walk.domain.Walk;
import sungdong29.backend.domain.walk.domain.WalkHistory;
import sungdong29.backend.domain.walk.dto.response.ShortestPathResponseDTO;
import sungdong29.backend.domain.walk.dto.response.TmapPathResponseDTO;
import sungdong29.backend.domain.walk.dto.response.WalkPathResponseDTO;
import sungdong29.backend.domain.walk.dto.response.WalkPathsResponseDTO;
import sungdong29.backend.domain.walk.helper.WalkHelper;
Expand All @@ -36,8 +37,8 @@ public class WalkService {
@Transactional
public ShortestPathResponseDTO getShortestPath(String startX, String startY, Long placeId) {
Place place = placeHelper.getPlaceById(placeId);
List<List<Double>> lineString = walkHelper.getLineString(startX, startY, place, 0, 0);
return ShortestPathResponseDTO.from(lineString);
TmapPathResponseDTO path = walkHelper.getLineString(startX, startY, place, 0, 0);
return ShortestPathResponseDTO.from(path);
}

@Transactional
Expand All @@ -49,8 +50,8 @@ public WalkPathsResponseDTO getWalkPath(String startX, String startY, Long place
Page<Walk> closeWalks = walkRepository.findClosestWalk(startX, startY, place.getXCoordinate(), place.getYCoordinate(), pageable);

for (Walk walk : closeWalks.getContent()) {
List<List<Double>> lineString = walkHelper.getLineString(startX, startY, place, walk.getXCoordinate(), walk.getYCoordinate());
paths.add(WalkPathResponseDTO.of(walk, lineString));
TmapPathResponseDTO path = walkHelper.getLineString(startX, startY, place, walk.getXCoordinate(), walk.getYCoordinate());
paths.add(WalkPathResponseDTO.of(walk, path));

}
return WalkPathsResponseDTO.from(paths);
Expand Down

0 comments on commit f8b0cae

Please sign in to comment.