Web Development

Parsing KML in Java using JavaApiForKml.jar

KML(Keyhole markup language)

KML(Keyhole markup language) is a file format used to display geographic data in an Earth browser such as Google Earth, Google Maps, and Google Maps for mobile. KML uses a tag-based structure with nested elements and attributes and is based on the XML standard. All tags are case-sensitive and must be appear exactly as they are listed in the KML Reference. The Reference indicates which tags are optional. Within a given element, tags must appear in the order shown in the Reference.(From google doc: https://developers.google.com/kml/documentation/kml_tut)

Sample kml file:

<?xml version="1.0"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<name>Images from 2012-04-19T03:00:00Z UTC to 2012-04-19T03:00:00Z UTC</name>
<open>1</open>
<Folder>
<name>Channel 1</name>
<GroundOverlay>
<LookAt>
<heading>0</heading>
<latitude>10.00</latitude>
<longitude>96.00</longitude>
<tilt>0</tilt>
</LookAt>
<name>2012-04-19T03:00:00Z</name>
<visibility>0</visibility>
<Icon>
<href>http://tt.jpg</href>
</Icon>
<LatLonBox>
<north>25</north>
<south>-5</south>
<east>116</east>
<west>76</west>
</LatLonBox>
<TimeSpan>
<begin>2012-04-19T02:00:00Z</begin>
<end>2012-04-19T03:00:00Z</end>
</TimeSpan>
</GroundOverlay>
</Folder>
</Document>

Parsing KML using JavaApiForKml:
1. Download JavaApiForKml.jar from: http://code.google.com/p/javaapiforkml/
2. Add JavaApiForKml.jar in your java project
3. Check out following code to parsing KML

package com.kml;

import de.micromata.opengis.kml.v_2_2_0.Document;
import de.micromata.opengis.kml.v_2_2_0.Feature;
import de.micromata.opengis.kml.v_2_2_0.Folder;
import de.micromata.opengis.kml.v_2_2_0.GroundOverlay;
import de.micromata.opengis.kml.v_2_2_0.Kml;
import de.micromata.opengis.kml.v_2_2_0.LatLonBox;
import de.micromata.opengis.kml.v_2_2_0.LookAt;
import java.io.File;
import java.util.List;

/**
 *
 * @author Shaiful Isalam(palash)
 */
public class KMLTest {

    public static void main(String[] args) {
        System.out.println("This is KML test");
        final Kml kml = Kml.unmarshal(new File("F:\\Custom.kml"));
        final Document document = (Document)kml.getFeature();
        System.out.println(document.getName());
        List<Feature> t = document.getFeature();
        for(Object o : t){
            Folder f = (Folder)o;
            List<Feature> tg = f.getFeature();
            for(Object ftg : tg){
                GroundOverlay g = (GroundOverlay) ftg;
                LatLonBox l = g.getLatLonBox();
                System.out.println(l.getNorth() );
                LookAt lk = (LookAt)g.getAbstractView();
                System.out.println(lk.getLatitude());                
            }
        }
        

    }
}
Advertisements

4 thoughts on “Parsing KML in Java using JavaApiForKml.jar

  1. Hi sorry, i’m having problem with the code. i need to parse kml file and take of all the placemarks. but this code gives me a lot of Exceptions like this
    “at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:512)”
    what could i do?Could you help me pleese?

    Tks a lot =)

  2. I run the example and give me this error
    This is KML test
    null
    Exception in thread “main” java.lang.ClassCastException: de.micromata.opengis.kml.v_2_2_0.Placemark cannot be cast to de.micromata.opengis.kml.v_2_2_0.Folder
    at KMLTest.main(KMLTest.java:25)

    1. Exception in thread “main” java.lang.ClassCastException: de.micromata.opengis.kml.v_2_2_0.Placemark cannot be cast to de.micromata.opengis.kml.v_2_2_0.Folder
      at KMLTest.main(KMLTest.java:25)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s