赞
踩
<!-- https://mvnrepository.com/artifact/de.micromata.jak/JavaAPIforKml -->
<dependency>
<groupId>de.micromata.jak</groupId>
<artifactId>JavaAPIforKml</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.11</version>
</dependency>
package org.example; import cn.hutool.core.util.ZipUtil; import de.micromata.opengis.kml.v_2_2_0.*; import java.io.File; import java.util.ArrayList; import java.util.List; /** * @description: 地图kml文件解析 * @author: fan * @date: 2022/7/7 15:48 * @version: 1.0 */ public class KmlFileParseUtil { public static KmlProperty parseKmzForJAK(File file){ File unzip = ZipUtil.unzip(file); File kmlFile = getKmlFile(unzip); return parseKmlForJAK(kmlFile); } private static File getKmlFile(File unzip){ File[] files = unzip.listFiles(); for (File f: files) { if(f.isFile()){ if(f.getName().endsWith("wpml")){ return f; } }else if(f.isDirectory()){ return getKmlFile(f); } } return null; } /** * 保存kml数据到临时表 * * @param file 上传的文件实体 * @return 自定义的KML文件实体 */ public static KmlProperty parseKmlForJAK(File file) { //以下三行都是自定义的KML类,用于获取名称name、所有点points、样式颜色color List<KmlPoint> kmlPointList = new ArrayList<>(); List<KmlLine> kmlLineList = new ArrayList<>(); List<KmlPolygon> kmlPolygonList = new ArrayList<>(); KmlProperty kmlProperty = new KmlProperty(); Kml kml = Kml.unmarshal(file); Feature feature = kml.getFeature(); kmlProperty.setKmlPoints(kmlPointList); kmlProperty.setKmlLines(kmlLineList); kmlProperty.setKmlPolygons(kmlPolygonList); parseFeature(feature,kmlProperty); return kmlProperty; } /** * 解析kml节点信息 * * @param feature 需要解析到要素信息 * @return */ private static void parseFeature(Feature feature, KmlProperty kmlProperty) { if (feature != null) { //判断根节点是否为Document if (feature instanceof Document) { List<Feature> featureList = ((Document) feature).getFeature(); //遍历已获取的节点信息(节点信息为List),将list使用forEach进行遍历(同for、while) featureList.forEach(documentFeature -> { //判断遍历节点是否为PlaceMark,否则迭代解析 if (documentFeature instanceof Placemark) { getPlaceMark((Placemark) documentFeature,kmlProperty); } else { parseFeature(documentFeature,kmlProperty); } } ); } else if (feature instanceof Folder) { //原理同上 List<Feature> featureList = ((Folder) feature).getFeature(); featureList.forEach(documentFeature -> { if (documentFeature instanceof Placemark) { getPlaceMark((Placemark) documentFeature,kmlProperty); } { parseFeature(documentFeature,kmlProperty); } } ); } } } /** * 解析PlaceMark节点下的信息 * * @return */ private static void getPlaceMark(Placemark placemark, KmlProperty kmlProperty) { Geometry geometry = placemark.getGeometry(); String name = placemark.getName(); parseGeometry(name, geometry,kmlProperty); } /** * 解析PlaceMark节点下的信息 * * @return */ private static void parseGeometry(String name, Geometry geometry, KmlProperty kmlProperty) { if (geometry != null) { if (geometry instanceof Polygon) { Polygon polygon = (Polygon) geometry; Boundary outerBoundaryIs = polygon.getOuterBoundaryIs(); if (outerBoundaryIs != null) { LinearRing linearRing = outerBoundaryIs.getLinearRing(); if (linearRing != null) { List<Coordinate> coordinates = linearRing.getCoordinates(); if (coordinates != null) { outerBoundaryIs = ((Polygon) geometry).getOuterBoundaryIs(); addPolygonToList(kmlProperty.getKmlPolygons(), name, outerBoundaryIs); } } } } else if (geometry instanceof LineString) { LineString lineString = (LineString) geometry; List<Coordinate> coordinates = lineString.getCoordinates(); if (coordinates != null) { coordinates = ((LineString) geometry).getCoordinates(); addLineStringToList(kmlProperty.getKmlLines(), coordinates, name); } } else if (geometry instanceof Point) { Point point = (Point) geometry; List<Coordinate> coordinates = point.getCoordinates(); if (coordinates != null) { coordinates = ((Point) geometry).getCoordinates(); addPointToList(kmlProperty.getKmlPoints(), coordinates, name); } } else if (geometry instanceof MultiGeometry) { List<Geometry> geometries = ((MultiGeometry) geometry).getGeometry(); for (Geometry geometryToMult : geometries) { Boundary outerBoundaryIs; List<Coordinate> coordinates; if (geometryToMult instanceof Point) { coordinates = ((Point) geometryToMult).getCoordinates(); addPointToList(kmlProperty.getKmlPoints(), coordinates, name); } else if (geometryToMult instanceof LineString) { coordinates = ((LineString) geometryToMult).getCoordinates(); addLineStringToList(kmlProperty.getKmlLines(), coordinates, name); } else if (geometryToMult instanceof Polygon) { outerBoundaryIs = ((Polygon) geometryToMult).getOuterBoundaryIs(); addPolygonToList(kmlProperty.getKmlPolygons(), name, outerBoundaryIs); } } } } } /** * 将kml中所有面添加到一个list * * @return */ private static void addPolygonToList(List<KmlPolygon> kmlPolygonList, String name, Boundary outerBoundaryIs) { LinearRing linearRing; List<Coordinate> coordinates; //面 linearRing = outerBoundaryIs.getLinearRing(); coordinates = linearRing.getCoordinates(); KmlPolygon kmlPolygon = new KmlPolygon(); kmlPolygon.setPoints(coordinates); kmlPolygon.setName(name); kmlPolygonList.add(kmlPolygon); } /** * 将kml中所有线添加到一个list * * @return */ private static void addLineStringToList(List<KmlLine> kmlLineList, List<Coordinate> coordinates, String name) { KmlLine kmlLine = new KmlLine(); kmlLine.setPoints(coordinates); kmlLine.setName(name); kmlLineList.add(kmlLine); } /** * 将kml中所有点添加到一个list * * @return */ private static void addPointToList(List<KmlPoint> kmlPointList, List<Coordinate> coordinates, String name) { KmlPoint kmlPoint = new KmlPoint(); kmlPoint.setName(name); kmlPoint.setPoints(coordinates); kmlPointList.add(kmlPoint); } /** * 航点信息 */ public static class KmlPoint { private List<Coordinate> points; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Coordinate> getPoints() { return points; } public void setPoints(List<Coordinate> points) { this.points = points; } @Override public String toString() { return "KmlPoint{" + "points=" + points + ", name='" + name + '\'' + '}'; } } /** * 航线信息 */ static class KmlLine { private List<Coordinate> points; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Coordinate> getPoints() { return points; } public void setPoints(List<Coordinate> points) { this.points = points; } @Override public String toString() { return "KmlLine{" + "points=" + points + ", name='" + name + '\'' + '}'; } } /** * 区域 */ static class KmlPolygon { private List<Coordinate> points; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Coordinate> getPoints() { return points; } public void setPoints(List<Coordinate> points) { this.points = points; } @Override public String toString() { return "KmlPolygon{" + "points=" + points + ", name='" + name + '\'' + '}'; } } /** * */ public static class KmlProperty { private List<KmlPoint> kmlPoints; private List<KmlLine> kmlLines; private List<KmlPolygon> kmlPolygons; public List<KmlPoint> getKmlPoints() { return kmlPoints; } public void setKmlPoints(List<KmlPoint> kmlPoints) { this.kmlPoints = kmlPoints; } public List<KmlLine> getKmlLines() { return kmlLines; } public void setKmlLines(List<KmlLine> kmlLines) { this.kmlLines = kmlLines; } public List<KmlPolygon> getKmlPolygons() { return kmlPolygons; } public void setKmlPolygons(List<KmlPolygon> kmlPolygons) { this.kmlPolygons = kmlPolygons; } @Override public String toString() { return "KmlProperty{" + "kmlPoints=" + kmlPoints + ", kmlLines=" + kmlLines + ", kmlPolygons=" + kmlPolygons + '}'; } } public static void main(String[] args) { File file = new File("C:\\Users\\Administrator\\Desktop\\长岭10kv集电线路杆塔航线\\二号起降点\\10kv长岭89-90.kmz"); File unzip = ZipUtil.unzip(file); File[] files = unzip.listFiles(); for (File f: files) { if(f.isFile()){ if(f.getName().endsWith("wpml")){ // 开始解析 } }else{ } } System.out.println(unzip); // File file1 = new File("C:\\Users\\Administrator\\Desktop\\长岭10kv集电线路杆塔航线\\二号起降点\\10kv长岭89-90\\wpmz\\waylines.wpml"); // // KmlProperty kmlProperty = parseKmlForJAK(file1); // if (kmlProperty.getKmlPoints().size() > 0) { // System.out.println("点"); // for (KmlPoint k : kmlProperty.getKmlPoints()) { // System.out.println(k.getPoints()); // } // // } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。