Java将读取Kml文件

参考:https://blog.csdn.net/yzj_xiaoyue/article/details/84889015

maven依赖

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/de.micromata.jak/JavaAPIforKml -->
        <dependency>
            <groupId>de.micromata.jak</groupId>
            <artifactId>JavaAPIforKml</artifactId>
            <version>2.2.0</version>
        </dependency>
    </dependencies>

import de.micromata.opengis.kml.v_2_2_0.Coordinate;
 
import java.util.List;
 
public 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;
    }
}

线

import de.micromata.opengis.kml.v_2_2_0.Coordinate;
 
import java.util.List;
 
public 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;
    }
}

import de.micromata.opengis.kml.v_2_2_0.Coordinate;
 
import java.util.List;
 
/**
 * @program: ParseKMLForJava
 * @description:
 * @author: Mr.Yue
 * @create: 2018-12-04 21:12
 **/
public 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;
    }
}

封装类KmlProperty :用于获取点线面。

import java.util.List;
 
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;
    }
}

KML文件解析方法

import de.micromata.opengis.kml.v_2_2_0.*;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @program: my_project
 * @description: KML文件解析:先获取kml文件的根节点,依次遍历当前节点的子节点的信息,
 * 如果遇到节点属于Folder、Document则继续解析其子节点;反之则解析PlaceMark节点(主要解析LineString、Point、Polygon)。
 * @author: Yue
 * @create: 2018-12-01 12:39
 **/
public class ParsingKmlUtil {
    //以下三行都是自定义的KML类,用于获取名称name、所有点points、样式颜色color
    private List<KmlPoint> kmlPointList = new ArrayList<>();
    private List<KmlLine> kmlLineList = new ArrayList<>();
    private List<KmlPolygon> kmlPolygonList = new ArrayList<>();
    private KmlProperty kmlProperty = new KmlProperty();
 
    /**
     * 保存kml数据到临时表
     *
     * @param file 上传的文件实体
     * @return 自定义的KML文件实体
     */
    public KmlProperty parseKmlForJAK(File file) {
        Kml kml = Kml.unmarshal(file);
        Feature feature = kml.getFeature();
        parseFeature(feature);
        kmlProperty.setKmlPoints(kmlPointList);
        kmlProperty.setKmlLines(kmlLineList);
        kmlProperty.setKmlPolygons(kmlPolygonList);
        return kmlProperty;
    }
 
    /**
     * 解析kml节点信息
     *
     * @param feature 需要解析到要素信息
     * @return
     */
    private void parseFeature(Feature feature) {
        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);
                            } else {
                                parseFeature(documentFeature);
                            }
                        }
                );
            } else if (feature instanceof Folder) {
                //原理同上
                List<Feature> featureList = ((Folder) feature).getFeature();
                featureList.forEach(documentFeature -> {
                            if (documentFeature instanceof Placemark) {
                                getPlaceMark((Placemark) documentFeature);
                            }
                            {
                                parseFeature(documentFeature);
                            }
                        }
                );
            }
        }
    }
 
    /**
     * 解析PlaceMark节点下的信息
     *
     * @return
     */
    private void getPlaceMark(Placemark placemark) {
        Geometry geometry = placemark.getGeometry();
        String name = placemark.getName();
        parseGeometry(name, geometry);
    }
 
    /**
     * 解析PlaceMark节点下的信息
     *
     * @return
     */
    private void parseGeometry(String name, Geometry geometry) {
        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(kmlPolygonList, name, outerBoundaryIs);
                        }
                    }
                }
            } else if (geometry instanceof LineString) {
                LineString lineString = (LineString) geometry;
                List<Coordinate> coordinates = lineString.getCoordinates();
                if (coordinates != null) {
                    coordinates = ((LineString) geometry).getCoordinates();
                    addLineStringToList(kmlLineList, coordinates, name);
                }
            } else if (geometry instanceof Point) {
                Point point = (Point) geometry;
                List<Coordinate> coordinates = point.getCoordinates();
                if (coordinates != null) {
                    coordinates = ((Point) geometry).getCoordinates();
                    addPointToList(kmlPointList, 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(kmlPointList, coordinates, name);
                    } else if (geometryToMult instanceof LineString) {
                        coordinates = ((LineString) geometryToMult).getCoordinates();
                        addLineStringToList(kmlLineList, coordinates, name);
                    } else if (geometryToMult instanceof Polygon) {
                        outerBoundaryIs = ((Polygon) geometryToMult).getOuterBoundaryIs();
                        addPolygonToList(kmlPolygonList, name, outerBoundaryIs);
                    }
                }
            }
        }
    }
 
    /**
     * 将kml中所有面添加到一个list
     *
     * @return
     */
    private 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 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 void addPointToList(List<KmlPoint> kmlPointList, List<Coordinate> coordinates, String name) {
        KmlPoint kmlPoint = new KmlPoint();
        kmlPoint.setName(name);
        kmlPoint.setPoints(coordinates);
        kmlPointList.add(kmlPoint);
    }
}

KML解析方法调用

import java.io.File;
public class Main {
    public static void main(String[] args) {
        KmlProperty kmlProperty;
        ParsingKmlUtil parsingKmlUtil =new ParsingKmlUtil();
        File file = new File("src/point.kml");
        kmlProperty = parsingKmlUtil.parseKmlForJAK(file);
        assert kmlProperty != null;
 
        if (kmlProperty.getKmlPoints().size() > 0) {
            for (KmlPoint k : kmlProperty.getKmlPoints()) {
                System.out.println(k.getName());
            }
            System.out.println("点");
        }
        if (kmlProperty.getKmlLines().size() > 0) {
            for (KmlLine k : kmlProperty.getKmlLines()) {
                System.out.println(k.getName());
            }
            System.out.println("线");
        }
        if (kmlProperty.getKmlPoints().size() > 0) {
            for (KmlPoint k : kmlProperty.getKmlPoints()) {
                System.out.println(k.getPoints());
            }
            System.out.println("面");
        }
    }
}

自己功能设置

    /**
     * 解析kml节点信息
     *
     * @param feature 需要解析到要素信息
     * @return
     */
    private void parseFeature(Feature feature) {
        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) {
                                // feature.getName() 获取省市区乡镇
                                if (!feature.getName().equals("乡镇")) {
                                    if (parentName != null && parentName != ""){
                                        parentName = parentName + "-" ;
                                    }
                                    parentName = parentName + documentFeature.getName();
                                    sonName=parentName;
                                }else {
                                    sonName = parentName + "-" + documentFeature.getName();

                                }
                                documentFeature.setName(sonName);
                                getPlaceMark((Placemark) documentFeature);
                            } else {
                                parseFeature(documentFeature);
                            }
                        }
                );
            } else if (feature instanceof Folder) {
                //原理同上
                List<Feature> featureList = ((Folder) feature).getFeature();
                featureList.forEach(documentFeature -> {
                            if (documentFeature instanceof Placemark) {
                                getPlaceMark((Placemark) documentFeature);
                            }
                            {
                                parseFeature(documentFeature);
                            }
                        }
                );
            }
        }
    }
posted @ 2021-05-19 10:37  邹笑傲  阅读(1360)  评论(0编辑  收藏  举报