Mercurial > hg > LGMap
annotate geotemco/js/GeoTemConfig.js @ 23:d864c58ae667
For avoiding ssl warning, changing the link, mapquest.com, from http to https
author | Calvin Yeh <cyeh@mpipw-berlin.mpg.com> |
---|---|
date | Wed, 29 Mar 2017 07:04:44 +0200 |
parents | 8f05c2a84bba |
children |
rev | line source |
---|---|
0 | 1 /* |
2 * GeoTemConfig.js | |
3 * | |
4 * Copyright (c) 2012, Stefan Jänicke. All rights reserved. | |
5 * | |
6 * This library is free software; you can redistribute it and/or | |
7 * modify it under the terms of the GNU Lesser General Public | |
8 * License as published by the Free Software Foundation; either | |
9 * version 3 of the License, or (at your option) any later version. | |
10 * | |
11 * This library is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Lesser General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Lesser General Public | |
17 * License along with this library; if not, write to the Free Software | |
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
19 * MA 02110-1301 USA | |
20 */ | |
21 | |
22 /** | |
23 * @class GeoTemConfig | |
24 * Global GeoTemCo Configuration File | |
25 * @author Stefan Jänicke (stjaenicke@informatik.uni-leipzig.de) | |
26 * @release 1.0 | |
27 * @release date: 2012-07-27 | |
28 * @version date: 2012-07-27 | |
29 */ | |
30 | |
31 | |
32 // credits: user76888, The Digital Gabeg (http://stackoverflow.com/questions/1539367) | |
33 $.fn.cleanWhitespace = function() { | |
34 textNodes = this.contents().filter( function() { | |
35 return (this.nodeType == 3 && !/\S/.test(this.nodeValue)); | |
36 }).remove(); | |
37 return this; | |
38 }; | |
39 | |
40 GeoTemConfig = { | |
41 debug : false, //show debug output (esp. regarding corrupt datasets) | |
42 incompleteData : true, // show/hide data with either temporal or spatial metadata | |
43 inverseFilter : true, // if inverse filtering is offered | |
44 mouseWheelZoom : true, // enable/disable zoom with mouse wheel on map & timeplot | |
45 language : 'en', // default language of GeoTemCo | |
46 allowFilter : true, // if filtering should be allowed | |
47 highlightEvents : true, // if updates after highlight events | |
48 selectionEvents : true, // if updates after selection events | |
49 tableExportDataset : true, // export dataset to KML | |
50 allowCustomColoring : false, // if DataObjects can have an own color (useful for weighted coloring) | |
51 allowUserShapeAndColorChange: false, // if the user can change the shapes and color of datasets | |
52 // this turns MapConfig.useGraphics auto-on, but uses circles as default | |
53 loadColorFromDataset : false, // if DataObject color should be loaded automatically (from column "color") | |
54 allowColumnRenaming : true, | |
55 //proxy : 'php/proxy.php?address=', //set this if a HTTP proxy shall be used (e.g. to bypass X-Domain problems) | |
56 //colors for several datasets; rgb1 will be used for selected objects, rgb0 for unselected | |
57 colors : [{ | |
58 r1 : 255, | |
59 g1 : 101, | |
60 b1 : 0, | |
61 r0 : 253, | |
62 g0 : 229, | |
63 b0 : 205 | |
64 }, { | |
65 r1 : 144, | |
66 g1 : 26, | |
67 b1 : 255, | |
68 r0 : 230, | |
69 g0 : 225, | |
70 b0 : 255 | |
71 }, { | |
72 r1 : 0, | |
73 g1 : 217, | |
74 b1 : 0, | |
75 r0 : 213, | |
76 g0 : 255, | |
77 b0 : 213 | |
78 }, { | |
79 r1 : 240, | |
80 g1 : 220, | |
81 b1 : 0, | |
82 r0 : 247, | |
83 g0 : 244, | |
84 b0 : 197 | |
85 }] | |
86 | |
87 } | |
88 | |
89 GeoTemConfig.ie = false; | |
90 GeoTemConfig.ie8 = false; | |
91 | |
92 GeoTemConfig.independentMapId = 0; | |
93 GeoTemConfig.independentTimeId = 0; | |
94 | |
95 if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { | |
96 GeoTemConfig.ie = true; | |
97 var ieversion = new Number(RegExp.$1); | |
98 if (ieversion == 8) { | |
99 GeoTemConfig.ie8 = true; | |
100 } | |
101 } | |
102 | |
103 GeoTemConfig.getIndependentId = function(target){ | |
104 if( target == 'map' ){ | |
105 return ++GeoTemConfig.independentMapId; | |
106 } | |
107 if( target == 'time' ){ | |
108 return ++GeoTemConfig.independentTimeId; | |
109 } | |
110 return 0; | |
111 }; | |
112 | |
113 GeoTemConfig.setHexColor = function(hex,index,fill){ | |
114 var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); | |
115 if( fill ){ | |
116 GeoTemConfig.colors[index].r0 = parseInt(result[1], 16); | |
117 GeoTemConfig.colors[index].g0 = parseInt(result[2], 16); | |
118 GeoTemConfig.colors[index].b0 = parseInt(result[3], 16); | |
119 } | |
120 else { | |
121 GeoTemConfig.colors[index].r1 = parseInt(result[1], 16); | |
122 GeoTemConfig.colors[index].g1 = parseInt(result[2], 16); | |
123 GeoTemConfig.colors[index].b1 = parseInt(result[3], 16); | |
124 } | |
125 } | |
126 | |
127 GeoTemConfig.setRgbColor = function(r,g,b,index,fill){ | |
128 if( fill ){ | |
129 GeoTemConfig.colors[index].r0 = r; | |
130 GeoTemConfig.colors[index].g0 = g; | |
131 GeoTemConfig.colors[index].b0 = b; | |
132 } | |
133 else { | |
134 GeoTemConfig.colors[index].r1 = r; | |
135 GeoTemConfig.colors[index].g1 = g; | |
136 GeoTemConfig.colors[index].b1 = b; | |
137 } | |
138 } | |
139 | |
140 GeoTemConfig.configure = function(urlPrefix) { | |
141 GeoTemConfig.urlPrefix = urlPrefix; | |
142 GeoTemConfig.path = GeoTemConfig.urlPrefix + "images/"; | |
143 } | |
144 | |
145 GeoTemConfig.applySettings = function(settings) { | |
146 $.extend(this, settings); | |
147 }; | |
148 | |
149 //Keeps track of how many colors where assigned yet. | |
150 GeoTemConfig.assignedColorCount = 0; | |
151 GeoTemConfig.getColor = function(id){ | |
152 if (typeof GeoTemConfig.datasets[id].color === "undefined"){ | |
153 var color; | |
154 | |
155 while (true){ | |
156 if( GeoTemConfig.colors.length <= GeoTemConfig.assignedColorCount ){ | |
157 color = { | |
158 r1 : Math.floor((Math.random()*255)+1), | |
159 g1 : Math.floor((Math.random()*255)+1), | |
160 b1 : Math.floor((Math.random()*255)+1), | |
161 r0 : 230, | |
162 g0 : 230, | |
163 b0 : 230 | |
164 }; | |
165 } else | |
166 color = GeoTemConfig.colors[GeoTemConfig.assignedColorCount]; | |
167 | |
168 //make sure that no other dataset has this color | |
169 //TODO: one could also check that they are not too much alike | |
170 var found = false; | |
171 for (var i = 0; i < GeoTemConfig.datasets.length; i++){ | |
172 var dataset = GeoTemConfig.datasets[i]; | |
173 | |
174 if (typeof dataset.color === "undefined") | |
175 continue; | |
176 | |
177 if ( (dataset.color.r1 == color.r1) && | |
178 (dataset.color.g1 == color.g1) && | |
179 (dataset.color.b1 == color.b1) ){ | |
180 found = true; | |
181 break; | |
182 } | |
183 } | |
184 if (found === true){ | |
185 if( GeoTemConfig.colors.length <= GeoTemConfig.assignedColorCount ){ | |
186 //next time skip over this color | |
187 GeoTemConfig.assignedColorCount++; | |
188 } | |
189 continue; | |
190 } else { | |
191 GeoTemConfig.colors.push(color); | |
192 break; | |
193 } | |
194 } | |
195 GeoTemConfig.datasets[id].color = color; | |
196 | |
197 GeoTemConfig.assignedColorCount++; | |
198 } | |
199 return GeoTemConfig.datasets[id].color; | |
200 }; | |
201 | |
202 GeoTemConfig.getAverageDatasetColor = function(id, objects){ | |
203 var c = new Object(); | |
204 var datasetColor = GeoTemConfig.getColor(id); | |
205 c.r0 = datasetColor.r0; | |
206 c.g0 = datasetColor.g0; | |
207 c.b0 = datasetColor.b0; | |
208 c.r1 = datasetColor.r1; | |
209 c.g1 = datasetColor.g1; | |
210 c.b1 = datasetColor.b1; | |
211 if (!GeoTemConfig.allowCustomColoring) | |
212 return c; | |
213 if (objects.length == 0) | |
214 return c; | |
215 var avgColor = new Object(); | |
216 avgColor.r0 = 0; | |
217 avgColor.g0 = 0; | |
218 avgColor.b0 = 0; | |
219 avgColor.r1 = 0; | |
220 avgColor.g1 = 0; | |
221 avgColor.b1 = 0; | |
222 | |
223 $(objects).each(function(){ | |
224 if (this.hasColorInformation){ | |
225 avgColor.r0 += this.color.r0; | |
226 avgColor.g0 += this.color.g0; | |
227 avgColor.b0 += this.color.b0; | |
228 avgColor.r1 += this.color.r1; | |
229 avgColor.g1 += this.color.g1; | |
230 avgColor.b1 += this.color.b1; | |
231 } else { | |
232 avgColor.r0 += datasetColor.r0; | |
233 avgColor.g0 += datasetColor.g0; | |
234 avgColor.b0 += datasetColor.b0; | |
235 avgColor.r1 += datasetColor.r1; | |
236 avgColor.g1 += datasetColor.g1; | |
237 avgColor.b1 += datasetColor.b1; | |
238 } | |
239 }); | |
240 | |
241 c.r0 = Math.floor(avgColor.r0/objects.length); | |
242 c.g0 = Math.floor(avgColor.g0/objects.length); | |
243 c.b0 = Math.floor(avgColor.b0/objects.length); | |
244 c.r1 = Math.floor(avgColor.r1/objects.length); | |
245 c.g1 = Math.floor(avgColor.g1/objects.length); | |
246 c.b1 = Math.floor(avgColor.b1/objects.length); | |
247 | |
248 return c; | |
249 }; | |
250 | |
251 GeoTemConfig.getString = function(field) { | |
252 if ( typeof Tooltips[GeoTemConfig.language] == 'undefined') { | |
253 GeoTemConfig.language = 'en'; | |
254 } | |
255 return Tooltips[GeoTemConfig.language][field]; | |
256 } | |
257 /** | |
258 * returns the actual mouse position | |
259 * @param {Event} e the mouseevent | |
260 * @return the top and left position on the screen | |
261 */ | |
262 GeoTemConfig.getMousePosition = function(e) { | |
263 if (!e) { | |
264 e = window.event; | |
265 } | |
266 var body = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ? window.document.documentElement : window.document.body; | |
267 return { | |
268 top : e.pageY ? e.pageY : e.clientY, | |
269 left : e.pageX ? e.pageX : e.clientX | |
270 }; | |
271 } | |
272 /** | |
273 * returns the json object of the file from the given url | |
274 * @param {String} url the url of the file to load | |
275 * @return json object of given file | |
276 */ | |
8
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
277 GeoTemConfig.getJson = function(url,asyncFunc) { |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
278 var async = false; |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
279 if( asyncFunc ){ |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
280 async = true; |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
281 } |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
282 |
0 | 283 var data; |
284 $.ajax({ | |
285 url : url, | |
8
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
286 async : async, |
0 | 287 dataType : 'json', |
288 success : function(json) { | |
289 data = json; | |
8
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
290 if (async){ |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
291 asyncFunc(data); |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
292 } |
0 | 293 } |
294 }); | |
8
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
295 |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
296 if (async){ |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
297 return data; |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
298 } |
0 | 299 } |
300 | |
301 GeoTemConfig.mergeObjects = function(set1, set2) { | |
302 var inside = []; | |
303 var newSet = []; | |
304 for (var i = 0; i < GeoTemConfig.datasets.length; i++){ | |
305 inside.push([]); | |
306 newSet.push([]); | |
307 } | |
308 for (var i = 0; i < set1.length; i++) { | |
309 for (var j = 0; j < set1[i].length; j++) { | |
310 inside[i][set1[i][j].index] = true; | |
311 newSet[i].push(set1[i][j]); | |
312 } | |
313 } | |
314 for (var i = 0; i < set2.length; i++) { | |
315 for (var j = 0; j < set2[i].length; j++) { | |
316 if (!inside[i][set2[i][j].index]) { | |
317 newSet[i].push(set2[i][j]); | |
318 } | |
319 } | |
320 } | |
321 return newSet; | |
322 }; | |
323 | |
324 GeoTemConfig.datasets = []; | |
325 | |
326 GeoTemConfig.addDataset = function(newDataset){ | |
327 GeoTemConfig.datasets.push(newDataset); | |
328 Publisher.Publish('filterData', GeoTemConfig.datasets, null); | |
329 }; | |
330 | |
331 GeoTemConfig.addDatasets = function(newDatasets){ | |
332 $(newDatasets).each(function(){ | |
333 GeoTemConfig.datasets.push(this); | |
334 }); | |
335 Publisher.Publish('filterData', GeoTemConfig.datasets, null); | |
336 }; | |
337 | |
338 GeoTemConfig.removeDataset = function(index){ | |
339 GeoTemConfig.datasets.splice(index,1); | |
340 Publisher.Publish('filterData', GeoTemConfig.datasets, null); | |
341 }; | |
342 | |
343 /** | |
344 * converts the csv-file into json-format | |
345 * | |
346 * @param {String} | |
347 * text | |
348 */ | |
349 GeoTemConfig.convertCsv = function(text){ | |
350 /* convert here from CSV to JSON */ | |
351 var json = []; | |
352 /* define expected csv table headers (first line) */ | |
353 var expectedHeaders = new Array("Name","Address","Description","Longitude","Latitude","TimeStamp","TimeSpan:begin","TimeSpan:end","weight"); | |
354 /* convert csv string to array of arrays using ucsv library */ | |
355 var csvArray = CSV.csvToArray(text); | |
356 /* get real used table headers from csv file (first line) */ | |
357 var usedHeaders = csvArray[0]; | |
358 /* loop outer array, begin with second line */ | |
359 for (var i = 1; i < csvArray.length; i++) { | |
360 var innerArray = csvArray[i]; | |
361 var dataObject = new Object(); | |
362 var tableContent = new Object(); | |
363 /* exclude lines with no content */ | |
364 var hasContent = false; | |
365 for (var j = 0; j < innerArray.length; j++) { | |
366 if (typeof innerArray[j] !== "undefined"){ | |
367 if (typeof innerArray[j] === "string"){ | |
368 if (innerArray[j].length > 0) | |
369 hasContent = true; | |
370 } else { | |
371 hasContent = true; | |
372 } | |
373 } | |
374 | |
375 if (hasContent === true) | |
376 break; | |
377 } | |
378 if (hasContent === false) | |
379 continue; | |
380 /* loop inner array */ | |
381 for (var j = 0; j < innerArray.length; j++) { | |
382 /* Name */ | |
383 if (usedHeaders[j] == expectedHeaders[0]) { | |
384 dataObject["name"] = ""+innerArray[j]; | |
385 tableContent["name"] = ""+innerArray[j]; | |
386 } | |
387 /* Address */ | |
388 else if (usedHeaders[j] == expectedHeaders[1]) { | |
389 dataObject["place"] = ""+innerArray[j]; | |
390 tableContent["place"] = ""+innerArray[j]; | |
391 } | |
392 /* Description */ | |
393 else if (usedHeaders[j] == expectedHeaders[2]) { | |
394 dataObject["description"] = ""+innerArray[j]; | |
395 tableContent["description"] = ""+innerArray[j]; | |
396 } | |
397 /* TimeStamp */ | |
398 else if (usedHeaders[j] == expectedHeaders[5]) { | |
399 dataObject["time"] = ""+innerArray[j]; | |
400 } | |
401 /* TimeSpan:begin */ | |
402 else if (usedHeaders[j] == expectedHeaders[6]) { | |
403 tableContent["TimeSpan:begin"] = ""+innerArray[j]; | |
404 } | |
405 /* TimeSpan:end */ | |
406 else if (usedHeaders[j] == expectedHeaders[7]) { | |
407 tableContent["TimeSpan:end"] = ""+innerArray[j]; | |
408 } | |
409 /* weight */ | |
410 else if (usedHeaders[j] == expectedHeaders[8]) { | |
411 dataObject["weight"] = ""+innerArray[j]; | |
412 } | |
413 /* Longitude */ | |
414 else if (usedHeaders[j] == expectedHeaders[3]) { | |
415 dataObject["lon"] = parseFloat(innerArray[j]); | |
416 } | |
417 /* Latitude */ | |
418 else if (usedHeaders[j] == expectedHeaders[4]) { | |
419 dataObject["lat"] = parseFloat(innerArray[j]); | |
420 } | |
421 else { | |
422 var header = new String(usedHeaders[j]); | |
423 //remove leading and trailing Whitespace | |
424 header = $.trim(header); | |
425 tableContent[header] = ""+innerArray[j]; | |
426 } | |
427 } | |
428 | |
429 dataObject["tableContent"] = tableContent; | |
430 | |
431 json.push(dataObject); | |
432 } | |
433 | |
434 return json; | |
435 }; | |
436 | |
437 /** | |
438 * returns the xml dom object of the file from the given url | |
439 * @param {String} url the url of the file to load | |
440 * @return xml dom object of given file | |
441 */ | |
442 GeoTemConfig.getKml = function(url,asyncFunc) { | |
443 var data; | |
444 var async = false; | |
445 if( asyncFunc ){ | |
446 async = true; | |
447 } | |
448 $.ajax({ | |
449 url : url, | |
450 async : async, | |
451 dataType : 'xml', | |
452 success : function(xml) { | |
453 if( asyncFunc ){ | |
454 asyncFunc(xml); | |
455 } | |
456 else { | |
457 data = xml; | |
458 } | |
459 } | |
460 }); | |
461 if( !async ){ | |
462 return data; | |
463 } | |
464 } | |
465 | |
466 /** | |
467 * returns an array of all xml dom object of the kmls | |
468 * found in the zip file from the given url | |
469 * | |
470 * can only be used with asyncFunc (because of browser | |
471 * constraints regarding arraybuffer) | |
472 * | |
473 * @param {String} url the url of the file to load | |
474 * @return xml dom object of given file | |
475 */ | |
476 GeoTemConfig.getKmz = function(url,asyncFunc) { | |
477 var kmlDom = new Array(); | |
478 | |
479 var async = true; | |
480 if( !asyncFunc ){ | |
481 //if no asyncFunc is given return an empty array | |
482 return kmlDom; | |
483 } | |
484 | |
485 //use XMLHttpRequest as "arraybuffer" is not | |
486 //supported in jQuery native $.get | |
487 var req = new XMLHttpRequest(); | |
488 req.open("GET",url,async); | |
489 req.responseType = "arraybuffer"; | |
490 req.onload = function() { | |
491 var zip = new JSZip(); | |
492 zip.load(req.response, {base64:false}); | |
493 var kmlFiles = zip.file(new RegExp("kml$")); | |
494 | |
495 $(kmlFiles).each(function(){ | |
496 var kml = this; | |
497 if (kml.data != null) { | |
498 kmlDom.push($.parseXML(kml.data)); | |
499 } | |
500 }); | |
501 | |
502 asyncFunc(kmlDom); | |
503 }; | |
504 req.send(); | |
505 }; | |
506 | |
507 /** | |
508 * returns the JSON "object" | |
509 * from the csv file from the given url | |
510 * @param {String} url the url of the file to load | |
511 * @return xml dom object of given file | |
512 */ | |
513 GeoTemConfig.getCsv = function(url,asyncFunc) { | |
514 var async = false; | |
515 if( asyncFunc ){ | |
516 async = true; | |
517 } | |
518 | |
519 //use XMLHttpRequest as synchronous behaviour | |
520 //is not supported in jQuery native $.get | |
521 var req = new XMLHttpRequest(); | |
522 req.open("GET",url,async); | |
523 //can only be set on asynchronous now | |
524 //req.responseType = "text"; | |
525 var json; | |
526 req.onload = function() { | |
527 json = GeoTemConfig.convertCsv(req.response); | |
528 if( asyncFunc ) | |
529 asyncFunc(json); | |
530 }; | |
531 req.send(); | |
532 | |
533 if( !async ){ | |
534 return json; | |
535 } | |
536 }; | |
537 | |
538 /** | |
8
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
539 * loads a binary file |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
540 * @param {String} url of the file to load |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
541 * @return binary data |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
542 */ |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
543 GeoTemConfig.getBinary = function(url,asyncFunc) { |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
544 var async = true; |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
545 |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
546 var req = new XMLHttpRequest(); |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
547 req.open("GET",url,async); |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
548 req.responseType = "arraybuffer"; |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
549 |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
550 var binaryData; |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
551 req.onload = function() { |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
552 var arrayBuffer = req.response; |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
553 asyncFunc(arrayBuffer); |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
554 }; |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
555 req.send(); |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
556 }; |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
557 |
8f05c2a84bba
Apply new platin and Add tree layers
nylin@mpiwg-berlin.mpg.de
parents:
0
diff
changeset
|
558 /** |
0 | 559 * returns a Date and a SimileAjax.DateTime granularity value for a given XML time |
560 * @param {String} xmlTime the XML time as String | |
561 * @return JSON object with a Date and a SimileAjax.DateTime granularity | |
562 */ | |
563 GeoTemConfig.getTimeData = function(xmlTime) { | |
564 if (!xmlTime) | |
565 return; | |
566 var dateData; | |
567 try { | |
568 var bc = false; | |
569 if (xmlTime.startsWith("-")) { | |
570 bc = true; | |
571 xmlTime = xmlTime.substring(1); | |
572 } | |
573 var timeSplit = xmlTime.split("T"); | |
574 var timeData = timeSplit[0].split("-"); | |
575 for (var i = 0; i < timeData.length; i++) { | |
576 parseInt(timeData[i]); | |
577 } | |
578 if (bc) { | |
579 timeData[0] = "-" + timeData[0]; | |
580 } | |
581 if (timeSplit.length == 1) { | |
582 dateData = timeData; | |
583 } else { | |
584 var dayData; | |
585 if (timeSplit[1].indexOf("Z") != -1) { | |
586 dayData = timeSplit[1].substring(0, timeSplit[1].indexOf("Z") - 1).split(":"); | |
587 } else { | |
588 dayData = timeSplit[1].substring(0, timeSplit[1].indexOf("+") - 1).split(":"); | |
589 } | |
590 for (var i = 0; i < timeData.length; i++) { | |
591 parseInt(dayData[i]); | |
592 } | |
593 dateData = timeData.concat(dayData); | |
594 } | |
595 } catch (exception) { | |
596 return null; | |
597 } | |
598 var date, granularity; | |
599 if (dateData.length == 6) { | |
600 granularity = SimileAjax.DateTime.SECOND; | |
601 date = new Date(Date.UTC(dateData[0], dateData[1] - 1, dateData[2], dateData[3], dateData[4], dateData[5])); | |
602 } else if (dateData.length == 3) { | |
603 granularity = SimileAjax.DateTime.DAY; | |
604 date = new Date(Date.UTC(dateData[0], dateData[1] - 1, dateData[2])); | |
605 } else if (dateData.length == 2) { | |
606 granularity = SimileAjax.DateTime.MONTH; | |
607 date = new Date(Date.UTC(dateData[0], dateData[1] - 1, 1)); | |
608 } else if (dateData.length == 1) { | |
609 granularity = SimileAjax.DateTime.YEAR; | |
610 date = new Date(Date.UTC(dateData[0], 0, 1)); | |
611 } | |
612 if (timeData[0] && timeData[0] < 100) { | |
613 date.setFullYear(timeData[0]); | |
614 } | |
615 | |
616 //check data validity; | |
617 var isValidDate = true; | |
618 if ( date instanceof Date ) { | |
619 if ( isNaN( date.getTime() ) ) | |
620 isValidDate = false; | |
621 } else | |
622 isValidDate = false; | |
623 | |
624 if (!isValidDate){ | |
625 if ((GeoTemConfig.debug)&&(typeof console !== "undefined")) | |
626 console.error(xmlTime + " is no valid time format"); | |
627 return null; | |
628 } | |
629 | |
630 return { | |
631 date : date, | |
632 granularity : granularity | |
633 }; | |
634 } | |
635 /** | |
636 * converts a JSON array into an array of data objects | |
637 * @param {JSON} JSON a JSON array of data items | |
638 * @return an array of data objects | |
639 */ | |
640 GeoTemConfig.loadJson = function(JSON) { | |
641 var mapTimeObjects = []; | |
642 var runningIndex = 0; | |
643 for (var i in JSON ) { | |
644 try { | |
645 var item = JSON[i]; | |
646 var index = item.index || item.id || runningIndex++; | |
647 var name = item.name || ""; | |
648 var description = item.description || ""; | |
649 var tableContent = item.tableContent || []; | |
650 var locations = []; | |
651 if (item.location instanceof Array) { | |
652 for (var j = 0; j < item.location.length; j++) { | |
653 var place = item.location[j].place || "unknown"; | |
654 var lon = item.location[j].lon; | |
655 var lat = item.location[j].lat; | |
656 if ((typeof lon === "undefined" || typeof lat === "undefined" || isNaN(lon) || isNaN(lat) ) && !GeoTemConfig.incompleteData) { | |
657 throw "e"; | |
658 } | |
659 locations.push({ | |
660 longitude : lon, | |
661 latitude : lat, | |
662 place : place | |
663 }); | |
664 } | |
665 } else { | |
666 var place = item.place || "unknown"; | |
667 var lon = item.lon; | |
668 var lat = item.lat; | |
669 if ((typeof lon === "undefined" || typeof lat === "undefined" || isNaN(lon) || isNaN(lat) ) && !GeoTemConfig.incompleteData) { | |
670 throw "e"; | |
671 } | |
672 locations.push({ | |
673 longitude : lon, | |
674 latitude : lat, | |
675 place : place | |
676 }); | |
677 } | |
678 var dates = []; | |
679 if (item.time instanceof Array) { | |
680 for (var j = 0; j < item.time.length; j++) { | |
681 var time = GeoTemConfig.getTimeData(item.time[j]); | |
682 if (time == null && !GeoTemConfig.incompleteData) { | |
683 throw "e"; | |
684 } | |
685 dates.push(time); | |
686 } | |
687 } else { | |
688 var time = GeoTemConfig.getTimeData(item.time); | |
689 if (time == null && !GeoTemConfig.incompleteData) { | |
690 throw "e"; | |
691 } | |
692 if (time != null) { | |
693 dates.push(time); | |
694 } | |
695 } | |
696 var weight = parseInt(item.weight) || 1; | |
697 //add all "other" attributes to table data | |
698 //this is a hack to allow "invalid" JSONs | |
699 var specialAttributes = ["id", "name", "description", "lon", "lat", "place", "time", | |
700 "tableContent", "location", "time"]; | |
701 for (var attribute in item){ | |
702 if ($.inArray(attribute, specialAttributes) == -1){ | |
703 tableContent[attribute] = item[attribute]; | |
704 } | |
705 } | |
706 | |
707 var mapTimeObject = new DataObject(name, description, locations, dates, weight, tableContent); | |
708 mapTimeObject.setIndex(index); | |
709 mapTimeObjects.push(mapTimeObject); | |
710 } catch(e) { | |
711 continue; | |
712 } | |
713 } | |
714 | |
715 if (GeoTemConfig.loadColorFromDataset) | |
716 GeoTemConfig.loadDataObjectColoring(mapTimeObjects); | |
717 | |
718 return mapTimeObjects; | |
719 } | |
720 /** | |
721 * converts a KML dom into an array of data objects | |
722 * @param {XML dom} kml the XML dom for the KML file | |
723 * @return an array of data objects | |
724 */ | |
725 GeoTemConfig.loadKml = function(kml) { | |
726 var mapObjects = []; | |
727 var elements = kml.getElementsByTagName("Placemark"); | |
728 if (elements.length == 0) { | |
729 return []; | |
730 } | |
731 var index = 0; | |
732 var descriptionTableHeaders = []; | |
733 var xmlSerializer = new XMLSerializer(); | |
734 | |
735 for (var i = 0; i < elements.length; i++) { | |
736 var placemark = elements[i]; | |
737 var name, description, place, granularity, lon, lat, tableContent = [], time = [], location = []; | |
738 var weight = 1; | |
739 var timeData = false, mapData = false; | |
740 | |
741 try { | |
742 description = placemark.getElementsByTagName("description")[0].childNodes[0].nodeValue; | |
743 | |
744 //cleanWhitespace removes non-sense text-nodes (space, tab) | |
745 //and is an addition to jquery defined above | |
746 try { | |
747 var descriptionDocument = $($.parseXML(description)).cleanWhitespace(); | |
748 | |
749 //check whether the description element contains a table | |
750 //if yes, this data will be loaded as separate columns | |
751 $(descriptionDocument).find("table").each(function(){ | |
752 $(this).find("tr").each( | |
753 function() { | |
754 var isHeader = true; | |
755 var lastHeader = ""; | |
756 | |
757 $(this).find("td").each( | |
758 function() { | |
759 if (isHeader) { | |
760 lastHeader = $.trim($(this).text()); | |
761 isHeader = false; | |
762 } else { | |
763 var value = ""; | |
764 | |
765 //if this td contains HTML, serialize all | |
766 //it's children (the "content"!) | |
767 $(this).children().each( | |
768 function() { | |
769 value += xmlSerializer.serializeToString(this); | |
770 } | |
771 ); | |
772 | |
773 //no HTML content (or no content at all) | |
774 if (value.length == 0) | |
775 value = $(this).text(); | |
776 if (typeof value === "undefined") | |
777 value = ""; | |
778 | |
779 if ($.inArray(lastHeader, descriptionTableHeaders) === -1) | |
780 descriptionTableHeaders.push(lastHeader); | |
781 | |
782 if (tableContent[lastHeader] != null) | |
783 //append if a field occures more than once | |
784 tableContent[lastHeader] += "\n" + value; | |
785 else | |
786 tableContent[lastHeader] = value; | |
787 | |
788 isHeader = true; | |
789 } | |
790 } | |
791 ); | |
792 } | |
793 ); | |
794 }); | |
795 } catch(e) { | |
796 //couldn't be parsed, so it contains no html table | |
797 //or is not in valid XHTML syntax | |
798 } | |
799 | |
800 //check whether the description element contains content in the form of equations | |
801 //e.g. someDescriptor = someValue, where these eqations are separated by <br/> | |
802 //if yes, this data will be loaded as separate columns | |
803 var descriptionRows = description.replace(/<\s*br\s*[\/]*\s*>/g,"<br/>"); | |
804 $(descriptionRows.split("<br/>")).each(function(){ | |
805 var row = this; | |
806 | |
807 if (typeof row === "undefined") | |
808 return; | |
809 | |
810 var headerAndValue = row.split("="); | |
811 if (headerAndValue.length != 2) | |
812 return; | |
813 | |
814 var header = $.trim(headerAndValue[0]); | |
815 var value = $.trim(headerAndValue[1]); | |
816 | |
817 if ($.inArray(header, descriptionTableHeaders) === -1) | |
818 descriptionTableHeaders.push(header); | |
819 | |
820 if (tableContent[header] != null) | |
821 //append if a field occures more than once | |
822 tableContent[header] += "\n" + value; | |
823 else | |
824 tableContent[header] = value; | |
825 }); | |
826 | |
827 tableContent["description"] = description; | |
828 } catch(e) { | |
829 description = ""; | |
830 } | |
831 | |
832 try { | |
833 name = placemark.getElementsByTagName("name")[0].childNodes[0].nodeValue; | |
834 tableContent["name"] = name; | |
835 } catch(e) { | |
836 if (typeof tableContent["name"] !== "undefined") | |
837 name = tableContent["name"]; | |
838 else | |
839 name = ""; | |
840 } | |
841 | |
842 try { | |
843 place = placemark.getElementsByTagName("address")[0].childNodes[0].nodeValue; | |
844 tableContent["place"] = place; | |
845 } catch(e) { | |
846 if (typeof tableContent["place"] !== "undefined") | |
847 place = tableContent["place"]; | |
848 else | |
849 place = ""; | |
850 } | |
851 | |
852 try { | |
853 var coordinates = placemark.getElementsByTagName("Point")[0].getElementsByTagName("coordinates")[0].childNodes[0].nodeValue; | |
854 var lonlat = coordinates.split(","); | |
855 lon = lonlat[0]; | |
856 lat = lonlat[1]; | |
857 if (lon == "" || lat == "" || isNaN(lon) || isNaN(lat)) { | |
858 throw "e"; | |
859 } | |
860 location.push({ | |
861 longitude : lon, | |
862 latitude : lat, | |
863 place : place | |
864 }); | |
865 } catch(e) { | |
866 if (!GeoTemConfig.incompleteData) { | |
867 continue; | |
868 } | |
869 } | |
870 | |
871 try { | |
872 var tuple = GeoTemConfig.getTimeData(placemark.getElementsByTagName("TimeStamp")[0].getElementsByTagName("when")[0].childNodes[0].nodeValue); | |
873 if (tuple != null) { | |
874 time.push(tuple); | |
875 timeData = true; | |
876 } else if (!GeoTemConfig.incompleteData) { | |
877 continue; | |
878 } | |
879 } catch(e) { | |
880 try { | |
881 if ( (typeof tableContent["TimeSpan:begin"] === "undefined") && | |
882 (typeof tableContent["TimeSpan:end"] === "undefined") ){ | |
883 var timeStart = $(placemark).find("TimeSpan begin").text(); | |
884 var timeEnd = $(placemark).find("TimeSpan end").text(); | |
885 | |
886 if ( (timeStart != "") && (timeStart != "") ){ | |
887 tableContent["TimeSpan:begin"] = timeStart; | |
888 tableContent["TimeSpan:end"] = timeEnd; | |
889 | |
890 timeData = true; | |
891 } | |
892 } | |
893 } catch(e) { | |
894 if (!GeoTemConfig.incompleteData) { | |
895 continue; | |
896 } | |
897 } | |
898 } | |
899 var object = new DataObject(name, description, location, time, 1, tableContent); | |
900 object.setIndex(index); | |
901 index++; | |
902 mapObjects.push(object); | |
903 } | |
904 | |
905 //make sure that all "description table" columns exists in all rows | |
906 if (descriptionTableHeaders.length > 0){ | |
907 $(mapObjects).each(function(){ | |
908 var object = this; | |
909 $(descriptionTableHeaders).each(function(){ | |
910 if (typeof object.tableContent[this] === "undefined") | |
911 object.tableContent[this] = ""; | |
912 }); | |
913 }); | |
914 } | |
915 | |
916 if (GeoTemConfig.loadColorFromDataset) | |
917 GeoTemConfig.loadDataObjectColoring(mapObjects); | |
918 | |
919 return mapObjects; | |
920 }; | |
921 | |
922 GeoTemConfig.createKMLfromDataset = function(index){ | |
923 var kmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><kml xmlns=\"http://www.opengis.net/kml/2.2\"><Document>"; | |
924 | |
925 //credits: Anatoly Mironov, http://stackoverflow.com/questions/2573521/how-do-i-output-an-iso-8601-formatted-string-in-javascript | |
926 function pad(number) { | |
927 var r = String(number); | |
928 if ( r.length === 1 ) { | |
929 r = '0' + r; | |
930 } | |
931 return r; | |
932 } | |
933 | |
934 var dateToISOString = function(date, granularity) { | |
935 var ISOString = date.getFullYear(); | |
936 | |
937 if (granularity <= SimileAjax.DateTime.MONTH) | |
938 ISOString += '-' + pad( date.getMonth() + 1 ); | |
939 if (granularity <= SimileAjax.DateTime.DAY) | |
940 ISOString += '-' + pad( date.getDate() ); | |
941 if (granularity <= SimileAjax.DateTime.HOUR){ | |
942 ISOString += 'T' + pad( date.getHours() ); | |
943 if (granularity <= SimileAjax.DateTime.MINUTE) | |
944 ISOString += ':' + pad( date.getMinutes() ); | |
945 if (granularity <= SimileAjax.DateTime.SECOND) | |
946 ISOString += ':' + pad( date.getSeconds() ); | |
947 if (granularity <= SimileAjax.DateTime.MILLISECOND) | |
948 ISOString += '.' + String( (date.getMilliseconds()/1000).toFixed(3) ).slice( 2, 5 ); | |
949 ISOString += 'Z'; | |
950 } | |
951 | |
952 return ISOString; | |
953 }; | |
954 | |
955 $(GeoTemConfig.datasets[index].objects).each(function(){ | |
956 var name = this.name; | |
957 var description = this.description; | |
958 //TODO: allow multiple time/date | |
959 var place = this.getPlace(0,0); | |
960 var lat = this.getLatitude(0); | |
961 var lon = this.getLongitude(0); | |
962 | |
963 var kmlEntry = "<Placemark>"; | |
964 | |
965 kmlEntry += "<name><![CDATA[" + name + "]]></name>"; | |
966 kmlEntry += "<address><![CDATA[" + place + "]]></address>"; | |
967 kmlEntry += "<description><![CDATA[" + description + "]]></description>"; | |
968 kmlEntry += "<Point><coordinates>" + lon + "," + lat + "</coordinates></Point>"; | |
969 | |
970 if (this.isTemporal){ | |
971 kmlEntry += "<TimeStamp><when>" + dateToISOString(this.getDate(0), this.getTimeGranularity(0)) + "</when></TimeStamp>"; | |
972 } else if (this.isFuzzyTemporal){ | |
973 kmlEntry += "<TimeSpan>"+ | |
974 "<begin>" + dateToISOString(this.TimeSpanBegin.utc().toDate(), this.TimeSpanBeginGranularity) + "</begin>" + | |
975 "<end>" + dateToISOString(this.TimeSpanEnd.utc().toDate(), this.TimeSpanEndGranularity) + "</end>" + | |
976 "</TimeSpan>"; | |
977 } | |
978 | |
979 kmlEntry += "</Placemark>"; | |
980 | |
981 kmlContent += kmlEntry; | |
982 }); | |
983 | |
984 kmlContent += "</Document></kml>"; | |
985 | |
986 return(kmlContent); | |
987 }; | |
988 | |
989 GeoTemConfig.createCSVfromDataset = function(index){ | |
990 var csvContent = ""; | |
991 var header = ["name", "description", "weight"]; | |
992 var tableContent = []; | |
993 | |
994 var firstDataObject = GeoTemConfig.datasets[index].objects[0]; | |
995 | |
996 for(var key in firstDataObject.tableContent){ | |
997 var found = false; | |
998 $(header).each(function(index,val){ | |
999 if (val === key){ | |
1000 found = true; | |
1001 return false; | |
1002 } | |
1003 }); | |
1004 if (found === true) | |
1005 continue; | |
1006 else | |
1007 tableContent.push(key); | |
1008 } | |
1009 | |
1010 var isFirst = true; | |
1011 $(header).each(function(key,val){ | |
1012 if (isFirst){ | |
1013 isFirst = false; | |
1014 } else { | |
1015 csvContent += ","; | |
1016 } | |
1017 | |
1018 //Rename according to CSV import definition | |
1019 if (val === "name") | |
1020 val = "Name"; | |
1021 else if (val === "description") | |
1022 val = "Description"; | |
1023 csvContent += "\""+val+"\""; | |
1024 }); | |
1025 $(tableContent).each(function(key,val){ | |
1026 if (isFirst){ | |
1027 isFirst = false; | |
1028 } else { | |
1029 csvContent += ","; | |
1030 } | |
1031 csvContent += "\""+val+"\""; | |
1032 }); | |
1033 //Names according to CSV import definition | |
1034 csvContent += ",\"Address\",\"Latitude\",\"Longitude\",\"TimeStamp\""; | |
1035 csvContent += "\n"; | |
1036 | |
1037 var isFirstRow = true; | |
1038 $(GeoTemConfig.datasets[index].objects).each(function(){ | |
1039 var elem = this; | |
1040 | |
1041 if (isFirstRow){ | |
1042 isFirstRow = false; | |
1043 } else { | |
1044 csvContent += "\n"; | |
1045 } | |
1046 | |
1047 var isFirst = true; | |
1048 $(header).each(function(key,val){ | |
1049 if (isFirst){ | |
1050 isFirst = false; | |
1051 } else { | |
1052 csvContent += ","; | |
1053 } | |
1054 csvContent += "\""+elem[val]+"\""; | |
1055 }); | |
1056 $(tableContent).each(function(key,val){ | |
1057 if (isFirst){ | |
1058 isFirst = false; | |
1059 } else { | |
1060 csvContent += ","; | |
1061 } | |
1062 csvContent += "\""+elem.tableContent[val]+"\""; | |
1063 }); | |
1064 | |
1065 csvContent += ","; | |
1066 csvContent += "\""; | |
1067 if (elem.isGeospatial){ | |
1068 csvContent += elem.locations[0].place; | |
1069 } | |
1070 csvContent += "\""; | |
1071 | |
1072 csvContent += ","; | |
1073 csvContent += "\""; | |
1074 if ( (elem.isGeospatial) && (typeof elem.getLatitude(0) !== "undefined") ){ | |
1075 csvContent += elem.getLatitude(0); | |
1076 } | |
1077 csvContent += "\""; | |
1078 | |
1079 csvContent += ","; | |
1080 csvContent += "\""; | |
1081 if ( (elem.isGeospatial) && (typeof elem.getLongitude(0) !== "undefined") ){ | |
1082 csvContent += elem.getLongitude(0); | |
1083 } | |
1084 csvContent += "\""; | |
1085 | |
1086 csvContent += ","; | |
1087 csvContent += "\""; | |
1088 if ( (elem.isTemporal) && (typeof elem.getDate(0) !== "undefined") ){ | |
1089 //TODO: not supported in IE8 switch to moment.js | |
1090 csvContent += elem.getDate(0).toISOString(); | |
1091 } | |
1092 csvContent += "\""; | |
1093 }); | |
1094 | |
1095 return(csvContent); | |
1096 }; | |
1097 /** | |
1098 * iterates over Datasets/DataObjects and loads color values | |
1099 * from the "color0" and "color1" elements, which contains RGB | |
1100 * values in hex (CSS style #RRGGBB) | |
1101 * @param {dataObjects} array of DataObjects | |
1102 */ | |
1103 GeoTemConfig.loadDataObjectColoring = function(dataObjects) { | |
1104 $(dataObjects).each(function(){ | |
1105 var r0,g0,b0,r1,g1,b1; | |
1106 if ( (typeof this.tableContent !== "undefined") && | |
1107 (typeof this.tableContent["color0"] !== "undefined") ){ | |
1108 var color = this.tableContent["color0"]; | |
1109 if ( (color.indexOf("#") == 0) && (color.length == 7) ){ | |
1110 r0 = parseInt("0x"+color.substr(1,2)); | |
1111 g0 = parseInt("0x"+color.substr(3,2)); | |
1112 b0 = parseInt("0x"+color.substr(5,2)); | |
1113 } | |
1114 } | |
1115 if ( (typeof this.tableContent !== "undefined") && | |
1116 (typeof this.tableContent["color1"] !== "undefined") ){ | |
1117 var color = this.tableContent["color1"]; | |
1118 if ( (color.indexOf("#") == 0) && (color.length == 7) ){ | |
1119 r1 = parseInt("0x"+color.substr(1,2)); | |
1120 g1 = parseInt("0x"+color.substr(3,2)); | |
1121 b1 = parseInt("0x"+color.substr(5,2)); | |
1122 } | |
1123 } | |
1124 | |
1125 if ( (typeof r0 !== "undefined") && (typeof g0 !== "undefined") && (typeof b0 !== "undefined") && | |
1126 (typeof r1 !== "undefined") && (typeof g1 !== "undefined") && (typeof b1 !== "undefined") ){ | |
1127 this.setColor(r0,g0,b0,r1,g1,b1); | |
1128 delete this.tableContent["color0"]; | |
1129 delete this.tableContent["color1"]; | |
1130 } else { | |
1131 if ((GeoTemConfig.debug)&&(typeof console !== undefined)) | |
1132 console.error("Object '" + this.name + "' has invalid color information"); | |
1133 } | |
1134 }); | |
1135 }; | |
1136 | |
1137 /** | |
1138 * renames (or copies, see below) a column of each DataObject in a Dataset | |
1139 * @param {Dataset} dataset the dataset where the rename should take place | |
1140 * @param {String} oldColumn name of column that will be renamed | |
1141 * @param {String} newColumn new name of column | |
1142 * @param {Boolean} keepOld keep old column (copy mode) | |
1143 * @return an array of data objects | |
1144 */ | |
1145 GeoTemConfig.renameColumns = function(dataset, renames){ | |
1146 if (renames.length===0){ | |
1147 return; | |
1148 } | |
1149 for (var renCnt = 0; renCnt < renames.length; renCnt++){ | |
1150 var oldColumn = renames[renCnt].oldColumn; | |
1151 var newColumn = renames[renCnt].newColumn; | |
1152 | |
1153 var keepOld = renames[renCnt].keepOld; | |
1154 if (typeof keepOld === "undefined"){ | |
1155 keepOld = true; | |
1156 } | |
1157 var oldColumObject = {}; | |
1158 if (oldColumn.indexOf("[") != -1){ | |
1159 oldColumObject.columnName = oldColumn.split("[")[0]; | |
1160 var IndexAndAttribute = oldColumn.split("[")[1]; | |
1161 if (IndexAndAttribute.indexOf("]") != -1){ | |
1162 oldColumObject.type = 2; | |
1163 oldColumObject.arrayIndex = IndexAndAttribute.split("]")[0]; | |
1164 var attribute = IndexAndAttribute.split("]")[1]; | |
1165 if (attribute.length > 0){ | |
1166 oldColumObject.type = 3; | |
1167 oldColumObject.attribute = attribute.split(".")[1]; | |
1168 } | |
1169 } | |
1170 } else { | |
1171 oldColumObject.type = 1; | |
1172 oldColumObject.name = oldColumn; | |
1173 } | |
1174 | |
1175 var newColumObject = {}; | |
1176 if (newColumn.indexOf("[") != -1){ | |
1177 newColumObject.name = newColumn.split("[")[0]; | |
1178 var IndexAndAttribute = newColumn.split("[")[1]; | |
1179 if (IndexAndAttribute.indexOf("]") != -1){ | |
1180 newColumObject.type = 2; | |
1181 newColumObject.arrayIndex = IndexAndAttribute.split("]")[0]; | |
1182 var attribute = IndexAndAttribute.split("]")[1]; | |
1183 if (attribute.length > 0){ | |
1184 newColumObject.type = 3; | |
1185 newColumObject.attribute = attribute.split(".")[1]; | |
1186 } | |
1187 } | |
1188 } else { | |
1189 newColumObject.type = 1; | |
1190 newColumObject.name = newColumn; | |
1191 } | |
1192 | |
1193 for (var i = 0; i < dataset.objects.length; i++){ | |
1194 var dataObject = dataset.objects[i]; | |
1195 | |
1196 //get value from old column name | |
1197 var value; | |
1198 if (oldColumObject.type == 1){ | |
1199 value = dataObject[oldColumObject.name]; | |
1200 if (typeof value === "undefined"){ | |
1201 value = dataObject.tableContent[oldColumObject.name]; | |
1202 } | |
1203 if (!keepOld){ | |
1204 delete dataObject.tableContent[oldColumObject.name]; | |
1205 delete dataObject[oldColumObject.name]; | |
1206 } | |
1207 } else if (oldColumObject.type == 2){ | |
1208 value = dataObject[oldColumObject.name][oldColumObject.arrayIndex]; | |
1209 if (!keepOld){ | |
1210 delete dataObject[oldColumObject.name][oldColumObject.arrayIndex]; | |
1211 } | |
1212 } else if (oldColumObject.type == 3){ | |
1213 value = dataObject[oldColumObject.name][oldColumObject.arrayIndex][oldColumObject.attribute]; | |
1214 if (!keepOld){ | |
1215 delete dataObject[oldColumObject.name][oldColumObject.arrayIndex][oldColumObject.attribute]; | |
1216 } | |
1217 } | |
1218 | |
1219 //create new column | |
1220 if (newColumObject.type == 1){ | |
1221 dataObject[newColumObject.name] = value; | |
1222 dataObject.tableContent[newColumObject.name] = value; | |
1223 } else if (newColumObject.type == 2){ | |
1224 if (typeof dataObject[newColumObject.name] == "undefined"){ | |
1225 dataObject[newColumObject.name] = []; | |
1226 } | |
1227 dataObject[newColumObject.name][newColumObject.arrayIndex] = value; | |
1228 } else if (newColumObject.type == 3){ | |
1229 if (typeof dataObject[newColumObject.name] == "undefined"){ | |
1230 dataObject[newColumObject.name] = []; | |
1231 } | |
1232 if (typeof dataObject[newColumObject.name][newColumObject.arrayIndex] == "undefined"){ | |
1233 dataObject[newColumObject.name][newColumObject.arrayIndex] = {}; | |
1234 } | |
1235 dataObject[newColumObject.name][newColumObject.arrayIndex][newColumObject.attribute] = value; | |
1236 } | |
1237 } | |
1238 } | |
1239 | |
1240 //actually create new dataObjects | |
1241 for (var i = 0; i < dataset.objects.length; i++){ | |
1242 var dataObject = dataset.objects[i]; | |
1243 //save index | |
1244 var index = dataObject.index; | |
1245 | |
1246 dataset.objects[i] = new DataObject(dataObject.name, dataObject.description, dataObject.locations, | |
1247 dataObject.dates, dataObject.weight, dataObject.tableContent, dataObject.projection); | |
1248 //set index | |
1249 dataset.objects[i].setIndex(index); | |
1250 } | |
1251 }; |