当前位置:   article > 正文

地图kmz和kml类型的文件解析_kmz格式文件解析

kmz格式文件解析
 <!-- https://mvnrepository.com/artifact/de.micromata.jak/JavaAPIforKml -->
        <dependency>
            <groupId>de.micromata.jak</groupId>
            <artifactId>JavaAPIforKml</artifactId>
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.11</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
package org.example;

import cn.hutool.core.util.ZipUtil;
import de.micromata.opengis.kml.v_2_2_0.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 地图kml文件解析
 * @author: fan
 * @date: 2022/7/7 15:48
 * @version: 1.0
 */
public class KmlFileParseUtil {


    public static KmlProperty parseKmzForJAK(File file){

        File unzip = ZipUtil.unzip(file);
        File kmlFile = getKmlFile(unzip);
        return parseKmlForJAK(kmlFile);
    }

    private static File getKmlFile(File unzip){
        File[] files = unzip.listFiles();

        for (File f: files) {

            if(f.isFile()){
                if(f.getName().endsWith("wpml")){
                    return f;
                }
            }else if(f.isDirectory()){
                return getKmlFile(f);
            }
        }

        return null;
    }


    /**
     * 保存kml数据到临时表
     *
     * @param file 上传的文件实体
     * @return 自定义的KML文件实体
     */
    public static KmlProperty parseKmlForJAK(File file) {

        //以下三行都是自定义的KML类,用于获取名称name、所有点points、样式颜色color
        List<KmlPoint> kmlPointList = new ArrayList<>();
        List<KmlLine> kmlLineList = new ArrayList<>();
        List<KmlPolygon> kmlPolygonList = new ArrayList<>();
        KmlProperty kmlProperty = new KmlProperty();

        Kml kml = Kml.unmarshal(file);
        Feature feature = kml.getFeature();

        kmlProperty.setKmlPoints(kmlPointList);
        kmlProperty.setKmlLines(kmlLineList);
        kmlProperty.setKmlPolygons(kmlPolygonList);

        parseFeature(feature,kmlProperty);

        return kmlProperty;
    }

    /**
     * 解析kml节点信息
     *
     * @param feature 需要解析到要素信息
     * @return
     */
    private static void parseFeature(Feature feature, KmlProperty kmlProperty) {
        if (feature != null) {
            //判断根节点是否为Document
            if (feature instanceof Document) {
                List<Feature> featureList = ((Document) feature).getFeature();
                //遍历已获取的节点信息(节点信息为List),将list使用forEach进行遍历(同for、while)
                featureList.forEach(documentFeature -> {
                            //判断遍历节点是否为PlaceMark,否则迭代解析
                            if (documentFeature instanceof Placemark) {
                                getPlaceMark((Placemark) documentFeature,kmlProperty);
                            } else {
                                parseFeature(documentFeature,kmlProperty);
                            }
                        }
                );
            } else if (feature instanceof Folder) {
                //原理同上
                List<Feature> featureList = ((Folder) feature).getFeature();
                featureList.forEach(documentFeature -> {
                            if (documentFeature instanceof Placemark) {
                                getPlaceMark((Placemark) documentFeature,kmlProperty);
                            }
                            {
                                parseFeature(documentFeature,kmlProperty);
                            }
                        }
                );
            }
        }
    }

    /**
     * 解析PlaceMark节点下的信息
     *
     * @return
     */
    private static void getPlaceMark(Placemark placemark, KmlProperty kmlProperty) {
        Geometry geometry = placemark.getGeometry();
        String name = placemark.getName();
        parseGeometry(name, geometry,kmlProperty);
    }

