Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/OfficialLineFinder.java @ 6385:ef08c4f57ede
Artifacts: First part of the official lines guessing.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Fri, 21 Jun 2013 12:46:59 +0200 |
parents | |
children | d2803cc7a338 |
comparison
equal
deleted
inserted
replaced
6384:2987d81ea719 | 6385:ef08c4f57ede |
---|---|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU AGPL (>=v3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
6 * documentation coming with Dive4Elements River for details. | |
7 */ | |
8 | |
9 package org.dive4elements.river.artifacts.model; | |
10 | |
11 import java.util.ArrayList; | |
12 import java.util.Collections; | |
13 import java.util.HashMap; | |
14 import java.util.List; | |
15 import java.util.Map; | |
16 | |
17 import net.sf.ehcache.Cache; | |
18 import net.sf.ehcache.Element; | |
19 | |
20 import org.dive4elements.river.artifacts.D4EArtifact; | |
21 import org.dive4elements.river.artifacts.cache.CacheFactory; | |
22 import org.dive4elements.river.model.Gauge; | |
23 import org.dive4elements.river.model.MainValue; | |
24 import org.dive4elements.river.model.NamedMainValue; | |
25 import org.dive4elements.river.model.OfficialLine; | |
26 import org.dive4elements.river.model.River; | |
27 import org.dive4elements.river.model.Wst; | |
28 import org.dive4elements.river.model.WstColumn; | |
29 | |
30 public class OfficialLineFinder | |
31 { | |
32 public static final String CACHE_NAME = "official-lines"; | |
33 | |
34 // We will only have one entry in this cache. | |
35 public static final String CACHE_KEY = CACHE_NAME; | |
36 | |
37 public static final double EPSILON = 1e-4; | |
38 | |
39 | |
40 public static class ValueRange extends Range { | |
41 | |
42 private double value; | |
43 private int wstId; | |
44 private int columnPos; | |
45 | |
46 public ValueRange( | |
47 double start, | |
48 double end, | |
49 double value, | |
50 int wstId, | |
51 int columnPos | |
52 ) { | |
53 super(start, end); | |
54 this.value = value; | |
55 this.wstId = wstId; | |
56 this.columnPos = columnPos; | |
57 } | |
58 | |
59 public boolean sameValue(double value) { | |
60 return Math.abs(value - this.value) < EPSILON; | |
61 } | |
62 | |
63 public int getWstId() { | |
64 return wstId; | |
65 } | |
66 | |
67 public int getColumnPos() { | |
68 return columnPos; | |
69 } | |
70 } | |
71 | |
72 public OfficialLineFinder() { | |
73 } | |
74 | |
75 public static Map<String, List<ValueRange>> getAll() { | |
76 Cache cache = CacheFactory.getCache(CACHE_NAME); | |
77 | |
78 if (cache == null) { | |
79 return getAllUncached(); | |
80 } | |
81 | |
82 Element element = cache.get(CACHE_KEY); | |
83 | |
84 if (element != null) { | |
85 return (Map<String, List<ValueRange>>)element.getValue(); | |
86 } | |
87 | |
88 Map<String, List<ValueRange>> result = getAllUncached(); | |
89 if (result != null) { | |
90 cache.put(new Element(CACHE_KEY, result)); | |
91 } | |
92 return result; | |
93 | |
94 } | |
95 | |
96 public static Map<String, List<ValueRange>> getAllUncached() { | |
97 | |
98 Map<String, List<ValueRange>> rivers2officialLines = | |
99 new HashMap<String, List<ValueRange>>(); | |
100 | |
101 | |
102 for (OfficialLine line: OfficialLine.fetchAllOfficalLines()) { | |
103 String name = line.getNamedMainValue().getName(); | |
104 WstColumn wc = line.getWstColumn(); | |
105 Wst wst = wc.getWst(); | |
106 | |
107 List<ValueRange> ranges = new ArrayList<ValueRange>(); | |
108 | |
109 River river = wst.getRiver(); | |
110 List<Gauge> gauges = river.getGauges(); | |
111 for (Gauge gauge: gauges) { | |
112 List<MainValue> mainValues = gauge.getMainValues(); | |
113 for (MainValue mainValue: mainValues) { | |
114 NamedMainValue nmv = mainValue.getMainValue(); | |
115 if (nmv.getName().equalsIgnoreCase(name)) { | |
116 // found gauge with this main value | |
117 | |
118 double from = gauge.getRange().getA().doubleValue(); | |
119 double to = gauge.getRange().getA().doubleValue(); | |
120 double value = mainValue.getValue().doubleValue(); | |
121 int wstId = wst.getId(); | |
122 int pos = wc.getPosition(); | |
123 ValueRange range = | |
124 new ValueRange(from, to, value, wstId, pos); | |
125 ranges.add(range); | |
126 break; | |
127 } | |
128 } | |
129 } | |
130 | |
131 if (!ranges.isEmpty()) { | |
132 rivers2officialLines.put(river.getName(), ranges); | |
133 } | |
134 } | |
135 | |
136 return rivers2officialLines; | |
137 } | |
138 | |
139 public static List<OfficialLine> findOfficialLines(D4EArtifact artifact) { | |
140 | |
141 Map<String, List<ValueRange>> rivers2officialLines = getAll(); | |
142 | |
143 String riverName = artifact.getDataAsString("river"); | |
144 | |
145 if (riverName == null) { | |
146 return Collections.<OfficialLine>emptyList(); | |
147 } | |
148 | |
149 List<ValueRange> ranges = rivers2officialLines.get(riverName); | |
150 | |
151 if (ranges.isEmpty()) { | |
152 return Collections.<OfficialLine>emptyList(); | |
153 } | |
154 | |
155 // TODO: Figure out all the cases here. | |
156 | |
157 return Collections.<OfficialLine>emptyList(); | |
158 } | |
159 } |