Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/histogram/HistogramHelper.java @ 657:af3f56758f59
merged gnv-artifacts/0.5
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:53 +0200 |
parents | 3d13fa281a7e |
children | d70cac2bafc0 |
comparison
equal
deleted
inserted
replaced
590:5f5f273c8566 | 657:af3f56758f59 |
---|---|
1 package de.intevation.gnv.histogram; | |
2 | |
3 import de.intevation.gnv.geobackend.base.Result; | |
4 import de.intevation.gnv.geobackend.base.ResultDescriptor; | |
5 | |
6 import de.intevation.gnv.state.describedata.KeyValueDescibeData; | |
7 | |
8 import java.util.ArrayList; | |
9 import java.util.Collection; | |
10 import java.util.Iterator; | |
11 import java.util.List; | |
12 | |
13 import org.apache.log4j.Logger; | |
14 | |
15 | |
16 /** | |
17 * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) | |
18 */ | |
19 public class HistogramHelper { | |
20 | |
21 private static Logger logger = Logger.getLogger(HistogramHelper.class); | |
22 | |
23 | |
24 private HistogramHelper() { | |
25 } | |
26 | |
27 | |
28 /* | |
29 public static Object[][] prepareHistogramData( | |
30 Collection input, | |
31 Collection parameters, | |
32 Collection measurements, | |
33 Collection dates | |
34 ) { | |
35 List<Double> values = new ArrayList<Double>(); | |
36 | |
37 ResultDescriptor rd; | |
38 int b1Idx, b2Idx, b3Idx, yIdx; | |
39 | |
40 Iterator it = parameters.iterator(); | |
41 KeyValueDescibeData param = (KeyValueDescibeData) it.hasNext(); | |
42 int parameter = param.getValue(); | |
43 | |
44 Iterator iter = input.iterator(); | |
45 while (iter.hasNext()) { | |
46 Result row = (Result) iter.next(); | |
47 | |
48 if (rd == null) { | |
49 rd = row.getResultDescriptor(); | |
50 b1Idx = rd.getColumnIndex("GROUP1"); | |
51 b2Idx = rd.getColumnIndex("GROUP2"); | |
52 b3Idx = rd.getColumnIndex("GROUP3"); | |
53 } | |
54 | |
55 if (row.getInteger(b1Idx) == parameter | |
56 && row.getInteger(b2Idx) == measurement) | |
57 { | |
58 | |
59 } | |
60 } | |
61 | |
62 return null; | |
63 } | |
64 */ | |
65 | |
66 public static Object[][] prepareHistogramData( | |
67 Collection input, | |
68 Collection parameters, | |
69 Collection measurements, | |
70 Collection dates | |
71 ) { | |
72 List names = new ArrayList<String>(); | |
73 List data = new ArrayList<Double[]>(); | |
74 | |
75 if (logger.isDebugEnabled()) { | |
76 logger.debug("############ prepare histogram data ##########"); | |
77 logger.debug("Input data size: " + input.size()); | |
78 } | |
79 | |
80 if (input == null) { | |
81 return new Object[0][0]; | |
82 } | |
83 | |
84 String break1, break2, break3; | |
85 int b1Idx = -1; | |
86 int b2Idx = -1; | |
87 int b3Idx = -1; | |
88 int yIdx = -1; | |
89 try { | |
90 Iterator iter = input.iterator(); | |
91 | |
92 if (iter.hasNext()) { | |
93 Result row = (Result) iter.next(); | |
94 Result previousRow = row; | |
95 | |
96 if (b1Idx == -1) { | |
97 ResultDescriptor rd = row.getResultDescriptor(); | |
98 b1Idx = rd.getColumnIndex("GROUP1"); | |
99 b2Idx = rd.getColumnIndex("GROUP2"); | |
100 b3Idx = rd.getColumnIndex("GROUP3"); | |
101 yIdx = rd.getColumnIndex("YORDINATE"); | |
102 | |
103 if (b1Idx == -1 || b2Idx == -1 || b3Idx == -1 || yIdx == -1) { | |
104 return new Object[0][0]; | |
105 } | |
106 } | |
107 break1 = row.getString(b1Idx); | |
108 break2 = row.getString(b2Idx); | |
109 break3 = row.getString(b3Idx); | |
110 | |
111 List values = new ArrayList<Double>(); | |
112 while (iter.hasNext()) { | |
113 | |
114 // found new series | |
115 if (!break1.equals(row.getString(b1Idx)) | |
116 || !break2.equals(row.getString(b2Idx)) | |
117 || !break3.equals(row.getString(b3Idx)) | |
118 ) { | |
119 | |
120 // get parameter name | |
121 String name = generateName( | |
122 break1, break2, break3, | |
123 parameters, measurements, dates | |
124 ); | |
125 | |
126 // add values and parameter name | |
127 data.add((Double[]) values.toArray(new Double[values.size()])); | |
128 names.add(name); | |
129 | |
130 if (logger.isDebugEnabled()) { | |
131 logger.debug(" --- series name: " + name); | |
132 logger.debug(" --- series items: " + values.size()); | |
133 } | |
134 | |
135 values.clear(); | |
136 | |
137 Double yValue = row.getDouble(yIdx); | |
138 if (yValue != null) | |
139 values.add(yValue); | |
140 | |
141 // set new conditions to find new series | |
142 break1 = row.getString(b1Idx); | |
143 break2 = row.getString(b2Idx); | |
144 break3 = row.getString(b3Idx); | |
145 | |
146 previousRow = row; | |
147 row = (Result) iter.next(); | |
148 } else { | |
149 | |
150 Double value = row.getDouble(yIdx); | |
151 if (value != null) | |
152 values.add(value); | |
153 | |
154 row = (Result) iter.next(); | |
155 } | |
156 } | |
157 | |
158 Double yValue = row.getDouble(yIdx); | |
159 if (yValue != null) | |
160 values.add(yValue); | |
161 | |
162 String name = generateName( | |
163 break1, break2, break3, parameters, measurements, dates); | |
164 | |
165 if (logger.isDebugEnabled()) { | |
166 logger.debug(" --- series name: " + name); | |
167 logger.debug(" --- series items: " + values.size()); | |
168 } | |
169 | |
170 data.add((Double[]) values.toArray(new Double[values.size()])); | |
171 names.add(name); | |
172 } | |
173 } | |
174 catch (Exception e) { | |
175 logger.error(e.getMessage(), e); | |
176 } | |
177 | |
178 int series = data.size(); | |
179 logger.debug(" === Found total: " + series); | |
180 Object[][] obj = new Object[series][2]; | |
181 for (int i = 0; i < series; i++) { | |
182 obj[i][0] = names.get(i); | |
183 obj[i][1] = (Double[]) data.get(i); | |
184 } | |
185 | |
186 return obj; | |
187 } | |
188 | |
189 | |
190 protected static String generateName( | |
191 String break1, String break2, String break3, | |
192 Collection parameters, Collection measurements, Collection dates) | |
193 { | |
194 return findValueTitle(parameters,break1) + " " + | |
195 findValueTitle(measurements,break2) + "m"; | |
196 } | |
197 | |
198 | |
199 protected static String findValueTitle(Collection values, String id) { | |
200 if (values != null) { | |
201 Iterator it = values.iterator(); | |
202 | |
203 while (it.hasNext()) { | |
204 KeyValueDescibeData data = (KeyValueDescibeData) it.next(); | |
205 | |
206 if (id.equals(data.getKey())) { | |
207 return data.getValue(); | |
208 } | |
209 } | |
210 } | |
211 return ""; | |
212 } | |
213 } | |
214 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |