changeset 8850:6823991e1ed1

Better helper for acessing gauges by station
author gernotbelger
date Thu, 18 Jan 2018 18:22:25 +0100
parents e4aadc953665
children 13650d8a2373
files artifacts/src/main/java/org/dive4elements/river/utils/GaugeIndex.java
diffstat 1 files changed, 52 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/GaugeIndex.java	Thu Jan 18 18:22:25 2018 +0100
@@ -0,0 +1,52 @@
+/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
+ * Software engineering by 
+ *  Björnsen Beratende Ingenieure GmbH 
+ *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+package org.dive4elements.river.utils;
+
+import java.util.List;
+
+import org.dive4elements.river.model.Gauge;
+
+/**
+ * Allows performant access to gauges by station.
+ * @author Gernot Belger
+ */
+public class GaugeIndex {
+	private List<Gauge> gauges;
+	
+	private Gauge lastGauge = null;
+
+	public GaugeIndex( List<Gauge> gauges) {
+		this.gauges = gauges;
+	}
+	
+	public Gauge findGauge(double km) {
+
+		// REMARK: this is code copied from WaterlevelExporter, which is honestly not very fast/good.
+		// Instead we need to index by range with an RTree and directly acccess the right gauge.
+		
+		if( lastGauge != null && lastGauge.getRange().contains(km) )
+			return lastGauge;
+		
+		final Gauge gauge = findGauge(km, gauges);
+
+        lastGauge = gauge;
+            
+        return gauge;
+	}
+	
+    private static Gauge findGauge(double km, List<Gauge> gauges) {
+        for (Gauge gauge: gauges) {
+            if (gauge.getRange().contains(km)) {
+                return gauge;
+            }
+        }
+        return null;
+    }
+}
\ No newline at end of file

http://dive4elements.wald.intevation.org