Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractSInfoProcessor.java @ 9204:4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
author | mschaefer |
---|---|
date | Mon, 02 Jul 2018 17:40:39 +0200 |
parents | 9831f6c60473 |
children | 3dae6b78e1da |
rev | line source |
---|---|
8884 | 1 /* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde |
2 * Software engineering by | |
3 * Björnsen Beratende Ingenieure GmbH | |
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt | |
5 * | |
6 * This file is Free Software under the GNU AGPL (>=v3) | |
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
8 * documentation coming with Dive4Elements River for details. | |
9 */ | |
10 | |
8916 | 11 package org.dive4elements.river.artifacts.sinfo.common; |
8884 | 12 |
9200 | 13 import java.util.Map; |
8884 | 14 import java.util.Set; |
15 | |
16 import org.apache.log4j.Logger; | |
17 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; | |
9203 | 18 import org.dive4elements.artifactdatabase.state.Facet; |
9200 | 19 import org.dive4elements.artifacts.Artifact; |
20 import org.dive4elements.artifacts.CallContext; | |
21 import org.dive4elements.river.artifacts.D4EArtifact; | |
22 import org.dive4elements.river.artifacts.access.RiverAccess; | |
23 import org.dive4elements.river.artifacts.common.AbstractCalculationResult; | |
24 import org.dive4elements.river.artifacts.common.IResultType; | |
25 import org.dive4elements.river.artifacts.context.RiverContext; | |
26 import org.dive4elements.river.artifacts.math.MovingAverage; | |
27 import org.dive4elements.river.artifacts.model.ZoomScale; | |
9203 | 28 import org.dive4elements.river.artifacts.resources.Resources; |
29 import org.dive4elements.river.artifacts.states.DefaultState.ComputeType; | |
8884 | 30 import org.dive4elements.river.exports.DiagramGenerator; |
9200 | 31 import org.dive4elements.river.exports.StyledSeriesBuilder; |
8884 | 32 import org.dive4elements.river.exports.process.DefaultProcessor; |
9200 | 33 import org.dive4elements.river.jfree.StyledAreaSeriesCollection; |
34 import org.dive4elements.river.jfree.StyledXYSeries; | |
8884 | 35 import org.dive4elements.river.themes.ThemeDocument; |
36 | |
37 /** | |
38 * Abstraction for some processor implementation within S-INFO. Probably this abstraction could also be used for other | |
39 * cases as well. | |
40 * | |
41 * @author Gernot Belger | |
42 * | |
43 */ | |
8916 | 44 public abstract class AbstractSInfoProcessor extends DefaultProcessor { |
8884 | 45 |
9200 | 46 protected static final double GAP_DISTANCE = 0.101; |
47 | |
8884 | 48 private final static Logger log = Logger.getLogger(AbstractSInfoProcessor.class); |
49 | |
50 private String yAxisLabel; | |
51 | |
52 private final Set<String> handled_facet_types; | |
53 | |
54 private final String i18n_axis_label; | |
55 | |
56 public AbstractSInfoProcessor(final String i18n_axis_label, final Set<String> handled_facet_types) { | |
57 this.i18n_axis_label = i18n_axis_label; | |
58 this.handled_facet_types = handled_facet_types; | |
59 } | |
60 | |
61 @Override | |
62 public final void doOut(final DiagramGenerator generator, final ArtifactAndFacet bundle, final ThemeDocument theme, final boolean visible) { | |
8886 | 63 try { |
64 this.yAxisLabel = generateSeries(generator, bundle, theme, visible); | |
8884 | 65 } |
8886 | 66 catch (final Exception e) { |
67 log.error(e.getMessage(), e); | |
68 } | |
8884 | 69 } |
70 | |
71 /** | |
8886 | 72 * @return The axis label |
8884 | 73 */ |
8886 | 74 protected abstract String generateSeries(final DiagramGenerator generator, final ArtifactAndFacet bundle, final ThemeDocument theme, final boolean visible); |
8884 | 75 |
9200 | 76 protected final String buildSeriesForType(final DiagramGenerator generator, final ArtifactAndFacet bundle, final ThemeDocument theme, final boolean visible, |
77 final IResultType resultType, final Double gapDistance) { | |
78 final CallContext context = generator.getContext(); | |
79 final Map<String, String> metaData = bundle.getFacet().getMetaData(); | |
80 | |
81 final Artifact artifact = bundle.getArtifact(); | |
82 | |
83 final StyledXYSeries series = new StyledXYSeries(bundle.getFacetDescription(), theme); | |
84 series.putMetaData(metaData, artifact, context); | |
85 | |
86 final String facetName = bundle.getFacetName(); | |
87 | |
88 final AbstractCalculationResult data = (AbstractCalculationResult) bundle.getData(context); | |
89 if (data == null) { | |
90 // Check has been here before so we keep it for security reasons | |
91 // this should never happen though. | |
92 throw new IllegalStateException("Data is null for facet: " + facetName); | |
93 } | |
94 | |
95 final double[][] points = generatePoints(context, artifact, data, facetName, resultType); | |
96 | |
97 if (gapDistance == null) | |
98 StyledSeriesBuilder.addPoints(series, points, true); | |
99 else | |
100 StyledSeriesBuilder.addPoints(series, points, true, gapDistance); | |
101 | |
102 generator.addAxisSeries(series, getAxisName(), visible); | |
103 | |
104 return metaData.get("Y"); | |
105 } | |
106 | |
9204
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
107 protected final String buildSeriesForType1(final DiagramGenerator generator, final ArtifactAndFacet bundle, final ThemeDocument theme, |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
108 final boolean visible, |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
109 final IResultType resultType, final Double gapDistance) { |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
110 final CallContext context = generator.getContext(); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
111 final Map<String, String> metaData = bundle.getFacet().getMetaData(); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
112 |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
113 final Artifact artifact = bundle.getArtifact(); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
114 |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
115 final StyledXYSeries series = new StyledXYSeries(bundle.getFacetDescription(), theme); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
116 series.putMetaData(metaData, artifact, context); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
117 |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
118 final String facetName = bundle.getFacetName(); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
119 |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
120 final AbstractCalculationResult data = (AbstractCalculationResult) bundle.getData(context); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
121 if (data == null) { |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
122 // Check has been here before so we keep it for security reasons |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
123 // this should never happen though. |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
124 throw new IllegalStateException("Data is null for facet: " + facetName); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
125 } |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
126 |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
127 final double[][] points = generatePoints(context, artifact, data, facetName, resultType, bundle.getFacet().getIndex()); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
128 |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
129 if (gapDistance == null) |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
130 StyledSeriesBuilder.addPoints(series, points, true); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
131 else |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
132 StyledSeriesBuilder.addPoints(series, points, true, gapDistance); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
133 |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
134 generator.addAxisSeries(series, getAxisName(), visible); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
135 |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
136 return metaData.get("Y"); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
137 } |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
138 |
9200 | 139 protected final String buildSeriesForTkh(final DiagramGenerator generator, final ArtifactAndFacet bundle, final ThemeDocument theme, |
140 final boolean visible) { | |
141 final CallContext context = generator.getContext(); | |
142 | |
143 final String facetName = bundle.getFacetName(); | |
144 final AbstractTkhCalculationResult data = (AbstractTkhCalculationResult) bundle.getData(context); | |
145 if (data == null) { | |
146 // Check has been here before so we keep it for security reasons | |
147 // this should never happen though. | |
148 throw new IllegalStateException("Data is null for facet: " + facetName); | |
149 } | |
150 | |
151 final StyledXYSeries seriesUp = new StyledXYSeries(bundle.getFacetDescription(), theme); | |
152 final double[][] pointsUp = data.getTkhUpPoints(); | |
153 StyledSeriesBuilder.addPoints(seriesUp, pointsUp, true); | |
154 | |
155 // REMARK: we add " " because the description is misused as id, which must be unique. | |
156 final StyledXYSeries seriesDown = new StyledXYSeries(bundle.getFacetDescription() + " ", theme); | |
157 final double[][] pointsDown = data.getTkhDownPoints(); | |
158 StyledSeriesBuilder.addPoints(seriesDown, pointsDown, true); | |
159 | |
160 final StyledAreaSeriesCollection area = new StyledAreaSeriesCollection(theme); | |
161 area.setMode(StyledAreaSeriesCollection.FILL_MODE.BETWEEN); | |
162 area.addSeries(seriesUp); | |
163 area.addSeries(seriesDown); | |
164 | |
165 generator.addAreaSeries(area, getAxisName(), visible); | |
166 | |
167 return null; | |
168 } | |
169 | |
170 private Double findRadius(final CallContext context, final Artifact artifact) { | |
171 final Double start = (Double) context.getContextValue("startkm"); | |
172 final Double end = (Double) context.getContextValue("endkm"); | |
173 | |
174 if (start == null || end == null) | |
175 return null; | |
176 | |
177 final RiverContext fc = (RiverContext) context.globalContext(); | |
178 final ZoomScale scales = (ZoomScale) fc.get("zoomscale"); | |
179 final RiverAccess access = new RiverAccess((D4EArtifact) artifact); | |
180 final String river = access.getRiverName(); | |
181 | |
182 return scales.getRadius(river, start, end); | |
183 } | |
184 | |
185 private double[][] generatePoints(final CallContext context, final Artifact artifact, final AbstractCalculationResult data, final String facetName, | |
186 final IResultType resultType) { | |
187 | |
188 final double[][] points = data.getStationPoints(resultType); | |
189 if (facetName.endsWith(".filtered")) { | |
190 final Double radius = findRadius(context, artifact); | |
191 return movingAverage(radius, points); | |
192 } | |
193 | |
194 return points; | |
195 } | |
196 | |
9204
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
197 private double[][] generatePoints(final CallContext context, final Artifact artifact, final AbstractCalculationResult data, final String facetName, |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
198 final IResultType resultType, final int index) { |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
199 |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
200 final double[][] points = data.getStationPoints(resultType, index); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
201 if (facetName.endsWith(".filtered")) { |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
202 final Double radius = findRadius(context, artifact); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
203 return movingAverage(radius, points); |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
204 } |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
205 |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
206 return points; |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
207 } |
4f411c6ee3ae
Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents:
9203
diff
changeset
|
208 |
9200 | 209 private double[][] movingAverage(final Double radius, final double[][] points) { |
210 | |
211 if (radius == null) | |
212 return points; | |
213 | |
214 return MovingAverage.weighted(points, radius); | |
215 } | |
216 | |
8884 | 217 @Override |
218 public final boolean canHandle(final String facettype) { | |
219 return this.handled_facet_types.contains(facettype); | |
220 } | |
221 | |
222 @Override | |
223 public final String getAxisLabel(final DiagramGenerator generator) { | |
224 if (this.yAxisLabel != null && !this.yAxisLabel.isEmpty()) { | |
225 // REMARK/UNINTENDED: yAxisLabel may also be a resolved message (side-effect of StyledXYSeries#putMetadata), | |
226 // and cannot be resolved, so we need to give the resolved value as default | |
9040 | 227 // TODO: In other implementations (i.e. FlowVelocityProcessor), an explicit (German) default label is given here, |
8884 | 228 // probably the English version will also show German (CHECK) |
229 return generator.msg(this.yAxisLabel, this.yAxisLabel); | |
230 } | |
231 return generator.msg(this.i18n_axis_label, "MISSING"); | |
232 } | |
9203 | 233 |
234 // Moved from SInfoLineProcessor: | |
235 protected static final Facet createFacet(final CallContext context, final String hash, final String id, final AbstractCalculationResult result, | |
236 final int index, final String axisLabel, final String facetId, final String description) { | |
237 final String facetFlowDepthFilteredDescription = Resources.getMsg(context.getMeta(), description, description, result.getLabel()); | |
238 return new SInfoResultFacet(index, facetId, facetFlowDepthFilteredDescription, axisLabel, ComputeType.ADVANCE, id, hash); | |
239 } | |
240 | |
8884 | 241 } |