comparison geotemco/js/Util/DataObject.js @ 0:57bde4830927

first commit
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Tue, 24 Mar 2015 11:37:17 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:57bde4830927
1 /*
2 * DataObject.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 DataObject
24 * GeoTemCo's data object class
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 * @param {String} name name of the data object
31 * @param {String} description description of the data object
32 * @param {JSON} locations a list of locations with longitude, latitide and place name
33 * @param {JSON} dates a list of dates
34 * @param {float} lon longitude value of the given place
35 * @param {float} lat latitude value of the given place
36 * @param {Date} timeStart start time of the data object
37 * @param {Date} timeEnd end time of the data object
38 * @param {int} granularity granularity of the given time
39 * @param {int} weight weight of the time object
40 * @param {Openlayers.Projection} projection of the coordinates (optional)
41 */
42
43 DataObject = function(name, description, locations, dates, weight, tableContent, projection) {
44
45 this.name = $.trim(name);
46 this.description = $.trim(description);
47 this.weight = weight;
48 this.tableContent = new Object();
49 var objectTableContent = this.tableContent;
50 for(key in tableContent){
51 value = tableContent[key];
52 objectTableContent[$.trim(key)]=$.trim(value);
53 }
54
55 this.percentage = 0;
56 this.setPercentage = function(percentage) {
57 this.percentage = percentage;
58 }
59
60 this.locations = [];
61 var objectLocations = this.locations;
62 $(locations).each(function(){
63 objectLocations.push({
64 latitude:this.latitude,
65 longitude:this.longitude,
66 place:$.trim(this.place)
67 });
68 });
69
70 //Check if locations are valid
71 if (!(projection instanceof OpenLayers.Projection)){
72 //per default GeoTemCo uses WGS84 (-90<=lat<=90, -180<=lon<=180)
73 projection = new OpenLayers.Projection("EPSG:4326");
74 }
75 this.projection = projection;
76
77 var tempLocations = [];
78 if (typeof this.locations !== "undefined"){
79 $(this.locations).each(function(){
80 //EPSG:4326 === WGS84
81 this.latitude = parseFloat(this.latitude);
82 this.longitude = parseFloat(this.longitude);
83 if (projection.getCode() === "EPSG:4326"){
84 if ( (typeof this.latitude === "number") &&
85 (this.latitude>=-90) &&
86 (this.latitude<=90) &&
87 (typeof this.longitude === "number") &&
88 (this.longitude>=-180) &&
89 (this.longitude<=180) )
90 tempLocations.push(this);
91 else{
92 if ((GeoTemConfig.debug)&&(typeof console !== undefined)){
93 console.error("Object " + name + " has no valid coordinate. ("+this.latitude+","+this.longitude+")");
94 }
95 }
96
97 //solve lat=-90 bug
98 if( this.longitude == 180 ){
99 this.longitude = 179.999;
100 }
101 if( this.longitude == -180 ){
102 this.longitude = -179.999;
103 }
104 if( this.latitude == 90 ){
105 this.latitude = 89.999;
106 }
107 if( this.latitude == -90 ){
108 this.latitude = -89.999;
109 }
110 }
111 });
112 this.locations = tempLocations;
113 }
114
115 this.isGeospatial = false;
116 if ((typeof this.locations !== "undefined") && (this.locations.length > 0)) {
117 this.isGeospatial = true;
118 }
119
120 this.placeDetails = [];
121 for (var i = 0; i < this.locations.length; i++) {
122 this.placeDetails.push(this.locations[i].place.split("/"));
123 }
124
125 this.getLatitude = function(locationId) {
126 return this.locations[locationId].latitude;
127 }
128
129 this.getLongitude = function(locationId) {
130 return this.locations[locationId].longitude;
131 }
132
133 this.getPlace = function(locationId, level) {
134 if (level >= this.placeDetails[locationId].length) {
135 return this.placeDetails[locationId][this.placeDetails[locationId].length - 1];
136 }
137 return this.placeDetails[locationId][level];
138 }
139
140 this.dates = dates;
141 this.isTemporal = false;
142 if ((typeof this.dates !== "undefined") && (this.dates.length > 0)) {
143 this.isTemporal = true;
144 //test if we already have date "objects" or if we should parse the dates
145 for (var i = 0; i < this.dates.length; i++){
146 if (typeof this.dates[i] === "string"){
147 var date = GeoTemConfig.getTimeData(this.dates[i]);
148 //check whether we got valid dates
149 if ((typeof date !== "undefined")&&(date != null)){
150 this.dates[i] = date;
151 } else {
152 //at least one date is invalid, so this dataObject has
153 //no valid date information and is therefor not "temporal"
154 this.isTemporal = false;
155 break;
156 }
157 }
158 }
159 }
160
161 //TODO: allow more than one timespan (as with dates/places)
162 this.isFuzzyTemporal = false;
163 if (this.isTemporal) {
164 this.isTemporal = false;
165 this.isFuzzyTemporal = true;
166
167 var date = this.dates[0].date;
168 var granularity = this.dates[0].granularity;
169
170 this.TimeSpanGranularity = granularity;
171
172 if (granularity === SimileAjax.DateTime.YEAR){
173 this.TimeSpanBegin = moment(date).startOf("year");
174 this.TimeSpanEnd = moment(date).endOf("year");
175 } else if (granularity === SimileAjax.DateTime.MONTH){
176 this.TimeSpanBegin = moment(date).startOf("month");
177 this.TimeSpanEnd = moment(date).endOf("month");
178 } else if (granularity === SimileAjax.DateTime.DAY){
179 this.TimeSpanBegin = moment(date).startOf("day");
180 this.TimeSpanEnd = moment(date).endOf("day");
181 } else if (granularity === SimileAjax.DateTime.HOUR){
182 this.TimeSpanBegin = moment(date).startOf("hour");
183 this.TimeSpanEnd = moment(date).endOf("hour");
184 } else if (granularity === SimileAjax.DateTime.MINUTE){
185 this.TimeSpanBegin = moment(date).startOf("minute");
186 this.TimeSpanEnd = moment(date).endOf("minute");
187 } else if (granularity === SimileAjax.DateTime.SECOND){
188 this.TimeSpanBegin = moment(date).startOf("second");
189 this.TimeSpanEnd = moment(date).endOf("second");
190 } else if (granularity === SimileAjax.DateTime.MILLISECOND){
191 //this is a "real" exact time
192 this.isTemporal = true;
193 this.isFuzzyTemporal = false;
194 }
195 } else if ( (typeof this.tableContent["TimeSpan:begin"] !== "undefined") &&
196 (typeof this.tableContent["TimeSpan:end"] !== "undefined") ){
197 //parse according to ISO 8601
198 //don't use the default "cross browser support" from moment.js
199 //cause it won't work correctly with negative years
200 var formats = [ "YYYYYY",
201 "YYYYYY-MM",
202 "YYYYYY-MM-DD",
203 "YYYYYY-MM-DDTHH",
204 "YYYYYY-MM-DDTHH:mm",
205 "YYYYYY-MM-DDTHH:mm:ss",
206 "YYYYYY-MM-DDTHH:mm:ss.SSS"
207 ];
208 this.TimeSpanBegin = moment(this.tableContent["TimeSpan:begin"],formats.slice());
209 this.TimeSpanEnd = moment(this.tableContent["TimeSpan:end"],formats.slice());
210 if ((this.TimeSpanBegin instanceof Object) && this.TimeSpanBegin.isValid() &&
211 (this.TimeSpanEnd instanceof Object) && this.TimeSpanEnd.isValid()){
212 //check whether dates are correctly sorted
213 if (this.TimeSpanBegin>this.TimeSpanEnd){
214 //dates are in the wrong order
215 if ((GeoTemConfig.debug)&&(typeof console !== undefined))
216 console.error("Object " + this.name + " has wrong fuzzy dating (twisted start/end?).");
217
218 } else {
219 var timeSpanBeginGranularity = formats.indexOf(this.TimeSpanBegin._f);
220 var timeSpanEndGranularity = formats.indexOf(this.TimeSpanEnd._f);
221 var timeSpanGranularity = Math.max( timeSpanBeginGranularity,
222 timeSpanEndGranularity );
223
224 //set granularity according to formats above
225 if (timeSpanGranularity === 0){
226 this.TimeSpanGranularity = SimileAjax.DateTime.YEAR;
227 } else if (timeSpanGranularity === 1){
228 this.TimeSpanGranularity = SimileAjax.DateTime.MONTH;
229 } else if (timeSpanGranularity === 2){
230 this.TimeSpanGranularity = SimileAjax.DateTime.DAY;
231 } else if (timeSpanGranularity === 3){
232 this.TimeSpanGranularity = SimileAjax.DateTime.HOUR;
233 } else if (timeSpanGranularity === 4){
234 this.TimeSpanGranularity = SimileAjax.DateTime.MINUTE;
235 } else if (timeSpanGranularity === 5){
236 this.TimeSpanGranularity = SimileAjax.DateTime.SECOND;
237 } else if (timeSpanGranularity === 6){
238 this.TimeSpanGranularity = SimileAjax.DateTime.MILLISECOND;
239 }
240
241 if (timeSpanBeginGranularity === 0){
242 this.TimeSpanBeginGranularity = SimileAjax.DateTime.YEAR;
243 } else if (timeSpanBeginGranularity === 1){
244 this.TimeSpanBeginGranularity = SimileAjax.DateTime.MONTH;
245 } else if (timeSpanBeginGranularity === 2){
246 this.TimeSpanBeginGranularity = SimileAjax.DateTime.DAY;
247 } else if (timeSpanBeginGranularity === 3){
248 this.TimeSpanBeginGranularity = SimileAjax.DateTime.HOUR;
249 } else if (timeSpanBeginGranularity === 4){
250 this.TimeSpanBeginGranularity = SimileAjax.DateTime.MINUTE;
251 } else if (timeSpanBeginGranularity === 5){
252 this.TimeSpanBeginGranularity = SimileAjax.DateTime.SECOND;
253 } else if (timeSpanBeginGranularity === 6){
254 this.TimeSpanBeginGranularity = SimileAjax.DateTime.MILLISECOND;
255 }
256
257 if (timeSpanEndGranularity === 0){
258 this.TimeSpanEndGranularity = SimileAjax.DateTime.YEAR;
259 } else if (timeSpanEndGranularity === 1){
260 this.TimeSpanEndGranularity = SimileAjax.DateTime.MONTH;
261 } else if (timeSpanEndGranularity === 2){
262 this.TimeSpanEndGranularity = SimileAjax.DateTime.DAY;
263 } else if (timeSpanEndGranularity === 3){
264 this.TimeSpanEndGranularity = SimileAjax.DateTime.HOUR;
265 } else if (timeSpanEndGranularity === 4){
266 this.TimeSpanEndGranularity = SimileAjax.DateTime.MINUTE;
267 } else if (timeSpanEndGranularity === 5){
268 this.TimeSpanEndGranularity = SimileAjax.DateTime.SECOND;
269 } else if (timeSpanEndGranularity === 6){
270 this.TimeSpanEndGranularity = SimileAjax.DateTime.MILLISECOND;
271 }
272
273 if (this.TimeSpanEnd.year()-this.TimeSpanBegin.year() >= 1000)
274 this.TimeSpanGranularity = SimileAjax.DateTime.MILLENNIUM;
275 else if (this.TimeSpanEnd.year()-this.TimeSpanBegin.year() >= 100)
276 this.TimeSpanGranularity = SimileAjax.DateTime.CENTURY;
277 else if (this.TimeSpanEnd.year()-this.TimeSpanBegin.year() >= 10)
278 this.TimeSpanGranularity = SimileAjax.DateTime.DECADE;
279
280 //also set upper bounds according to granularity
281 //(lower bound is already correct)
282 if (timeSpanEndGranularity === 0){
283 this.TimeSpanEnd.endOf("year");
284 } else if (timeSpanEndGranularity === 1){
285 this.TimeSpanEnd.endOf("month");
286 } else if (timeSpanEndGranularity === 2){
287 this.TimeSpanEnd.endOf("day");
288 } else if (timeSpanEndGranularity === 3){
289 this.TimeSpanEnd.endOf("hour");
290 } else if (timeSpanEndGranularity === 4){
291 this.TimeSpanEnd.endOf("minute");
292 } else if (timeSpanEndGranularity === 5){
293 this.TimeSpanEnd.endOf("second");
294 } else if (timeSpanEndGranularity === 6){
295 //has max accuracy, so no change needed
296 }
297
298 this.isFuzzyTemporal = true;
299 }
300 }
301 }
302
303
304 this.getDate = function(dateId) {
305 return this.dates[dateId].date;
306 }
307
308 this.getTimeGranularity = function(dateId) {
309 return this.dates[dateId].granularity;
310 }
311
312 this.setIndex = function(index) {
313 this.index = index;
314 }
315
316 this.getTimeString = function() {
317 if (this.timeStart != this.timeEnd) {
318 return (SimileAjax.DateTime.getTimeString(this.granularity, this.timeStart) + " - " + SimileAjax.DateTime.getTimeString(this.granularity, this.timeEnd));
319 } else {
320 return SimileAjax.DateTime.getTimeString(this.granularity, this.timeStart) + "";
321 }
322 };
323
324 this.contains = function(text) {
325 var allCombined = this.name + " " + this.description + " " + this.weight + " ";
326
327 $.each(this.dates, function(key, value){
328 $.each(value, function(){
329 allCombined += this + " ";
330 });
331 });
332
333 $.each(this.locations, function(key, value){
334 $.each(value, function(){
335 allCombined += this + " ";
336 });
337 });
338
339 $.each(this.tableContent, function(key, value){
340 allCombined += value + " ";
341 });
342
343 return (allCombined.indexOf(text) != -1);
344 };
345
346 this.hasColorInformation = false;
347
348 this.setColor = function(r0,g0,b0,r1,g1,b1) {
349 this.hasColorInformation = true;
350
351 this.color = new Object();
352 this.color.r0 = r0;
353 this.color.g0 = g0;
354 this.color.b0 = b0;
355 this.color.r1 = r1;
356 this.color.g1 = g1;
357 this.color.b1 = b1;
358 };
359
360 this.getColor = function() {
361 if (!this.hasColorInformation)
362 return;
363
364 color = new Object();
365 color.r0 = this.r0;
366 color.g0 = this.g0;
367 color.b0 = this.b0;
368 color.r1 = this.r1;
369 color.g1 = this.g1;
370 color.b1 = this.b1;
371
372 return color;
373 };
374
375 Publisher.Publish('dataobjectAfterCreation', this);
376 };
377