    /**
     * 解析PlaceMark节点下的信息
     *
     * @return
     */
    private static void parseGeometry(String name, Geometry geometry, 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(kmlProperty.getKmlPolygons(), name, outerBoundaryIs);
                        }
                    }
                }
            } else if (geometry instanceof LineString) {
                LineString lineString = (LineString) geometry;
                List<Coordinate> coordinates = lineString.getCoordinates();
                if (coordinates != null) {
                    coordinates = ((LineString) geometry).getCoordinates();
                    addLineStringToList(kmlProperty.getKmlLines(), coordinates, name);
                }
            } else if (geometry instanceof Point) {
                Point point = (Point) geometry;
                List<Coordinate> coordinates = point.getCoordinates();
                if (coordinates != null) {
                    coordinates = ((Point) geometry).getCoordinates();
                    addPointToList(kmlProperty.getKmlPoints(), coordinates, name);
                }
            } 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(kmlProperty.getKmlPoints(), coordinates, name);
                    } else if (geometryToMult instanceof LineString) {
                        coordinates = ((LineString) geometryToMult).getCoordinates();
                        addLineStringToList(kmlProperty.getKmlLines(), coordinates, name);
                    } else if (geometryToMult instanceof Polygon) {
                        outerBoundaryIs = ((Polygon) geometryToMult).getOuterBoundaryIs();
                        addPolygonToList(kmlProperty.getKmlPolygons(), name, outerBoundaryIs);
                    }
                }
            }
        }
    }

    /**
     * 将kml中所有面添加到一个list
     *
     * @return
     */
    private static void addPolygonToList(List<KmlPolygon> kmlPolygonList, String name, Boundary outerBoundaryIs) {
        LinearRing linearRing;
        List<Coordinate> coordinates;
        //面
        linearRing = outerBoundaryIs.getLinearRing();
        coordinates = linearRing.getCoordinates();
        KmlPolygon kmlPolygon = new KmlPolygon();
        kmlPolygon.setPoints(coordinates);
        kmlPolygon.setName(name);
        kmlPolygonList.add(kmlPolygon);
    }

    /**
     * 将kml中所有线添加到一个list
     *
     * @return
     */
    private static void addLineStringToList(List<KmlLine> kmlLineList, List<Coordinate> coordinates, String name) {
        KmlLine kmlLine = new KmlLine();
        kmlLine.setPoints(coordinates);
        kmlLine.setName(name);
        kmlLineList.add(kmlLine);
    }

    /**
     * 将kml中所有点添加到一个list
     *
     * @return
     */
    private static void addPointToList(List<KmlPoint> kmlPointList, List<Coordinate> coordinates, String name) {
        KmlPoint kmlPoint = new KmlPoint();
        kmlPoint.setName(name);
        kmlPoint.setPoints(coordinates);
        kmlPointList.add(kmlPoint);
    }


    /**
     * 航点信息
     */
    public static class KmlPoint {
        private List<Coordinate> points;
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<Coordinate> getPoints() {
            return points;
        }

        public void setPoints(List<Coordinate> points) {
            this.points = points;
        }

        @Override
        public String toString() {
            return "KmlPoint{" +
                    "points=" + points +
                    ", name='" + name + '\'' +
                    '}';
        }
    }

    /**
     * 航线信息
     */
    static class KmlLine {
        private List<Coordinate> points;
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<Coordinate> getPoints() {
            return points;
        }

        public void setPoints(List<Coordinate> points) {
            this.points = points;
        }


        @Override
        public String toString() {
            return "KmlLine{" +
                    "points=" + points +
                    ", name='" + name + '\'' +
                    '}';
        }
    }

    /**
     * 区域
     */
    static class KmlPolygon {
        private List<Coordinate> points;
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<Coordinate> getPoints() {
            return points;
        }

        public void setPoints(List<Coordinate> points) {
            this.points = points;
        }

        @Override
        public String toString() {
            return "KmlPolygon{" +
                    "points=" + points +
                    ", name='" + name + '\'' +
                    '}';
        }
    }

    /**
     *
     */
    public static class KmlProperty {
        private List<KmlPoint> kmlPoints;
        private List<KmlLine> kmlLines;
        private List<KmlPolygon> kmlPolygons;

        public List<KmlPoint> getKmlPoints() {
            return kmlPoints;
        }

        public void setKmlPoints(List<KmlPoint> kmlPoints) {
            this.kmlPoints = kmlPoints;
        }

        public List<KmlLine> getKmlLines() {
            return kmlLines;
        }

        public void setKmlLines(List<KmlLine> kmlLines) {
            this.kmlLines = kmlLines;
        }

        public List<KmlPolygon> getKmlPolygons() {
            return kmlPolygons;
        }

        public void setKmlPolygons(List<KmlPolygon> kmlPolygons) {
            this.kmlPolygons = kmlPolygons;
        }

        @Override
        public String toString() {
            return "KmlProperty{" +
                    "kmlPoints=" + kmlPoints +
                    ", kmlLines=" + kmlLines +
                    ", kmlPolygons=" + kmlPolygons +
                    '}';
        }
    }

    public static void main(String[] args) {
        File file = new File("C:\\Users\\Administrator\\Desktop\\长岭10kv集电线路杆塔航线\\二号起降点\\10kv长岭89-90.kmz");
        File unzip = ZipUtil.unzip(file);
        File[] files = unzip.listFiles();
        for (File f: files) {
            if(f.isFile()){
                if(f.getName().endsWith("wpml")){
                    // 开始解析
                }
            }else{

            }

        }


        System.out.println(unzip);
//        File file1 = new File("C:\\Users\\Administrator\\Desktop\\长岭10kv集电线路杆塔航线\\二号起降点\\10kv长岭89-90\\wpmz\\waylines.wpml");
//
//        KmlProperty kmlProperty = parseKmlForJAK(file1);
//        if (kmlProperty.getKmlPoints().size() > 0) {
//            System.out.println("点");
//            for (KmlPoint k : kmlProperty.getKmlPoints()) {
//                System.out.println(k.getPoints());
//            }
//
//        }

    }



}


  • 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
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/928264
推荐阅读
相关标签
  

闽ICP备14008679号