rain
2024-08-21 2db1aa88e8ab53096a936163d686b90d8e056a99
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
package com.dji.sample.patches.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);
    }
 
}