Skip to content

Commit

Permalink
Merge pull request #77 from Team-PLAT/feat/#74/trackOverlap
Browse files Browse the repository at this point in the history
[FEAT] 겹치는 트랙 있으면 위치 약간 조정해서 업로드
  • Loading branch information
dgh06175 authored Nov 2, 2024
2 parents 81cbc08 + 2a657ab commit a7a91c9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -103,6 +104,20 @@ public TrackResponse.TrackId likeTrack(Member member, Long trackId, Boolean isLi
public TrackResponse.TrackId addTrack(Member member, TrackRequest.TrackUpload trackUpload) {
double latitude = trackUpload.getLatitude();
double longitude = trackUpload.getLongitude();

List<Track> nearTracks = trackRepository.findAllTracksWithinBounds(
latitude - 0.001,
latitude + 0.001,
longitude - 0.001,
longitude + 0.001
);

if (hasNearTrack(latitude, longitude, nearTracks)) {
List<Double> editedPosition = editOverlapPosition(latitude, longitude, nearTracks);
latitude = editedPosition.get(0);
longitude = editedPosition.get(1);
}

AddressInfo addressInfo = reverseGeoCoding.getAddressInfo(latitude, longitude);

Location location = locationRepository.save(trackMapper.toLocation(
Expand All @@ -117,6 +132,38 @@ public TrackResponse.TrackId addTrack(Member member, TrackRequest.TrackUpload tr
return trackMapper.toTrackId(savedTrack.getId());
}

private List<Double> editOverlapPosition(double latitude, double longitude, List<Track> nearTracks) {
List<Double> editedPosition = new ArrayList<>();
Random random = new Random();

int count = 0;
while (hasNearTrack(latitude, longitude, nearTracks)) {
if (count > 10) {
return editedPosition;
}
count++;
latitude += -0.0001 + (0.0002 * random.nextDouble());
longitude += -0.0001 + (0.0002 * random.nextDouble());
latitude = Math.round(latitude * 1_000_000) / 1_000_000.0;
longitude = Math.round(longitude * 1_000_000) / 1_000_000.0;
}

return List.of(latitude, longitude);
}

private boolean hasNearTrack(double la1, double lo1, List<Track> overlayTracks) {
for (Track track : overlayTracks) {
double la2 = track.getLocation().getLatitude();
double lo2 = track.getLocation().getLongitude();
double distance = Math.sqrt(Math.pow(la1 - la2, 2) + Math.pow(lo1 - lo2, 2));

if (distance <= 0.0001) {
return true;
}
}
return false;
}

@Override
public TrackResponse.TrackDetailList getTrackFeeds(Member member, int page, int size) {
List<Sort.Order> sorts = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,39 @@ void addTrackTest() {
assertThat(trackDetail.getMember().getMemberId()).isEqualTo(member.getId());
}

@Test
void 겹치는_트랙이_있으면_위치를_조정한다() {
// given
Member member = members.get(0);
TrackRequest.TrackUpload trackUpload1 = TrackRequest.TrackUpload.builder()
.isrc("isrc9")
.imageUrl("https://testimage9.com")
.content("테스트9")
.latitude(36.014188)
.longitude(129.325802)
.build();

TrackRequest.TrackUpload trackUpload2 = TrackRequest.TrackUpload.builder()
.isrc("test")
.imageUrl("https://testtest.com")
.content("테스트test")
.latitude(36.014108)
.longitude(129.325841)
.build();

// when
Long trackId1 = trackService.addTrack(member, trackUpload1).getTrackId();
Long trackId2 = trackService.addTrack(member, trackUpload2).getTrackId();

// then
TrackResponse.TrackDetail trackDetail1 = trackService.getTrackById(member, trackId1);
assertThat(trackDetail1.getLatitude()).isEqualTo(36.014188);
assertThat(trackDetail1.getLongitude()).isEqualTo(129.325802);
TrackResponse.TrackDetail trackDetail2 = trackService.getTrackById(member, trackId2);
assertThat(trackDetail2.getLatitude()).isNotEqualTo(36.014108);
assertThat(trackDetail2.getLongitude()).isNotEqualTo(129.325841);
}

@Test
void getTrackFeedsTest_개수만() {
// given
Expand Down

0 comments on commit a7a91c9

Please sign in to comment.