Home » Java » Extract coordinates from KML file in Java

Extract coordinates from KML file in Java

Posted by: admin December 28, 2021 Leave a comment

Questions:

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<Feature> 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

Answers:

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

###

With respect to A4L answer , thank you so much while i have added some more extraction methods which will get data from folder and document with point line and polygon extraction with spring MVC using javaapi4kml using package

###

Came across this post, so here is part of the code of function I have been using in my app to extract Place mark name & coordinates from a String kmlText.

###

Thanks @A4L This is really an update and a more groovier way of doing the same So changed for Groovy and also attempts more types than the example given as well digs deep within a document layer:

###

The answers above make use of the library de.micromata.opengis.kml which appears to be unsupported now.
There is a newer library, OSM Bonus Pack which has a good KmlDocument class. I have used this to extract the coords as below. It only extracts lines (roads), not polygons, but it should be modifiable.