/* * Timemap.js Copyright 2010 Nick Rabinowitz. * Licensed under the MIT License (see LICENSE.txt) */ /** * @fileOverview * KML Loader * * @author Nick Rabinowitz (www.nickrabinowitz.com) */ /*globals GXml, TimeMap */ /** * @class * KML loader: load KML files. * *
This is a loader class for KML files. Currently supports all geometry * types (point, polyline, polygon, and overlay) and multiple geometries. Supports loading * ExtendedData * through the extendedData parameter. *
* * @augments TimeMap.loaders.xml * @requires loaders/xml.js * @requires param.js * @borrows TimeMap.loaders.kml.parse as #parse * * @example TimeMap.init({ datasets: [ { title: "KML Dataset", type: "kml", options: { url: "mydata.kml" // Must be local } } ], // etc... }); * @see KML Example * @see KML ExtendedData Example * * @param {Object} options All options for the loader * @param {String} options.url URL of KML file to load (NB: must be local address) * @param {String[]} [options.extendedData] Array of names for ExtendedData data elements * @param {mixed} [options[...]] Other options (see {@link TimeMap.loaders.xml}) * @return {TimeMap.loaders.xml} Loader configured for KML */ TimeMap.loaders.kml = function(options) { var loader = new TimeMap.loaders.xml(options), tagMap = options.tagMap || {}, extendedData = options.extendedData || [], tagName, x; // Add ExtendedData parameters to extra params for (x=0; x < extendedData.length; x++) { tagName = extendedData[x]; loader.extraParams.push( new TimeMap.params.ExtendedDataParam(tagMap[tagName] || tagName, tagName) ); } // set custom parser loader.parse = TimeMap.loaders.kml.parse; return loader; }; /** * Static function to parse KML with time data. * * @param {XML string} kml KML to be parsed * @return {TimeMapItem Array} Array of TimeMapItems */ TimeMap.loaders.kml.parse = function(kml) { var items = [], data, kmlnode, placemarks, pm, i, j; kmlnode = GXml.parse(kml); // get TimeMap utilty functions // assigning to variables should compress better var util = TimeMap.util, getTagValue = util.getTagValue, getNodeList = util.getNodeList, makePoint = util.makePoint, makePoly = util.makePoly, formatDate = util.formatDate; // recursive time data search var findNodeTime = function(n, data) { var check = false; // look for instant timestamp var nList = getNodeList(n, "TimeStamp"); if (nList.length > 0) { data.start = getTagValue(nList[0], "when"); check = true; } // otherwise look for span else { nList = getNodeList(n, "TimeSpan"); if (nList.length > 0) { data.start = getTagValue(nList[0], "begin"); data.end = getTagValue(nList[0], "end") || // unbounded spans end at the present time formatDate(new Date()); check = true; } } // try looking recursively at parent nodes if (!check) { var pn = n.parentNode; if (pn.nodeName == "Folder" || pn.nodeName=="Document") { findNodeTime(pn, data); } pn = null; } }; // look for placemarks placemarks = getNodeList(kmlnode, "Placemark"); for (i=0; i