From 661a68c37a8cc0d914a2b2d7132413e270c71cba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?scnoh=28=EB=85=B8=EC=8A=B9=EC=B2=A0=29?= Date: Tue, 2 Aug 2022 17:41:57 +0900 Subject: [PATCH] =?UTF-8?q?=EB=B9=84=ED=96=89=20=EC=9E=A5=EC=95=A0?= =?UTF-8?q?=EB=AC=BC=EC=98=81=EC=97=AD=20=EC=B2=B4=ED=81=AC=20=EC=9E=91?= =?UTF-8?q?=EC=97=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bas/flight/service/BasFlightService.java | 7 +- .../java/com/palnet/comn/utils/AreaUtils.java | 167 +++++++++++++++++- 2 files changed, 162 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/palnet/biz/api/bas/flight/service/BasFlightService.java b/src/main/java/com/palnet/biz/api/bas/flight/service/BasFlightService.java index 187e31d..0f36ea9 100644 --- a/src/main/java/com/palnet/biz/api/bas/flight/service/BasFlightService.java +++ b/src/main/java/com/palnet/biz/api/bas/flight/service/BasFlightService.java @@ -316,14 +316,15 @@ public class BasFlightService { if("LINE".equals(area.getAreaType())) { List convertCoordinates = areaUtils.convertCoordinates(area.getCoordList()); - List bufferList = areaUtils.buffer(convertCoordinates, area.getBufferZone()); + + List bufferList = areaUtils.buffer(convertCoordinates, area.getBufferZone()); // buffer 영역 생성 + boolean overlaps = areaUtils.overlaps(bufferList); // buffer 영역 장애물 포함 여부 체크 + List bufferCoordList = areaUtils.convertModel(bufferList); area.setBufferCoordList(bufferCoordList); log.info("RQ: {}", rq); - } else { - throw new CustomException(ErrorCode.FAIL); } } diff --git a/src/main/java/com/palnet/comn/utils/AreaUtils.java b/src/main/java/com/palnet/comn/utils/AreaUtils.java index dade21c..2db7357 100644 --- a/src/main/java/com/palnet/comn/utils/AreaUtils.java +++ b/src/main/java/com/palnet/comn/utils/AreaUtils.java @@ -2,11 +2,19 @@ package com.palnet.comn.utils; import com.palnet.biz.api.bas.flight.model.BasFlightPlanAreaCoordModel; import lombok.extern.slf4j.Slf4j; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; import org.locationtech.jts.geom.*; +import org.locationtech.jts.geom.impl.CoordinateArraySequence; import org.locationtech.jts.operation.buffer.BufferOp; import org.locationtech.jts.operation.buffer.BufferParameters; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; import org.springframework.stereotype.Component; +import java.io.FileReader; +import java.io.Reader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -15,19 +23,85 @@ import java.util.List; @Slf4j public class AreaUtils { - public List buffer(List coordList, Integer bufferZone) { - List bufferList = new ArrayList<>(); + private List polygons = new ArrayList<>(); + private List points = new ArrayList<>(); + + public AreaUtils() throws Exception { + this.init(); + } + + /** + * TODO 비행 구역 생성시 장애물 영역 포함 체크 + * + * @param targetCoordList - 비행구역 좌표 리스트 + * @return boolean - true(비정상), false(정상) + */ + public boolean overlaps(List targetCoordList) { + boolean result = false; + + GeometryFactory geometryFactory = new GeometryFactory(); + + Polygon targetPolygon = geometryFactory.createPolygon(targetCoordList.toArray(new Coordinate[]{})); + + /* 공역 데이터 */ + if(polygons.size() > 0) { + for(Polygon polygon : polygons) { + Geometry overlabsGeometry = geometryFactory.createGeometry(polygon); + Geometry targetGeometry = geometryFactory.createGeometry(targetPolygon); + + boolean over = targetGeometry.overlaps(overlabsGeometry); + + if(over) { + result = true; + break; + } + } + } + + return result; + } + /** + * TODO 드론 관제시 정상 비행 체크 + * + * @param areaCoordList - 비행 구역 좌표 리스트 + * @param targetCoordinate - 드론 좌표 정보 + * @return boolean - true(비정상), false(정상) + */ + public boolean contains(List areaCoordList, Coordinate targetCoordinate) { GeometryFactory geometryFactory = new GeometryFactory(); - LineString line = new GeometryFactory().createLineString(coordList.toArray(new Coordinate[] {})); - Geometry geometry = geometryFactory.createGeometry(line); - // buffer - int nSegments = 10; - int cap = BufferParameters.CAP_SQUARE; - int join = BufferParameters.JOIN_ROUND; +// CoordinateSequence coordinateSequence = new CoordinateArraySequence((CoordinateSequence) targetCoordinate); + + LineString line = new GeometryFactory().createLineString(areaCoordList.toArray(new Coordinate[]{})); + + Point point = new Point((CoordinateSequence) targetCoordinate, geometryFactory); + + boolean contains = line.contains(point); + + return contains; + } + + /** + * TODO 비행 구역 - Buffer 영역 생성 + * + * @param coordList - 비행 구역 기초 좌표 리스트 + * @param bufferZone - 반경 + * + */ + public List buffer(List coordList, Integer bufferZone) { + List bufferList = new ArrayList<>(); + GeometryFactory geometryFactory = new GeometryFactory(); + + LineString line = geometryFactory.createLineString(coordList.toArray(new Coordinate[] {})); + Geometry geometry = geometryFactory.createGeometry(line); - BufferParameters bufferParam = new BufferParameters(nSegments, cap, join, join); + // buffer + int nSegments = 10; + int cap = BufferParameters.CAP_SQUARE; + int join = BufferParameters.JOIN_ROUND; + + BufferParameters bufferParam = new BufferParameters(nSegments, cap, join, join); // Geometry buffer = geometry.buffer(bufferZone,20,cap); BufferOp ops = new BufferOp(geometry, bufferParam); @@ -69,4 +143,79 @@ public class AreaUtils { return bufferCoordList; } + public void init() throws Exception { + GeometryFactory geometryFactory = new GeometryFactory(); + + // 1. file read + Resource resource = new ClassPathResource("air/airgeo.json"); + Reader jsonFile = new FileReader(resource.getFile()); + + // 2. json parsing + JSONParser jsonParser = new JSONParser(); + JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonFile); + + List features = (List) jsonObject.get("features"); + + for(int i=0; i coordinates = (List) geometry.get("coordinates"); + String type = (String) geometry.get("type"); + + if("Polygon".equals(type)) { + for(JSONArray coordList : coordinates) { + List polygonPaths = new ArrayList<>(); + + for(Object coord : coordList) { + Object y = ((JSONArray) coord).get(0); + Object x = ((JSONArray) coord).get(1); + + Double lon = y instanceof Double ? (Double) y : Double.valueOf((Long) y); + Double lat = x instanceof Double ? (Double) x : Double.valueOf((Long) x); + + Coordinate areaCoord = new Coordinate(lon, lat); + + polygonPaths.add(areaCoord); + } + + Polygon polygon = geometryFactory.createPolygon(polygonPaths.toArray(new Coordinate[] {})); + + polygons.add(polygon); + } + } + + if("LineString".equals(type)) { + List lineStringPaths = new ArrayList<>(); + for(JSONArray coordList : coordinates) { + + Object y = coordList.get(0); + Object x = coordList.get(1); + + Double lon = y instanceof Double ? (Double) y : Double.valueOf((Long) y); + Double lat = x instanceof Double ? (Double) x : Double.valueOf((Long) x); + + Coordinate areaCoord = new Coordinate(lon, lat); + + lineStringPaths.add(areaCoord); + } + + Polygon polygon = geometryFactory.createPolygon(lineStringPaths.toArray(new Coordinate[] {})); + + polygons.add(polygon); + } + + if("Point".equals(type)) { + Object y = coordinates.get(0); + Object x = coordinates.get(1); + + Double lon = y instanceof Double ? (Double) y : Double.valueOf((Long) y); + Double lat = x instanceof Double ? (Double) x : Double.valueOf((Long) x); + + Coordinate areaCoord = new Coordinate(lon, lat); + + Point point = geometryFactory.createPoint(areaCoord); + points.add(point); + } + } + } }