Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/river/RiverInfoProvider.java @ 9499:853f2dafc16e
VegetationZones in CrossSectionsDiagram
author | gernotbelger |
---|---|
date | Thu, 27 Sep 2018 18:06:26 +0200 |
parents | artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/RiverInfoProvider.java@6e7094368e97 |
children | 3f230fe8eb19 |
comparison
equal
deleted
inserted
replaced
9496:d8e753d0fdb9 | 9499:853f2dafc16e |
---|---|
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 package org.dive4elements.river.artifacts.model.river; | |
11 | |
12 import java.util.List; | |
13 | |
14 import org.apache.commons.lang.math.DoubleRange; | |
15 import org.dive4elements.artifacts.CallContext; | |
16 import org.dive4elements.river.artifacts.model.LocationProvider; | |
17 import org.dive4elements.river.artifacts.model.WKms; | |
18 import org.dive4elements.river.artifacts.resources.Resources; | |
19 import org.dive4elements.river.artifacts.states.WaterlevelData; | |
20 import org.dive4elements.river.model.Gauge; | |
21 import org.dive4elements.river.model.River; | |
22 import org.dive4elements.river.utils.GaugeIndex; | |
23 | |
24 /** | |
25 * @author Gernot Belger | |
26 * | |
27 */ | |
28 public final class RiverInfoProvider { | |
29 | |
30 private static final String CSV_NOT_IN_GAUGE_RANGE = "export.waterlevel.csv.not.in.gauge.range"; | |
31 | |
32 private final River river; | |
33 private final GaugeIndex gaugeIndex; | |
34 private final Gauge refGauge; | |
35 private final boolean showAllGauges; | |
36 private final String notinrange; | |
37 | |
38 public static RiverInfoProvider forRange(final CallContext context, final River river, final DoubleRange calcRange) { | |
39 | |
40 return forRange(context, river, calcRange, false); | |
41 } | |
42 | |
43 public static RiverInfoProvider forRange(final CallContext context, final River river, final DoubleRange calcRange, final boolean firstGaugeIsRefGauge) { | |
44 | |
45 final List<Gauge> gauges; | |
46 if (calcRange == null) | |
47 gauges = river.getGauges(); | |
48 else | |
49 gauges = river.determineGauges(calcRange.getMinimumDouble(), calcRange.getMaximumDouble()); | |
50 | |
51 final GaugeIndex gaugeIndex = new GaugeIndex(gauges); | |
52 | |
53 final String notinrange = Resources.getMsg(context.getMeta(), CSV_NOT_IN_GAUGE_RANGE, CSV_NOT_IN_GAUGE_RANGE); | |
54 | |
55 if (firstGaugeIsRefGauge && !gauges.isEmpty()) | |
56 return new RiverInfoProvider(notinrange, river, false, gaugeIndex, gauges.get(0)); | |
57 return new RiverInfoProvider(notinrange, river, false, gaugeIndex, null); | |
58 } | |
59 | |
60 private RiverInfoProvider(final String notinrange, final River river, final boolean showAllGauges, final GaugeIndex gaugeIndex, final Gauge refGauge) { | |
61 this.notinrange = notinrange; | |
62 this.river = river; | |
63 this.showAllGauges = showAllGauges; | |
64 this.gaugeIndex = gaugeIndex; | |
65 this.refGauge = refGauge; | |
66 } | |
67 | |
68 public RiverInfoProvider forWaterlevel(final WaterlevelData waterlevel) { | |
69 final WKms wstKms = waterlevel.getWkms(); | |
70 final Gauge waterlevelRefGauge = findReferenceGauge(wstKms); | |
71 final boolean waterlevelShowAllGauges = waterlevel.isShowAllGauges(); | |
72 | |
73 return new RiverInfoProvider(this.notinrange, this.river, waterlevelShowAllGauges, this.gaugeIndex, waterlevelRefGauge); | |
74 } | |
75 | |
76 /** | |
77 * Re-determines the reference gauge, in the same way as the WaterlevelArtifact would do it | |
78 */ | |
79 private Gauge findReferenceGauge(final WKms wkms) { | |
80 | |
81 final double[] wstFromTo = findWstFromTo(wkms); | |
82 return this.river.determineRefGauge(wstFromTo, true); | |
83 } | |
84 | |
85 private static double[] findWstFromTo(final WKms wkms) { | |
86 | |
87 final double from = wkms.getKm(0); | |
88 final double to = wkms.getKm(wkms.size() - 1); | |
89 | |
90 final boolean waterIncreasing = wkms.guessWaterIncreasing(); | |
91 if (waterIncreasing) | |
92 return new double[] { to, from }; | |
93 | |
94 return new double[] { from, to }; | |
95 } | |
96 | |
97 public String getLocation(final double km) { | |
98 return LocationProvider.getLocation(this.river.getName(), km); | |
99 } | |
100 | |
101 public String findGauge(final double km) { | |
102 // REMARK: access the gauge once only during calculation | |
103 final Gauge gauge = getGauge(km); | |
104 | |
105 return gauge == null ? this.notinrange : gauge.getName(); | |
106 } | |
107 | |
108 private String findGauge(final double km, final boolean allGauges) { | |
109 // REMARK: access the gauge once only during calculation | |
110 final Gauge gauge = getGauge(km, allGauges); | |
111 | |
112 return gauge == null ? this.notinrange : gauge.getName(); | |
113 } | |
114 | |
115 public Gauge getGauge(final double km) { | |
116 | |
117 // REMARK: using same logic as in WaterlevelExporter here | |
118 | |
119 return getGauge(km, this.showAllGauges); | |
120 } | |
121 | |
122 public Gauge getGauge(final double km, final boolean allGauges) { | |
123 if (allGauges) | |
124 return this.gaugeIndex.findGauge(km); | |
125 | |
126 if ((this.refGauge != null) && this.refGauge.getRange().contains(km)) | |
127 return this.refGauge; | |
128 | |
129 return null; | |
130 } | |
131 | |
132 public String getReferenceGauge() { | |
133 return this.refGauge == null ? this.notinrange : this.refGauge.getName(); | |
134 } | |
135 | |
136 public River getRiver() { | |
137 return this.river; | |
138 } | |
139 | |
140 public List<Gauge> getGauges() { | |
141 return this.gaugeIndex.getGauges(); | |
142 } | |
143 } |