Mercurial > dive4elements > river
changeset 4213:0674d89210f5
Added a new service to query a diagram with discharge tables.
This service takes a gauge (required) and a timerange (optional) as input parameters.
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 23 Oct 2012 07:39:09 +0200 |
parents | fbaba8e1a3a2 |
children | e39ac9767a69 a30647abf009 |
files | flys-artifacts/doc/conf/conf.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/AbstractChartService.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DischargeTablesOverview.java flys-artifacts/src/main/resources/messages.properties flys-artifacts/src/main/resources/messages_de.properties flys-artifacts/src/main/resources/messages_de_DE.properties flys-artifacts/src/main/resources/messages_en.properties |
diffstat | 7 files changed, 427 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-artifacts/doc/conf/conf.xml Mon Oct 22 17:13:23 2012 +0200 +++ b/flys-artifacts/doc/conf/conf.xml Tue Oct 23 07:39:09 2012 +0200 @@ -202,6 +202,10 @@ name="gaugeoverviewinfo" service="de.intevation.flys.artifacts.services.GaugeOverviewInfoService" description="Returns an overview of the fixings of a given river.">de.intevation.artifactdatabase.DefaultServiceFactory</service-factory> + <service-factory + name="dischargetablesoverview" + service="de.intevation.flys.artifacts.services.DischargeTablesOverview" + description="Returns an overview of the fixings of a given river.">de.intevation.artifactdatabase.DefaultServiceFactory</service-factory> </service-factories> </factories>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/AbstractChartService.java Tue Oct 23 07:39:09 2012 +0200 @@ -0,0 +1,162 @@ +package de.intevation.flys.artifacts.services; + +import java.awt.Dimension; +import java.awt.Transparency; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import org.apache.log4j.Logger; +import org.jfree.chart.JFreeChart; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import de.intevation.artifactdatabase.DefaultService; +import de.intevation.artifacts.CallMeta; +import de.intevation.artifacts.GlobalContext; +import de.intevation.artifacts.Service; + + +public abstract class AbstractChartService extends DefaultService { + + public static final int DEFAULT_WIDTH = 240; + public static final int DEFAULT_HEIGHT = 180; + + public static final String DEFAULT_FORMAT = "png"; + + private static final Logger log = Logger + .getLogger(AbstractChartService.class); + + // TODO: Load fancy image from resources. + public static final byte[] EMPTY = { (byte) 0x89, (byte) 0x50, (byte) 0x4e, + (byte) 0x47, (byte) 0x0d, (byte) 0x0a, (byte) 0x1a, (byte) 0x0a, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0d, (byte) 0x49, + (byte) 0x48, (byte) 0x44, (byte) 0x52, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x01, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x3a, (byte) 0x7e, (byte) 0x9b, (byte) 0x55, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x73, + (byte) 0x52, (byte) 0x47, (byte) 0x42, (byte) 0x00, (byte) 0xae, + (byte) 0xce, (byte) 0x1c, (byte) 0xe9, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x09, (byte) 0x70, (byte) 0x48, (byte) 0x59, + (byte) 0x73, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x13, + (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x13, (byte) 0x01, + (byte) 0x00, (byte) 0x9a, (byte) 0x9c, (byte) 0x18, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0x07, (byte) 0x74, (byte) 0x49, + (byte) 0x4d, (byte) 0x45, (byte) 0x07, (byte) 0xdc, (byte) 0x04, + (byte) 0x04, (byte) 0x10, (byte) 0x30, (byte) 0x15, (byte) 0x7d, + (byte) 0x77, (byte) 0x36, (byte) 0x0b, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x08, (byte) 0x74, (byte) 0x45, (byte) 0x58, + (byte) 0x74, (byte) 0x43, (byte) 0x6f, (byte) 0x6d, (byte) 0x6d, + (byte) 0x65, (byte) 0x6e, (byte) 0x74, (byte) 0x00, (byte) 0xf6, + (byte) 0xcc, (byte) 0x96, (byte) 0xbf, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x0a, (byte) 0x49, (byte) 0x44, (byte) 0x41, + (byte) 0x54, (byte) 0x08, (byte) 0xd7, (byte) 0x63, (byte) 0xf8, + (byte) 0x0f, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x01, + (byte) 0x00, (byte) 0x1b, (byte) 0xb6, (byte) 0xee, (byte) 0x56, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x49, + (byte) 0x45, (byte) 0x4e, (byte) 0x44, (byte) 0xae, (byte) 0x42, + (byte) 0x60, (byte) 0x82 }; + + private static final Output empty() { + return new Output(EMPTY, "image/png"); + } + + protected abstract JFreeChart createChart(Document data, + GlobalContext globalContext, CallMeta callMeta); + + protected void init() { + }; + + protected void finish() { + }; + + @Override + public Service.Output process(Document data, GlobalContext globalContext, + CallMeta callMeta) { + log.debug("process"); + + init(); + try { + JFreeChart chart = createChart(data, globalContext, callMeta); + + if (chart == null) { + return empty(); + } + + Dimension extent = getExtent(data); + String format = getFormat(data); + + return encode(chart, extent, format); + } + finally { + finish(); + } + } + + protected static Output encode(JFreeChart chart, Dimension extent, + String format) { + BufferedImage image = chart.createBufferedImage(extent.width, + extent.height, Transparency.BITMASK, null); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + try { + ImageIO.write(image, format, out); + } + catch (IOException ioe) { + log.warn("writing image failed", ioe); + return empty(); + } + + return new Output(out.toByteArray(), "image/" + format); + } + + protected static Dimension getExtent(Document input) { + + int width = DEFAULT_WIDTH; + int height = DEFAULT_HEIGHT; + + NodeList extents = input.getElementsByTagName("extent"); + + if (extents.getLength() > 0) { + Element element = (Element) extents.item(0); + String w = element.getAttribute("width"); + String h = element.getAttribute("height"); + + try { + width = Math.max(1, Integer.parseInt(w)); + } + catch (NumberFormatException nfe) { + log.warn("width '" + w + "' is not a valid."); + } + + try { + height = Math.max(1, Integer.parseInt(h)); + } + catch (NumberFormatException nfe) { + log.warn("height '" + h + "' is not a valid"); + } + } + + return new Dimension(width, height); + } + + protected static String getFormat(Document input) { + String format = DEFAULT_FORMAT; + + NodeList formats = input.getElementsByTagName("format"); + + if (formats.getLength() > 0) { + String type = ((Element) formats.item(0)).getAttribute("type"); + if (type.length() > 0) { + format = type; + } + } + + return format; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DischargeTablesOverview.java Tue Oct 23 07:39:09 2012 +0200 @@ -0,0 +1,240 @@ +package de.intevation.flys.artifacts.services; + +import java.awt.Color; +import java.text.DateFormat; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Locale; + +import org.apache.log4j.Logger; +import org.hibernate.Session; +import org.jfree.chart.ChartFactory; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.plot.PlotOrientation; +import org.jfree.chart.plot.XYPlot; +import org.jfree.data.xy.XYSeries; +import org.jfree.data.xy.XYSeriesCollection; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import de.intevation.artifacts.CallMeta; +import de.intevation.artifacts.GlobalContext; +import de.intevation.flys.artifacts.model.DischargeTables; +import de.intevation.flys.artifacts.model.GaugesFactory; +import de.intevation.flys.artifacts.resources.Resources; +import de.intevation.flys.backend.SessionHolder; +import de.intevation.flys.model.DischargeTable; +import de.intevation.flys.model.Gauge; +import de.intevation.flys.model.TimeInterval; + + +public class DischargeTablesOverview extends AbstractChartService { + + private static final Logger log = Logger + .getLogger(DischargeTablesOverview.class); + + private static final long serialVersionUID = 1L; + + public static final String I18N_CHART_TITLE = "gauge.discharge.service.chart.title"; + public static final String DEFAULT_CHART_TITLE = "Pegel: XXX"; + + public static final String I18N_CHART_X_AXIS_TITLE = "gauge.discharge.service.chart.x.title"; + public static final String DEFAULT_X_AXIS_TITLE = "Q [m^3/s]"; + + public static final String I18N_CHART_Y_AXIS_TITLE = "gauge.discharge.service.chart.y.title"; + public static final String DEFAULT_Y_AXIS_TITLE = "W [cm]"; + + public static final String I18N_CHART_SERIES_TITLE = "gauge.discharge.service.chart.series.title"; + public static final String DEFAULT_CHART_SERIES_TITLE = "Abflusskurve"; + + public static final String I18N_CHART_SERIES_TITLE_MASTER = "gauge.discharge.service.chart.series.title.master"; + public static final String DEFAULT_CHART_SERIES_TITLE_MASTER = "Aktuelle Abflusskurve"; + + public static final DateFormat DATE_FORMAT = DateFormat.getDateInstance( + DateFormat.SHORT, Locale.GERMANY); + + private Session session; + + @Override + protected void init() { + session = SessionHolder.acquire(); + } + + @Override + protected void finish() { + if (session != null) { + session.close(); + SessionHolder.release(); + } + } + + protected JFreeChart createChart(Document data, + GlobalContext globalContext, CallMeta callMeta) { + + Gauge gauge = extractGauge(data); + + if (gauge == null) { + log.warn("Could not determine Gauge from request!"); + return null; + } + + log.info("create discharge chart for gauge '" + gauge.getName() + "'"); + TimeInterval timerange = extractTimeInterval(data); + + List<DischargeTable> dts = getDischargeTables(gauge, timerange); + XYSeriesCollection dataset = new XYSeriesCollection(); + + for (DischargeTable dt : dts) { + try { + XYSeries series = createSeries(callMeta, dt); + if (series != null) { + dataset.addSeries(series); + } + } + catch (IllegalArgumentException iae) { + log.warn("unable to create discharge curve: " + + iae.getMessage()); + } + } + + String title = Resources.format(callMeta, I18N_CHART_TITLE, + DEFAULT_CHART_TITLE, gauge.getName()); + + String xAxis = Resources.getMsg(callMeta, I18N_CHART_X_AXIS_TITLE, + DEFAULT_X_AXIS_TITLE); + + String yAxis = Resources.format(callMeta, I18N_CHART_Y_AXIS_TITLE, + DEFAULT_Y_AXIS_TITLE); + + JFreeChart chart = ChartFactory.createXYLineChart(title, xAxis, yAxis, + null, PlotOrientation.VERTICAL, true, true, false); + + chart.setBackgroundPaint(Color.white); + + XYPlot plot = (XYPlot) chart.getPlot(); + plot.setDataset(0, dataset); + plot.setBackgroundPaint(Color.white); + plot.setDomainGridlinePaint(Color.gray); + plot.setRangeGridlinePaint(Color.gray); + plot.setDomainGridlinesVisible(true); + plot.setRangeGridlinesVisible(true); + + return chart; + } + + protected XYSeries createSeries(CallMeta callMeta, DischargeTable dt) + throws IllegalArgumentException { + + double[][] xy = null; + + if (dt.getKind() == DischargeTables.MASTER) { + xy = DischargeTables.loadDischargeTableValues(dt, + DischargeTables.MASTER_SCALE); + } + else { + xy = DischargeTables.loadDischargeTableValues(dt, + DischargeTables.HISTORICAL_SCALE); + } + + XYSeries series = new XYSeries(createSeriesTitle(callMeta, dt), false); + for (int i = 0, n = xy[0].length; i < n; i++) { + series.add(xy[0][i], xy[1][i]); + } + + return series; + } + + protected String createSeriesTitle(CallMeta callMeta, DischargeTable dt) + throws IllegalArgumentException { + TimeInterval timeInterval = dt.getTimeInterval(); + Date start = timeInterval.getStartTime(); + Date end = timeInterval.getStopTime(); + + if (start != null && end != null) { + return Resources.format(callMeta, I18N_CHART_SERIES_TITLE, + DEFAULT_CHART_SERIES_TITLE, start, end); + } + else if (start != null) { + return Resources.format(callMeta, I18N_CHART_SERIES_TITLE_MASTER, + DEFAULT_CHART_SERIES_TITLE, start); + } + else { + throw new IllegalArgumentException( + "Missing start date of DischargeTable " + dt.getId()); + } + } + + protected Gauge extractGauge(Document data) { + NodeList gauges = data.getElementsByTagName("gauge"); + + if (gauges.getLength() > 0) { + String name = ((Element) gauges.item(0)).getAttribute("name"); + if (name != null && name.length() > 0) { + return GaugesFactory.getGauge(name); + } + } + + return null; + } + + protected TimeInterval extractTimeInterval(Document data) { + NodeList timeranges = data.getElementsByTagName("timerange"); + + if (timeranges != null && timeranges.getLength() > 0) { + Element timerange = (Element) timeranges.item(0); + + String lower = timerange.getAttribute("lower"); + String upper = timerange.getAttribute("upper"); + + if (lower != null && upper != null) { + try { + Date d1 = DATE_FORMAT.parse(lower); + Date d2 = DATE_FORMAT.parse(upper); + + return new TimeInterval(d1, d2); + } + catch (ParseException pe) { + log.warn("Wrong time format: " + pe.getMessage()); + } + } + } + + return null; + } + + protected List<DischargeTable> getDischargeTables(Gauge gauge, + TimeInterval timerange) { + List<DischargeTable> all = gauge.getDischargeTables(); + Collections.sort(all); + + if (timerange == null) { + return all; + } + + List<DischargeTable> dts = new ArrayList<DischargeTable>(all.size()); + long startDate = timerange.getStartTime().getTime(); + long stopDate = timerange.getStopTime().getTime(); + + for (DischargeTable dt : all) { + TimeInterval tmp = dt.getTimeInterval(); + Date start = tmp.getStartTime(); + Date stop = tmp.getStartTime(); + + if (start.getTime() > startDate && start.getTime() < stopDate) { + dts.add(dt); + continue; + } + else if (stop != null && stop.getTime() < stopDate + && stop.getTime() > startDate) { + dts.add(dt); + continue; + } + } + + return dts; + } +}
--- a/flys-artifacts/src/main/resources/messages.properties Mon Oct 22 17:13:23 2012 +0200 +++ b/flys-artifacts/src/main/resources/messages.properties Tue Oct 23 07:39:09 2012 +0200 @@ -508,3 +508,8 @@ area.label.template = Area = %s m\u00b3 +gauge.discharge.service.chart.title = Discharge Tables at Gauge {0} +gauge.discharge.service.chart.x.title = Q [m\u00b3/s] +gauge.discharge.service.chart.y.title = W [cm] +gauge.discharge.service.chart.series.title = Discharge Curve {0,date,short} - {1,date,short} +gauge.discharge.service.chart.series.title.master = Current Discharge Curve since {0,date,short} \ No newline at end of file
--- a/flys-artifacts/src/main/resources/messages_de.properties Mon Oct 22 17:13:23 2012 +0200 +++ b/flys-artifacts/src/main/resources/messages_de.properties Tue Oct 23 07:39:09 2012 +0200 @@ -510,3 +510,8 @@ area.label.template = Fl\u00e4che = %s m\u00b3 +gauge.discharge.service.chart.title = Abflusskurven am Pegel {0} +gauge.discharge.service.chart.x.title = Q [m\u00b3/s] +gauge.discharge.service.chart.y.title = W [cm] +gauge.discharge.service.chart.series.title = Abflusskurve {0,date,short} - {1,date,short} +gauge.discharge.service.chart.series.title.master = Aktuelle Abflusskurve ab {0,date,short} \ No newline at end of file
--- a/flys-artifacts/src/main/resources/messages_de_DE.properties Mon Oct 22 17:13:23 2012 +0200 +++ b/flys-artifacts/src/main/resources/messages_de_DE.properties Tue Oct 23 07:39:09 2012 +0200 @@ -509,3 +509,8 @@ area.label.template = Fl\u00e4che = %s m\u00b3 +gauge.discharge.service.chart.title = Abflusskurven am Pegel {0} +gauge.discharge.service.chart.x.title = Q [m\u00b3/s] +gauge.discharge.service.chart.y.title = W [cm] +gauge.discharge.service.chart.series.title = Abflusskurve {0,date,short} - {1,date,short} +gauge.discharge.service.chart.series.title.master = Aktuelle Abflusskurve ab {0,date,short} \ No newline at end of file
--- a/flys-artifacts/src/main/resources/messages_en.properties Mon Oct 22 17:13:23 2012 +0200 +++ b/flys-artifacts/src/main/resources/messages_en.properties Tue Oct 23 07:39:09 2012 +0200 @@ -507,3 +507,9 @@ bed_diameter = Bed Diameter area.label.template = Area = %s m\u00b3 + +gauge.discharge.service.chart.title = Discharge Tables at Gauge {0} +gauge.discharge.service.chart.x.title = Q [m\u00b3/s] +gauge.discharge.service.chart.y.title = W [cm] +gauge.discharge.service.chart.series.title = Discharge Curve {0,date,short} - {1,date,short} +gauge.discharge.service.chart.series.title.master = Current Discharge Curve since {0,date,short} \ No newline at end of file