view flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/FixingsKMChartService.java @ 2782:7c2b24fb0316

Corrected config for wms fixpoints and floodmaps. flys-artifacts/trunk@4521 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 25 May 2012 13:40:22 +0000
parents f0cc556eda29
children 5642a83420f2
line wrap: on
line source
package de.intevation.flys.artifacts.services;

import de.intevation.artifactdatabase.DefaultService;

import de.intevation.artifacts.CallMeta;
import de.intevation.artifacts.GlobalContext;
import de.intevation.artifacts.Service;

import de.intevation.flys.artifacts.model.FixingsColumn;
import de.intevation.flys.artifacts.model.FixingsColumnFactory;
import de.intevation.flys.artifacts.model.FixingsFilterBuilder;

import de.intevation.flys.artifacts.model.FixingsOverview.Fixing;

import de.intevation.flys.artifacts.model.FixingsOverview;
import de.intevation.flys.artifacts.model.FixingsOverviewFactory;

import de.intevation.flys.backend.SessionHolder;

import de.intevation.flys.utils.Pair;

import gnu.trove.TDoubleArrayList;

import java.awt.Dimension;
import java.awt.Transparency;

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;

import org.jfree.data.xy.DefaultXYDataset;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class FixingsKMChartService
extends      DefaultService
{
    private static final Logger log =
        Logger.getLogger(FixingsKMChartService.class);

    public static final int DEFAULT_WIDTH  = 240;
    public static final int DEFAULT_HEIGHT = 180;

    public static final String DEFAULT_FORMAT = "png";

    // 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");
    }

    @Override
    public Service.Output process(
        Document      data,
        GlobalContext globalContext,
        CallMeta      callMeta
    ) {
        log.debug("FixingsKMChartService.process");

        SessionHolder.acquire();
        try {
            return doProcess(data, globalContext, callMeta);
        }
        finally {
            SessionHolder.HOLDER.get().close();
            SessionHolder.release();
        }
    }

    protected Service.Output doProcess(
        Document      input,
        GlobalContext globalContext,
        CallMeta      callMeta
    ) {
        String    river  = getRiverName(input);
        Double    km     = getKM(input);
        Dimension extent = getExtent(input);
        String    format = getFormat(input);

        if (river == null || km == null) {
            log.warn("River and/or km invalid.");
            return empty();
        }

        FixingsOverview overview = FixingsOverviewFactory.getOverview(river);

        if (overview == null) {
            log.warn("No overview found for river '" + river + "'");
            return empty();
        }

        FixingsFilterBuilder ffb = new FixingsFilterBuilder(input);

        List<Fixing.Column> columns = overview.filter(
            ffb.getRange(),
            ffb.getFilter());

        List<Pair<Fixing.Column, FixingsColumn>> cols =
            new ArrayList<Pair<Fixing.Column, FixingsColumn>>();

        for (Fixing.Column col: columns) {
            FixingsColumn data =
                FixingsColumnFactory.INSTANCE.getColumnData(col);
            if (data != null) {
                cols.add(new Pair<Fixing.Column, FixingsColumn>(col, data));
            }
        }

        JFreeChart chart = createChart(cols, river, km);

        return encode(chart, extent, format);
    }

    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 JFreeChart createChart(
        List<Pair<Fixing.Column, FixingsColumn>> cols,
        String river,
        double km
    ) {

        TDoubleArrayList ws = new TDoubleArrayList(cols.size());
        TDoubleArrayList qs = new TDoubleArrayList(cols.size());

        double [] w = new double[1];
        for (Pair<Fixing.Column, FixingsColumn> col: cols) {
            boolean interpolated = col.getB().getW(km, w);
            // TODO: Do something special with the interpolated values.
            double q = col.getB().getQ(km);
            if (!Double.isNaN(w[0]) && !Double.isNaN(q)) {
                ws.add(w[0]);
                qs.add(q);
                // TODO: Generate labels depending on sectors.
            }
        }

        DefaultXYDataset dataset = new DefaultXYDataset();

        dataset.addSeries(
            "Fixierungen", // TODO: i18n
            new double [][] { qs.toNativeArray(), ws.toNativeArray() });

        JFreeChart chart = ChartFactory.createScatterPlot(
            "Fixierungen " + river + ": km " + km, // TODO: i18n
            "Q", // TODO: i18n
            "W", // TODO: i18n
            dataset,
            PlotOrientation.VERTICAL,
            true,
            false,
            false);

        //XYPlot plot = chart.getXYPlot();

        //plot.setDataset(dataset);

        ChartUtilities.applyCurrentTheme(chart);

        return chart;
    }

    protected static String getRiverName(Document input) {
        NodeList rivers = input.getElementsByTagName("river");

        if (rivers.getLength() == 0) {
            return null;
        }

        String river = ((Element)rivers.item(0)).getAttribute("name");

        return river.length() > 0 ? river : null;
    }

    protected static Double getKM(Document input) {
        NodeList kms = input.getElementsByTagName("km");

        if (kms.getLength() == 0) {
            return null;
        }

        String km = ((Element)kms.item(0)).getAttribute("value");

        try {
            return Double.valueOf(km);
        }
        catch (NumberFormatException nfe) {
            log.warn("Km '" + km + " is not a valid number.");
            return null;
        }
    }

    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;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org