Skip to content

Commit

Permalink
Merge pull request #8 from SWM-M3PRO/feature/M3-140-individualPixelRe…
Browse files Browse the repository at this point in the history
…impl

M3-140 픽셀 개인전 정보를 가져오는 API 다시 만들기
  • Loading branch information
qjvk2880 authored Jun 26, 2024
2 parents ec8e288 + e7a2a1d commit 28c9c87
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 20 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/m3pro/groundflip/config/GeometryConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.m3pro.groundflip.config;

import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.PrecisionModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GeometryConfig {
private static final int WGS84_SRID = 4326;

@Bean
public GeometryFactory geometryFactory() {
return new GeometryFactory(new PrecisionModel(), WGS84_SRID);
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/m3pro/groundflip/config/GeometryConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.m3pro.groundflip.config;

import org.geolatte.geom.G2D;
import org.geolatte.geom.Point;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.PrecisionModel;
import org.springframework.stereotype.Component;

@Component
public class GeometryConverter {
private static final GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);

public static org.locationtech.jts.geom.Point convertGeomToJts(Object geolattePoint) {
Point<G2D> point = (Point<G2D>)geolattePoint;

G2D position = point.getPosition();
Coordinate coordinate = new Coordinate(position.getLon(), position.getLat());
return geometryFactory.createPoint(coordinate);
}
}
21 changes: 15 additions & 6 deletions src/main/java/com/m3pro/groundflip/controller/PixelController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import com.m3pro.groundflip.domain.dto.pixel.IndividualPixelResponse;
import com.m3pro.groundflip.service.PixelService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import lombok.RequiredArgsConstructor;

@RestController
Expand All @@ -19,13 +22,19 @@
public class PixelController {
private final PixelService pixelService;

@GetMapping("/individual")
@Operation(summary = "개인전 픽셀 조회", description = "특정 좌표를 중심으로 반경 내 개인전 픽셀 정보를 조회 API")
@Parameters({
@Parameter(name = "current-latitude", description = "원의 중심 좌표의 위도", example = "37.503717"),
@Parameter(name = "current-longitude", description = "원의 중심 좌표의 경도", example = "127.044317"),
@Parameter(name = "radius", description = "미터 단위의 반경", example = "1000"),
})
@GetMapping("/individual-mode")
public Response<List<IndividualPixelResponse>> getNearIndividualPixels(
@RequestParam(name = "current-x") int currentX,
@RequestParam(name = "current-y") int currentY,
@RequestParam(name = "x-range", required = false, defaultValue = "20") int xRange,
@RequestParam(name = "y-range", required = false, defaultValue = "10") int yRange) {
return Response.createSuccess(pixelService.getNearIndividualPixels(currentX, currentY, xRange, yRange));
@RequestParam(name = "current-latitude") double currentLatitude,
@RequestParam(name = "current-longitude") double currentLongitude,
@RequestParam(name = "radius") int radius) {
return Response.createSuccess(
pixelService.getNearIndividualPixelsByCoordinate(currentLatitude, currentLongitude, radius));
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
package com.m3pro.groundflip.domain.dto.pixel;

import com.m3pro.groundflip.domain.entity.Pixel;
import org.locationtech.jts.geom.Point;

import com.m3pro.groundflip.config.GeometryConverter;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@Schema(title = "개인전 픽셀 정보")
public class IndividualPixelResponse {
@Schema(description = "픽셀 ID", example = "78611")
private long pixelId;

@Schema(description = "픽셀 좌측 상단 위도", example = "37.503717")
private double latitude;

@Schema(description = "픽셀 좌측 상단 경도", example = "127.044317")
private double longitude;
private double x;
private double y;

public static IndividualPixelResponse from(Pixel pixel) {
return new IndividualPixelResponse(pixel.getCoordinate().getX(), pixel.getCoordinate().getY(), pixel.getX(),
pixel.getY());
@Schema(description = "소유주의 ID", example = "3")
private long userId;

@Schema(description = "픽셀 세로 상대 좌표", example = "224")
private long x;

@Schema(description = "픽셀 가로 상대 좌표", example = "210")
private long y;

public static IndividualPixelResponse from(Object[] queryResult) {
Point coordinate = GeometryConverter.convertGeomToJts(queryResult[1]);

return IndividualPixelResponse.builder()
.pixelId((long)queryResult[0])
.latitude(coordinate.getY())
.longitude(coordinate.getX())
.userId((long)queryResult[2])
.x((long)queryResult[3])
.y((long)queryResult[4])
.build();
}
}
46 changes: 41 additions & 5 deletions src/main/java/com/m3pro/groundflip/repository/PixelRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,52 @@

import java.util.List;

import org.locationtech.jts.geom.Point;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.m3pro.groundflip.domain.entity.Pixel;

public interface PixelRepository extends JpaRepository<Pixel, Long> {
@Query(value = "select pixel from Pixel pixel "
+ "where pixel.x between :currentX - :xRange / 2 and :currentX + :xRange / 2 "
+ "and pixel.y between :currentY - :yRange / 2 and :currentY + :yRange / 2 ")
List<Pixel> findAllNearPixels(@Param("currentX") int currentX, @Param("currentY") int currentY,
@Param("xRange") int xRange, @Param("yRange") int yRange);
@Query(value = """
WITH PixelsInRange AS (
SELECT
p.pixel_id,
p.coordinate,
p.x,
p.y
FROM
pixel p
WHERE
ST_CONTAINS((ST_Buffer(:center, :radius)), p.coordinate)
),
RecentVisits AS (
SELECT
pu.pixel_id,
pu.user_id,
pu.created_at,
ROW_NUMBER() OVER (PARTITION BY pu.pixel_id ORDER BY pu.created_at DESC) AS rn
FROM
pixel_user pu
JOIN
PixelsInRange pir ON pu.pixel_id = pir.pixel_id
)
SELECT
pir.pixel_id AS pixelId,
pir.coordinate,
rv.user_id AS userId,
pir.x,
pir.y
FROM
PixelsInRange pir
JOIN
RecentVisits rv ON pir.pixel_id = rv.pixel_id
WHERE
rv.rn = 1
""", nativeQuery = true)
List<Object[]> findAllIndividualPixelsByCoordinate(
@Param("center") Point center,
@Param("radius") int radius);

}
14 changes: 11 additions & 3 deletions src/main/java/com/m3pro/groundflip/service/PixelService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.List;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.springframework.stereotype.Service;

import com.m3pro.groundflip.domain.dto.pixel.IndividualPixelResponse;
Expand All @@ -12,11 +15,16 @@
@Service
@RequiredArgsConstructor
public class PixelService {
private final GeometryFactory geometryFactory;
private final PixelRepository pixelRepository;
private static final int WGS84_SRID = 4326;

public List<IndividualPixelResponse> getNearIndividualPixels(int currentX, int currentY, int xRange, int yRange) {
return pixelRepository.findAllNearPixels(currentX, currentY, xRange, yRange)
.stream()
public List<IndividualPixelResponse> getNearIndividualPixelsByCoordinate(double currentLatitude,
double currentLongitude, int radius) {
Point point = geometryFactory.createPoint(new Coordinate(currentLongitude, currentLatitude));
point.setSRID(WGS84_SRID);

return pixelRepository.findAllIndividualPixelsByCoordinate(point, radius).stream()
.map(IndividualPixelResponse::from)
.toList();
}
Expand Down

0 comments on commit 28c9c87

Please sign in to comment.