sascha@2614: package de.intevation.flys.artifacts.services; sascha@2614: sascha@2614: import de.intevation.artifactdatabase.DefaultService; sascha@2614: sascha@2614: import de.intevation.artifacts.CallMeta; sascha@2614: import de.intevation.artifacts.GlobalContext; sascha@2614: import de.intevation.artifacts.Service; sascha@2614: sascha@2615: import de.intevation.flys.artifacts.model.FixingsColumn; sascha@2615: import de.intevation.flys.artifacts.model.FixingsColumnFactory; sascha@2620: import de.intevation.flys.artifacts.model.FixingsFilterBuilder; sascha@2615: sascha@2615: import de.intevation.flys.artifacts.model.FixingsOverview.Fixing; sascha@2615: sascha@2620: import de.intevation.flys.artifacts.model.FixingsOverview; sascha@2620: import de.intevation.flys.artifacts.model.FixingsOverviewFactory; sascha@3146: import de.intevation.flys.artifacts.model.GaugeFinder; sascha@3146: import de.intevation.flys.artifacts.model.GaugeFinderFactory; sascha@3146: import de.intevation.flys.artifacts.model.GaugeRange; sascha@2620: sascha@2615: import de.intevation.flys.backend.SessionHolder; sascha@2615: sascha@2620: import de.intevation.flys.utils.Pair; sascha@2620: sascha@2620: import gnu.trove.TDoubleArrayList; sascha@2620: sascha@3146: import java.awt.BasicStroke; sascha@3146: import java.awt.Color; sascha@2620: import java.awt.Dimension; sascha@2620: import java.awt.Transparency; sascha@2620: sascha@2620: import java.awt.image.BufferedImage; sascha@2620: sascha@2620: import java.io.ByteArrayOutputStream; sascha@2620: import java.io.IOException; sascha@2620: sascha@2620: import java.util.ArrayList; sascha@2620: import java.util.List; sascha@2620: sascha@2620: import javax.imageio.ImageIO; sascha@2620: sascha@2614: import org.apache.log4j.Logger; sascha@2614: sascha@2620: import org.jfree.chart.ChartFactory; sascha@2620: import org.jfree.chart.ChartUtilities; sascha@2620: import org.jfree.chart.JFreeChart; sascha@2620: sascha@3146: import org.jfree.chart.plot.Marker; sascha@2620: import org.jfree.chart.plot.PlotOrientation; sascha@3146: import org.jfree.chart.plot.ValueMarker; sascha@2620: import org.jfree.chart.plot.XYPlot; sascha@2620: sascha@2620: import org.jfree.data.xy.DefaultXYDataset; sascha@2620: sascha@3146: import org.jfree.ui.RectangleAnchor; sascha@3146: import org.jfree.ui.TextAnchor; sascha@3146: sascha@2614: import org.w3c.dom.Document; sascha@2615: import org.w3c.dom.Element; sascha@2615: import org.w3c.dom.NodeList; sascha@2614: sascha@2614: public class FixingsKMChartService sascha@2614: extends DefaultService sascha@2614: { sascha@2614: private static final Logger log = sascha@2614: Logger.getLogger(FixingsKMChartService.class); sascha@2614: sascha@2620: public static final int DEFAULT_WIDTH = 240; sascha@2620: public static final int DEFAULT_HEIGHT = 180; sascha@2620: sascha@3146: // TODO: i18n sascha@3146: public static final String [] Q_SECTOR_LABELS = { sascha@3146: "(MNQ + MQ)/2", sascha@3146: "(MQ + MHQ)/2", sascha@3146: "HQ5" sascha@3146: }; sascha@3146: sascha@2620: public static final String DEFAULT_FORMAT = "png"; sascha@2620: sascha@2620: // TODO: Load fancy image from resources. sascha@2615: public static final byte [] EMPTY = { sascha@3076: (byte)0x89, (byte)0x50, (byte)0x4e, (byte)0x47, sascha@3076: (byte)0x0d, (byte)0x0a, (byte)0x1a, (byte)0x0a, sascha@3076: (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0d, sascha@3076: (byte)0x49, (byte)0x48, (byte)0x44, (byte)0x52, sascha@3076: (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, sascha@3076: (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, sascha@3076: (byte)0x08, (byte)0x00, (byte)0x00, (byte)0x00, sascha@3076: (byte)0x00, (byte)0x3a, (byte)0x7e, (byte)0x9b, sascha@3076: (byte)0x55, (byte)0x00, (byte)0x00, (byte)0x00, sascha@3076: (byte)0x01, (byte)0x73, (byte)0x52, (byte)0x47, sascha@3076: (byte)0x42, (byte)0x00, (byte)0xae, (byte)0xce, sascha@3076: (byte)0x1c, (byte)0xe9, (byte)0x00, (byte)0x00, sascha@3076: (byte)0x00, (byte)0x09, (byte)0x70, (byte)0x48, sascha@3076: (byte)0x59, (byte)0x73, (byte)0x00, (byte)0x00, sascha@3076: (byte)0x0b, (byte)0x13, (byte)0x00, (byte)0x00, sascha@3076: (byte)0x0b, (byte)0x13, (byte)0x01, (byte)0x00, sascha@3076: (byte)0x9a, (byte)0x9c, (byte)0x18, (byte)0x00, sascha@3076: (byte)0x00, (byte)0x00, (byte)0x07, (byte)0x74, sascha@3076: (byte)0x49, (byte)0x4d, (byte)0x45, (byte)0x07, sascha@3076: (byte)0xdc, (byte)0x04, (byte)0x04, (byte)0x10, sascha@3076: (byte)0x30, (byte)0x15, (byte)0x7d, (byte)0x77, sascha@3076: (byte)0x36, (byte)0x0b, (byte)0x00, (byte)0x00, sascha@3076: (byte)0x00, (byte)0x08, (byte)0x74, (byte)0x45, sascha@3076: (byte)0x58, (byte)0x74, (byte)0x43, (byte)0x6f, sascha@3076: (byte)0x6d, (byte)0x6d, (byte)0x65, (byte)0x6e, sascha@3076: (byte)0x74, (byte)0x00, (byte)0xf6, (byte)0xcc, sascha@3076: (byte)0x96, (byte)0xbf, (byte)0x00, (byte)0x00, sascha@3076: (byte)0x00, (byte)0x0a, (byte)0x49, (byte)0x44, sascha@3076: (byte)0x41, (byte)0x54, (byte)0x08, (byte)0xd7, sascha@3076: (byte)0x63, (byte)0xf8, (byte)0x0f, (byte)0x00, sascha@3076: (byte)0x01, (byte)0x01, (byte)0x01, (byte)0x00, sascha@3076: (byte)0x1b, (byte)0xb6, (byte)0xee, (byte)0x56, sascha@3076: (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, sascha@3076: (byte)0x49, (byte)0x45, (byte)0x4e, (byte)0x44, sascha@2615: (byte)0xae, (byte)0x42, (byte)0x60, (byte)0x82 sascha@2615: }; sascha@2615: sascha@2615: private static final Output empty() { sascha@2615: return new Output(EMPTY, "image/png"); sascha@2615: } sascha@2614: sascha@2614: @Override sascha@2614: public Service.Output process( sascha@2614: Document data, sascha@2614: GlobalContext globalContext, sascha@2614: CallMeta callMeta sascha@2614: ) { sascha@2614: log.debug("FixingsKMChartService.process"); sascha@2615: sascha@2615: SessionHolder.acquire(); sascha@2615: try { sascha@2615: return doProcess(data, globalContext, callMeta); sascha@2615: } sascha@2615: finally { sascha@2615: SessionHolder.HOLDER.get().close(); sascha@2615: SessionHolder.release(); sascha@2615: } sascha@2615: } sascha@2615: sascha@2615: protected Service.Output doProcess( sascha@2620: Document input, sascha@2615: GlobalContext globalContext, sascha@2615: CallMeta callMeta sascha@2615: ) { sascha@2620: String river = getRiverName(input); sascha@2620: Double km = getKM(input); sascha@2620: Dimension extent = getExtent(input); sascha@2620: String format = getFormat(input); sascha@2615: sascha@2620: if (river == null || km == null) { sascha@2620: log.warn("River and/or km invalid."); sascha@2615: return empty(); sascha@2615: } sascha@2615: sascha@2615: FixingsOverview overview = FixingsOverviewFactory.getOverview(river); sascha@2615: sascha@2615: if (overview == null) { sascha@2615: log.warn("No overview found for river '" + river + "'"); sascha@2615: return empty(); sascha@2615: } sascha@2615: sascha@2620: FixingsFilterBuilder ffb = new FixingsFilterBuilder(input); sascha@2615: sascha@2615: List columns = overview.filter( sascha@2615: ffb.getRange(), sascha@2615: ffb.getFilter()); sascha@2615: sascha@2620: List> cols = sascha@2620: new ArrayList>(); sascha@2620: sascha@2620: for (Fixing.Column col: columns) { sascha@2620: FixingsColumn data = sascha@2620: FixingsColumnFactory.INSTANCE.getColumnData(col); sascha@2620: if (data != null) { sascha@2620: cols.add(new Pair(col, data)); sascha@2615: } sascha@2615: } sascha@2615: sascha@2620: JFreeChart chart = createChart(cols, river, km); sascha@2620: sascha@2620: return encode(chart, extent, format); sascha@2620: } sascha@2620: sascha@2620: protected static Output encode( sascha@2620: JFreeChart chart, sascha@3076: Dimension extent, sascha@2620: String format sascha@2620: ) { sascha@2620: BufferedImage image = chart.createBufferedImage( sascha@2620: extent.width, extent.height, sascha@2620: Transparency.BITMASK, sascha@2620: null); sascha@2620: sascha@2620: ByteArrayOutputStream out = new ByteArrayOutputStream(); sascha@2620: sascha@2620: try { sascha@2620: ImageIO.write(image, format, out); sascha@2620: } sascha@2620: catch (IOException ioe) { sascha@2620: log.warn("writing image failed", ioe); sascha@2620: return empty(); sascha@2620: } sascha@2620: sascha@2620: return new Output(out.toByteArray(), "image/" + format); sascha@2620: } sascha@2620: sascha@2620: protected static JFreeChart createChart( sascha@2620: List> cols, sascha@2620: String river, sascha@2620: double km sascha@2620: ) { sascha@2620: sascha@2620: TDoubleArrayList ws = new TDoubleArrayList(cols.size()); sascha@2620: TDoubleArrayList qs = new TDoubleArrayList(cols.size()); sascha@2620: sascha@2620: double [] w = new double[1]; sascha@2620: for (Pair col: cols) { sascha@2620: boolean interpolated = col.getB().getW(km, w); sascha@2620: // TODO: Do something special with the interpolated values. sascha@2620: double q = col.getB().getQ(km); sascha@2620: if (!Double.isNaN(w[0]) && !Double.isNaN(q)) { sascha@2620: ws.add(w[0]); sascha@2620: qs.add(q); sascha@2620: // TODO: Generate labels depending on sectors. sascha@2620: } sascha@2620: } sascha@2620: sascha@2620: DefaultXYDataset dataset = new DefaultXYDataset(); sascha@2620: sascha@2620: dataset.addSeries( sascha@2620: "Fixierungen", // TODO: i18n sascha@2621: new double [][] { qs.toNativeArray(), ws.toNativeArray() }); sascha@2620: sascha@2621: JFreeChart chart = ChartFactory.createScatterPlot( sascha@2620: "Fixierungen " + river + ": km " + km, // TODO: i18n sascha@2620: "Q", // TODO: i18n sascha@2620: "W", // TODO: i18n sascha@2621: dataset, sascha@2620: PlotOrientation.VERTICAL, sascha@2620: true, sascha@2621: false, sascha@2620: false); sascha@2620: sascha@3146: applyQSectorMarkers(chart.getXYPlot(), river, km); sascha@2620: sascha@2620: ChartUtilities.applyCurrentTheme(chart); sascha@2620: sascha@2620: return chart; sascha@2620: } sascha@2620: sascha@3146: protected static void applyQSectorMarkers( sascha@3146: XYPlot plot, sascha@3152: String river, sascha@3146: double km sascha@3146: ) { sascha@3146: GaugeFinderFactory ggf = GaugeFinderFactory.getInstance(); sascha@3146: GaugeFinder gf = ggf.getGaugeFinder(river); sascha@3146: sascha@3146: if (gf == null) { sascha@3146: log.warn("No gauge finder found for river '" + river + "'"); sascha@3146: return; sascha@3146: } sascha@3146: sascha@3152: GaugeRange gr = gf.find(km); sascha@3146: if (gr == null) { sascha@3146: log.debug("No gauge range found for km " sascha@3146: + km + " on river " + river + "."); sascha@3146: return; sascha@3146: } sascha@3146: sascha@3151: if (log.isDebugEnabled()) { sascha@3151: log.debug(gr); sascha@3151: } sascha@3151: sascha@3146: for (int i = 0; i < Q_SECTOR_LABELS.length; ++i) { sascha@3146: Marker m = createQSectorMarker( sascha@3146: gr.getSectorBorder(i), sascha@3146: Q_SECTOR_LABELS[i]); sascha@3146: if (m != null) { sascha@3146: plot.addDomainMarker(m); sascha@3146: } sascha@3146: } sascha@3146: } sascha@3146: sascha@3146: protected static Marker createQSectorMarker(double value, String label) { sascha@3146: if (Double.isNaN(value)) { sascha@3146: return null; sascha@3146: } sascha@3146: Marker m = new ValueMarker(value); sascha@3146: m.setPaint(Color.black); sascha@3146: m.setStroke(new BasicStroke()); sascha@3146: m.setLabel(label); sascha@3146: m.setLabelAnchor(RectangleAnchor.TOP_LEFT); sascha@3146: m.setLabelTextAnchor(TextAnchor.TOP_LEFT); sascha@3146: return m; sascha@3146: } sascha@3146: sascha@2620: protected static String getRiverName(Document input) { sascha@2620: NodeList rivers = input.getElementsByTagName("river"); sascha@2620: sascha@2620: if (rivers.getLength() == 0) { sascha@2620: return null; sascha@2620: } sascha@2620: sascha@2620: String river = ((Element)rivers.item(0)).getAttribute("name"); sascha@2620: sascha@2621: return river.length() > 0 ? river : null; sascha@2620: } sascha@2620: sascha@2620: protected static Double getKM(Document input) { sascha@2620: NodeList kms = input.getElementsByTagName("km"); sascha@2620: sascha@2620: if (kms.getLength() == 0) { sascha@2620: return null; sascha@2620: } sascha@2620: sascha@2620: String km = ((Element)kms.item(0)).getAttribute("value"); sascha@2620: sascha@2620: try { sascha@2620: return Double.valueOf(km); sascha@2620: } sascha@2620: catch (NumberFormatException nfe) { sascha@2620: log.warn("Km '" + km + " is not a valid number."); sascha@2620: return null; sascha@2620: } sascha@2620: } sascha@2620: sascha@2620: protected static Dimension getExtent(Document input) { sascha@2620: sascha@2620: int width = DEFAULT_WIDTH; sascha@2620: int height = DEFAULT_HEIGHT; sascha@2620: sascha@2620: NodeList extents = input.getElementsByTagName("extent"); sascha@2620: sascha@2620: if (extents.getLength() > 0) { sascha@2620: Element element = (Element)extents.item(0); sascha@2620: String w = element.getAttribute("width"); sascha@2620: String h = element.getAttribute("height"); sascha@2620: sascha@2620: try { sascha@2620: width = Math.max(1, Integer.parseInt(w)); sascha@2620: } sascha@2620: catch (NumberFormatException nfe) { sascha@2620: log.warn("width '" + w + "' is not a valid."); sascha@2620: } sascha@2620: sascha@2620: try { sascha@2620: height = Math.max(1, Integer.parseInt(h)); sascha@2620: } sascha@2620: catch (NumberFormatException nfe) { sascha@2620: log.warn("height '" + h + "' is not a valid"); sascha@2620: } sascha@2620: } sascha@2620: sascha@2620: return new Dimension(width, height); sascha@2620: } sascha@2620: sascha@2620: protected static String getFormat(Document input) { sascha@2620: String format = DEFAULT_FORMAT; sascha@2620: sascha@2620: NodeList formats = input.getElementsByTagName("format"); sascha@2620: sascha@2620: if (formats.getLength() > 0) { sascha@2620: String type = ((Element)formats.item(0)).getAttribute("type"); sascha@2620: if (type.length() > 0) { sascha@2620: format = type; sascha@2620: } sascha@2620: } sascha@2620: sascha@2620: return format; sascha@2614: } sascha@2614: } sascha@2614: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :