changeset 33:24c45a9048f2

corrected FileReader usage (onloaded callback) for local KML support added eror messages to KML parser
author Sebastian Kruse <skruse@mpiwg-berlin.mpg.de>
date Fri, 30 Nov 2012 17:03:17 +0100
parents f635bbdc6a03
children 5ef409e34638
files war/scripts/sti/STICore.js
diffstat 1 files changed, 71 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/war/scripts/sti/STICore.js	Fri Nov 30 15:17:25 2012 +0100
+++ b/war/scripts/sti/STICore.js	Fri Nov 30 17:03:17 2012 +0100
@@ -320,19 +320,22 @@
 				return timeData.concat(dayData);
 			} 
 			catch (exception) {
+				core.alert("\""+timeString+"\" is no valid time/date");
 				return null;
 			}
 		}
 
+		var core = this;
+
 		var time = SimileAjax.DateTime;     
         var newObjects = new Array();
         var elements = xml.getElementsByTagName("Placemark");
         var granularity = 0;
 
-	if( elements.length == 0 ){
-		this.unblockUI();
-		return;
-	}
+		if( elements.length == 0 ){
+			this.unblockUI();
+			return;
+		}
 
         for (var i = 0; i < elements.length; i++) {
 
@@ -376,31 +379,37 @@
 			}
 
 			var getDate = function(timeData){
-				var date;
-				if (timeData.length == 6) {
-                	g = time.SECOND;		
-                	date = new Date(Date.UTC(timeData[0], timeData[1]-1, timeData[2], timeData[3], timeData[4], timeData[5]));
-				}
-            	else if (timeData.length == 3) {
-                	g = time.DAY;
-                	date = new Date(Date.UTC(timeData[0], timeData[1]-1, timeData[2]));
-            	}
-				else if (timeData.length == 2) {
-                   	g = time.MONTH;
-                   	date = new Date(Date.UTC(timeData[0], timeData[1]-1, 1));
-              	}
-				else if (timeData.length == 1) {
-                   	g = time.YEAR;
-                   	date = new Date(Date.UTC(timeData[0], 0, 1));
-              	}
-            	if (granularity < g) {
-                	granularity = g;
-            	}
-		if( timeData[0] && timeData[0] < 100 ){
-			date.setFullYear(timeData[0]);
-		}
-            	return date;
-            }			
+				try {
+					var date;
+					if (timeData.length == 6) {
+	                	g = time.SECOND;		
+	                	date = new Date(Date.UTC(timeData[0], timeData[1]-1, timeData[2], timeData[3], timeData[4], timeData[5]));
+					}
+	            	else if (timeData.length == 3) {
+	                	g = time.DAY;
+	                	date = new Date(Date.UTC(timeData[0], timeData[1]-1, timeData[2]));
+	            	}
+					else if (timeData.length == 2) {
+	                   	g = time.MONTH;
+	                   	date = new Date(Date.UTC(timeData[0], timeData[1]-1, 1));
+	              	}
+					else if (timeData.length == 1) {
+	                   	g = time.YEAR;
+	                   	date = new Date(Date.UTC(timeData[0], 0, 1));
+	              	}
+	            	if (granularity < g) {
+	                	granularity = g;
+	            	}
+					if( timeData[0] && timeData[0] < 100 ){
+						date.setFullYear(timeData[0]);
+					}
+	            	return date;
+				} catch (e) {
+					core.alert("\""+timeData+"\" is no valid time/date");
+	            	
+	            	return null;
+	            }
+            }
 
 			try {
 				timeData = getTimeData( placemark.getElementsByTagName("TimeStamp")[0].getElementsByTagName("when")[0].childNodes[0].nodeValue );
@@ -422,6 +431,8 @@
 					timeSpan = { start: startTime, end: endTime };					
 				}
 				catch(e){
+					core.alert("No valid timestamp or timespan information for element " + i + ".");
+
 					continue;
 				}
 			}
@@ -435,20 +446,21 @@
             	newObjects.push(new DataObject(name, description, place, timeStamp, timeSpan, g, lonlat[0], lonlat[1]));
 			}
 			catch(e){
+				core.alert("No valid coordinate information for element " + i + ".");
+
 				continue;
 			}
+        }
 
-        }
-	var dataset = new DataSet( newObjects, term, granularity );
-	dataset.source = ds;
-	var core = this;
-	var status = document.getElementById("statusText");
-	status.innerHTML = "Space & Time Aggregation ...";
-	setTimeout( function(){ 
-				core.addDataSet( dataset );
-				core.unblockUI();
-			},
-		1 );
+		var dataset = new DataSet( newObjects, term, granularity );
+		dataset.source = ds;
+		var status = document.getElementById("statusText");
+		status.innerHTML = "Space & Time Aggregation ...";
+		setTimeout( function(){ 
+						core.addDataSet( dataset );
+						core.unblockUI();
+					},
+					1 );
 	},
     
     /**
@@ -476,11 +488,27 @@
 			var file = filelist[0];
 			
 			var reader = new FileReader();
+			
+			reader.onloadend = (function(theFile) {
+		        return function(e) {
+					status.innerHTML = "Parsing Data ...";
+					
+					var localkmlDoc;
+					try {
+						localkmlDoc = $.parseXML(reader.result);
+					} catch (e) {
+						core.alert("KML file is not valid XML. Please check opening/closing tags and check the spelling.");
+						
+						core.unblockUI();
+						
+						return;
+					}
+					
+					setTimeout( function(){ core.parseIt(localkmlDoc,filename,ds); }, 1 );
+				};
+		    })(file);
 						
 			reader.readAsText(file);
-
-			status.innerHTML = "Parsing Data ...";
-			core.parseIt($.parseXML(reader.result),filename,ds);
 		}
 
 		return;