# HG changeset patch # User Ingo Weinzierl # Date 1302083798 0 # Node ID 8940b0885865cdaa4cc4660c60f46db4e165d98c # Parent fc694a43a7e5e9f23814f6dfbeb3c07809cb38ff Added a DischargeCurveGenerator that creates discharge curves. flys-artifacts/trunk@1648 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r fc694a43a7e5 -r 8940b0885865 flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Wed Apr 06 09:53:14 2011 +0000 +++ b/flys-artifacts/ChangeLog Wed Apr 06 09:56:38 2011 +0000 @@ -1,3 +1,11 @@ +2011-04-06 Ingo Weinzierl + + * src/main/java/de/intevation/flys/exports/OutGenerator.java: The + generate() method throws an IOException now. + + * src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java: + New. This OutGenerator creates discharge curves. + 2011-04-06 Ingo Weinzierl * src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java: Changed diff -r fc694a43a7e5 -r 8940b0885865 flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java Wed Apr 06 09:56:38 2011 +0000 @@ -0,0 +1,147 @@ +package de.intevation.flys.exports; + +import java.awt.Color; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; + +import org.w3c.dom.Document; + +import org.jfree.data.xy.DefaultXYDataset; + +import org.jfree.chart.ChartFactory; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.plot.PlotOrientation; + +import de.intevation.artifacts.Artifact; +import de.intevation.artifacts.CallContext; + +import de.intevation.flys.model.Gauge; + +import de.intevation.flys.artifacts.FLYSArtifact; +import de.intevation.flys.artifacts.model.DischargeTables; +import de.intevation.flys.artifacts.model.GaugesFactory; +import de.intevation.flys.exports.ChartExportHelper; + + +/** + * An OutGenerator that generates discharge curves. + * + * @author Ingo Weinzierl + */ +public class DischargeCurveGenerator implements OutGenerator { + + /** The logger used in this generator.*/ + private static Logger logger = + Logger.getLogger(DischargeCurveGenerator.class); + + /** The document of the incoming out() request.*/ + protected Document request; + + /** The output stream where the data should be written to.*/ + protected OutputStream out; + + /** The CallContext object.*/ + protected CallContext context; + + /** The Dataset that contains the data for the chart.*/ + protected DefaultXYDataset dataset; + + + public DischargeCurveGenerator() { + } + + + public void init(Document request, OutputStream out, CallContext context) { + logger.debug("DischargeCurveGenerator.init"); + + this.request = request; + this.out = out; + this.context = context; + + this.dataset = new DefaultXYDataset(); + } + + + public void doOut(Artifact artifact, Document attr) { + logger.debug("DischargeCurveGenerator.doOut"); + + if (!(artifact instanceof FLYSArtifact)) { + logger.error("Artifact is no instance of FLYSArtifact."); + return; + } + + FLYSArtifact flysArtifact = (FLYSArtifact) artifact; + + String river = (String) flysArtifact.getData("river").getValue(); + String from = (String) flysArtifact.getData("ld_from").getValue(); + String to = (String) flysArtifact.getData("ld_to").getValue(); + + double f = Double.parseDouble(from); + double t = Double.parseDouble(to); + + List ranges = new ArrayList(); + ranges.add(new double [] { f, t }); + + List gauges = GaugesFactory.getGauges(river); + List filtered = GaugesFactory.filterRanges(gauges, ranges); + + if (logger.isDebugEnabled()) { + int numGauges = gauges != null ? gauges.size() : 0; + int numFiltered = filtered != null ? filtered.size() : 0; + + logger.debug("++++++++++++++++++++"); + logger.debug("Search gauges for river: " + river); + logger.debug("-> ... from range: " + from); + logger.debug("-> ... to range: " + to); + logger.debug("-> Found " + numGauges + " gauges in total."); + logger.debug("-> Found " + numFiltered + " gauges after filtering."); + logger.debug("++++++++++++++++++++"); + } + + String[] gaugeNames = new String[filtered.size()]; + int idx = 0; + for (Gauge gauge: filtered) { + gaugeNames[idx++] = gauge.getName(); + } + + DischargeTables dt = new DischargeTables(river, gaugeNames); + Map gaugeValues = dt.getValues(100d); + + for (String gauge: gaugeNames) { + double[][] values = gaugeValues.get(gauge); + dataset.addSeries(gauge, values); + } + + } + + + public void generate() + throws IOException + { + logger.debug("DischargeCurveGenerator.generate"); + + JFreeChart chart = ChartFactory.createXYLineChart( + "Abflusskurven", + "Q", "W", + dataset, + PlotOrientation.VERTICAL, + true, + false, + false); + + chart.setBackgroundPaint(Color.WHITE); + chart.getPlot().setBackgroundPaint(Color.WHITE); + + ChartExportHelper.exportImage( + out, + chart, + "png", + 600, 400); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r fc694a43a7e5 -r 8940b0885865 flys-artifacts/src/main/java/de/intevation/flys/exports/OutGenerator.java --- a/flys-artifacts/src/main/java/de/intevation/flys/exports/OutGenerator.java Wed Apr 06 09:53:14 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/OutGenerator.java Wed Apr 06 09:56:38 2011 +0000 @@ -1,5 +1,6 @@ package de.intevation.flys.exports; +import java.io.IOException; import java.io.OutputStream; import org.w3c.dom.Document; @@ -42,6 +43,6 @@ * request (see init()) document to the OutputStream out (see * init()). */ - void generate(); + void generate() throws IOException; } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :