comparison gnv-artifacts/src/main/java/de/intevation/gnv/statistics/VectorStatistics.java @ 1119:7c4f81f74c47

merged gnv-artifacts
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:00 +0200
parents f953c9a559d8
children
comparison
equal deleted inserted replaced
1027:fca4b5eb8d2f 1119:7c4f81f74c47
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.gnv.statistics;
10
11 import de.intevation.gnv.geobackend.base.Result;
12 import de.intevation.gnv.geobackend.base.ResultDescriptor;
13
14 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
15
16 import de.intevation.gnv.statistics.exception.StatisticsException;
17
18 import java.sql.SQLException;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Iterator;
23
24 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
25
26 import org.apache.commons.math.stat.regression.SimpleRegression;
27
28 import org.apache.log4j.Logger;
29
30
31 /**
32 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
33 */
34 public abstract class VectorStatistics
35 extends AbstractStatistics
36 {
37 private static Logger logger =
38 Logger.getLogger(VectorStatistics.class);
39
40 private ResultDescriptor rd;
41
42
43 public VectorStatistics() {
44 }
45
46
47 public Collection<StatisticSet> calculateStatistics(
48 Object result,
49 Collection<KeyValueDescibeData> parameters,
50 Collection<KeyValueDescibeData> measurements,
51 Collection<KeyValueDescibeData> dates
52 ) throws StatisticsException {
53 if (!(result instanceof Collection))
54 return new ArrayList<StatisticSet>();
55
56 Collection<Result> results = (Collection<Result>) result;
57 Collection<StatisticSet> stats = new ArrayList<StatisticSet>();
58
59 SimpleRegression lRegression = new SimpleRegression();
60 DescriptiveStatistics lStatistics = new DescriptiveStatistics();
61
62 Result previous = null;
63 Result row = null;
64 String seriesName = null;
65 int valueIdx = -1;
66 int seriesIdx = -1;
67
68 Iterator<Result> iter = results.iterator();
69
70 while (iter.hasNext()) {
71 row = iter.next();
72 previous = previous == null ? row : previous;
73
74 if (rd == null || seriesIdx == -1) {
75 rd = row.getResultDescriptor();
76 valueIdx = getValueIdx(rd);
77 seriesIdx = rd.getColumnIndex("SERIES");
78 }
79
80 if (seriesName != null
81 && !row.getString(seriesIdx).equals(seriesName))
82 {
83 stats.add(generateStatisticsValues(
84 lStatistics, lRegression, seriesName));
85
86 lStatistics.clear();
87 lRegression.clear();
88
89 clear();
90 }
91
92 Double yValue = getValue(row, valueIdx);
93
94 if (yValue != null) {
95 lStatistics.addValue(yValue);
96
97 try {
98 double x = calculateXOrdinateValue(previous, row);
99 lRegression.addData(x, yValue);
100 }
101 catch (SQLException sqle) {
102 logger.warn(sqle, sqle);
103 }
104 }
105
106 seriesName = row.getString(seriesIdx);
107 previous = row;
108 }
109
110 if (seriesName != null && row != null) {
111 stats.add(generateStatisticsValues(
112 lStatistics, lRegression, seriesName));
113
114 lStatistics.clear();
115 lRegression.clear();
116 }
117
118 return stats;
119 }
120
121
122 protected Double getValue(Result row, int idx) {
123 return row.getDouble(idx);
124 }
125
126
127 abstract protected void clear();
128
129 abstract protected int getValueIdx(ResultDescriptor rd);
130
131 abstract protected double calculateXOrdinateValue(Result prev, Result now)
132 throws SQLException;
133 }
134 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org