Search code examples
javaparsingcoordinateskmljak

Extract coordinates from KML file in Java


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


Solution

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