从Java中的KML文件中提取坐标 [英] Extract coordinates from KML file in Java

查看:1668
本文介绍了从Java中的KML文件中提取坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java解析Kml文件。因为我需要获取地标的坐标,在java中生成一个poligon,然后使用它。

I'm trying to parse a Kml file in Java. Cause I need to take the coordinates of a Placemark, to generate a poligon in java, and use it.

但我的问题是,我正在使用 JAK 此库解析它,我无法提取我想要的信息。(I阅读官方页面中的帮助,但我没有找到任何帮助来解决我的问题)

But my problem , is that i'm using JAK this library to parse it, and i'm not able to extract the information that i want.(I read the "help" in the official page, but I didn't found any help abut my problem)

我正在尝试做类似的事情:

I'm trying to do something like that:

final Kml kml = Kml.unmarshal(new File("C:/Users/A556520/Documents/Proyectos/GeoFencing/res/labasa.kml"));
final Document document = (Document)kml.getFeature();       
List<Feature> listafeatures = document.getFeature();        

但在这一点上我不知道如何提取坐标。

But in this point I don't know how to extract the coordinates.

我正在尝试解析的文件是: la basa

The file I'm trying to parse is this one: la basa

推荐答案

关注 javadocs 非官方)你需要检查 - 使用 instanceof - 每个功能是否是地标,如果是,则投放到它并获得 Geometry 本身需要要检查它是否是 Polygon ,如果是,则转换为它。之后,坐标的路径如下(就像它在kml文件中一样):

Following the javadocs (unofficial) you need to check - using instanceof - each Feature whether is is a Placemark, if yes cast to it and get the Geometry which itself needs to be checked whether it is a Polygon, if yes then cast to it. After that the path to the coordinates is the following (just as it come in the kml-file):

getOuterBoundaryIs > getlinearRing > getCoordinates

以下是代码中的样子:

@Test
public void parseKml() {
    String src = "misctests/stackoverflow/kml/labasa.kml";
    try(InputStream is = getClass().getClassLoader().getResourceAsStream(src)) {
        Assert.assertNotNull(is);
        Kml kml = Kml.unmarshal(is);
        Feature feature = kml.getFeature();
        parseFeature(feature);
    }
}

private void parseFeature(Feature feature) {
    if(feature != null) {
        if(feature instanceof Document) {
            Document document = (Document) feature;
            List<Feature> featureList = document.getFeature();
            for(Feature documentFeature : featureList) {
                if(documentFeature instanceof Placemark) {
                    Placemark placemark = (Placemark) documentFeature;
                    Geometry geometry = placemark.getGeometry();
                    parseGeometry(geometry);
                }
            }
        }
    }
}

private void parseGeometry(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) {
                        for(Coordinate coordinate : coordinates) {
                            parseCoordinate(coordinate);
                        }
                    }
                }
            }
        }
    }
}

private void parseCoordinate(Coordinate coordinate) {
    if(coordinate != null) {
        System.out.println("Longitude: " +  coordinate.getLongitude());
        System.out.println("Latitude : " +  coordinate.getLatitude());
        System.out.println("Altitude : " +  coordinate.getAltitude());
        System.out.println("");
    }
}

这篇关于从Java中的KML文件中提取坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