diff gnv-artifacts/src/main/java/de/intevation/gnv/exports/VerticalCrossODVExport.java @ 837:43f3c0cd60f2

First implementation of an odv export of a 'Profilschnitt' (issue217). gnv-artifacts/trunk@944 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 19 Apr 2010 10:55:25 +0000
parents
children 39d06d01825a
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/VerticalCrossODVExport.java	Mon Apr 19 10:55:25 2010 +0000
@@ -0,0 +1,202 @@
+package de.intevation.gnv.exports;
+
+import com.vividsolutions.jts.geom.Coordinate;
+
+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.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+
+import au.com.bytecode.opencsv.CSVWriter;
+import de.intevation.gnv.geobackend.base.Result;
+import de.intevation.gnv.math.Interpolation3D;
+import de.intevation.gnv.state.describedata.KeyValueDescibeData;
+import de.intevation.gnv.state.exception.StateException;
+
+
+public class VerticalCrossODVExport implements Export {
+
+    /**
+     * 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);
+
+    /**
+     * Logger used for logging via log4j.
+     */
+    private static Logger logger =
+        Logger.getLogger(VerticalCrossODVExport.class);
+
+    /**
+     * The path in coordinates.
+     */
+    protected Coordinate[] coordinates;
+
+    /**
+     *
+     */
+    protected double       cellHeight;
+
+    /**
+     * The raster array storing the values for a specific coordinate in a
+     * specific depth.
+     */
+    protected double[]     raster;
+
+    /**
+     * The raster width.
+     */
+    protected int          width;
+
+    /**
+     * The raster height.
+     */
+    protected int          height;
+
+    /**
+     * The date of this export.
+     */
+    protected String       time;
+
+    /**
+     * The constructor used to create a new export helper object.
+     */
+    public VerticalCrossODVExport(
+        Coordinate[] coordinates,
+        double       cellHeight,
+        double[]     raster,
+        String       time,
+        int          width,
+        int          height)
+    {
+        this.coordinates = coordinates;
+        this.cellHeight  = cellHeight;
+        this.raster      = raster;
+        this.width       = width;
+        this.height      = height;
+        this.time        = time;
+    }
+
+
+    /**
+     * 
+     * @param profile The Profile used to create column headers.
+     * @param outputStream The stream where the odv data are written to.
+     * @param result Not used here, might be null.
+     */
+    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());
+
+        writer.writeNext(profile.getHeader());
+
+        writeData(writer, time, coordinates, cellHeight, raster);
+
+        writer.close();
+    }
+
+    protected void writeData(
+        CSVWriter    writer,
+        String       time,
+        Coordinate[] coordinates,
+        double       cellHeight,
+        double[]     raster)
+    {
+        if (logger.isDebugEnabled()) {
+            logger.debug("+++++++ ODV Export information ++++++++++");
+            logger.debug("+ raster width: " + width);
+            logger.debug("+ raster height: " + height);
+            logger.debug("+ items in raster: " + raster.length);
+            logger.debug("+ number of coordinates: " + coordinates.length);
+            logger.debug("+++++++++++++++++++++++++++++++++++++++++");
+        }
+
+        String datetime = null;
+        try {
+            Date tmp = srcFormat.parse(time);
+            datetime = destFormat.format(tmp);
+        }
+        catch (ParseException pe) {
+            logger.error(pe, pe);
+        }
+
+        String[] row    = new String[10];
+        for (int i = 0; i < width; i++) {
+            row[0] = "Station_" + i;
+            //row[0] = "";
+            row[1] = "";
+            row[2] = "*";
+            row[3] = datetime;
+            row[4] = Double.toString(coordinates[i].x);
+            row[5] = Double.toString(coordinates[i].y);
+            row[6] = "0";
+
+            for (int j = 0; j < height; j++) {
+                if (j == 1) {
+                    row[0] = "";
+                    //row[0] = "Station_";
+                    row[1] = "";
+                    row[2] = "";
+                    row[3] = "";
+                    row[4] = "";
+                    row[5] = "";
+                    row[6] = "";
+                }
+
+                // row[2] = Double.toString(depths[j]);
+                //
+                row[7] = Double.toString(j * cellHeight + cellHeight / 2d);
+                row[8] = "1";
+                double value = raster[i+(j*height)];
+
+                if (Double.isNaN(value)) {
+                    break;
+                }
+
+                row[9] = Double.toString(value);
+
+                writer.writeNext(row);
+            }
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org