java kml,从Java中的KML文件中提取坐标

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.

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 listafeatures = document.getFeature();

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

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

解决方案

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

Here is how it looks like in code:

@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 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 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("");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值