-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
M3-140 픽셀 개인전 정보를 가져오는 API 다시 만들기 #8
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/main/java/com/m3pro/groundflip/config/GeometryConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/main/java/com/m3pro/groundflip/config/GeometryConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 33 additions & 6 deletions
39
src/main/java/com/m3pro/groundflip/domain/dto/pixel/IndividualPixelResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
Comment on lines
+13
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 쿼리 잘 짜셨네요. 저번에 말한 쿼리보다 길긴 하지만 성능이 좋아진 것 같아 좋은 것 같습니다. gis 인덱스가 빠르네요~ |
||
List<Object[]> findAllIndividualPixelsByCoordinate( | ||
@Param("center") Point center, | ||
@Param("radius") int radius); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -12,11 +15,15 @@ | |
@Service | ||
@RequiredArgsConstructor | ||
public class PixelService { | ||
private final GeometryFactory geometryFactory; | ||
private final PixelRepository pixelRepository; | ||
|
||
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(4326); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 매직 넘버 아닌가요... |
||
|
||
return pixelRepository.findAllIndividualPixelsByCoordinate(point, radius).stream() | ||
.map(IndividualPixelResponse::from) | ||
.toList(); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
radius이 무엇을 중심으로 하는지 나타내는 것은 어떨까요?