Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read and parse KML in java

Tags:

java

parsing

kml

Is there any library available to parse KML ?

like image 690
Evgeny Shepelyuk Avatar asked Jul 16 '09 20:07

Evgeny Shepelyuk


3 Answers

You'll be making your own library, but you won't be writing any code.

I suggest looking at http://code.google.com/apis/kml/documentation/kmlreference.html. From there you can get the XML Schema. Once you've got the schema you can use JAXB to generate an object tree to easily parse and write KML.

This may also be a good resource, looks like someone else has already done it!

like image 130
basszero Avatar answered Oct 04 '22 09:10

basszero


This library looks promising as well:

http://code.google.com/p/javaapiforkml/

The library provides support till now.

like image 26
Ajay Avatar answered Oct 04 '22 08:10

Ajay


Here's my JSOUP implementation hope it helps

public ArrayList<ArrayList<LatLng>> getCoordinateArrays() {
    ArrayList<ArrayList<LatLng>> allTracks = new ArrayList<ArrayList<LatLng>>();

    try {
        StringBuilder buf = new StringBuilder();
        InputStream json = MyApplication.getInstance().getAssets().open("track.kml");
        BufferedReader in = new BufferedReader(new InputStreamReader(json));
        String str;
                      String buffer;
        while ((str = in.readLine()) != null) {
            buf.append(str);
        }

        in.close();
        String html = buf.toString();
        Document doc = Jsoup.parse(html, "", Parser.xmlParser());
        ArrayList<String> tracksString = new ArrayList<String>();
        for (Element e : doc.select("coordinates")) {
            tracksString.add(e.toString().replace("<coordinates>", "").replace("</coordinates>", ""));
        }

        for (int i = 0; i < tracksString.size(); i++) {
            ArrayList<LatLng> oneTrack = new ArrayList<LatLng>();
            ArrayList<String> oneTrackString = new ArrayList<String>(Arrays.asList(tracksString.get(i).split("\\s+")));
            for (int k = 1; k < oneTrackString.size(); k++) {
                LatLng latLng = new LatLng(Double.parseDouble(oneTrackString.get(k).split(",")[0]),
                        Double.parseDouble(oneTrackString.get(k).split(",")[1]));
                oneTrack.add(latLng);
            }
            allTracks.add(oneTrack);
        }}

    } catch (Exception e) {
        e.printStackTrace();
    }
    return allTracks;
}
like image 43
alexandrius Avatar answered Oct 04 '22 09:10

alexandrius