1,引入pom
<dependency>
<groupId>de.micromata.jak</groupId>
<artifactId>JavaAPIforKml</artifactId>
<version>2.2.0</version>
</dependency>
2,解析
package com.chint.module.kml;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import de.micromata.opengis.kml.v_2_2_0.*;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;
public class KmlParser {
private final Snowflake snowflake = IdUtil.getSnowflake(1, 1);
/**
* 保存kml数据到临时表
*
* @param file 上传的文件实体
* @return 自定义的KML文件实体
*/
public static KmlProperty toData(File file) {
Kml kml = Kml.unmarshal(file);
Feature feature = kml.getFeature();
KmlProperty kmlProperty = new KmlProperty();
if(Objects.isNull(feature)){
return kmlProperty;
}
kmlProperty.setName(feature.getName());
KmlParser kmlParser = new KmlParser();
kmlParser.parseFeature(feature, kmlProperty);
return kmlProperty;
}
public static KmlProperty toData(InputStream content) {
Kml kml = Kml.unmarshal(content);
Feature feature = kml.getFeature();
KmlProperty kmlProperty = new KmlProperty();
if(Objects.isNull(feature)){
return kmlProperty;
}
kmlProperty.setName(feature.getName());
KmlParser kmlParser = new KmlParser();
kmlParser.parseFeature(feature, kmlProperty);
return kmlProperty;
}
private void parseFeature(Feature feature, KmlProperty kmlProperty) {
if (feature instanceof Document) {
List<Feature> featureList = ((Document) feature).getFeature();
List<KmlProperty> kmlPropertyList = kmlProperty.getKmlPropertyList();
featureList.forEach(d -> {
if (d instanceof Placemark) {
getPlaceMark((Placemark) d, kmlProperty);
} else {
KmlProperty kmlProperty1 = new KmlProperty();
kmlProperty1.setName(d.getName());
kmlProperty1.setId(snowflake.nextIdStr());
kmlPropertyList.add(kmlProperty1);
parseFeature(d, kmlProperty1);
}
});
} else if (feature instanceof Folder) {
List<Feature> featureList = ((Folder)feature).getFeature();
List<KmlProperty> kmlPropertyList = kmlProperty.getKmlPropertyList();
featureList.forEach(d -> {
if (d instanceof Placemark) {
getPlaceMark((Placemark) d, kmlProperty);
}else {
KmlProperty kmlProperty1 = new KmlProperty();
kmlProperty1.setName(d.getName());
kmlProperty1.setId(snowflake.nextIdStr());
kmlPropertyList.add(kmlProperty1);
parseFeature(d, kmlProperty1);
}
});
}
}
private void getPlaceMark(Placemark placemark, KmlProperty kmlProperty) {
Geometry geometry = placemark.getGeometry();
String name = placemark.getName();
String description = placemark.getDescription();
parseGeometry(name, geometry, description, kmlProperty);
}
private void parseGeometry(String name, Geometry geometry, String description, 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(name, outerBoundaryIs, description, kmlProperty);
}
}
}
} else if (geometry instanceof LineString) {
LineString lineString = (LineString) geometry;
List<Coordinate> coordinates = lineString.getCoordinates();
if (coordinates != null) {
int width = 0;
coordinates = ((LineString) geometry).getCoordinates();
addLineStringToList(coordinates, name, description, kmlProperty);
}
} else if (geometry instanceof Point) {
Point point = (Point) geometry;
List<Coordinate> coordinates = point.getCoordinates();
if (coordinates != null) {
coordinates = ((Point) geometry).getCoordinates();
addPointToList(coordinates, name, description, kmlProperty);
}
} 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(coordinates, name, description, kmlProperty);
} else if (geometryToMult instanceof LineString) {
coordinates = ((LineString) geometryToMult).getCoordinates();
addLineStringToList(coordinates, name, description, kmlProperty);
} else if (geometryToMult instanceof Polygon) {
outerBoundaryIs = ((Polygon) geometryToMult).getOuterBoundaryIs();
addPolygonToList(name, outerBoundaryIs, description, kmlProperty);
}
}
}
}
}
private void addPolygonToList(String name, Boundary outerBoundaryIs, String description, KmlProperty kmlProperty) {
LinearRing linearRing = outerBoundaryIs.getLinearRing();//面
List<Coordinate> coordinates = linearRing.getCoordinates();
KmlProperty kmlProperty1 = new KmlProperty();
kmlProperty1.setId(snowflake.nextIdStr());
kmlProperty1.setName(name);
kmlProperty1.setPoints(coordinates);
kmlProperty1.setDescription(description);
kmlProperty1.setType(KmlPointTypeEnum.POLYGON.getCode());
List<KmlProperty> kmlPropertyList = kmlProperty.getKmlPropertyList();
kmlPropertyList.add(kmlProperty1);
}
private void addLineStringToList(List<Coordinate> coordinates, String name, String description, KmlProperty kmlProperty) {
KmlProperty kmlProperty1 = new KmlProperty();
kmlProperty1.setId(snowflake.nextIdStr());
kmlProperty1.setName(name);
kmlProperty1.setPoints(coordinates);
kmlProperty1.setDescription(description);
kmlProperty1.setType(KmlPointTypeEnum.LINE.getCode());
List<KmlProperty> kmlPropertyList = kmlProperty.getKmlPropertyList();
kmlPropertyList.add(kmlProperty1);
}
private void addPointToList(List<Coordinate> coordinates, String name, String description, KmlProperty kmlProperty) {
KmlProperty kmlProperty1 = new KmlProperty();
kmlProperty1.setId(snowflake.nextIdStr());
kmlProperty1.setName(name);
kmlProperty1.setPoints(coordinates);
kmlProperty1.setDescription(description);
kmlProperty1.setType(KmlPointTypeEnum.POINT.getCode());
List<KmlProperty> kmlPropertyList = kmlProperty.getKmlPropertyList();
kmlPropertyList.add(kmlProperty1);
}
}
package com.chint.module.kml;
import de.micromata.opengis.kml.v_2_2_0.Coordinate;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Data
public class KmlPoint implements Serializable {
private static final long serialVersionUID = 1L;
private List<Coordinate> points = new ArrayList<>();
private String name;
private String description;
//0:点 1:线 2:面
private Integer type;
}
package com.chint.module.kml;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum KmlPointTypeEnum {
//kml文件数据类型
POINT(0,"点"),
LINE(1,"线"),
POLYGON(2,"面")
;
private final Integer code;
private final String name;
}
package com.chint.module.kml;
import de.micromata.opengis.kml.v_2_2_0.Coordinate;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Data
public class KmlProperty implements Serializable {
private static final long serialVersionUID = 1L;
private String id = "0";
private Set<String> ids = new HashSet<>();
private String name;
private String description;
private List<Coordinate> points = new ArrayList<>();
//0:点 1:线 2:面
private Integer type;
private List<KmlProperty> kmlPropertyList = new ArrayList<>();
}