Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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;

}

share|improve this question
    
Can you show us your code? That way it'd be easier to spot errors. –  R.K. Sep 13 '12 at 6:25
    
GroundOverlays in Openlayers is not supported -gis.stackexchange.com/questions/16816/… - use LayerImage instead dev.openlayers.org/releases/OpenLayers-2.12/doc/apidocs/files/… –  Mapperz Sep 13 '12 at 13:34

2 Answers 2

up vote 1 down vote accepted

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...

share|improve this answer
    
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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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