Revision 4054 branches/v05/extensions/extWMS/src/com/iver/cit/gvsig/fmap/layers/TimeDimension.java
TimeDimension.java | ||
---|---|---|
43 | 43 |
* |
44 | 44 |
* $Id$ |
45 | 45 |
* $Log$ |
46 |
* Revision 1.3.2.3 2006-01-31 16:25:24 jaume |
|
46 |
* Revision 1.3.2.4 2006-02-10 13:22:35 jaume |
|
47 |
* now analyzes dimensions on demand |
|
48 |
* |
|
49 |
* Revision 1.3.2.3 2006/01/31 16:25:24 jaume |
|
47 | 50 |
* correcciones de bugs |
48 | 51 |
* |
49 | 52 |
* Revision 1.4 2006/01/26 16:07:14 jaume |
... | ... | |
177 | 180 |
private String period; |
178 | 181 |
private long step; // Distance between two points in milliseconds. |
179 | 182 |
private int type; |
183 |
private boolean compiled = false; |
|
180 | 184 |
|
181 | 185 |
/** |
182 | 186 |
* Creates a new instance of TimeDimension. |
... | ... | |
233 | 237 |
} |
234 | 238 |
|
235 | 239 |
public Object valueOf(String value) throws IllegalArgumentException { |
236 |
// TODO Missing geological dates |
|
237 |
String myValue = value.toUpperCase(); |
|
238 |
if (isValidValue(myValue)) { |
|
239 |
Object val = null; |
|
240 |
if (!isGeologic){ |
|
241 |
// This is a normal date |
|
242 |
int myYear; |
|
243 |
int myMonth; |
|
244 |
int myDay; |
|
245 |
int myHour; |
|
246 |
int myMinute; |
|
247 |
float mySecond; |
|
248 |
String[] s = myValue.split("-"); |
|
249 |
myYear = (s[0].charAt(0)=='B')? -Integer.parseInt(s[0].substring(1, 5)) : Integer.parseInt(s[0].substring(0, 4)); |
|
250 |
myMonth = (s.length>1) ? Integer.parseInt(s[1])-1 : 0; |
|
251 |
if (myValue.matches(regexDateExtendedForBCE4)){ |
|
252 |
if (s[2].endsWith("Z")) |
|
253 |
s[2] = s[2].substring(0,s[2].length()-1); |
|
254 |
s = (s[2].indexOf('T')!=-1) ? s[2].split("T") : s[2].split(" "); |
|
255 |
myDay = Integer.parseInt(s[0]); |
|
256 |
|
|
257 |
// Go with the time |
|
258 |
s = s[1].split(":"); |
|
259 |
myHour = Integer.parseInt(s[0]); |
|
260 |
myMinute = (s.length>1) ? Integer.parseInt(s[1]) : 0; |
|
261 |
mySecond = (s.length>2) ? Float.parseFloat(s[2]) : 0; |
|
262 |
} else { |
|
263 |
myDay = (s.length>2) ? Integer.parseInt(s[2]) : 1; |
|
264 |
|
|
265 |
myHour = 0; |
|
266 |
myMinute = 0; |
|
267 |
mySecond = 0; |
|
268 |
} |
|
269 |
GregorianCalendar cal = new GregorianCalendar(myYear, myMonth, myDay, myHour, myMinute, (int)mySecond); |
|
270 |
val = cal; |
|
271 |
} else{ |
|
272 |
// this is a geological date >:-( |
|
273 |
} |
|
274 |
return val; |
|
275 |
} else throw new IllegalArgumentException(myValue); |
|
276 |
|
|
240 |
if (compiled) { |
|
241 |
// TODO Missing geological dates |
|
242 |
String myValue = value.toUpperCase(); |
|
243 |
if (isValidValue(myValue)) { |
|
244 |
Object val = null; |
|
245 |
if (!isGeologic){ |
|
246 |
// This is a normal date |
|
247 |
int myYear; |
|
248 |
int myMonth; |
|
249 |
int myDay; |
|
250 |
int myHour; |
|
251 |
int myMinute; |
|
252 |
float mySecond; |
|
253 |
String[] s = myValue.split("-"); |
|
254 |
myYear = (s[0].charAt(0)=='B')? -Integer.parseInt(s[0].substring(1, 5)) : Integer.parseInt(s[0].substring(0, 4)); |
|
255 |
myMonth = (s.length>1) ? Integer.parseInt(s[1])-1 : 0; |
|
256 |
if (myValue.matches(regexDateExtendedForBCE4)){ |
|
257 |
if (s[2].endsWith("Z")) |
|
258 |
s[2] = s[2].substring(0,s[2].length()-1); |
|
259 |
s = (s[2].indexOf('T')!=-1) ? s[2].split("T") : s[2].split(" "); |
|
260 |
myDay = Integer.parseInt(s[0]); |
|
261 |
|
|
262 |
// Go with the time |
|
263 |
s = s[1].split(":"); |
|
264 |
myHour = Integer.parseInt(s[0]); |
|
265 |
myMinute = (s.length>1) ? Integer.parseInt(s[1]) : 0; |
|
266 |
mySecond = (s.length>2) ? Float.parseFloat(s[2]) : 0; |
|
267 |
} else { |
|
268 |
myDay = (s.length>2) ? Integer.parseInt(s[2]) : 1; |
|
269 |
|
|
270 |
myHour = 0; |
|
271 |
myMinute = 0; |
|
272 |
mySecond = 0; |
|
273 |
} |
|
274 |
GregorianCalendar cal = new GregorianCalendar(myYear, myMonth, myDay, myHour, myMinute, (int)mySecond); |
|
275 |
val = cal; |
|
276 |
} else{ |
|
277 |
// this is a geological date >:-( |
|
278 |
} |
|
279 |
return val; |
|
280 |
} else throw new IllegalArgumentException(myValue); |
|
281 |
} |
|
282 |
return null; |
|
277 | 283 |
} |
278 | 284 |
|
279 | 285 |
public String valueAt(int pos) throws ArrayIndexOutOfBoundsException { |
280 |
if (pos<0 || pos>valueCount()) |
|
281 |
throw new ArrayIndexOutOfBoundsException(pos+"(must be >= 0 and <="+valueCount()+")"); |
|
282 |
|
|
283 |
if (type == SINGLE_VALUE) |
|
284 |
return expression; |
|
285 |
|
|
286 |
if (type == MULTIPLE_VALUE) |
|
287 |
return expression.split(",")[pos]; |
|
288 |
|
|
289 |
if (!isGeologic){ |
|
290 |
long newTime = ((GregorianCalendar) minValue).getTimeInMillis(); |
|
291 |
newTime += (step*pos); |
|
292 |
GregorianCalendar cal = new GregorianCalendar(); |
|
293 |
cal.setTimeInMillis(newTime); |
|
294 |
if (cal.after(maxValue)) |
|
295 |
return toString((GregorianCalendar) maxValue); |
|
296 |
else if (cal.before(minValue)) |
|
297 |
return toString((GregorianCalendar) minValue); |
|
298 |
else |
|
299 |
return toString(cal); |
|
300 |
} |
|
286 |
if (compiled) { |
|
287 |
if (pos<0 || pos>valueCount()) |
|
288 |
throw new ArrayIndexOutOfBoundsException(pos+"(must be >= 0 and <="+valueCount()+")"); |
|
289 |
|
|
290 |
if (type == SINGLE_VALUE) |
|
291 |
return expression; |
|
292 |
|
|
293 |
if (type == MULTIPLE_VALUE) |
|
294 |
return expression.split(",")[pos]; |
|
295 |
|
|
296 |
if (!isGeologic){ |
|
297 |
long newTime = ((GregorianCalendar) minValue).getTimeInMillis(); |
|
298 |
newTime += (step*pos); |
|
299 |
GregorianCalendar cal = new GregorianCalendar(); |
|
300 |
cal.setTimeInMillis(newTime); |
|
301 |
if (cal.after(maxValue)) |
|
302 |
return toString((GregorianCalendar) maxValue); |
|
303 |
else if (cal.before(minValue)) |
|
304 |
return toString((GregorianCalendar) minValue); |
|
305 |
else |
|
306 |
return toString(cal); |
|
307 |
} |
|
308 |
} |
|
301 | 309 |
return null; |
302 | 310 |
} |
303 | 311 |
|
... | ... | |
337 | 345 |
} |
338 | 346 |
|
339 | 347 |
public int valueCount() { |
340 |
if (valueCount==null){ |
|
341 |
if (type == MULTIPLE_VALUE) { |
|
342 |
return expression.split(",").length; |
|
343 |
} else if (type == INTERVAL) { |
|
344 |
if (period == null) { |
|
345 |
valueCount = new Integer(0); |
|
346 |
return valueCount.intValue(); |
|
347 |
} |
|
348 |
|
|
349 |
if (!isGeologic){ |
|
350 |
|
|
351 |
long x1 = ((GregorianCalendar) maxValue).getTimeInMillis(); |
|
352 |
long x0 = ((GregorianCalendar) minValue).getTimeInMillis(); |
|
353 |
long distance = x1-x0; |
|
354 |
step = 0; |
|
355 |
|
|
356 |
boolean isTimeField = false; |
|
357 |
String val = ""; |
|
358 |
|
|
359 |
for (int i = 0; i < period.length(); i++) { |
|
360 |
if (period.charAt(i) == 'P') |
|
361 |
continue; |
|
362 |
if (period.charAt(i) == 'T'){ |
|
363 |
isTimeField = true; |
|
364 |
continue; |
|
365 |
} |
|
366 |
switch (period.charAt(i)){ |
|
367 |
case 'Y': |
|
368 |
step += Integer.parseInt(val) * millisXyear; |
|
369 |
val = ""; |
|
370 |
break; |
|
371 |
case 'M': |
|
372 |
if (isTimeField) |
|
373 |
step += Integer.parseInt(val) * millisXminute; |
|
374 |
else |
|
375 |
step += Integer.parseInt(val) * millisXmonth; |
|
376 |
val = ""; |
|
377 |
break; |
|
378 |
case 'D': |
|
379 |
step += Integer.parseInt(val) * millisXday; |
|
380 |
val = ""; |
|
381 |
break; |
|
382 |
case 'H': |
|
383 |
step += Integer.parseInt(val) * millisXhour; |
|
384 |
val = ""; |
|
385 |
break; |
|
386 |
case 'S': |
|
387 |
step += Integer.parseInt(val) * 1000; |
|
388 |
val = ""; |
|
389 |
break; |
|
390 |
default: |
|
391 |
val += period.charAt(i); |
|
392 |
break; |
|
393 |
} |
|
394 |
} |
|
395 |
valueCount = new Integer((int)(distance/step) + 1); // + 1 for the initial point |
|
396 |
} |
|
397 |
} else { |
|
398 |
// this is a single value expression |
|
399 |
valueCount = new Integer(1); |
|
400 |
return valueCount.intValue(); |
|
401 |
} |
|
402 |
} |
|
403 |
return valueCount.intValue(); |
|
348 |
if (compiled) { |
|
349 |
if (valueCount==null){ |
|
350 |
if (type == MULTIPLE_VALUE) { |
|
351 |
return expression.split(",").length; |
|
352 |
} else if (type == INTERVAL) { |
|
353 |
if (period == null) { |
|
354 |
valueCount = new Integer(0); |
|
355 |
return valueCount.intValue(); |
|
356 |
} |
|
357 |
|
|
358 |
if (!isGeologic){ |
|
359 |
|
|
360 |
long x1 = ((GregorianCalendar) maxValue).getTimeInMillis(); |
|
361 |
long x0 = ((GregorianCalendar) minValue).getTimeInMillis(); |
|
362 |
long distance = x1-x0; |
|
363 |
step = 0; |
|
364 |
|
|
365 |
boolean isTimeField = false; |
|
366 |
String val = ""; |
|
367 |
|
|
368 |
for (int i = 0; i < period.length(); i++) { |
|
369 |
if (period.charAt(i) == 'P') |
|
370 |
continue; |
|
371 |
if (period.charAt(i) == 'T'){ |
|
372 |
isTimeField = true; |
|
373 |
continue; |
|
374 |
} |
|
375 |
switch (period.charAt(i)){ |
|
376 |
case 'Y': |
|
377 |
step += Integer.parseInt(val) * millisXyear; |
|
378 |
val = ""; |
|
379 |
break; |
|
380 |
case 'M': |
|
381 |
if (isTimeField) |
|
382 |
step += Integer.parseInt(val) * millisXminute; |
|
383 |
else |
|
384 |
step += Integer.parseInt(val) * millisXmonth; |
|
385 |
val = ""; |
|
386 |
break; |
|
387 |
case 'D': |
|
388 |
step += Integer.parseInt(val) * millisXday; |
|
389 |
val = ""; |
|
390 |
break; |
|
391 |
case 'H': |
|
392 |
step += Integer.parseInt(val) * millisXhour; |
|
393 |
val = ""; |
|
394 |
break; |
|
395 |
case 'S': |
|
396 |
step += Integer.parseInt(val) * 1000; |
|
397 |
val = ""; |
|
398 |
break; |
|
399 |
default: |
|
400 |
val += period.charAt(i); |
|
401 |
break; |
|
402 |
} |
|
403 |
} |
|
404 |
valueCount = new Integer((int)(distance/step) + 1); // + 1 for the initial point |
|
405 |
} |
|
406 |
} else { |
|
407 |
// this is a single value expression |
|
408 |
valueCount = new Integer(1); |
|
409 |
return valueCount.intValue(); |
|
410 |
} |
|
411 |
} |
|
412 |
|
|
413 |
return valueCount.intValue(); |
|
414 |
} |
|
415 |
return -1; |
|
404 | 416 |
} |
405 | 417 |
|
406 |
public void setExpression(String expr) throws IllegalArgumentException {
|
|
418 |
public void setExpression(String expr) { |
|
407 | 419 |
expression = expr.toUpperCase(); |
408 |
long t1 = System.currentTimeMillis(); |
|
409 |
if (expression.matches(regexTimeDimension)){ |
|
420 |
} |
|
421 |
|
|
422 |
public String getExpression() { |
|
423 |
return expression; |
|
424 |
} |
|
425 |
|
|
426 |
public int getType() { |
|
427 |
return type; |
|
428 |
} |
|
429 |
|
|
430 |
public void compile() { |
|
431 |
if (expression.matches(regexTimeDimension)){ |
|
410 | 432 |
isGeologic = false; |
411 | 433 |
} else if (expression.matches(regexDateExtendedForBCE)) { |
412 | 434 |
isGeologic = false; |
... | ... | |
415 | 437 |
} else { |
416 | 438 |
throw new IllegalArgumentException(); |
417 | 439 |
} |
418 |
long t2 = System.currentTimeMillis();
|
|
419 |
String separator; |
|
440 |
String separator;
|
|
441 |
|
|
420 | 442 |
if (expression.indexOf("/")!=-1) { |
421 | 443 |
separator = "/"; |
422 | 444 |
type = INTERVAL; |
... | ... | |
428 | 450 |
type = SINGLE_VALUE; |
429 | 451 |
} |
430 | 452 |
|
431 |
|
|
453 |
compiled = true; |
|
432 | 454 |
String[] s = expression.split(separator); |
433 | 455 |
minValue = valueOf(s[0]); |
434 | 456 |
if (type == INTERVAL) { |
... | ... | |
439 | 461 |
} else { |
440 | 462 |
maxValue = valueOf(s[0]); |
441 | 463 |
} |
442 |
|
|
443 |
long t3 = System.currentTimeMillis(); |
|
444 |
// System.err.println("TimeDimension>>> temps per a recon?ixer l'expressi?: "+(t2-t1)); |
|
445 |
// System.err.println("TimeDimension>>> temps total (calculant valors extrems: "+(t3-t1)); |
|
446 |
} |
|
447 |
|
|
448 |
public String getExpression() { |
|
449 |
return expression; |
|
464 |
|
|
465 |
|
|
466 |
|
|
450 | 467 |
} |
451 |
|
|
452 |
public int getType() { |
|
453 |
return type; |
|
454 |
} |
|
455 |
|
|
456 | 468 |
} |
Also available in: Unified diff