KML to CSV or JSON help

Sayth

Gawd
Joined
Oct 7, 2001
Messages
618
Hi all!

I'd like to know what would be the best way to parse an kml for coordinates, pull them out and place them in a .json file. I'm running Ubuntu 10.11.

Scripting? Perl? What do you think?

idea is:

Read KML file, parse out the coordinates of th first point, count how many times that point appears in the file and output in the following sequence without spaces:

[["FilenameWihoutExt",[Lat,Long,#OccurencesDivededBy10,000,Lat,Long,#OccurencesDivededBy10,000,Lat,Long,#OccurencesDivededBy10,000,]]]


Code:
KML sample:

<Folder>

<name>Placemarks</name>

<Placemark>

	<name>Ireland</name>

	<description>Trip to Ireland</description>

	<Point>

		<coordinates>-73.5833,45.5</coordinates>

	</Point>

</Placemark>

and then output to a text file such as:

Code:
[["UnitedKingodom",[-73.5833,45.5,0.001,-122.083469,37.42222,0.080,-122.084075,37.4210039,0.050,-76.488393,44.33,003]]]

This is for my 3D earth. An experimental webgl project by Chrome. http://www.chromeexperiments.com/globe

Thanks in advance!
 
Based on an xml file of:

UnitedKindom.kml
Code:
<Folder>

<name>Placemarks</name>

<Placemark>

	<name>Ireland</name>

	<description>Trip to Ireland</description>

	<Point>

		<coordinates>-73.5833,45.5</coordinates>

	</Point>

</Placemark>

<Placemark>

	<name>Ireland</name>

	<description>Trip to Ireland</description>

	<Point>

		<coordinates>-73.5833,45.5</coordinates>

	</Point>

</Placemark>

<Placemark>

	<name>Dummy</name>

	<description>Trip to Dummy</description>

	<Point>

		<coordinates>-22.5833,32.5</coordinates>

	</Point>

</Placemark>

</Folder>

on my desktop, I can load:

kml_to_txt.html
Code:
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title></title>
        <script>
            window.addEventListener("DOMContentLoaded", function() {
                var req = new XMLHttpRequest();
                var filename = "UnitedKingdom";
                var ext = ".kml";
                req.onreadystatechange = function() {
                    if (this.readyState === 4) {
                        var doc = this.responseXML;
                        if (doc && doc.documentElement && doc.documentElement.nodeName === "Folder") {
                            var placemarks = doc.getElementsByTagName("Placemark");
                            var coord_count = {};
                            for (var i = 0; i < placemarks.length; ++i) {
                                var place = placemarks[i].getElementsByTagName("name")[0];
                                var point = placemarks[i].getElementsByTagName("Point")[0];
                                var coords = point.getElementsByTagName("coordinates")[0].textContent;
                                if (!coord_count.hasOwnProperty(coords)) {
                                    coord_count[coords] = {count : 1};
                                } else {
                                    coord_count[coords].count += 1;
                                }
                            }
                            var output = '[["' + filename +'",[';
                            var first = true;
                            for (var i in coord_count) {
                                if (coord_count.hasOwnProperty(i)) {
                                    if (!first) {
                                        output += ",";
                                    }
                                    var count = coord_count[i].count;
                                    var latlong = i.split(",");
                                    var lat = latlong[0];
                                    var lon = latlong[1];
                                    output += lat;
                                    output += ",";
                                    output += lon;
                                    output += ",";
                                    output += count / 10000;
                                    first = false;
                                }
                            }
                            output += "]]]";
                            var file = "data:text/plain;charset=utf-8,";
                            file += encodeURIComponent(output);
                            var link = document.createElement("a");
                            link.textContent = "open generated text file";
                            link.href = file;
                            link.target = "_blank";
                            document.body.appendChild(link);
                        }
                    }
                };
                req.open("GET", filename + ext, true);
                req.send();
            }, false);
        </script>
    </head>
    <body>
    
    </body>
</html>

on my desktop in Opera (start chrome with --allow-file-access-from-files I guess) and get:

Code:
[["UnitedKingdom",[-73.5833,45.5,0.0002,-22.5833,32.5,0.0001]]]

I don't know anything about kml files or 3D earth and if they're even XML files (wasn't sure from your sample and wasn't sure if Folder is the root element or not (didn't see an end tag for it)).

Also not sure if I interpreted what you want correctly. But, even if that's not right, that should give you an idea that you can adapt to any language that has an XMLParser with DOM capability (Elements and textContent at least). Also, I made use of a JS object as an associative array and used the coordinate text at the name of each element in it. If that doesn't store them in the order you want (if order matters), then you'd have to use a regular array.

You'd of course have to add error handling for getElementsByTagName etc. if the kml files might have errors. Also, if the textContent for the coordinates element might have leading and trailing white-space, you'll have to trim that white-space.
 
Back
Top