Browse Source

Refactor: CoordUtil 코드정리

pull/18/head
qkr7828 8 months ago
parent
commit
51b42ab1f6
  1. 89
      pav-server/src/main/java/com/palnet/comn/utils/CoordUtils.java

89
pav-server/src/main/java/com/palnet/comn/utils/CoordUtils.java

@ -36,6 +36,8 @@ public class CoordUtils {
private List<JSONObject> allLocation; private List<JSONObject> allLocation;
private GeometryFactory geometryFactory = new GeometryFactory();
private CoordUtils() { private CoordUtils() {
this.locationInit(); this.locationInit();
@ -196,84 +198,47 @@ public class CoordUtils {
} }
} }
public static JSONObject parseGeoJson(JSONObject obj, Coordinate coordinate) throws IOException, ParseException { public JSONObject parseGeoJson(JSONObject obj, Coordinate coordinate) throws IOException, ParseException {
GeometryFactory geometryFactory = new GeometryFactory();
Point point = geometryFactory.createPoint(coordinate); Point point = geometryFactory.createPoint(coordinate);
String type = (String) obj.get("type"); String type = (String) obj.get("type");
List<JSONObject> features = (List<JSONObject>) obj.get("features"); List<JSONObject> features = (List<JSONObject>) obj.get("features");
JSONObject result = new JSONObject(); return this.contains(features, point);
for(int i=0; i<features.size(); i++) {
JSONObject geometry = (JSONObject) features.get(i).get("geometry");
JSONObject properties = (JSONObject) features.get(i).get("properties");
List<JSONArray> coordinates = (List<JSONArray>) geometry.get("coordinates");
for(int k = 0; k< coordinates.size(); k++) {
List<Coordinate> polygonPaths = new ArrayList<>();
for(Object coords : coordinates.get(k)) {
for(int j = 0; j<((JSONArray)coords).size(); j++) {
Object coord = ((JSONArray) coords).get(j);
Object x = ((JSONArray) coord).get(0);
Object y = ((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);
} }
polygonPaths.add(polygonPaths.get(0));
} public JSONObject parseGeoJson(String path, Coordinate coordinate) throws IOException, ParseException {
Polygon polygon = geometryFactory.createPolygon(polygonPaths.toArray(new Coordinate[] {})); List<JSONObject> features = new ArrayList<JSONObject>();
if(polygon.contains(point)) return properties; Point point = null;
} try(InputStream inputStream = new ClassPathResource(path).getInputStream();
} BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8192)) {
return null;
}
public static JSONObject parseGeoJson(String path, Coordinate coordinate) throws IOException, ParseException { JSONParser jsonParser = new JSONParser();
GeometryFactory geometryFactory = new GeometryFactory(); JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
List<JSONObject> features = new ArrayList<JSONObject>(); point = geometryFactory.createPoint(coordinate);
Point point = null; features = (List<JSONObject>) jsonObject.get("features");
try(InputStream inputStream = new ClassPathResource(path).getInputStream(); }catch(Exception e) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8192)) {
JSONParser jsonParser = new JSONParser(); e.getStackTrace();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader); }
point = geometryFactory.createPoint(coordinate); return this.contains(features, point);
features = (List<JSONObject>) jsonObject.get("features"); }
}catch(Exception e) {
e.getStackTrace();
} public JSONObject contains(List<JSONObject> features, Point point) {
for(int i=0; i<features.size(); i++) { for(int i=0; i<features.size(); i++) {
@ -309,12 +274,14 @@ public class CoordUtils {
} }
Polygon polygon = geometryFactory.createPolygon(polygonPaths.toArray(new Coordinate[] {})); Polygon polygon = geometryFactory.createPolygon(polygonPaths.toArray(new Coordinate[] {}));
if(polygon.contains(point)) return properties;
if(polygon.contains(point)) return properties; }
}
return null;
} }
}
return null;
}
} }

Loading…
Cancel
Save