rain
2024-08-20 74d1c21ebb6b3b916904d95d13d289df23dcdedd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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;
    }
}