1 | /* |
---|
2 | * Timemap.js Copyright 2008 Nick Rabinowitz. |
---|
3 | * Licensed under the MIT License (see LICENSE.txt) |
---|
4 | */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @fileOverview |
---|
8 | * KML Loader |
---|
9 | * |
---|
10 | * @author Nick Rabinowitz (www.nickrabinowitz.com) |
---|
11 | */ |
---|
12 | |
---|
13 | /*globals GXml, TimeMap */ |
---|
14 | |
---|
15 | /** |
---|
16 | * @class |
---|
17 | * KML loader factory - inherits from remote loader |
---|
18 | * |
---|
19 | * <p>This is a loader class for KML files. Currently supports all geometry |
---|
20 | * types (point, polyline, polygon, and overlay) and multiple geometries.</p> |
---|
21 | * |
---|
22 | * @augments TimeMap.loaders.remote |
---|
23 | * @borrows TimeMap.loaders.kml.parse as #parse |
---|
24 | * |
---|
25 | * @example Usage in TimeMap.init(): |
---|
26 | |
---|
27 | datasets: [ |
---|
28 | { |
---|
29 | title: "KML Dataset", |
---|
30 | type: "kml", |
---|
31 | options: { |
---|
32 | url: "mydata.kml" // Must be local |
---|
33 | } |
---|
34 | } |
---|
35 | ] |
---|
36 | * |
---|
37 | * @param {Object} options All options for the loader:<pre> |
---|
38 | * {Array} url URL of KML file to load (NB: must be local address) |
---|
39 | * {Function} preloadFunction Function to call on data before loading |
---|
40 | * {Function} transformFunction Function to call on individual items before loading |
---|
41 | * </pre> |
---|
42 | * @return {TimeMap.loaders.remote} Remote loader configured for KML |
---|
43 | */ |
---|
44 | TimeMap.loaders.kml = function(options) { |
---|
45 | var loader = new TimeMap.loaders.remote(options); |
---|
46 | loader.parse = TimeMap.loaders.kml.parse; |
---|
47 | return loader; |
---|
48 | }; |
---|
49 | |
---|
50 | /** |
---|
51 | * Static function to parse KML with time data. |
---|
52 | * |
---|
53 | * @param {XML string} kml KML to be parsed |
---|
54 | * @return {TimeMapItem Array} Array of TimeMapItems |
---|
55 | */ |
---|
56 | TimeMap.loaders.kml.parse = function(kml) { |
---|
57 | var items = [], data, kmlnode, placemarks, pm, i, j; |
---|
58 | kmlnode = GXml.parse(kml); |
---|
59 | |
---|
60 | // get TimeMap utilty functions |
---|
61 | // assigning to variables should compress better |
---|
62 | var util = TimeMap.util; |
---|
63 | var getTagValue = util.getTagValue, |
---|
64 | getNodeList = util.getNodeList, |
---|
65 | makePoint = util.makePoint, |
---|
66 | makePoly = util.makePoly, |
---|
67 | formatDate = util.formatDate; |
---|
68 | |
---|
69 | // recursive time data search |
---|
70 | var findNodeTime = function(n, data) { |
---|
71 | var check = false; |
---|
72 | // look for instant timestamp |
---|
73 | var nList = getNodeList(n, "TimeStamp"); |
---|
74 | if (nList.length > 0) { |
---|
75 | data.start = getTagValue(nList[0], "when"); |
---|
76 | check = true; |
---|
77 | } |
---|
78 | // otherwise look for span |
---|
79 | else { |
---|
80 | nList = getNodeList(n, "TimeSpan"); |
---|
81 | if (nList.length > 0) { |
---|
82 | data.start = getTagValue(nList[0], "begin"); |
---|
83 | data.end = getTagValue(nList[0], "end") || |
---|
84 | // unbounded spans end at the present time |
---|
85 | formatDate(new Date()); |
---|
86 | check = true; |
---|
87 | } |
---|
88 | } |
---|
89 | // try looking recursively at parent nodes |
---|
90 | if (!check) { |
---|
91 | var pn = n.parentNode; |
---|
92 | if (pn.nodeName == "Folder" || pn.nodeName=="Document") { |
---|
93 | findNodeTime(pn, data); |
---|
94 | } |
---|
95 | pn = null; |
---|
96 | } |
---|
97 | }; |
---|
98 | |
---|
99 | // look for placemarks |
---|
100 | placemarks = getNodeList(kmlnode, "Placemark"); |
---|
101 | for (i=0; i<placemarks.length; i++) { |
---|
102 | pm = placemarks[i]; |
---|
103 | data = { options: {} }; |
---|
104 | // get title & description |
---|
105 | data.title = getTagValue(pm, "name"); |
---|
106 | data.options.description = getTagValue(pm, "description"); |
---|
107 | // get time information |
---|
108 | findNodeTime(pm, data); |
---|
109 | // find placemark(s) |
---|
110 | var nList, coords, pmobj; |
---|
111 | data.placemarks = []; |
---|
112 | // look for marker |
---|
113 | nList = getNodeList(pm, "Point"); |
---|
114 | for (j=0; j<nList.length; j++) { |
---|
115 | pmobj = { point: {} }; |
---|
116 | // get lat/lon |
---|
117 | coords = getTagValue(nList[j], "coordinates"); |
---|
118 | pmobj.point = makePoint(coords, 1); |
---|
119 | data.placemarks.push(pmobj); |
---|
120 | } |
---|
121 | // look for polylines |
---|
122 | nList = getNodeList(pm, "LineString"); |
---|
123 | for (j=0; j<nList.length; j++) { |
---|
124 | pmobj = { polyline: [] }; |
---|
125 | // get lat/lon |
---|
126 | coords = getTagValue(nList[j], "coordinates"); |
---|
127 | pmobj.polyline = makePoly(coords, 1); |
---|
128 | data.placemarks.push(pmobj); |
---|
129 | } |
---|
130 | // look for polygons |
---|
131 | nList = getNodeList(pm, "Polygon"); |
---|
132 | for (j=0; j<nList.length; j++) { |
---|
133 | pmobj = { polygon: [] }; |
---|
134 | // get lat/lon |
---|
135 | coords = getTagValue(nList[j], "coordinates"); |
---|
136 | pmobj.polygon = makePoly(coords, 1); |
---|
137 | // XXX: worth closing unclosed polygons? |
---|
138 | data.placemarks.push(pmobj); |
---|
139 | } |
---|
140 | items.push(data); |
---|
141 | } |
---|
142 | |
---|
143 | // look for ground overlays |
---|
144 | placemarks = getNodeList(kmlnode, "GroundOverlay"); |
---|
145 | for (i=0; i<placemarks.length; i++) { |
---|
146 | pm = placemarks[i]; |
---|
147 | data = { options: {}, overlay: {} }; |
---|
148 | // get title & description |
---|
149 | data.title = getTagValue(pm, "name"); |
---|
150 | data.options.description = getTagValue(pm, "description"); |
---|
151 | // get time information |
---|
152 | findNodeTime(pm, data); |
---|
153 | // get image |
---|
154 | nList = getNodeList(pm, "Icon"); |
---|
155 | data.overlay.image = getTagValue(nList[0], "href"); |
---|
156 | // get coordinates |
---|
157 | nList = getNodeList(pm, "LatLonBox"); |
---|
158 | data.overlay.north = getTagValue(nList[0], "north"); |
---|
159 | data.overlay.south = getTagValue(nList[0], "south"); |
---|
160 | data.overlay.east = getTagValue(nList[0], "east"); |
---|
161 | data.overlay.west = getTagValue(nList[0], "west"); |
---|
162 | items.push(data); |
---|
163 | } |
---|
164 | |
---|
165 | // clean up |
---|
166 | kmlnode = null; |
---|
167 | placemarks = null; |
---|
168 | pm = null; |
---|
169 | nList = null; |
---|
170 | return items; |
---|
171 | }; |
---|