Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java @ 1106:e9f66d63bdd0
Write user defined barriers into a shapefile that is placed in the Artifact's directory.
flys-artifacts/trunk@2609 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 30 Aug 2011 11:09:54 +0000 |
parents | 72bcbc308501 |
children | 3ebbe497d7f7 |
line wrap: on
line source
package de.intevation.flys.exports; import java.io.IOException; import java.io.OutputStream; import java.text.NumberFormat; import java.util.ArrayList; import java.util.List; import org.w3c.dom.Document; import org.apache.log4j.Logger; import au.com.bytecode.opencsv.CSVWriter; import de.intevation.artifacts.CallContext; import de.intevation.flys.artifacts.model.CalculationResult; import de.intevation.flys.artifacts.model.WQCKms; import de.intevation.flys.artifacts.model.WQKms; import de.intevation.flys.utils.Formatter; /** * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public class WaterlevelExporter extends AbstractExporter { /** The logger used in this exporter.*/ private static Logger logger = Logger.getLogger(WaterlevelExporter.class); public static final String FACET_WST = "wst"; public static final String CSV_KM_HEADER = "export.waterlevel.csv.header.km"; public static final String CSV_W_HEADER = "export.waterlevel.csv.header.w"; public static final String CSV_Q_HEADER = "export.waterlevel.csv.header.q"; public static final String DEFAULT_CSV_KM_HEADER = "Fluss-Km"; public static final String DEFAULT_CSV_W_HEADER = "W [NN + m]"; public static final String DEFAULT_CSV_Q_HEADER = "Q [m\u00b3/s]"; /** The storage that contains all WQKms objects for the different facets.*/ protected List<WQKms[]> data; public void init(Document request, OutputStream out, CallContext context) { logger.debug("WaterlevelExporter.init"); super.init(request, out, context); this.data = new ArrayList<WQKms[]>(); } @Override public void generate() throws IOException { logger.debug("WaterlevelExporter.generate"); if (facet != null && facet.equals(AbstractExporter.FACET_CSV)) { generateCSV(); } else if (facet != null && facet.equals(FACET_WST)) { generateWST(); } else { throw new IOException("invalid facet for exporter"); } } @Override protected void addData(Object d) { if (d instanceof CalculationResult) { d = ((CalculationResult)d).getData(); if (d instanceof WQKms []) { data.add((WQKms [])d); } } } @Override protected void writeCSVData(CSVWriter writer) { logger.info("WaterlevelExporter.writeData"); writeCSVHeader(writer); for (WQKms[] tmp: data) { for (WQKms wqkms: tmp) { wQKms2CSV(writer, wqkms); } } } protected void writeCSVHeader(CSVWriter writer) { logger.info("WaterlevelExporter.writeCSVHeader"); writer.writeNext(new String[] { msg(CSV_KM_HEADER, DEFAULT_CSV_KM_HEADER), msg(CSV_W_HEADER, DEFAULT_CSV_W_HEADER), msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER) }); } protected void wQKms2CSV(CSVWriter writer, WQKms wqkms) { logger.debug("WaterlevelExporter.wQKms2CSV"); NumberFormat kmf = getKmFormatter(); NumberFormat wf = getWFormatter(); NumberFormat qf = getQFormatter(); int size = wqkms.size(); double[] result = new double[3]; for (int i = 0; i < size; i ++) { result = wqkms.get(i, result); writer.writeNext(new String[] { kmf.format(result[2]), wf.format(result[0]), qf.format(result[1]) }); } } /** * Generates the output in WST format. */ protected void generateWST() throws IOException { logger.info("WaterlevelExporter.generateWST"); int cols = data.get(0).length; WstWriter writer = new WstWriter(cols); writeWSTData(writer); writer.write(out); } protected void writeWSTData(WstWriter writer) { logger.debug("WaterlevelExporter.writeWSTData"); double[] result = new double[4]; for (WQKms[] tmp: data) { for (WQKms wqkms: tmp) { int size = wqkms != null ? wqkms.size() : 0; addWSTColumn(writer, wqkms); for (int i = 0; i < size; i++) { result = wqkms.get(i, result); writer.add(result); } if (wqkms instanceof WQCKms) { addWSTColumn(writer, wqkms); for (int c = 0; c < size; c++) { result = wqkms.get(c, result); writer.addCorrected(result); } } } } } protected void addWSTColumn(WstWriter writer, WQKms wqkms) { writer.addColumn(wqkms.getName()); } /** * Returns the number formatter for kilometer values. * * @return the number formatter for kilometer values. */ protected NumberFormat getKmFormatter() { return Formatter.getWaterlevelKM(context); } /** * Returns the number formatter for W values. * * @return the number formatter for W values. */ protected NumberFormat getWFormatter() { return Formatter.getWaterlevelW(context); } /** * Returns the number formatter for Q values. * * @return the number formatter for Q values. */ protected NumberFormat getQFormatter() { return Formatter.getWaterlevelQ(context); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :