Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java @ 299:8940b0885865
Added a DischargeCurveGenerator that creates discharge curves.
flys-artifacts/trunk@1648 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 06 Apr 2011 09:56:38 +0000 |
parents | |
children | 635af5381a4d |
comparison
equal
deleted
inserted
replaced
298:fc694a43a7e5 | 299:8940b0885865 |
---|---|
1 package de.intevation.flys.exports; | |
2 | |
3 import java.awt.Color; | |
4 import java.io.IOException; | |
5 import java.io.OutputStream; | |
6 import java.util.ArrayList; | |
7 import java.util.List; | |
8 import java.util.Map; | |
9 | |
10 import org.apache.log4j.Logger; | |
11 | |
12 import org.w3c.dom.Document; | |
13 | |
14 import org.jfree.data.xy.DefaultXYDataset; | |
15 | |
16 import org.jfree.chart.ChartFactory; | |
17 import org.jfree.chart.JFreeChart; | |
18 import org.jfree.chart.plot.PlotOrientation; | |
19 | |
20 import de.intevation.artifacts.Artifact; | |
21 import de.intevation.artifacts.CallContext; | |
22 | |
23 import de.intevation.flys.model.Gauge; | |
24 | |
25 import de.intevation.flys.artifacts.FLYSArtifact; | |
26 import de.intevation.flys.artifacts.model.DischargeTables; | |
27 import de.intevation.flys.artifacts.model.GaugesFactory; | |
28 import de.intevation.flys.exports.ChartExportHelper; | |
29 | |
30 | |
31 /** | |
32 * An OutGenerator that generates discharge curves. | |
33 * | |
34 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
35 */ | |
36 public class DischargeCurveGenerator implements OutGenerator { | |
37 | |
38 /** The logger used in this generator.*/ | |
39 private static Logger logger = | |
40 Logger.getLogger(DischargeCurveGenerator.class); | |
41 | |
42 /** The document of the incoming out() request.*/ | |
43 protected Document request; | |
44 | |
45 /** The output stream where the data should be written to.*/ | |
46 protected OutputStream out; | |
47 | |
48 /** The CallContext object.*/ | |
49 protected CallContext context; | |
50 | |
51 /** The Dataset that contains the data for the chart.*/ | |
52 protected DefaultXYDataset dataset; | |
53 | |
54 | |
55 public DischargeCurveGenerator() { | |
56 } | |
57 | |
58 | |
59 public void init(Document request, OutputStream out, CallContext context) { | |
60 logger.debug("DischargeCurveGenerator.init"); | |
61 | |
62 this.request = request; | |
63 this.out = out; | |
64 this.context = context; | |
65 | |
66 this.dataset = new DefaultXYDataset(); | |
67 } | |
68 | |
69 | |
70 public void doOut(Artifact artifact, Document attr) { | |
71 logger.debug("DischargeCurveGenerator.doOut"); | |
72 | |
73 if (!(artifact instanceof FLYSArtifact)) { | |
74 logger.error("Artifact is no instance of FLYSArtifact."); | |
75 return; | |
76 } | |
77 | |
78 FLYSArtifact flysArtifact = (FLYSArtifact) artifact; | |
79 | |
80 String river = (String) flysArtifact.getData("river").getValue(); | |
81 String from = (String) flysArtifact.getData("ld_from").getValue(); | |
82 String to = (String) flysArtifact.getData("ld_to").getValue(); | |
83 | |
84 double f = Double.parseDouble(from); | |
85 double t = Double.parseDouble(to); | |
86 | |
87 List<double[]> ranges = new ArrayList<double[]>(); | |
88 ranges.add(new double [] { f, t }); | |
89 | |
90 List<Gauge> gauges = GaugesFactory.getGauges(river); | |
91 List<Gauge> filtered = GaugesFactory.filterRanges(gauges, ranges); | |
92 | |
93 if (logger.isDebugEnabled()) { | |
94 int numGauges = gauges != null ? gauges.size() : 0; | |
95 int numFiltered = filtered != null ? filtered.size() : 0; | |
96 | |
97 logger.debug("++++++++++++++++++++"); | |
98 logger.debug("Search gauges for river: " + river); | |
99 logger.debug("-> ... from range: " + from); | |
100 logger.debug("-> ... to range: " + to); | |
101 logger.debug("-> Found " + numGauges + " gauges in total."); | |
102 logger.debug("-> Found " + numFiltered + " gauges after filtering."); | |
103 logger.debug("++++++++++++++++++++"); | |
104 } | |
105 | |
106 String[] gaugeNames = new String[filtered.size()]; | |
107 int idx = 0; | |
108 for (Gauge gauge: filtered) { | |
109 gaugeNames[idx++] = gauge.getName(); | |
110 } | |
111 | |
112 DischargeTables dt = new DischargeTables(river, gaugeNames); | |
113 Map<String, double[][]> gaugeValues = dt.getValues(100d); | |
114 | |
115 for (String gauge: gaugeNames) { | |
116 double[][] values = gaugeValues.get(gauge); | |
117 dataset.addSeries(gauge, values); | |
118 } | |
119 | |
120 } | |
121 | |
122 | |
123 public void generate() | |
124 throws IOException | |
125 { | |
126 logger.debug("DischargeCurveGenerator.generate"); | |
127 | |
128 JFreeChart chart = ChartFactory.createXYLineChart( | |
129 "Abflusskurven", | |
130 "Q", "W", | |
131 dataset, | |
132 PlotOrientation.VERTICAL, | |
133 true, | |
134 false, | |
135 false); | |
136 | |
137 chart.setBackgroundPaint(Color.WHITE); | |
138 chart.getPlot().setBackgroundPaint(Color.WHITE); | |
139 | |
140 ChartExportHelper.exportImage( | |
141 out, | |
142 chart, | |
143 "png", | |
144 600, 400); | |
145 } | |
146 } | |
147 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |