Permalink
Browse files

Adds custom geonet kml loader

  • Loading branch information...
1 parent 4954c38 commit 1a1c73f1bce01452d1082908f652c7e122fd086a @malclocke committed Sep 8, 2010
Showing with 4,534 additions and 1 deletion.
  1. +7 −1 index.html
  2. +92 −0 loaders/flickr.js
  3. +254 −0 loaders/geonetkml.js
  4. +210 −0 loaders/georss.js
  5. +153 −0 loaders/google_spreadsheet.js
  6. +120 −0 loaders/json.js
  7. +233 −0 loaders/kml.js
  8. +111 −0 loaders/metaweb.js
  9. +197 −0 loaders/progressive.js
  10. +71 −0 loaders/xml.js
  11. +274 −0 param.js
  12. +2,812 −0 timemap.js
View
@@ -6,7 +6,13 @@
<title>Darfield earthquake timeline</title>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAA69DfOEx1y1daALUHQUXAXBTTo2ugwXF-rwj8SU4JkiH73zqXbRQctklcY5DNaxz_swtlbgPjP3YmXg" type="text/javascript"></script>
<script type="text/javascript" src="http://static.simile.mit.edu/timeline/api/timeline-api.js"></script>
+ <!--
<script src="timemap_full.pack.js" type="text/javascript"></script>
+ -->
+ <script src="timemap.js" type="text/javascript"></script>
+ <script src="param.js" type="text/javascript"></script>
+ <script src="loaders/xml.js" type="text/javascript"></script>
+ <script src="loaders/geonetkml.js" type="text/javascript"></script>
<script type="text/javascript">
var tm;
@@ -25,7 +31,7 @@
{
title: "Darfield Earthquake",
theme: "red",
- type: "kml", // Data to be loaded in KML - must be a local URL
+ type: "geonetkml",
options: {
url: "earthquakes.kml",
extendedData: ["date","z","mag","info"],
View
@@ -0,0 +1,92 @@
+/*
+ * Timemap.js Copyright 2010 Nick Rabinowitz.
+ * Licensed under the MIT License (see LICENSE.txt)
+ */
+
+/**
+ * @fileOverview
+ * Flickr Loader
+ *
+ * @author Nick Rabinowitz (www.nickrabinowitz.com)
+ */
+
+// for JSLint
+/*global TimeMap */
+
+/**
+ * @class
+ * Flickr loader: Load JSONP data from Flickr.
+ *
+ * <p>This is a loader for Flickr data. You probably want to use it with a
+ * URL for the Flickr Geo Feed API: <a href="http://www.flickr.com/services/feeds/geo/">http://www.flickr.com/services/feeds/geo/</a></p>
+ *
+ * <p>The loader takes a full URL, minus the JSONP callback function.</p>
+ *
+ * @augments TimeMap.loaders.jsonp
+ * @requires loaders/json.js
+ *
+ * @example
+TimeMap.init({
+ datasets: [
+ {
+ title: "Flickr Dataset",
+ type: "flickr",
+ options: {
+ // This is just the latest geotagged photo stream - try adding
+ // an "id" or "tag" or "photoset" parameter to get what you want
+ url: "http://www.flickr.com/services/feeds/geo/?format=json&jsoncallback="
+ }
+ }
+ ],
+ // etc...
+});
+ * @see <a href="../../examples/pathlines.html">Flickr Pathlines Example</a>
+ *
+ * @param {Object} options All options for the loader
+ * @param {String} options.url Full JSONP url of Flickr feed to load
+ * @param {mixed} [options[...]] Other options (see {@link TimeMap.loaders.jsonp})
+ */
+TimeMap.loaders.flickr = function(options) {
+ var loader = new TimeMap.loaders.jsonp(options);
+
+ /**
+ * Preload function for Flickr feeds
+ * @name TimeMap.loaders.flickr#preload
+ * @function
+ * @parameter {Object} data Data to preload
+ * @return {Array} data Array of item data
+ */
+ loader.preload = function(data) {
+ return data["items"];
+ };
+
+ /**
+ * Transform function for Flickr feeds
+ * @name TimeMap.loaders.flickr#transform
+ * @function
+ * @parameter {Object} data Data to transform
+ * @return {Object} data Transformed data for one item
+ */
+ loader.transform = function(data) {
+ var item = {
+ title: data.title,
+ start: data.date_taken,
+ point: {
+ lat: data.latitude,
+ lon: data.longitude
+ },
+ options: {
+ description: data.description
+ .replace(/&gt;/g, ">")
+ .replace(/&lt;/g, "<")
+ .replace(/&quot;/g, '"')
+ }
+ };
+ if (options.transformFunction) {
+ item = options.transformFunction(item);
+ }
+ return item;
+ };
+
+ return loader;
+};
View
@@ -0,0 +1,254 @@
+/*
+ * 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.
+ *
+ * <p>This is a loader class for KML files. Currently supports all geometry
+ * types (point, polyline, polygon, and overlay) and multiple geometries. Supports loading
+ * <a href="http://code.google.com/apis/kml/documentation/extendeddata.html">ExtendedData</a>
+ * through the extendedData parameter.
+ * </p>
+ *
+ * @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 <a href="../../examples/kenya.html">KML Example</a>
+ * @see <a href="../../examples/kml_extendeddata.html">KML ExtendedData Example</a>
+ *
+ * @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.geonetkml = 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.geonetkml.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.geonetkml.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<placemarks.length; i++) {
+ pm = placemarks[i];
+ data = { options: {} };
+ // get title & description
+ data.title = getTagValue(pm, "name");
+ data.options.description = getTagValue(pm, "description");
+ // get time information
+ findNodeTime(pm, data);
+ // find placemark(s)
+ var nList, coords, pmobj;
+ data.placemarks = [];
+ // look for marker
+ nList = getNodeList(pm, "Point");
+ for (j=0; j<nList.length; j++) {
+ pmobj = { point: {} };
+ // get lat/lon
+ coords = getTagValue(nList[j], "coordinates");
+ pmobj.point = makePoint(coords, 1);
+ data.placemarks.push(pmobj);
+ }
+ // look for polylines
+ nList = getNodeList(pm, "LineString");
+ for (j=0; j<nList.length; j++) {
+ pmobj = { polyline: [] };
+ // get lat/lon
+ coords = getTagValue(nList[j], "coordinates");
+ pmobj.polyline = makePoly(coords, 1);
+ data.placemarks.push(pmobj);
+ }
+ // look for polygons
+ nList = getNodeList(pm, "Polygon");
+ for (j=0; j<nList.length; j++) {
+ pmobj = { polygon: [] };
+ // get lat/lon
+ coords = getTagValue(nList[j], "coordinates");
+ pmobj.polygon = makePoly(coords, 1);
+ // XXX: worth closing unclosed polygons?
+ data.placemarks.push(pmobj);
+ }
+ // look for any extra tags and/or ExtendedData specified
+ this.parseExtra(data, pm);
+ // set theme based on style
+ switch (Math.floor(data.options.mag)) {
+ case 7:
+ data.options.theme = 'red';
+ break;
+ case 6:
+ data.options.theme = 'orange';
+ break;
+ case 5:
+ data.options.theme = 'yellow';
+ break;
+ case 4:
+ data.options.theme = 'green';
+ break;
+ case 3:
+ data.options.theme = 'blue';
+ break;
+ default:
+ data.options.theme = 'ltblue';
+ break;
+ }
+
+ items.push(data);
+ }
+
+ // look for ground overlays
+ placemarks = getNodeList(kmlnode, "GroundOverlay");
+ for (i=0; i<placemarks.length; i++) {
+ pm = placemarks[i];
+ data = { options: {}, overlay: {} };
+ // get title & description
+ data.title = getTagValue(pm, "name");
+ data.options.description = getTagValue(pm, "description");
+ // get time information
+ findNodeTime(pm, data);
+ // get image
+ nList = getNodeList(pm, "Icon");
+ data.overlay.image = getTagValue(nList[0], "href");
+ // get coordinates
+ nList = getNodeList(pm, "LatLonBox");
+ data.overlay.north = getTagValue(nList[0], "north");
+ data.overlay.south = getTagValue(nList[0], "south");
+ data.overlay.east = getTagValue(nList[0], "east");
+ data.overlay.west = getTagValue(nList[0], "west");
+ // look for any extra tags and/or ExtendedData specified
+ this.parseExtra(data, pm);
+ items.push(data);
+ }
+
+ // clean up
+ kmlnode = placemarks = pm = nList = null;
+
+ return items;
+};
+
+/**
+ * @class
+ * Class for parameters loaded from KML ExtendedData elements
+ *
+ * @augments TimeMap.params.OptionParam
+ *
+ * @constructor
+ * @param {String} paramName String name of the parameter
+ * @param {String} [tagName] Tag name, if different
+ */
+TimeMap.params.ExtendedDataParam = function(paramName, tagName) {
+ return new TimeMap.params.OptionParam(paramName, {
+
+ /**
+ * Set a config object based on an ExtendedData element
+ * @name TimeMap.params.ExtendedDataParam#setConfigKML
+ * @function
+ *
+ * @param {Object} config Config object to modify
+ * @param {XML NodeList} node Parent node to look for tags in
+ */
+ setConfigXML: function(config, node) {
+ var util = TimeMap.util,
+ nList = util.getNodeList(node, "Data"),
+ i;
+ for (i=0; i<nList.length; i++) {
+ if (nList[i].getAttribute("name") == tagName) {
+ this.setConfig(config, util.getTagValue(nList[i], "value"))
+ }
+ }
+ node = nList = null;
+ },
+
+ sourceName: tagName
+
+ });
+};
Oops, something went wrong.

0 comments on commit 1a1c73f

Please sign in to comment.