Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/utils/GaugeIndex.java @ 8850:6823991e1ed1
Better helper for acessing gauges by station
author | gernotbelger |
---|---|
date | Thu, 18 Jan 2018 18:22:25 +0100 |
parents | |
children | 385b52ccde23 |
comparison
equal
deleted
inserted
replaced
8849:e4aadc953665 | 8850:6823991e1ed1 |
---|---|
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.utils; | |
11 | |
12 import java.util.List; | |
13 | |
14 import org.dive4elements.river.model.Gauge; | |
15 | |
16 /** | |
17 * Allows performant access to gauges by station. | |
18 * @author Gernot Belger | |
19 */ | |
20 public class GaugeIndex { | |
21 private List<Gauge> gauges; | |
22 | |
23 private Gauge lastGauge = null; | |
24 | |
25 public GaugeIndex( List<Gauge> gauges) { | |
26 this.gauges = gauges; | |
27 } | |
28 | |
29 public Gauge findGauge(double km) { | |
30 | |
31 // REMARK: this is code copied from WaterlevelExporter, which is honestly not very fast/good. | |
32 // Instead we need to index by range with an RTree and directly acccess the right gauge. | |
33 | |
34 if( lastGauge != null && lastGauge.getRange().contains(km) ) | |
35 return lastGauge; | |
36 | |
37 final Gauge gauge = findGauge(km, gauges); | |
38 | |
39 lastGauge = gauge; | |
40 | |
41 return gauge; | |
42 } | |
43 | |
44 private static Gauge findGauge(double km, List<Gauge> gauges) { | |
45 for (Gauge gauge: gauges) { | |
46 if (gauge.getRange().contains(km)) { | |
47 return gauge; | |
48 } | |
49 } | |
50 return null; | |
51 } | |
52 } |