Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java @ 1034:50a5ce7a47b7
Implemented an odv exporter for product type 'Horizontales Schnittprofil' (issue260).
gnv-artifacts/trunk@1082 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Mon, 10 May 2010 10:29:55 +0000 |
parents | c07d9f9a738c |
children | cc4ec127d666 |
comparison
equal
deleted
inserted
replaced
1033:35c442410738 | 1034:50a5ce7a47b7 |
---|---|
1 package de.intevation.gnv.state.profile.horizontal; | 1 package de.intevation.gnv.state.profile.horizontal; |
2 | |
3 import au.com.bytecode.opencsv.CSVWriter; | |
2 | 4 |
3 import com.vividsolutions.jts.geom.Coordinate; | 5 import com.vividsolutions.jts.geom.Coordinate; |
4 | 6 |
5 import de.intevation.artifactdatabase.Config; | 7 import de.intevation.artifactdatabase.Config; |
6 | 8 |
11 import de.intevation.gnv.artifacts.context.GNVArtifactContext; | 13 import de.intevation.gnv.artifacts.context.GNVArtifactContext; |
12 | 14 |
13 import de.intevation.gnv.chart.Chart; | 15 import de.intevation.gnv.chart.Chart; |
14 import de.intevation.gnv.chart.ChartLabels; | 16 import de.intevation.gnv.chart.ChartLabels; |
15 import de.intevation.gnv.chart.HorizontalCrossProfileChart; | 17 import de.intevation.gnv.chart.HorizontalCrossProfileChart; |
18 | |
19 import de.intevation.gnv.exports.DefaultProfile; | |
20 import de.intevation.gnv.exports.Export; | |
21 import de.intevation.gnv.exports.StringArrayKey; | |
16 | 22 |
17 import de.intevation.gnv.geobackend.base.DefaultResult; | 23 import de.intevation.gnv.geobackend.base.DefaultResult; |
18 import de.intevation.gnv.geobackend.base.DefaultResultDescriptor; | 24 import de.intevation.gnv.geobackend.base.DefaultResultDescriptor; |
19 import de.intevation.gnv.geobackend.base.Result; | 25 import de.intevation.gnv.geobackend.base.Result; |
20 import de.intevation.gnv.geobackend.base.ResultDescriptor; | 26 import de.intevation.gnv.geobackend.base.ResultDescriptor; |
27 import de.intevation.gnv.math.Interpolation2D; | 33 import de.intevation.gnv.math.Interpolation2D; |
28 import de.intevation.gnv.math.LinearMetrics; | 34 import de.intevation.gnv.math.LinearMetrics; |
29 import de.intevation.gnv.math.Point2d; | 35 import de.intevation.gnv.math.Point2d; |
30 | 36 |
31 import de.intevation.gnv.state.InputData; | 37 import de.intevation.gnv.state.InputData; |
38 import de.intevation.gnv.state.describedata.KeyValueDescibeData; | |
39 import de.intevation.gnv.state.exception.StateException; | |
32 | 40 |
33 import de.intevation.gnv.utils.DistanceCalculator; | 41 import de.intevation.gnv.utils.DistanceCalculator; |
34 import de.intevation.gnv.utils.StringUtils; | 42 import de.intevation.gnv.utils.StringUtils; |
35 import de.intevation.gnv.utils.WKTUtils; | 43 import de.intevation.gnv.utils.WKTUtils; |
36 | 44 |
45 import java.io.IOException; | |
46 import java.io.OutputStream; | |
47 import java.io.OutputStreamWriter; | |
48 import java.io.UnsupportedEncodingException; | |
49 | |
50 import java.text.DateFormat; | |
51 import java.text.ParseException; | |
52 import java.text.SimpleDateFormat; | |
53 | |
37 import java.util.ArrayList; | 54 import java.util.ArrayList; |
38 import java.util.Arrays; | 55 import java.util.Arrays; |
39 import java.util.Collection; | 56 import java.util.Collection; |
57 import java.util.Date; | |
58 import java.util.HashMap; | |
59 import java.util.Iterator; | |
40 import java.util.List; | 60 import java.util.List; |
41 import java.util.Locale; | 61 import java.util.Locale; |
62 import java.util.Map; | |
42 | 63 |
43 import org.apache.log4j.Logger; | 64 import org.apache.log4j.Logger; |
44 | 65 |
45 import org.jfree.chart.ChartTheme; | 66 import org.jfree.chart.ChartTheme; |
46 | 67 |
54 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | 75 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> |
55 */ | 76 */ |
56 public class HorizontalProfileMeshCrossOutputState | 77 public class HorizontalProfileMeshCrossOutputState |
57 extends HorizontalProfileMeshOutputState | 78 extends HorizontalProfileMeshOutputState |
58 { | 79 { |
80 | |
81 /** | |
82 * Constant field which defines the source format of a given datetime. | |
83 */ | |
84 public static final String SRC_FORMAT = "yyyy.MM.dd HH:mm:ss"; | |
85 | |
86 /** | |
87 * Constant field which defines the target format of a given datetime. | |
88 */ | |
89 public static final String DEST_FORMAT = "yyyy-MM-dd HH:mm"; | |
90 | |
91 /** | |
92 * Source format. | |
93 */ | |
94 public static DateFormat srcFormat = new SimpleDateFormat(SRC_FORMAT); | |
95 | |
96 /** | |
97 * Target format. | |
98 */ | |
99 public static DateFormat destFormat = new SimpleDateFormat(DEST_FORMAT); | |
100 | |
101 | |
102 /** | |
103 * This class represents an exporter used for exporting 'Horizontale | |
104 * Schnittprofile' as odv. | |
105 * | |
106 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
107 */ | |
108 public class HorizontalProfileMeshCrossODVExporter | |
109 implements Export | |
110 { | |
111 private ResultDescriptor rd; | |
112 | |
113 private Collection parameters; | |
114 | |
115 private int dateIdx; | |
116 private int paramIdx; | |
117 private int depthIdx; | |
118 private int valueIdx; | |
119 private int shpIdx; | |
120 | |
121 public HorizontalProfileMeshCrossODVExporter(Collection parameters) { | |
122 this.parameters = parameters; | |
123 } | |
124 | |
125 public void create( | |
126 Profile profile, | |
127 OutputStream outputStream, | |
128 Collection result) | |
129 throws | |
130 IOException, | |
131 UnsupportedEncodingException, | |
132 StateException | |
133 { | |
134 CSVWriter writer = new CSVWriter( | |
135 new OutputStreamWriter(outputStream, profile.getEncoding()), | |
136 profile.getSeparator(), | |
137 profile.getQuoteCharacter(), | |
138 profile.getEscapeCharacter()); | |
139 | |
140 writeData(profile, writer, result); | |
141 | |
142 writer.close(); | |
143 } | |
144 | |
145 protected void writeData( | |
146 Profile profile, | |
147 CSVWriter writer, | |
148 Collection<Result> results) | |
149 { | |
150 log.debug("Put " + results.size() + " elements into odv export."); | |
151 | |
152 // just write header into odv export | |
153 String[] header = profile.getHeader(); | |
154 ArrayList<String> headerList = new ArrayList<String>(); | |
155 for (int i= 0; i < header.length; i++){ | |
156 headerList.add(header[i]); | |
157 } | |
158 | |
159 ArrayList<String> paramids = new ArrayList<String>(); | |
160 | |
161 Map<StringArrayKey, Map<String,String>> aggregatedRows = | |
162 new HashMap<StringArrayKey, Map<String,String>>(); | |
163 | |
164 for (Result res: results) { | |
165 if (rd == null) { | |
166 rd = res.getResultDescriptor(); | |
167 paramIdx = rd.getColumnIndex("GROUP1"); | |
168 depthIdx = rd.getColumnIndex("GROUP2"); | |
169 dateIdx = rd.getColumnIndex("GROUP3"); | |
170 shpIdx = rd.getColumnIndex("SHAPE"); | |
171 valueIdx = rd.getColumnIndex("YORDINATE"); | |
172 } | |
173 | |
174 String[] row = generateRow(res); | |
175 StringArrayKey key = new StringArrayKey(row); | |
176 String parameterValue = res.getString(valueIdx); | |
177 String parameterID = res.getString(paramIdx); | |
178 | |
179 if (!paramids.contains(parameterID)) { | |
180 paramids.add(parameterID); | |
181 headerList.add(findParamTitle(parameters, parameterID)); | |
182 headerList.add("QF"); | |
183 } | |
184 | |
185 Map<String,String> aggregatedRow = aggregatedRows.get(key); | |
186 if (aggregatedRow != null) { | |
187 aggregatedRow.put(parameterID, parameterValue); | |
188 } | |
189 else{ | |
190 Map<String,String> params = new HashMap<String, String>(); | |
191 params.put(parameterID, parameterValue); | |
192 aggregatedRows.put(key, params); | |
193 } | |
194 } | |
195 | |
196 if (header != null){ | |
197 writer.writeNext(headerList.toArray(header)); | |
198 } | |
199 | |
200 Iterator<StringArrayKey> rows = aggregatedRows.keySet().iterator(); | |
201 int idx = 1; | |
202 while (rows.hasNext()){ | |
203 StringArrayKey row = rows.next(); | |
204 Map<String,String> params = aggregatedRows.get(row); | |
205 | |
206 ArrayList<String> rowList = new ArrayList<String>(); | |
207 String[] rowArray = row.getValue(); | |
208 for (int i= 0; i < rowArray.length; i++){ | |
209 String value = rowArray[i]; | |
210 if (value != null && value.equals("GNV_STATION")) { | |
211 value += "_" + idx++; | |
212 } | |
213 rowList.add(value); | |
214 } | |
215 for (int i = 0; i < paramids.size();i++){ | |
216 String key = paramids.get(i); | |
217 String value = params.get(key); | |
218 if (value == null){ | |
219 value = ""; | |
220 } | |
221 rowList.add(value); | |
222 rowList.add("1"); | |
223 } | |
224 log.debug("Write new line into odv export."); | |
225 writer.writeNext(rowList.toArray(rowArray)); | |
226 } | |
227 } | |
228 | |
229 protected String[] generateRow(Result res) { | |
230 Date tmpDate = null; | |
231 String dateTmp = res.getString(dateIdx); | |
232 try { | |
233 tmpDate = srcFormat.parse(dateTmp); | |
234 } | |
235 catch (ParseException pe) { | |
236 log.warn(pe,pe); | |
237 } | |
238 | |
239 String shapeTmp = res.getString(shpIdx); | |
240 Coordinate p = WKTUtils.toCoordinate(shapeTmp); | |
241 | |
242 String cruise = "GNV_EXPORT"; | |
243 String station = "GNV_STATION"; | |
244 String type = "*"; | |
245 String date = tmpDate != null | |
246 ? destFormat.format(tmpDate) | |
247 : dateTmp; | |
248 String lon = "" + p.x; | |
249 String lat = "" + p.y; | |
250 String botDepth = "0"; | |
251 String depth = res.getString(depthIdx); | |
252 | |
253 return new String[] { | |
254 cruise, station, type, date, lon, lat, botDepth, depth | |
255 }; | |
256 } | |
257 | |
258 /** | |
259 * This method is used to search specific value coresponding to its key | |
260 * <code>id</code> and return its description. | |
261 * | |
262 * @param values Collection of parameters. | |
263 * @param id Key used to find the value. | |
264 * | |
265 * @return Description of searched value. | |
266 */ | |
267 protected String findParamTitle(Collection values, String id) { | |
268 if (values != null){ | |
269 Iterator it = values.iterator(); | |
270 while (it.hasNext()) { | |
271 KeyValueDescibeData data = (KeyValueDescibeData) it.next(); | |
272 | |
273 if (id.equals(data.getKey())) | |
274 return data.getValue(); | |
275 } | |
276 } | |
277 return ""; | |
278 } | |
279 } // HorizontalProfileMeshCrossODVExporter | |
280 | |
59 | 281 |
60 public static final boolean USE_INDEX_BUFFER = | 282 public static final boolean USE_INDEX_BUFFER = |
61 Boolean.getBoolean("gnv.horizontal.profile.mesh.cross.index.buffer"); | 283 Boolean.getBoolean("gnv.horizontal.profile.mesh.cross.index.buffer"); |
62 | 284 |
63 private static final long serialVersionUID = 2205958041745637263L; | 285 private static final long serialVersionUID = 2205958041745637263L; |
148 } | 370 } |
149 | 371 |
150 return chart; | 372 return chart; |
151 } | 373 } |
152 | 374 |
375 | |
376 @Override | |
377 protected void createODV( | |
378 OutputStream outputStream, String uuid, CallContext callContext) | |
379 throws IOException, StateException { | |
380 | |
381 String [] COLUMN_HEADER = { | |
382 "Cruise", | |
383 "Station", | |
384 "Type", | |
385 "yyyy-mm-dd hh:mm", | |
386 "Lon (°E)", | |
387 "Lat (°N)", | |
388 "Bot. Depth [m]", | |
389 "Depth [m]" | |
390 }; | |
391 | |
392 Export.Profile ODV_PROFILE = new DefaultProfile( | |
393 COLUMN_HEADER, | |
394 '\t', | |
395 CSVWriter.NO_QUOTE_CHARACTER, | |
396 CSVWriter.NO_ESCAPE_CHARACTER, | |
397 "ODV", | |
398 "ISO-8859-1"); | |
399 | |
400 Collection<Result> result = (Collection<Result>) getChartResult( | |
401 uuid, callContext); | |
402 | |
403 if (result == null) { | |
404 log.error("No data for export found."); | |
405 return; | |
406 } | |
407 | |
408 Export export = new HorizontalProfileMeshCrossODVExporter( | |
409 getParameters(uuid)); | |
410 | |
411 export.create(ODV_PROFILE, outputStream, result); | |
412 } | |
413 | |
414 | |
153 private static int numSamples(CallContext callContext) { | 415 private static int numSamples(CallContext callContext) { |
154 GNVArtifactContext context = | 416 GNVArtifactContext context = |
155 (GNVArtifactContext)callContext.globalContext(); | 417 (GNVArtifactContext)callContext.globalContext(); |
156 Integer samples = (Integer)context.get( | 418 Integer samples = (Integer)context.get( |
157 GNVArtifactContext.HORIZONTAL_CROSS_SECTION_PROFILE_SAMPLES_KEY); | 419 GNVArtifactContext.HORIZONTAL_CROSS_SECTION_PROFILE_SAMPLES_KEY); |
272 int columns = rd.getColumnCount(); | 534 int columns = rd.getColumnCount(); |
273 | 535 |
274 DefaultResultDescriptor resultDescriptor = | 536 DefaultResultDescriptor resultDescriptor = |
275 new DefaultResultDescriptor(); | 537 new DefaultResultDescriptor(); |
276 | 538 |
539 log.debug("------------------------------------------------------"); | |
277 for (int j = 0; j < columns; ++j) { | 540 for (int j = 0; j < columns; ++j) { |
278 String columnName = rd.getColumnName(j); | 541 String columnName = rd.getColumnName(j); |
279 if (!StringUtils.contains(COLUMN_BLACKLIST, columnName)) { | 542 if (!StringUtils.contains(COLUMN_BLACKLIST, columnName)) { |
543 log.debug("!!! COLUMN NAME: " + columnName); | |
280 resultDescriptor.addColumn( | 544 resultDescriptor.addColumn( |
281 columnName, | 545 columnName, |
282 rd.getColumnClassName(j)); | 546 rd.getColumnClassName(j)); |
283 } | 547 } |
284 } | 548 } |