comparison artifacts/src/main/java/org/dive4elements/river/artifacts/access/RangeAccess.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/access/RangeAccess.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts.access;
2
3 import org.apache.log4j.Logger;
4
5 import gnu.trove.TDoubleArrayList;
6 import org.dive4elements.artifacts.CallContext;
7 import org.dive4elements.river.artifacts.FLYSArtifact;
8 import org.dive4elements.river.artifacts.WINFOArtifact;
9
10 import org.dive4elements.river.utils.FLYSUtils;
11
12
13 /** For the moment, light-weight wrapper around FLYSUtils. */
14 // TODO employ 'Caching' like other Accesses, remove usage of FLYSUtils.
15 public class RangeAccess
16 extends RiverAccess
17 {
18 private static Logger logger = Logger.getLogger(RangeAccess.class);
19
20 private CallContext context;
21
22 public static enum KM_MODE { RANGE, LOCATIONS, NONE };
23
24 double[] kmRange;
25
26 Double from;
27
28 Double to;
29
30 Double step;
31
32 private KM_MODE mode;
33
34
35 public RangeAccess(FLYSArtifact artifact, CallContext context) {
36 super(artifact);
37 this.context = context;
38 }
39
40
41 /** Evaluate the ld_mode data of artifact. */
42 public KM_MODE getKmRangeMode() {
43 if (mode != null) {
44 return mode;
45 }
46 String modeData = getString("ld_mode");
47
48 if (modeData == null || modeData.length() == 0) {
49 mode = KM_MODE.NONE;
50 }
51 else if (modeData.equals("distance")) {
52 mode = KM_MODE.RANGE;
53 }
54 else if (modeData.equals("locations")) {
55 mode = KM_MODE.LOCATIONS;
56 }
57 else {
58 mode = KM_MODE.NONE;
59 }
60
61 return mode;
62 }
63
64 /**
65 * Return sorted array of locations at which stuff was calculated
66 * (from ld_locations data), null if not parameterized this way.
67 */
68 public double[] getLocations() {
69 String locationStr = getString("ld_locations");
70
71 if (locationStr == null || locationStr.length() == 0) {
72 if (getArtifact() instanceof WINFOArtifact) {
73 WINFOArtifact winfo = (WINFOArtifact) getArtifact();
74 if (winfo.getReferenceStartKm() != null && winfo.getReferenceEndKms() != null) {
75 return new double[]
76 {
77 winfo.getReferenceStartKm().doubleValue(),
78 winfo.getReferenceEndKms()[0]
79 };
80 }
81 else if (winfo.getReferenceStartKm() != null) {
82 return new double[]
83 {
84 winfo.getReferenceStartKm().doubleValue(),
85 winfo.getReferenceStartKm().doubleValue()
86 };
87 }
88 }
89 return null;
90 }
91
92 String[] tmp = locationStr.split(" ");
93 TDoubleArrayList locations = new TDoubleArrayList();
94
95 for (String l: tmp) {
96 try {
97 locations.add(Double.parseDouble(l));
98 }
99 catch (NumberFormatException nfe) {
100 logger.debug(nfe.getLocalizedMessage(), nfe);
101 }
102 }
103
104 locations.sort();
105
106 return locations.toNativeArray();
107 }
108
109
110 /** Return ld_from data (in km). */
111 public double getFrom() {
112 if (from == null) {
113 from = getDouble("ld_from");
114 }
115
116 if (logger.isDebugEnabled()) {
117 logger.debug("from: '" + from + "'");
118 }
119
120 return from.doubleValue();
121 }
122
123
124 /** Return ld_to data (in km). */
125 public double getTo() {
126 if (to == null) {
127 to = getDouble("ld_to");
128 }
129
130 if (logger.isDebugEnabled()) {
131 logger.debug("to: '" + to + "'");
132 }
133
134 return to.doubleValue();
135 }
136
137
138 /** Step width for calculation. */
139 public Double getStep() {
140
141 if (step == null) {
142 step = getDouble("ld_step");
143 }
144
145 if (logger.isDebugEnabled()) {
146 logger.debug("step: '" + step + "'");
147 }
148
149 return step;
150 }
151
152
153 /**
154 * Get min and max kilometer, independent of parametization
155 * (ld_from/to vs ld_locations).
156 */
157 public double[] getKmRange() {
158 // TODO store kmRange in field.
159 switch (getKmRangeMode()) {
160 case RANGE: {
161 return getKmFromTo();
162 }
163
164 case LOCATIONS: {
165 double[] locs = getLocations();
166 // if no locations, nPE.
167 if (locs == null) {
168 logger.warn("no locations to get km range from.");
169 return new double[] { Double.NaN, Double.NaN };
170 }
171 return new double[] { locs[0], locs[locs.length-1] };
172 }
173
174 case NONE: {
175 double[] locs = getLocations();
176 if (locs != null) {
177 return new double[] { locs[0], locs[locs.length-1] };
178 }
179 else {
180 return getKmFromTo();
181 }
182 }
183 }
184
185 return new double[] { Double.NaN, Double.NaN };
186 }
187
188
189 public double[] getKmFromTo() {
190 return FLYSUtils.getKmFromTo(this.getArtifact());
191 }
192 }
193 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org