|
|
|
@ -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<Coordinate> buffer(List<Coordinate> coordList, Integer bufferZone) { |
|
|
|
|
List<Coordinate> bufferList = new ArrayList<>(); |
|
|
|
|
private List<Polygon> polygons = new ArrayList<>(); |
|
|
|
|
private List<Point> points = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
public AreaUtils() throws Exception { |
|
|
|
|
this.init(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* TODO 비행 구역 생성시 장애물 영역 포함 체크 |
|
|
|
|
* |
|
|
|
|
* @param targetCoordList - 비행구역 좌표 리스트 |
|
|
|
|
* @return boolean - true(비정상), false(정상) |
|
|
|
|
*/ |
|
|
|
|
public boolean overlaps(List<Coordinate> 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<Coordinate> 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<Coordinate> buffer(List<Coordinate> coordList, Integer bufferZone) { |
|
|
|
|
List<Coordinate> 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<JSONObject> features = (List<JSONObject>) jsonObject.get("features"); |
|
|
|
|
|
|
|
|
|
for(int i=0; i<features.size(); i++) { |
|
|
|
|
JSONObject geometry = (JSONObject) features.get(i).get("geometry"); |
|
|
|
|
|
|
|
|
|
List<JSONArray> coordinates = (List<JSONArray>) geometry.get("coordinates"); |
|
|
|
|
String type = (String) geometry.get("type"); |
|
|
|
|
|
|
|
|
|
if("Polygon".equals(type)) { |
|
|
|
|
for(JSONArray coordList : coordinates) { |
|
|
|
|
List<Coordinate> 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<Coordinate> 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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|