From 2b54df5911e1df5c3b7fc281fa9064dddd00a3fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dhji=28=EC=A7=80=EB=8C=80=ED=95=9C=29?= Date: Thu, 25 Jul 2024 16:22:15 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20kml=20=EC=9C=A0=ED=8B=B8=20=EA=B5=AC?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/palnet/comn/utils/KmlUtils.java | 162 ++++++------------ 1 file changed, 55 insertions(+), 107 deletions(-) diff --git a/pav-server/src/main/java/com/palnet/comn/utils/KmlUtils.java b/pav-server/src/main/java/com/palnet/comn/utils/KmlUtils.java index 8c229603..e605e664 100644 --- a/pav-server/src/main/java/com/palnet/comn/utils/KmlUtils.java +++ b/pav-server/src/main/java/com/palnet/comn/utils/KmlUtils.java @@ -1,136 +1,84 @@ package com.palnet.comn.utils; +import de.micromata.opengis.kml.v_2_2_0.*; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlRootElement; +import java.io.StringWriter; +import java.util.ArrayList; import java.util.List; public class KmlUtils { -// public static void main(String[] args) { -// KmlPlacemark placemark = new KmlPlacemark(); -// placemark.setName("Test"); -// placemark.setDescription("This is a test placemark"); -// placemark.setLatitude(37.5665); -// placemark.setLongitude(126.9780); -// placemark.setElevation(0.0); -// -// String kml = convertToKml(placemark); -// System.out.println(kml); -// } -// public static String convertToKml(KmlPlacemark placemark) { -// try { -// JAXBContext jaxbContext = JAXBContext.newInstance(KmlPlacemark.class); -// Marshaller marshaller = jaxbContext.createMarshaller(); -// -// marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); -// -// StringWriter writer = new StringWriter(); -// marshaller.marshal(placemark, writer); -// -// return writer.toString(); -// } catch (JAXBException e) { -// e.printStackTrace(); -// return null; -// } -// } + final static Kml kml = new Kml(); - @Data - @NoArgsConstructor - @AllArgsConstructor - @Builder - @XmlRootElement(name = "kml", namespace = "http://www.opengis.net/kml/2.2") - public static class KmlRoot { - T kml; - } + public static String createKmlPolygons(List rqList) { + // validation + if(rqList == null || rqList.isEmpty()) { + return null; + } - @Data - @NoArgsConstructor - @AllArgsConstructor - @Builder - @XmlRootElement(name = "Document") - public static class KmlDocument { - private String name; - private String open; - private String description; - private List styles; - private List folders; - } + // document + Document doc = kml.createAndSetDocument().withName("kml-export"); + Style style = doc.createAndAddStyle().withId("styled001"); + style.createAndSetLineStyle().withColor("ff0000ff").withWidth(2.0); + style.createAndSetPolyStyle().withColor("7f00ff00"); + // folder + Folder folder = doc.createAndAddFolder().withName("kml-folder"); - @Data - @NoArgsConstructor - @AllArgsConstructor - @Builder - @XmlRootElement(name = "Style") - public static class KmlStyle { - @XmlAttribute(name = "id") - private String id; - private KmlLineStyle LineStyle; - private KmlPolyStyle PolyStyle; - } + rqList.forEach(rq -> { + // placemark - polygon + Placemark placemark = folder.createAndAddPlacemark() + .withName(rq.getName()) + .withVisibility(true) + .withStyleUrl("#styled001"); + LinearRing linearRing = placemark.createAndSetPolygon() + .createAndSetOuterBoundaryIs() + .createAndSetLinearRing() + .withCoordinates(rq.getCoordinates()); - @Data - @NoArgsConstructor - @AllArgsConstructor - @Builder - @XmlRootElement(name = "LineStyle") - public static class KmlLineStyle { - private String color; - private String width; - } + }); - @Data - @NoArgsConstructor - @AllArgsConstructor - @Builder - @XmlRootElement(name = "PolyStyle") - public static class KmlPolyStyle { - private String color; + StringWriter sw = new StringWriter(); + kml.marshal(sw); + return sw.toString(); } @Data @NoArgsConstructor @AllArgsConstructor @Builder - @XmlRootElement(name = "Folder") - public static class KmlFolder { + public static class KmlRq { private String name; - private String description; - private KmlLookAt lookAt; - } - - @Data - @NoArgsConstructor - @AllArgsConstructor - @Builder - @XmlRootElement(name = "LookAt") - public static class KmlLookAt { - double longitude; - double latitude; - @Builder.Default - double altitude = 0; @Builder.Default - double heading = 0; - double tilt; - double range; + private List coordinates = new ArrayList<>(); } - @Data - @NoArgsConstructor - @AllArgsConstructor - @Builder - @XmlRootElement(name = "Placemark") - public static class KmlPlacemark { - private String name; - private String description; - @Builder.Default - private int visibility = 1; - private String styleUrl; - private KmlLookAt lookAt; + public static void main(String[] args) { + KmlRq rq1 = KmlRq.builder() + .name("test1") + .coordinates(List.of( + new Coordinate(126.73229515507134,37.622273460242354), + new Coordinate(126.73329515507134,37.622273460242354), + new Coordinate(126.73329515507134,37.622373460242354), + new Coordinate(126.73229515507134,37.622373460242354), + new Coordinate(126.73229515507134,37.622273460242354) + )) + .build(); + KmlRq rq2 = KmlRq.builder() + .name("test2") + .coordinates(List.of( + new Coordinate(126.74229515507134,37.632273460242354), + new Coordinate(126.74329515507134,37.632273460242354), + new Coordinate(126.74329515507134,37.632373460242354), + new Coordinate(126.74229515507134,37.632373460242354), + new Coordinate(126.74229515507134,37.632273460242354) + )) + .build(); + String str = createKmlPolygons(List.of(rq1, rq2)); + System.out.println(str); } }