comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/OfficialLineFinder.java @ 6395:d2803cc7a338

Artifacts: Official lines: Only apply in Q-Calculations and filter against the ranges of the gauges.
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 21 Jun 2013 16:28:47 +0200
parents ef08c4f57ede
children f579e4a80b84
comparison
equal deleted inserted replaced
6394:cf4889a257cb 6395:d2803cc7a338
134 } 134 }
135 135
136 return rivers2officialLines; 136 return rivers2officialLines;
137 } 137 }
138 138
139 public static final Range MAX_RANGE = new Range(-Double.MAX_VALUE, +Double.MAX_VALUE);
140
141 private static final String nn(String s) {
142 return s != null ? s : "";
143 }
144
145 public static Range extractRange(D4EArtifact artifact) {
146 String mode = nn(artifact.getDataAsString("ld_mode"));
147 String locations = nn(artifact.getDataAsString("ld_locations"));
148 String from = nn(artifact.getDataAsString("ld_from"));
149 String to = nn(artifact.getDataAsString("ld_to"));
150
151 if (mode.equals("location")) {
152 try {
153 String loc = locations.replace(" ", "");
154 String[] split = loc.split(",");
155 if (split.length < 1) {
156 return MAX_RANGE;
157 }
158 double min = Double.parseDouble(split[0]);
159 double max = min;
160 for (int i = 1; i < split.length; ++i) {
161 double v = Double.parseDouble(split[i]);
162 if (v > max) max = v;
163 if (v < min) min = v;
164 }
165 return new Range(min, max);
166 }
167 catch (NumberFormatException nfe) {
168 return MAX_RANGE;
169 }
170 }
171 try {
172 return new Range(Double.parseDouble(from), Double.parseDouble(to));
173 }
174 catch (NumberFormatException nfe) {
175 return MAX_RANGE;
176 }
177 }
178
179 private static List<ValueRange> filterByRange(Range range, List<ValueRange> ranges) {
180 List<ValueRange> list = new ArrayList<ValueRange>(ranges.size());
181 for (ValueRange r: ranges) {
182 if (r.intersects(range)) {
183 list.add(r);
184 }
185 }
186 return list;
187 }
188
189 private static boolean isQ(D4EArtifact artifact) {
190 Boolean b = artifact.getDataAsBoolean("wq_isq");
191 return b != null && b;
192 }
193
139 public static List<OfficialLine> findOfficialLines(D4EArtifact artifact) { 194 public static List<OfficialLine> findOfficialLines(D4EArtifact artifact) {
140 195
196 if (!isQ(artifact)) { // Only handle Q calculations
197 return Collections.<OfficialLine>emptyList();
198 }
199
141 Map<String, List<ValueRange>> rivers2officialLines = getAll(); 200 Map<String, List<ValueRange>> rivers2officialLines = getAll();
142 201
143 String riverName = artifact.getDataAsString("river"); 202 String riverName = nn(artifact.getDataAsString("river"));
144 203
145 if (riverName == null) { 204 List<ValueRange> ranges = rivers2officialLines.get(riverName);
205
206 if (ranges == null) {
146 return Collections.<OfficialLine>emptyList(); 207 return Collections.<OfficialLine>emptyList();
147 } 208 }
148 209
149 List<ValueRange> ranges = rivers2officialLines.get(riverName); 210 ranges = filterByRange(extractRange(artifact), ranges);
150 211
151 if (ranges.isEmpty()) { 212 if (ranges.isEmpty()) {
152 return Collections.<OfficialLine>emptyList(); 213 return Collections.<OfficialLine>emptyList();
153 } 214 }
154 215
216
155 // TODO: Figure out all the cases here. 217 // TODO: Figure out all the cases here.
156 218
157 return Collections.<OfficialLine>emptyList(); 219 return Collections.<OfficialLine>emptyList();
158 } 220 }
159 } 221 }

http://dive4elements.wald.intevation.org