package com.dji.sample.patches.utils;
|
|
import com.dji.sample.patches.kml.KmlData;
|
import com.dji.sample.patches.kml.KmlLine;
|
import com.dji.sample.patches.kml.KmlPoint;
|
import com.dji.sample.patches.kml.KmlPolygon;
|
import de.micromata.opengis.kml.v_2_2_0.*;
|
import org.apache.commons.compress.archivers.ArchiveEntry;
|
import org.apache.commons.compress.archivers.ArchiveInputStream;
|
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
|
|
import java.io.*;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* @description: KML文件解析
|
**/
|
public class ParsingKmlUtil {
|
/**
|
* 解析kml文件
|
*/
|
public static KmlData parseKmlByFile(File file) {
|
Kml kml = Kml.unmarshal(file);
|
return getByKml(kml);
|
}
|
/**
|
* 解析kml文件流
|
*
|
* @param inputstream
|
* @return
|
*/
|
public static KmlData parseKmlByInputstream(InputStream inputstream) {
|
Kml kml = Kml.unmarshal(inputstream);
|
return getByKml(kml);
|
}
|
/**
|
* Kml对象转自定义存储对象
|
*
|
* @param kml
|
* @return
|
*/
|
public static KmlData getByKml(Kml kml) {
|
KmlData kmlData = new KmlData();
|
kmlData.setKmlPoints(new ArrayList<>());
|
kmlData.setKmlLines(new ArrayList<>());
|
kmlData.setKmlPolygons(new ArrayList<>());
|
Feature feature = kml.getFeature();
|
parseFeature(feature, kmlData);
|
return kmlData;
|
}
|
|
/**
|
* 解析kml节点
|
* @param feature
|
* @param kmlData
|
*/
|
private static void parseFeature(Feature feature, KmlData kmlData) {
|
if (feature != null) {
|
if (feature instanceof Document) {
|
List<Feature> featureList = ((Document) feature).getFeature();
|
featureList.forEach(documentFeature -> {
|
if (documentFeature instanceof Placemark) {
|
getPlaceMark((Placemark) documentFeature, kmlData);
|
} else {
|
parseFeature(documentFeature, kmlData);
|
}
|
});
|
} else if (feature instanceof Folder) {
|
List<Feature> featureList = ((Folder) feature).getFeature();
|
featureList.forEach(documentFeature -> {
|
if (documentFeature instanceof Placemark) {
|
getPlaceMark((Placemark) documentFeature, kmlData);
|
} else {
|
parseFeature(documentFeature, kmlData);
|
}
|
});
|
}
|
}
|
}
|
|
private static void getPlaceMark(Placemark placemark, KmlData kmlData) {
|
Geometry geometry = placemark.getGeometry();
|
/*String name = placemark.getName();
|
placemark.getDescription();
|
System.out.println(placemark.getDescription());
|
if (name == null) {
|
name = placemark.getDescription();
|
}
|
parseGeometry(name, geometry, kmlData);*/
|
parseGeometry(placemark,geometry,kmlData);
|
}
|
/**
|
* 解析点线面形状的数据分别放入存储对象
|
* @param placemark placemark对象
|
* @param geometry 形状类型
|
* @param kmlData 存储对象
|
*/
|
private static void parseGeometry(Placemark placemark, Geometry geometry, KmlData kmlData) {
|
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(kmlData.getKmlPolygons(), placemark, outerBoundaryIs);
|
}
|
}
|
}
|
} else if (geometry instanceof LineString) {
|
LineString lineString = (LineString) geometry;
|
List<Coordinate> coordinates = lineString.getCoordinates();
|
if (coordinates != null) {
|
coordinates = ((LineString) geometry).getCoordinates();
|
addLineStringToList(kmlData.getKmlLines(), coordinates, placemark);
|
}
|
} else if (geometry instanceof Point) {
|
Point point = (Point) geometry;
|
List<Coordinate> coordinates = point.getCoordinates();
|
if (coordinates != null) {
|
coordinates = ((Point) geometry).getCoordinates();
|
addPointToList(kmlData.getKmlPoints(), coordinates, placemark);
|
}
|
} 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(kmlData.getKmlPoints(), coordinates, placemark);
|
} else if (geometryToMult instanceof LineString) {
|
coordinates = ((LineString) geometryToMult).getCoordinates();
|
addLineStringToList(kmlData.getKmlLines(), coordinates, placemark);
|
} else if (geometryToMult instanceof Polygon) {
|
outerBoundaryIs = ((Polygon) geometryToMult).getOuterBoundaryIs();
|
addPolygonToList(kmlData.getKmlPolygons(), placemark, outerBoundaryIs);
|
}
|
}
|
}
|
}
|
}
|
|
/**
|
* 保存面状数据
|
*
|
* @param kmlPolygonList 已有面状数据
|
* @param placemark placemark对象
|
* @param outerBoundaryIs 面状信息
|
*/
|
private static void addPolygonToList(List<KmlPolygon> kmlPolygonList, Placemark placemark, Boundary outerBoundaryIs) {
|
LinearRing linearRing = outerBoundaryIs.getLinearRing();// 面
|
KmlPolygon kmlPolygon = new KmlPolygon();
|
kmlPolygon.setPoints(linearRing.getCoordinates());
|
kmlPolygon.setName(placemark.getName());
|
kmlPolygon.setDescription(placemark.getDescription());
|
kmlPolygonList.add(kmlPolygon);
|
}
|
/**
|
* 保存线状数据
|
*
|
* @param kmlLineList 已有线状数据
|
* @param coordinates 线状经纬度数据
|
* @param placemark 线状名称
|
*/
|
private static void addLineStringToList(List<KmlLine> kmlLineList, List<Coordinate> coordinates, Placemark placemark) {
|
KmlLine kmlLine = new KmlLine();
|
kmlLine.setPoints(coordinates);
|
kmlLine.setName(placemark.getName());
|
kmlLine.setDescription(placemark.getDescription());
|
kmlLineList.add(kmlLine);
|
}
|
/**
|
* 保存点状数据
|
*
|
* @param kmlPointList 已有点状数据
|
* @param coordinates 点状经纬度数据
|
* @param placemark 点状名称
|
*/
|
private static void addPointToList(List<KmlPoint> kmlPointList, List<Coordinate> coordinates, Placemark placemark) {
|
KmlPoint kmlPoint = new KmlPoint();
|
kmlPoint.setName(placemark.getName());
|
kmlPoint.setDescription(placemark.getDescription());
|
kmlPoint.setPoints(coordinates);
|
kmlPointList.add(kmlPoint);
|
}
|
|
/**
|
* kmz 文件解析
|
* @param inputStream
|
* @throws IOException
|
*/
|
public static KmlData parseKMZFile(InputStream inputStream) throws IOException {
|
KmlData kmlData = new KmlData();
|
ArchiveInputStream archiveInputStream = new ZipArchiveInputStream(inputStream);
|
ArchiveEntry entry;
|
while ((entry = archiveInputStream.getNextEntry()) != null) {
|
String name = entry.getName();
|
if (name.toLowerCase().endsWith(".kml") || name.toLowerCase().endsWith(".kmz")) {
|
// 如果发现.kml或.kmz文件,可以将其内容读取出来并传递给KMLParser处理
|
kmlData = parseKML(archiveInputStream,kmlData);
|
}
|
}
|
return kmlData;
|
}
|
|
/**
|
* 解析kml 文件
|
* @param kmlInputStream
|
* @throws IOException
|
*/
|
public static KmlData parseKML(InputStream kmlInputStream, KmlData kmlData) {
|
// 取出kml中的内容
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(kmlInputStream))) {
|
String line;
|
StringBuffer xmlContent = new StringBuffer(1024);
|
while ((line = reader.readLine()) != null) {
|
// 处理每一行KML数据
|
xmlContent.append(line);
|
}
|
// System.out.println(xmlContent);
|
Kml kml = Kml.unmarshal(xmlContent.toString());
|
ParsingKmlUtil pku = new ParsingKmlUtil();
|
// 赋值
|
kmlData = pku.getByKml(kml);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
// 返回
|
return kmlData;
|
}
|
}
|