2

I have found some info on here regarding opening a KML file (with groundoverlay) from a url in Openlayers. In order to open a KML with groundoverlay, it must first be parsed. However, after I GET, I am getting back a downloadable KML file. From the response in FireBug, I just see a bunch of giberish as the response! I want the KML(XML) so I can get the info out of it. Does anyone know exactly how to parse a downloadable kml from a url? Thanks!

Here is my code below. The url takes me to a kml that I can see in Google Earth. However, the response I get back is just a bunch of symbols according to Firebug, when I send as a GET. Any idea what I am doing wrong here to get the response in xml format?

      kmlURL='http://capstonedev/cgi-bin/proxy.cgi?url=http%3A//10.60.26.64%3A8080/viewshed.kml%3FREQUEST%3DExecute%26DataSource%3DSRTM3%26Operation%3DLegionViewshedOperation%26Parameters%3DobserverCoord%3A-113+42%2CouterRadius%3A40000%26FORMAT%3DKML';
OpenLayers.Request.GET({
    url: kmlURL
    ,
    callback: function(request) {
        if(request.status==200)
           alert(kmlGroundOverlayParse(request.responseText));
        else
        {   alert(request.status + "::" + request.responseText);
            console.log(request.status + "::" + request.responseText);
        }
    }
})









    function kmlGroundOverlayParse(strKML)
    {
    var NL = "\r\n";
    var parserXML = new OpenLayers.Format.XML();
    try {
    objKMLxml = parserXML.read(strKML);
    } catch(err) {
    console.log(err.message);
    //alert(err.message);
    return;
    }

    var Msg="";
    var objLayerList = parserXML.getElementsByTagNameNS(objKMLxml, "*", "GroundOverlay");

    for(var i=0;i<objLayerList.length;i++)
    {

    var derName = parserXML.getElementsByTagNameNS(objLayerList[i], "*", "name");

    var dasIcon = parserXML.getElementsByTagNameNS(objLayerList[i], "*", "Icon");
    var dieHREF = parserXML.getElementsByTagNameNS(dasIcon[0], "*", "href");

    var dieBox = parserXML.getElementsByTagNameNS(objLayerList[i], "*", "LatLonBox");
    var derNorden = parserXML.getElementsByTagNameNS(dieBox [0], "*", "north");
    var derWesten = parserXML.getElementsByTagNameNS(dieBox [0], "*", "west");
    var derSueden = parserXML.getElementsByTagNameNS(dieBox [0], "*", "south");
    var derOsten  = parserXML.getElementsByTagNameNS(dieBox [0], "*", "east");

    Msg += "name:" + derName[0].firstChild.nodeValue + ", ";
    Msg += "url:" + dieHREF[0].firstChild.nodeValue.substr(0,20) + ", ";
    Msg += "BOX:" + derWesten[0].firstChild.nodeValue + ", ";
    Msg += derSueden[0].firstChild.nodeValue + ", ";
    Msg += derOsten[0].firstChild.nodeValue + ", ";
    Msg += derNorden[0].firstChild.nodeValue + ", ";

    Msg += NL;

    var title = derName[0].firstChild.nodeValue;
    var url = dieHREF[0].firstChild.nodeValue;
    var projection = "EPSG:4326";

    var ptmin = [derWesten[0].firstChild.nodeValue,derSueden[0].firstChild.nodeValue];
    var ptmax = [derOsten[0].firstChild.nodeValue,derNorden[0].firstChild.nodeValue];

    var bounds = ptmin[0] + "," + ptmin[1] + "," + ptmax[0] + "," + ptmax[1];
    var size = "100,100"; //uninteressant, notwendig für addLayer??

    addKMLGroundOverlay(i, title, url, projection, bounds, size);
}

Msg += objLayerList.length;
parseMsg = Msg;

}

1

if u use django or python on server side, u can use pykml(Python KML library) here.

dont forget to check out tutorials here about pykml.

from lxml import etree
from pykml.parser import Schema
from pykml.factory import KML_ElementMaker as KML
from pykml.factory import GX_ElementMaker as GX
....

i hope it helps you...

  • A coworker was able to make Grails specify the xml format I needed.. Guess I need to study up on server side a bit more! Thanks – KyleK Sep 14 '12 at 12:32
2

I've never tried to parse a KML, just a geojson file. You may want to convert the KML to a geojson file first. Plus json files are smaller and easier to parse. I got some help here with looping over the geojson file. I'd use the ogr2ogr tool to convert the file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.