package com.yelang.kmzcase;
import de.micromata.opengis.kml.v_2_2_0.*;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* @description: KML文件解析
**/
public class ParsingKmlUtil {
/**
* 解析kml文件
*/
public KmlData parseKmlByFile(File file) {
Kml kml = Kml.unmarshal(file);
return getByKml(kml);
}
/**
* 解析kml文件流
*
* @param inputstream
* @return
*/
public KmlData parseKmlByInputstream(InputStream inputstream) {
Kml kml = Kml.unmarshal(inputstream);
return getByKml(kml);
}
/**
* Kml对象转自定义存储对象
*
* @param kml
* @return
*/
public 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 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 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 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 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 name 线状名称
*/
private 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 name 点状名称
*/
private 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);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
纯用JAVA解析Google的KMZ和KML空间数据的示例代码
共9个文件
java:8个
xml:1个
需积分: 5 2 下载量 182 浏览量
2024-06-01
21:37:54
上传
评论 1
收藏 6KB RAR 举报
温馨提示
这是直接使用JAVA来解析Google的KMZ和KML类型文件的主要示例代码和POM.xml,pom.xml中的一些依赖项可以忽略不计。在实际解析过程中用不到,网上大多数资源都是解析的KML,但是对于KMZ文件的在线解析模式,并没有明确的例子。这里提供了一种解决方案,方便大家在实际项目中使用。包括示例工程,参考的CSDN博客地址:https://blog.csdn.net/yelangkingwuzuhu/article/details/139375604
资源推荐
资源详情
资源评论
收起资源包目录
读取KMZ.rar (9个子文件)
读取KMZ
pom.xml 2KB
kmzcase
KmlPolygon.java 267B
KmlBaseEntity.java 994B
KmlLine.java 362B
KMZParser.java 4KB
ParsingKmlUtil.java 6KB
KmlData.java 937B
IKMLParser.java 351B
KmlPoint.java 511B
共 9 条
- 1
资源评论
夜郎king
- 粉丝: 6w+
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功