teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.exports.minfo; raimund@3898: raimund@3898: import org.apache.log4j.Logger; raimund@3898: import org.jfree.data.xy.XYSeries; raimund@3898: import org.w3c.dom.Document; raimund@3898: teichmann@5831: import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; teichmann@5831: import org.dive4elements.artifactdatabase.state.Facet; felix@6123: import org.dive4elements.river.artifacts.D4EArtifact; teichmann@5831: import org.dive4elements.river.artifacts.model.FacetTypes; teichmann@5831: import org.dive4elements.river.artifacts.model.WKms; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.BedDiffYearResult; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.BedDifferencesResult; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.MorphologicWidth; teichmann@5831: import org.dive4elements.river.exports.StyledSeriesBuilder; teichmann@5831: import org.dive4elements.river.exports.fixings.FixChartGenerator; teichmann@5831: import org.dive4elements.river.exports.process.KMIndexProcessor; teichmann@5831: import org.dive4elements.river.exports.process.Processor; teichmann@5831: import org.dive4elements.river.exports.process.WOutProcessor; teichmann@5831: import org.dive4elements.river.jfree.Bounds; teichmann@5831: import org.dive4elements.river.jfree.DoubleBounds; teichmann@5864: import org.dive4elements.river.jfree.RiverAnnotation; teichmann@5831: import org.dive4elements.river.jfree.StyledXYSeries; teichmann@5831: import org.dive4elements.river.utils.DataUtil; felix@6123: import org.dive4elements.river.utils.RiverUtils; raimund@3898: raimund@3898: raimund@3898: public class BedDifferenceYearGenerator bjoern@4446: extends FixChartGenerator raimund@3898: implements FacetTypes raimund@3898: { raimund@3898: public enum YAXIS { rrenkert@5681: D(0), M(1), H(2), dW(3), W(4); raimund@3898: raimund@3898: protected int idx; raimund@3898: raimund@3898: private YAXIS(int c) { raimund@3898: idx = c; raimund@3898: } raimund@3898: } raimund@3898: raimund@3898: /** The logger that is used in this generator. */ raimund@3898: private static Logger logger = Logger.getLogger(BedDifferenceYearGenerator.class); raimund@3898: raimund@3898: public static final String I18N_CHART_TITLE = "chart.beddifference.year.title"; raimund@3898: public static final String I18N_XAXIS_LABEL = "chart.beddifference.xaxis.label"; raimund@3898: public static final String I18N_YAXIS_LABEL = "chart.beddifference.yaxis.label.diff"; raimund@3898: public static final String I18N_SECOND_YAXIS_LABEL = "chart.beddifference.yaxis.label.morph"; raimund@3898: public static final String I18N_THIRD_YAXIS_LABEL = "chart.beddifference.yaxis.label.heights"; raimund@3898: raimund@3898: public static final String I18N_CHART_TITLE_DEFAULT = "Sohlenhöhen Differenz"; raimund@3898: public static final String I18N_XAXIS_LABEL_DEFAULT = "Fluss-Km"; raimund@3898: public static final String I18N_YAXIS_LABEL_DEFAULT = "delta S [m]"; rrenkert@5766: public static final String I18N_SECOND_YAXIS_LABEL_DEFAULT = "Breite [m]"; raimund@3898: public static final String I18N_THIRD_YAXIS_LABEL_DEFAULT = "Höhe [m]"; bjoern@4446: public static final String I18N_DW_YAXIS_LABEL_DEFAULT = bjoern@4446: "delta W [cm]"; bjoern@4446: public static final String I18N_DW_YAXIS_LABEL = bjoern@4446: "chart.fixings.longitudinalsection.yaxis.label"; rrenkert@5681: private static final String I18N_W_YAXIS_LABEL = rrenkert@5681: "chart.longitudinal.section.yaxis.label"; rrenkert@5681: private static final String I18N_W_YAXIS_LABEL_DEFAULT = "W [NN + m]"; raimund@3898: raimund@3898: @Override raimund@3898: protected YAxisWalker getYAxisWalker() { raimund@3898: return new YAxisWalker() { raimund@3898: raimund@3898: @Override raimund@3898: public int length() { raimund@3898: return YAXIS.values().length; raimund@3898: } raimund@3898: raimund@3898: @Override raimund@3898: public String getId(int idx) { raimund@3898: YAXIS[] yaxes = YAXIS.values(); raimund@3898: return yaxes[idx].toString(); raimund@3898: } raimund@3898: }; raimund@3898: } raimund@3898: felix@6550: public void setContextBounds(ArtifactAndFacet bundle) { felix@6550: if (getXBounds(0) != null && getDomainAxisRange() != null) { felix@6550: Bounds bounds = felix@6550: calculateZoom(getXBounds(0), getDomainAxisRange()); felix@6550: context.putContextValue("startkm", bounds.getLower()); felix@6550: context.putContextValue("endkm", bounds.getUpper()); felix@6550: } felix@6550: else if (getXBounds(0) != null && getDomainAxisRange() == null) { felix@6550: context.putContextValue("startkm", getXBounds(0).getLower()); felix@6550: context.putContextValue("endkm", getXBounds(0).getUpper()); felix@6550: } felix@6550: else if (getXBounds(0) == null && getDomainAxisRange() == null) { felix@6550: BedDifferencesResult data = (BedDifferencesResult)bundle.getData(context); felix@6550: if (data.getKms().size() > 0) { felix@6550: context.putContextValue("startkm", data.getKms().min()); felix@6550: context.putContextValue("endkm", data.getKms().max()); felix@6550: } felix@6550: else { felix@6550: logger.warn("No data to define start and end km"); felix@6550: } felix@6550: } felix@6550: else if (getXBounds(0) == null && getDomainAxisRange() != null){ felix@6550: BedDifferencesResult data = (BedDifferencesResult)bundle.getData(context); felix@6550: Bounds b = new DoubleBounds(data.getKms().min(), data.getKms().max()); felix@6550: Bounds bounds = felix@6550: calculateZoom(b, getDomainAxisRange()); felix@6550: context.putContextValue("startkm", bounds.getLower()); felix@6550: context.putContextValue("endkm", bounds.getUpper()); felix@6550: } felix@6550: } felix@6550: raimund@3898: @Override raimund@3898: public void doOut(ArtifactAndFacet bundle, Document attr, boolean visible) { raimund@3898: String name = bundle.getFacetName(); raimund@3898: raimund@3898: logger.debug("doOut: " + name); raimund@3898: raimund@3898: if (name == null) { raimund@3898: logger.error("No facet name for doOut(). No output generated!"); raimund@3898: return; raimund@3898: } raimund@3898: raimund@3898: Facet facet = bundle.getFacet(); raimund@3898: raimund@3898: if (facet == null) { raimund@3898: return; raimund@3898: } raimund@3898: felix@6550: setContextBounds(bundle); felix@6550: bjoern@4446: Processor processor = new KMIndexProcessor(); rrenkert@5681: Processor woutp = new WOutProcessor(); raimund@3898: if (name.equals(BED_DIFFERENCE_YEAR)) { raimund@3898: doBedDifferenceYearOut( raimund@3898: (BedDiffYearResult) bundle.getData(context), raimund@3898: bundle, attr, visible); raimund@3898: } raimund@3898: else if (name.equals(BED_DIFFERENCE_MORPH_WIDTH)) { raimund@3898: doBedDifferenceMorphWidthOut( raimund@3898: (BedDiffYearResult) bundle.getData(context), raimund@3898: bundle, attr, visible); raimund@3898: } raimund@3898: else if (name.equals(BED_DIFFERENCE_YEAR_HEIGHT1)) { raimund@3898: doBedDifferenceHeightsOut( raimund@3898: (BedDiffYearResult)bundle.getData(context), raimund@3898: bundle, attr, visible, 0); raimund@3898: } raimund@3898: else if (name.equals(BED_DIFFERENCE_YEAR_HEIGHT2)) { raimund@3898: doBedDifferenceHeightsOut( raimund@3898: (BedDiffYearResult)bundle.getData(context), raimund@3898: bundle, attr, visible, 1); rrenkert@4673: } rrenkert@4673: else if (name.equals(BED_DIFFERENCE_YEAR_FILTERED)) { rrenkert@4673: doBedDifferenceYearOut( rrenkert@4673: (BedDiffYearResult) bundle.getData(context), rrenkert@4673: bundle, attr, visible); rrenkert@4673: } rrenkert@4673: else if (name.equals(BED_DIFFERENCE_YEAR_HEIGHT1_FILTERED)) { rrenkert@4673: doBedDifferenceHeightsOut( rrenkert@4673: (BedDiffYearResult)bundle.getData(context), rrenkert@4673: bundle, attr, visible, 0); rrenkert@4673: } rrenkert@4673: else if (name.equals(BED_DIFFERENCE_YEAR_HEIGHT2_FILTERED)) { rrenkert@4673: doBedDifferenceHeightsOut( rrenkert@4673: (BedDiffYearResult)bundle.getData(context), rrenkert@4673: bundle, attr, visible, 1); teichmann@4736: } rrenkert@5766: else if (name.equals(MORPHOLOGIC_WIDTH)) { rrenkert@5766: doMorphologicWidthOut( rrenkert@5766: (MorphologicWidth)bundle.getData(context), rrenkert@5766: bundle, rrenkert@5766: attr, rrenkert@5766: visible, rrenkert@5766: 0); rrenkert@5766: } bjoern@4446: else if (processor.canHandle(name)) { bjoern@4446: processor.doOut(this, bundle, attr, visible, YAXIS.dW.idx); raimund@3898: } raimund@3898: else if (name.equals(LONGITUDINAL_ANNOTATION)) { raimund@3898: doAnnotations( teichmann@5864: (RiverAnnotation) bundle.getData(context), raimund@3898: bundle, raimund@3898: attr, raimund@3898: visible); raimund@3898: } rrenkert@5681: else if (woutp.canHandle(name)) { rrenkert@5681: woutp.doOut(this, bundle, attr, visible, YAXIS.W.idx); rrenkert@5681: } rrenkert@5681: else if (name.equals(W_DIFFERENCES)) { rrenkert@5681: doWDifferencesOut( rrenkert@5681: (WKms) bundle.getData(context), rrenkert@5681: bundle, rrenkert@5681: attr, rrenkert@5681: visible); bjoern@4446: } bjoern@4446: else { bjoern@4446: logger.warn("Unknown facet name " + name); bjoern@4446: } raimund@3898: } raimund@3898: rrenkert@5766: private void doMorphologicWidthOut( rrenkert@5766: MorphologicWidth data, rrenkert@5766: ArtifactAndFacet bundle, rrenkert@5766: Document attr, rrenkert@5766: boolean visible, rrenkert@5766: int i) { rrenkert@5766: XYSeries series = new StyledXYSeries(bundle.getFacetDescription(), attr); rrenkert@5766: StyledSeriesBuilder.addPoints(series, data.getAsArray(), true); rrenkert@5766: rrenkert@5766: addAxisSeries(series, YAXIS.M.idx, visible); rrenkert@5766: } rrenkert@5766: raimund@3898: private void doBedDifferenceHeightsOut( raimund@3898: BedDiffYearResult data, raimund@3898: ArtifactAndFacet bundle, raimund@3898: Document attr, raimund@3898: boolean visible, raimund@3898: int idx) { raimund@3898: logger.debug("doBedDifferenceYearOut()"); raimund@3898: raimund@3898: XYSeries series = new StyledXYSeries(bundle.getFacetDescription(), attr); raimund@3898: if (idx == 0) { raimund@3898: StyledSeriesBuilder.addPoints(series, data.getHeights1Data(), true); raimund@3898: } raimund@3898: else { raimund@3898: StyledSeriesBuilder.addPoints(series, data.getHeights2Data(), true); raimund@3898: } raimund@3898: raimund@3898: addAxisSeries(series, YAXIS.H.idx, visible); raimund@3898: } raimund@3898: raimund@3898: @Override raimund@3898: protected String getDefaultChartTitle() { raimund@3898: return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT); raimund@3898: } raimund@3898: raimund@3898: @Override raimund@3898: protected String getDefaultXAxisLabel() { felix@6116: return msg(I18N_XAXIS_LABEL, felix@6116: I18N_XAXIS_LABEL_DEFAULT, felix@6116: new Object[] { getRiverName() }); raimund@3898: } raimund@3898: raimund@3898: @Override raimund@3898: protected String getDefaultYAxisLabel(int pos) { raimund@3898: String label = "default"; rrenkert@6227: D4EArtifact flys = (D4EArtifact) master; raimund@3898: if (pos == YAXIS.D.idx) { raimund@3898: label = msg(I18N_YAXIS_LABEL, I18N_YAXIS_LABEL_DEFAULT); raimund@3898: } raimund@3898: else if (pos == YAXIS.M.idx) { raimund@3898: label = msg(I18N_SECOND_YAXIS_LABEL, I18N_SECOND_YAXIS_LABEL_DEFAULT); raimund@3898: } raimund@3898: else if (pos == YAXIS.H.idx) { felix@6123: label = msg(I18N_THIRD_YAXIS_LABEL, felix@6123: I18N_THIRD_YAXIS_LABEL_DEFAULT, felix@6123: new Object[] felix@6123: { RiverUtils.getRiver(flys).getWstUnit().getName() }); raimund@3898: } bjoern@4378: else if (pos == YAXIS.dW.idx) { bjoern@4378: label = msg(I18N_DW_YAXIS_LABEL, I18N_DW_YAXIS_LABEL_DEFAULT); bjoern@4378: } rrenkert@5681: else if (pos == YAXIS.W.idx) { rrenkert@6227: return msg(I18N_W_YAXIS_LABEL, I18N_W_YAXIS_LABEL_DEFAULT, rrenkert@6227: new Object[] rrenkert@6227: { RiverUtils.getRiver(flys).getWstUnit().getName() }); rrenkert@5681: } rrenkert@5681: raimund@3898: return label; raimund@3898: } raimund@3898: raimund@3898: protected void doBedDifferenceYearOut(BedDiffYearResult data, raimund@3898: ArtifactAndFacet aandf, Document theme, boolean visible) { raimund@3898: logger.debug("doBedDifferenceYearOut()"); raimund@3898: raimund@3898: XYSeries series = new StyledXYSeries(aandf.getFacetDescription(), theme); raimund@3898: StyledSeriesBuilder.addPoints(series, data.getDifferencesData(), true); raimund@3898: raimund@3898: addAxisSeries(series, YAXIS.D.idx, visible); raimund@3898: } raimund@3898: raimund@3898: protected void doBedDifferenceMorphWidthOut(BedDiffYearResult data, raimund@3898: ArtifactAndFacet aandf, Document theme, boolean visible) { raimund@3898: logger.debug("doBedDifferencesMorphWidthOut()"); raimund@3898: raimund@3898: XYSeries series = new StyledXYSeries(aandf.getFacetDescription(), theme); raimund@3898: StyledSeriesBuilder.addPoints(series, data.getMorphWidthData(), true); raimund@3898: raimund@3898: addAxisSeries(series, YAXIS.M.idx, visible); raimund@3898: } rrenkert@5681: rrenkert@5681: protected void doWDifferencesOut( rrenkert@5681: WKms wkms, rrenkert@5681: ArtifactAndFacet aandf, rrenkert@5681: Document theme, rrenkert@5681: boolean visible rrenkert@5681: ) { rrenkert@5681: if (wkms == null) { rrenkert@5681: logger.warn("No data to add to WDifferencesChart."); rrenkert@5681: return; rrenkert@5681: } rrenkert@5681: rrenkert@5681: XYSeries series = new StyledXYSeries(aandf.getFacetDescription(), theme); rrenkert@5681: StyledSeriesBuilder.addPoints(series, wkms); rrenkert@5681: rrenkert@5681: addAxisSeries(series, YAXIS.D.idx, visible); rrenkert@5681: if (DataUtil.guessWaterIncreasing(wkms.allWs())) { rrenkert@5681: setInverted(true); rrenkert@5681: } rrenkert@5681: } teichmann@5864: }