Skip to content

Commit

Permalink
Merge pull request #4 from Seoul-Public-Data/feat/3
Browse files Browse the repository at this point in the history
feat: 500m 내의 heatShelter 정보 반환 api 추가, DistanceUtils 추가
  • Loading branch information
jungnoeun authored Apr 8, 2024
2 parents 6c1f975 + 71a9ed9 commit d808d07
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 33 deletions.
27 changes: 27 additions & 0 deletions src/main/java/seouldata/seoul_backend/common/DistanceUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package seouldata.seoul_backend.common;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class DistanceUtils {

/**
* 하버사인 공식을 이용하여 두 지점간의 거리를 계산
*/
public static double calculateDistance(double userLon, double userLat, double lon, double lat) {

double earthRadius = 6371000; // 지구 반지름(미터)

double dLat = Math.toRadians(lat - userLat);
double dLon = Math.toRadians(lon - userLon);

double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(userLat)) * Math.cos(Math.toRadians(lat))
* Math.sin(dLon / 2) * Math.sin(dLon / 2);

double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return earthRadius * c;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seouldata.seoul_backend.cctv.application.dto.request;
package seouldata.seoul_backend.domain.cctv.application.dto.request;

import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seouldata.seoul_backend.cctv.application.dto.response;
package seouldata.seoul_backend.domain.cctv.application.dto.response;

import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seouldata.seoul_backend.cctv.domain.entity;
package seouldata.seoul_backend.domain.cctv.domain.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package seouldata.seoul_backend.cctv.domain.repository;
package seouldata.seoul_backend.domain.cctv.domain.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import seouldata.seoul_backend.cctv.domain.entity.AnCctv;
import seouldata.seoul_backend.domain.cctv.domain.entity.AnCctv;

public interface AnCctvRepository extends JpaRepository<AnCctv, String> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package seouldata.seoul_backend.cctv.domain.service;
package seouldata.seoul_backend.domain.cctv.domain.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import seouldata.seoul_backend.cctv.application.dto.request.CctvRequest;
import seouldata.seoul_backend.cctv.application.dto.response.CctvResponse;
import seouldata.seoul_backend.cctv.domain.entity.AnCctv;
import seouldata.seoul_backend.cctv.domain.repository.AnCctvRepository;
import seouldata.seoul_backend.domain.cctv.application.dto.request.CctvRequest;
import seouldata.seoul_backend.domain.cctv.application.dto.response.CctvResponse;
import seouldata.seoul_backend.domain.cctv.domain.entity.AnCctv;
import seouldata.seoul_backend.domain.cctv.domain.repository.AnCctvRepository;
import seouldata.seoul_backend.common.DistanceUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -38,7 +38,7 @@ public List<CctvResponse.CctvNearResponse> getCctvNear(CctvRequest.CctvNearReque
double lon = cctv.getLon();
double lat = cctv.getLat();

double distance = calculationDistance(userLon, userLat, lon, lat);
double distance = DistanceUtils.calculateDistance(userLon, userLat, lon, lat);

if(distance <= 500) {
CctvResponse.CctvNearResponse nearCctv = CctvResponse.CctvNearResponse.builder()
Expand All @@ -55,21 +55,4 @@ public List<CctvResponse.CctvNearResponse> getCctvNear(CctvRequest.CctvNearReque
return nearCctvs;

}

// 하버 사인 적용하여 두 점 사이 거리 구하기
public double calculationDistance(double userLon, double userLat, double lon, double lat) {
double earthRadius = 6371000; // 지구 반지름(미터)

double dLat = Math.toRadians(lat - userLat);
double dLon = Math.toRadians(lon - userLon);

double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(userLat)) * Math.cos(Math.toRadians(lat))
* Math.sin(dLon / 2) * Math.sin(dLon / 2);

double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return earthRadius * c;
}


}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package seouldata.seoul_backend.cctv.presentation;
package seouldata.seoul_backend.domain.cctv.presentation;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import seouldata.seoul_backend.cctv.application.dto.request.CctvRequest;
import seouldata.seoul_backend.cctv.application.dto.response.CctvResponse;
import seouldata.seoul_backend.cctv.domain.service.AnCctvService;
import seouldata.seoul_backend.domain.cctv.application.dto.request.CctvRequest;
import seouldata.seoul_backend.domain.cctv.application.dto.response.CctvResponse;
import seouldata.seoul_backend.domain.cctv.domain.service.AnCctvService;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package seouldata.seoul_backend.domain.heatshelter.appplication.dto.request;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

public class HeatShelterRequest {

@Getter
@NoArgsConstructor
public static class HeatShelterNearRequest {
private double userLongitude;
private double userLatitude;

@Builder
public HeatShelterNearRequest(double userLongitude, double userLatitude) {
this.userLongitude = userLongitude;
this.userLatitude = userLatitude;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package seouldata.seoul_backend.domain.heatshelter.appplication.dto.response;

import jakarta.persistence.Column;
import jakarta.persistence.Id;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

public class HeatShelterResponse {

@Getter
@NoArgsConstructor
public static class HeatShelterNearResponse {
private String name;
private String address;
private double longitude;
private double latitude;

@Builder
public HeatShelterNearResponse(String name, String address, double longitude, double latitude) {
this.name = name;
this.address = address;
this.longitude = longitude;
this.latitude = latitude;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package seouldata.seoul_backend.domain.heatshelter.appplication.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import seouldata.seoul_backend.common.DistanceUtils;
import seouldata.seoul_backend.domain.heatshelter.domain.entity.HeatShelter;
import seouldata.seoul_backend.domain.heatshelter.domain.service.HeatShelterQueryService;
import seouldata.seoul_backend.domain.heatshelter.appplication.dto.request.HeatShelterRequest;
import seouldata.seoul_backend.domain.heatshelter.appplication.dto.response.HeatShelterResponse;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
public class HeatShelterGetService {

private final HeatShelterQueryService heatShelterQueryService;

public List<HeatShelterResponse.HeatShelterNearResponse> getHeatShelterNear(HeatShelterRequest.HeatShelterNearRequest heatShelterNearRequest) {

List<HeatShelter> heatShelters = heatShelterQueryService.findAll();
double userLatitude = heatShelterNearRequest.getUserLatitude();
double userLongitude = heatShelterNearRequest.getUserLongitude();
System.out.println("userLatitude: " + userLatitude);

List<HeatShelterResponse.HeatShelterNearResponse> heatShelterNearList = new ArrayList<>();
for (HeatShelter heatShelter : heatShelters) {
double longitude = heatShelter.getLongitude();
double latitude = heatShelter.getLatitude();
double distance = DistanceUtils.calculateDistance(userLongitude, userLatitude, longitude, latitude);
if (distance < 500) {
HeatShelterResponse.HeatShelterNearResponse heatShelterNearResponse = HeatShelterResponse.HeatShelterNearResponse.builder()
.name(heatShelter.getName())
.address(heatShelter.getAddress())
.longitude(heatShelter.getLongitude())
.latitude(heatShelter.getLatitude())
.build();
heatShelterNearList.add(heatShelterNearResponse);
}
}
return heatShelterNearList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package seouldata.seoul_backend.domain.heatshelter.domain.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "an_heat_shelter")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class HeatShelter {

@Id @Column(name = "shelter_name")
private String name;

private String address;

@Column(name = "lon")
private double longitude;

@Column(name = "lat")
private double latitude;

@Builder
public HeatShelter(String name, String address, double longitude, double latitude) {
this.name = name;
this.address = address;
this.longitude = longitude;
this.latitude = latitude;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package seouldata.seoul_backend.domain.heatshelter.domain.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import seouldata.seoul_backend.domain.heatshelter.domain.entity.HeatShelter;

public interface HeatShelterRepository extends JpaRepository<HeatShelter, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package seouldata.seoul_backend.domain.heatshelter.domain.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import seouldata.seoul_backend.domain.heatshelter.domain.entity.HeatShelter;
import seouldata.seoul_backend.domain.heatshelter.domain.repository.HeatShelterRepository;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
public class HeatShelterQueryService {

private final HeatShelterRepository heatShelterRepository;

public List<HeatShelter> findAll() {
return heatShelterRepository.findAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seouldata.seoul_backend.domain.heatshelter.presentation;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import seouldata.seoul_backend.domain.heatshelter.appplication.dto.request.HeatShelterRequest;
import seouldata.seoul_backend.domain.heatshelter.appplication.dto.response.HeatShelterResponse;
import seouldata.seoul_backend.domain.heatshelter.appplication.service.HeatShelterGetService;


import java.util.List;

@RestController
@RequiredArgsConstructor
public class HeatShelterController {

private final HeatShelterGetService heatShelterGetService;

@GetMapping("/api/heatshelter")
public List<HeatShelterResponse.HeatShelterNearResponse> getHeatShelterNear(@RequestBody HeatShelterRequest.HeatShelterNearRequest heatShelterNearRequest) {
return heatShelterGetService.getHeatShelterNear(heatShelterNearRequest);
}

}

0 comments on commit d808d07

Please sign in to comment.