# HG changeset patch # User Ingo Weinzierl # Date 1273487395 0 # Node ID 50a5ce7a47b7f19ac32d0a2950f79deabe61f2f1 # Parent 35c4424107388220083cd41b2d09ab957777b96c Implemented an odv exporter for product type 'Horizontales Schnittprofil' (issue260). gnv-artifacts/trunk@1082 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 35c442410738 -r 50a5ce7a47b7 gnv-artifacts/ChangeLog --- a/gnv-artifacts/ChangeLog Fri May 07 06:50:47 2010 +0000 +++ b/gnv-artifacts/ChangeLog Mon May 10 10:29:55 2010 +0000 @@ -1,3 +1,16 @@ +2010-05-10 Ingo Weinzierl + + Issue260 (Implemented an odv export for 'Horizontales Schnittprofil') + + * doc/conf/products/horizontalprofile/conf_mesh_cross.xml: Added odv as + further export format of product type 'Horizontales Schnittprofil'. + + * src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java: + Implemented an odv exporter for this product type. + + * src/main/java/de/intevation/gnv/exports/StringArrayKey.java: Added a + getKey() method to be able to retrieve the key of such objects. + 2010-05-07 Hans Plum * doc/conf/queries.properties: diff -r 35c442410738 -r 50a5ce7a47b7 gnv-artifacts/doc/conf/products/horizontalprofile/conf_mesh_cross.xml --- a/gnv-artifacts/doc/conf/products/horizontalprofile/conf_mesh_cross.xml Fri May 07 06:50:47 2010 +0000 +++ b/gnv-artifacts/doc/conf/products/horizontalprofile/conf_mesh_cross.xml Mon May 10 10:29:55 2010 +0000 @@ -281,6 +281,7 @@ + diff -r 35c442410738 -r 50a5ce7a47b7 gnv-artifacts/src/main/java/de/intevation/gnv/exports/StringArrayKey.java --- a/gnv-artifacts/src/main/java/de/intevation/gnv/exports/StringArrayKey.java Fri May 07 06:50:47 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/StringArrayKey.java Mon May 10 10:29:55 2010 +0000 @@ -58,4 +58,8 @@ public String[] getValue() { return value; } + + public String getKey() { + return key; + } } diff -r 35c442410738 -r 50a5ce7a47b7 gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java --- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java Fri May 07 06:50:47 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java Mon May 10 10:29:55 2010 +0000 @@ -1,5 +1,7 @@ package de.intevation.gnv.state.profile.horizontal; +import au.com.bytecode.opencsv.CSVWriter; + import com.vividsolutions.jts.geom.Coordinate; import de.intevation.artifactdatabase.Config; @@ -14,6 +16,10 @@ import de.intevation.gnv.chart.ChartLabels; import de.intevation.gnv.chart.HorizontalCrossProfileChart; +import de.intevation.gnv.exports.DefaultProfile; +import de.intevation.gnv.exports.Export; +import de.intevation.gnv.exports.StringArrayKey; + import de.intevation.gnv.geobackend.base.DefaultResult; import de.intevation.gnv.geobackend.base.DefaultResultDescriptor; import de.intevation.gnv.geobackend.base.Result; @@ -29,16 +35,31 @@ import de.intevation.gnv.math.Point2d; import de.intevation.gnv.state.InputData; +import de.intevation.gnv.state.describedata.KeyValueDescibeData; +import de.intevation.gnv.state.exception.StateException; import de.intevation.gnv.utils.DistanceCalculator; import de.intevation.gnv.utils.StringUtils; import de.intevation.gnv.utils.WKTUtils; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Locale; +import java.util.Map; import org.apache.log4j.Logger; @@ -57,6 +78,207 @@ extends HorizontalProfileMeshOutputState { + /** + * Constant field which defines the source format of a given datetime. + */ + public static final String SRC_FORMAT = "yyyy.MM.dd HH:mm:ss"; + + /** + * Constant field which defines the target format of a given datetime. + */ + public static final String DEST_FORMAT = "yyyy-MM-dd HH:mm"; + + /** + * Source format. + */ + public static DateFormat srcFormat = new SimpleDateFormat(SRC_FORMAT); + + /** + * Target format. + */ + public static DateFormat destFormat = new SimpleDateFormat(DEST_FORMAT); + + + /** + * This class represents an exporter used for exporting 'Horizontale + * Schnittprofile' as odv. + * + * @author Ingo Weinzierl + */ + public class HorizontalProfileMeshCrossODVExporter + implements Export + { + private ResultDescriptor rd; + + private Collection parameters; + + private int dateIdx; + private int paramIdx; + private int depthIdx; + private int valueIdx; + private int shpIdx; + + public HorizontalProfileMeshCrossODVExporter(Collection parameters) { + this.parameters = parameters; + } + + public void create( + Profile profile, + OutputStream outputStream, + Collection result) + throws + IOException, + UnsupportedEncodingException, + StateException + { + CSVWriter writer = new CSVWriter( + new OutputStreamWriter(outputStream, profile.getEncoding()), + profile.getSeparator(), + profile.getQuoteCharacter(), + profile.getEscapeCharacter()); + + writeData(profile, writer, result); + + writer.close(); + } + + protected void writeData( + Profile profile, + CSVWriter writer, + Collection results) + { + log.debug("Put " + results.size() + " elements into odv export."); + + // just write header into odv export + String[] header = profile.getHeader(); + ArrayList headerList = new ArrayList(); + for (int i= 0; i < header.length; i++){ + headerList.add(header[i]); + } + + ArrayList paramids = new ArrayList(); + + Map> aggregatedRows = + new HashMap>(); + + for (Result res: results) { + if (rd == null) { + rd = res.getResultDescriptor(); + paramIdx = rd.getColumnIndex("GROUP1"); + depthIdx = rd.getColumnIndex("GROUP2"); + dateIdx = rd.getColumnIndex("GROUP3"); + shpIdx = rd.getColumnIndex("SHAPE"); + valueIdx = rd.getColumnIndex("YORDINATE"); + } + + String[] row = generateRow(res); + StringArrayKey key = new StringArrayKey(row); + String parameterValue = res.getString(valueIdx); + String parameterID = res.getString(paramIdx); + + if (!paramids.contains(parameterID)) { + paramids.add(parameterID); + headerList.add(findParamTitle(parameters, parameterID)); + headerList.add("QF"); + } + + Map aggregatedRow = aggregatedRows.get(key); + if (aggregatedRow != null) { + aggregatedRow.put(parameterID, parameterValue); + } + else{ + Map params = new HashMap(); + params.put(parameterID, parameterValue); + aggregatedRows.put(key, params); + } + } + + if (header != null){ + writer.writeNext(headerList.toArray(header)); + } + + Iterator rows = aggregatedRows.keySet().iterator(); + int idx = 1; + while (rows.hasNext()){ + StringArrayKey row = rows.next(); + Map params = aggregatedRows.get(row); + + ArrayList rowList = new ArrayList(); + String[] rowArray = row.getValue(); + for (int i= 0; i < rowArray.length; i++){ + String value = rowArray[i]; + if (value != null && value.equals("GNV_STATION")) { + value += "_" + idx++; + } + rowList.add(value); + } + for (int i = 0; i < paramids.size();i++){ + String key = paramids.get(i); + String value = params.get(key); + if (value == null){ + value = ""; + } + rowList.add(value); + rowList.add("1"); + } + log.debug("Write new line into odv export."); + writer.writeNext(rowList.toArray(rowArray)); + } + } + + protected String[] generateRow(Result res) { + Date tmpDate = null; + String dateTmp = res.getString(dateIdx); + try { + tmpDate = srcFormat.parse(dateTmp); + } + catch (ParseException pe) { + log.warn(pe,pe); + } + + String shapeTmp = res.getString(shpIdx); + Coordinate p = WKTUtils.toCoordinate(shapeTmp); + + String cruise = "GNV_EXPORT"; + String station = "GNV_STATION"; + String type = "*"; + String date = tmpDate != null + ? destFormat.format(tmpDate) + : dateTmp; + String lon = "" + p.x; + String lat = "" + p.y; + String botDepth = "0"; + String depth = res.getString(depthIdx); + + return new String[] { + cruise, station, type, date, lon, lat, botDepth, depth + }; + } + + /** + * This method is used to search specific value coresponding to its key + * id and return its description. + * + * @param values Collection of parameters. + * @param id Key used to find the value. + * + * @return Description of searched value. + */ + protected String findParamTitle(Collection values, String id) { + if (values != null){ + Iterator it = values.iterator(); + while (it.hasNext()) { + KeyValueDescibeData data = (KeyValueDescibeData) it.next(); + + if (id.equals(data.getKey())) + return data.getValue(); + } + } + return ""; + } + } // HorizontalProfileMeshCrossODVExporter + + public static final boolean USE_INDEX_BUFFER = Boolean.getBoolean("gnv.horizontal.profile.mesh.cross.index.buffer"); @@ -150,6 +372,46 @@ return chart; } + + @Override + protected void createODV( + OutputStream outputStream, String uuid, CallContext callContext) + throws IOException, StateException { + + String [] COLUMN_HEADER = { + "Cruise", + "Station", + "Type", + "yyyy-mm-dd hh:mm", + "Lon (°E)", + "Lat (°N)", + "Bot. Depth [m]", + "Depth [m]" + }; + + Export.Profile ODV_PROFILE = new DefaultProfile( + COLUMN_HEADER, + '\t', + CSVWriter.NO_QUOTE_CHARACTER, + CSVWriter.NO_ESCAPE_CHARACTER, + "ODV", + "ISO-8859-1"); + + Collection result = (Collection) getChartResult( + uuid, callContext); + + if (result == null) { + log.error("No data for export found."); + return; + } + + Export export = new HorizontalProfileMeshCrossODVExporter( + getParameters(uuid)); + + export.create(ODV_PROFILE, outputStream, result); + } + + private static int numSamples(CallContext callContext) { GNVArtifactContext context = (GNVArtifactContext)callContext.globalContext(); @@ -274,9 +536,11 @@ DefaultResultDescriptor resultDescriptor = new DefaultResultDescriptor(); + log.debug("------------------------------------------------------"); for (int j = 0; j < columns; ++j) { String columnName = rd.getColumnName(j); if (!StringUtils.contains(COLUMN_BLACKLIST, columnName)) { + log.debug("!!! COLUMN NAME: " + columnName); resultDescriptor.addColumn( columnName, rd.getColumnClassName(j));