• Java解析kml文件


    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<>();
    
    
    }
    既然我已经踏上这条道路,那么,任何东西都不应妨碍我沿着这条路走下去!!!!!!!!!! !!! ! !! ! 个人公众号《后端技术开发之路》,欢迎您关注!

    如果您觉得我写还不过,请打赏下在下吧!【高木子】!

  • 相关阅读:
    Flex通信-Java服务端通信实例
    myeclips破解
    最简单的基于FFmpeg的移动端例子:Android 视频转码器
    Kapacitor行为测试
    mydumper备份原理和使用方法
    csdn课堂学习
    maven 遇到failOnMissingWebXml有关问题解决方法
    Oracle中NVL、NVL2、DECODE函数的用法
    Mybatis 一对多 多对1
    Linux编译ffmpeg
  • 原文地址:https://www.cnblogs.com/gaomanito/p/15409746.html
Copyright © 2020-2023  润新知